001    
002    package railo.runtime.op.date;
003    
004    import java.util.HashMap;
005    import java.util.Map;
006    
007    import railo.runtime.op.Constants;
008    
009    
010    /**
011     * helper class to convert a string to a Object
012     */
013    public final class DateString {
014        
015        private static int[][] ints= { 
016                {0,1,2,3,4,5,6,7,8,9}, 
017                {0,10,20,30,40,50,60,70,80,90}, 
018                {0,100,200,300,400,500,600,700,800,900}, 
019                {0,1000,2000,3000,4000,5000,6000,7000,8000,9000}
020        }; 
021    
022        private String str;
023        private int pos;
024    
025            private static Map months=new HashMap();
026            static{
027                    months.put("january",Constants.INTEGER_1);
028                    months.put("januar",Constants.INTEGER_1);
029                    months.put("janua",Constants.INTEGER_1);
030                    months.put("janu",Constants.INTEGER_1);
031                    months.put("jan",Constants.INTEGER_1);
032                    
033                    months.put("february",Constants.INTEGER_2);
034                    months.put("februar",Constants.INTEGER_2);
035                    months.put("februa",Constants.INTEGER_2);
036                    months.put("febru",Constants.INTEGER_2);
037                    months.put("febr",Constants.INTEGER_2);
038                    months.put("feb",Constants.INTEGER_2);
039                    
040                    months.put("march",Constants.INTEGER_3);
041                    months.put("marc",Constants.INTEGER_3);
042                    months.put("mar",Constants.INTEGER_3);
043                    
044                    months.put("april",Constants.INTEGER_4);
045                    months.put("apri",Constants.INTEGER_4);
046                    months.put("apr",Constants.INTEGER_4);
047                    
048                    months.put("may",Constants.INTEGER_5);
049    
050                    months.put("june",Constants.INTEGER_6);
051                    months.put("jun",Constants.INTEGER_6);
052                    
053                    months.put("july",Constants.INTEGER_7);
054                    months.put("jul",Constants.INTEGER_7);
055    
056                    months.put("august",Constants.INTEGER_8);
057                    months.put("augus",Constants.INTEGER_8);
058                    months.put("augu",Constants.INTEGER_8);
059                    months.put("aug",Constants.INTEGER_8);
060    
061                    months.put("september",Constants.INTEGER_9);
062                    months.put("septembe",Constants.INTEGER_9);
063                    months.put("septemb",Constants.INTEGER_9);
064                    months.put("septem",Constants.INTEGER_9);
065                    months.put("septe",Constants.INTEGER_9);
066                    months.put("sept",Constants.INTEGER_9);
067                    months.put("sep",Constants.INTEGER_9);
068    
069                    months.put("october",Constants.INTEGER_10);
070                    months.put("octobe",Constants.INTEGER_10);
071                    months.put("octob",Constants.INTEGER_10);
072                    months.put("octo",Constants.INTEGER_10);
073                    months.put("oct",Constants.INTEGER_10);
074    
075                    months.put("november",Constants.INTEGER_11);
076                    months.put("novembe",Constants.INTEGER_11);
077                    months.put("novemb",Constants.INTEGER_11);
078                    months.put("novem",Constants.INTEGER_11);
079                    months.put("nove",Constants.INTEGER_11);
080                    months.put("nov",Constants.INTEGER_11);
081                    
082                    months.put("december",Constants.INTEGER_12);
083                    months.put("decembe",Constants.INTEGER_12);
084                    months.put("decemb",Constants.INTEGER_12);
085                    months.put("decem",Constants.INTEGER_12);
086                    months.put("dece",Constants.INTEGER_12);
087                    months.put("dec",Constants.INTEGER_12);
088                    
089    
090            }
091    
092        /**
093         * constructor of the class
094         * @param str Date String
095         */
096        public DateString(String str) {
097            this.str=str;
098        }
099    
100        /**
101         * check if char a actuell position of the inner cursor is same value like given value
102         * @param c char to compare
103         * @return is same or not
104         */
105        public boolean isNext(char c) {
106            return str.length()>pos+1 && str.charAt(pos+1)==c;
107        }
108    
109        /**
110         * check if char a actuell position of the inner cursor is same value like given value
111         * @param c char to compare
112         * @return is same or not
113         */
114        public boolean isCurrent(char c) {
115            return str.length()>pos && str.charAt(pos)==c;
116        }
117    
118        /**
119         * check if last char has same value than given char
120         * @param c char to check
121         * @return is same or not
122         */
123        public boolean isLast(char c) {
124            return str.charAt(str.length()-1)==c;
125        }
126    
127        /**
128         * set inner cursor one forward
129         */
130        public void next() {
131            pos++;
132        }
133    
134        /**
135         * set inner cursor [count] forward
136         * @param count forward count
137         */
138        public void next(int count) {
139            pos+=count;
140        }
141    
142        /**
143         * @return the length of the inner String
144         */
145        public int length() {
146           return str.length();
147        }
148    
149        /**
150         * forward inner cursor if value at actuell position is same as given.
151         * @param c char to compare
152         * @return has forwared or not
153         */
154        public boolean fwIfCurrent(char c) {
155            if(isCurrent(c)) {
156                pos++;
157                return true;
158            }
159            return false;
160        }
161    
162        /**
163         * forward inner cursor if value at the next position is same as given.
164         * @param c char to compare
165         * @return has forwared or not
166         */
167        public boolean fwIfNext(char c) {
168            if(isNext(c)) {
169                pos++;
170                return true;
171            }
172            return false;
173        }
174    
175        /*
176         * read in the next four digits from current position
177         * @return value from the 4 digits
178         *
179        public int read4Digit() {
180            // first
181            char c=str.charAt(pos++);
182            if(!isDigit(c)) return -1;
183                    int value=ints[3][c-48]; 
184                    
185                // second
186            c=str.charAt(pos++);
187            if(!isDigit(c)) return -1;
188                    value+=ints[2][c-48]; 
189                    
190                // third
191            c=str.charAt(pos++);
192            if(!isDigit(c)) return -1;
193                    value+=ints[1][c-48]; 
194                    
195                // fourt
196            c=str.charAt(pos++);
197            if(!isDigit(c)) return -1;
198                    value+=ints[0][c-48]; 
199                                    
200            return value;
201        }*/
202    
203        /*
204         * read in the next four digits from current position
205         * @return value from the 4 digits
206         *
207        public int read2Digit() {
208            // first
209            char c=str.charAt(pos++);
210            if(!isDigit(c)) return -1;
211                    int value=ints[1][c-48]; 
212                    
213                // second
214            c=str.charAt(pos++);
215            if(!isDigit(c)) return -1;
216                    value+=ints[0][c-48]; 
217                                    
218            return value;
219        }*/
220    
221        /**
222         * read in the next digits from current position
223         * @return value from the digits
224         */
225        public int readDigits() {
226            int value=0;
227            char c;
228            if(isValidIndex() && isDigit(c=str.charAt(pos))) {
229                value=ints[0][c-48];
230                pos++;
231            }
232            else return -1;
233            while(isValidIndex() && isDigit(c=str.charAt(pos))) {
234                value*=10;
235                value+=ints[0][c-48];
236                pos++;
237            }
238            return value;
239        }
240        public boolean removeWhitespace() {
241            boolean rtn=false;
242            while(isValidIndex() && Character.isWhitespace(str.charAt(pos))) {
243                pos++;
244            }
245            return rtn;
246        }
247        
248        
249    
250            public int readMonthString() {
251                    char c;
252                    int start=pos;
253                    StringBuilder sb=new StringBuilder();
254            while(isValidIndex() && isMonthChar(c=str.charAt(pos))) {
255                pos++;
256                sb.append(Character.toLowerCase(c));
257            }
258            Integer month=(Integer) months.get(sb.toString().trim());
259            if(month!=null) return month.intValue();
260            pos=start;
261            return -1;
262            }
263        
264        
265        private boolean isMonthChar(char c) {
266            return c>='a' && c<='z' || c>='A' && c<='Z';
267            }
268    
269            /**
270         * returns if c is a digit or not
271         * @param c char to check
272         * @return is digit
273         */
274        public boolean isDigit(char c) {
275            return c>='0' && c<='9';
276            }
277        
278    
279        /**
280         * returns if value at cursor position is a digit or not
281         * @return is dgit
282         */
283        public boolean isDigit() {
284            return isValidIndex() && isDigit(str.charAt(pos));
285        }
286    
287        /**
288         * returns if last char is a digit or not
289         * @return is dgit
290         */
291        public boolean isLastDigit() {
292            return isDigit(str.charAt(str.length()-1));
293        }
294    
295        /**
296         * return char at given position
297         * @param pos postion to get value
298         * @return character from given position
299         */
300        public char charAt(int pos) {
301            return str.charAt(pos);
302        }
303    
304        /**
305         * returns if cursor is on the last position
306         * @return is on last
307         */
308        public boolean isLast() {
309            return pos+1==str.length();
310        }
311    
312        /**
313         * returns if cursor is after the last position
314         * @return is after the last
315         */
316        public boolean isAfterLast() {
317           return pos>=str.length();
318        }
319    
320        /**
321         * returns if cursor is on a valid position
322         * @return is after the last
323         */
324        public boolean isValidIndex() {
325           return pos<str.length();
326        }
327    
328            public char current() {
329                    return str.charAt(pos);
330            }
331            public int getPos() {
332                    return pos;
333            }
334            
335            public void reset(){
336                    pos=0;
337            }
338    
339    }