source: contrib/MailArchiver/sources/vendor/mime4j/custom/dom/src/main/javacc/org/apache/james/mime4j/field/datetime/DateTimeParser.jj @ 6785

Revision 6785, 12.2 KB checked in by rafaelraymundo, 12 years ago (diff)

Ticket #2946 - Liberado codigo do MailArchiver?. Documentação na subpasta DOCS.

Line 
1/****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one   *
3 * or more contributor license agreements.  See the NOTICE file *
4 * distributed with this work for additional information        *
5 * regarding copyright ownership.  The ASF licenses this file   *
6 * to you under the Apache License, Version 2.0 (the            *
7 * "License"); you may not use this file except in compliance   *
8 * with the License.  You may obtain a copy of the License at   *
9 *                                                              *
10 *   http://www.apache.org/licenses/LICENSE-2.0                 *
11 *                                                              *
12 * Unless required by applicable law or agreed to in writing,   *
13 * software distributed under the License is distributed on an  *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15 * KIND, either express or implied.  See the License for the    *
16 * specific language governing permissions and limitations      *
17 * under the License.                                           *
18 ****************************************************************/
19
20
21/**
22 * RFC2822 date parser.
23 *
24 * Created 9/28/2004
25 * by Joe Cheng <code@joecheng.com>
26 */
27
28options {
29        STATIC=false;
30        LOOKAHEAD=1;
31        IGNORE_CASE=true;
32        JDK_VERSION = "1.5";
33        OUTPUT_DIRECTORY = "../../../../../../../../../target/generated-sources/javacc";
34        //DEBUG_PARSER=true;
35        //DEBUG_TOKEN_MANAGER=true;
36}
37
38PARSER_BEGIN(DateTimeParser)
39/****************************************************************
40 * Licensed to the Apache Software Foundation (ASF) under one   *
41 * or more contributor license agreements.  See the NOTICE file *
42 * distributed with this work for additional information        *
43 * regarding copyright ownership.  The ASF licenses this file   *
44 * to you under the Apache License, Version 2.0 (the            *
45 * "License"); you may not use this file except in compliance   *
46 * with the License.  You may obtain a copy of the License at   *
47 *                                                              *
48 *   http://www.apache.org/licenses/LICENSE-2.0                 *
49 *                                                              *
50 * Unless required by applicable law or agreed to in writing,   *
51 * software distributed under the License is distributed on an  *
52 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
53 * KIND, either express or implied.  See the License for the    *
54 * specific language governing permissions and limitations      *
55 * under the License.                                           *
56 ****************************************************************/
57package org.apache.james.mime4j.field.datetime.parser;
58
59import org.apache.james.mime4j.dom.datetime.DateTime;
60
61public class DateTimeParser {
62    private static final boolean ignoreMilitaryZoneOffset = true;
63
64    public static void main(String args[]) throws ParseException {
65                while (true) {
66                    try {
67                                DateTimeParser parser = new DateTimeParser(System.in);
68                        parser.parseLine();
69                    } catch (Exception x) {
70                                x.printStackTrace();
71                                return;
72                    }
73                }
74    }
75
76    private static int parseDigits(Token token) {
77        return Integer.parseInt(token.image, 10);
78    }
79
80    private static int getMilitaryZoneOffset(char c) {
81        if (ignoreMilitaryZoneOffset)
82            return 0;
83
84        c = Character.toUpperCase(c);
85
86        switch (c) {
87            case 'A': return 1;
88            case 'B': return 2;
89            case 'C': return 3;
90            case 'D': return 4;
91            case 'E': return 5;
92            case 'F': return 6;
93            case 'G': return 7;
94            case 'H': return 8;
95            case 'I': return 9;
96            case 'K': return 10;
97            case 'L': return 11;
98            case 'M': return 12;
99
100            case 'N': return -1;
101            case 'O': return -2;
102            case 'P': return -3;
103            case 'Q': return -4;
104            case 'R': return -5;
105            case 'S': return -6;
106            case 'T': return -7;
107            case 'U': return -8;
108            case 'V': return -9;
109            case 'W': return -10;
110            case 'X': return -11;
111            case 'Y': return -12;
112
113            case 'Z': return 0;
114            default: return 0;
115        }
116    }
117}
118
119PARSER_END(DateTimeParser)
120
121DateTime parseLine() :
122{DateTime dt;}
123{
124        dt=date_time() ["\r"] "\n"
125        { return dt; }
126}
127
128DateTime parseAll() :
129{DateTime dt;}
130{
131        dt=date_time() <EOF>
132        { return dt; }
133}
134
135DateTime date_time() :
136{String y; int m, d, h, m2, s=0, z;}
137{
138(       LOOKAHEAD(2)
139        [ day_of_week() "," ] d=day() [<MINUS>] m=month() [<MINUS>] y=year() h=hour() ":" m2=minute() [ ":" s=second() ] z=zone()
140|
141        m=month() d=day() h=hour() ":" m2=minute() [ ":" s=second() ] y=year() z=zone()
142|
143        day_of_week() m=month() d=day() h=hour() ":" m2=minute() [ ":" s=second() ] z=zone() y=year()
144)
145        {
146            return new DateTime(y, m, d, h, m2, s, z);
147        }
148}
149
150String day_of_week() :
151{}
152{
153(   "Domingo" | "Dom" | "Sunday" | "Sun"
154|   "Segunda" | "Seg" | "Monday" | "Mon" | "Lunes" | "Lun"
155|   "Terça" | "Terca" | "Ter" | "Tuesday" | "Tue" | "Marte" | "Mar"
156|   "Quarta" | "Qua" | "Wednesday" | "Wed" | "Miércoles" | "Miercoles" | "Mie"
157|   "Quinta" | "Qui" | "Thursday" | "Thu" | "Jueves" | "Jue"
158|   "Sexta" | "Sex" | "Friday" | "Fri" | "Viernes" | "Vie"
159|   "Sábado" | "Sabado" | "Sáb" | "Sab" | "Saturday" | "Sat"
160)
161    { return token.image; }
162}
163
164int day() :
165{Token t;}
166{
167    t=<DIGITS> { return parseDigits(t); }
168}
169
170int month() :
171{}
172{
173    "Janeiro"    { return  1; }
174|   "Jan"        { return  1; }
175|   "January"    { return  1; }
176|   "Enero"      { return  1; }
177|   "Ene"        { return  1; }
178|   "Fevereiro"  { return  2; }
179|   "Fev"        { return  2; }
180|   "February"   { return  2; }
181|   "Feb"        { return  2; }
182|   "Febrero"    { return  2; }
183|   "Março"      { return  3; }
184|   "Marco"      { return  3; }
185|   "Mar"        { return  3; }
186|   "March"      { return  3; }
187|   "Marzo"      { return  3; }
188|   "Abril"      { return  4; }
189|   "Abr"        { return  4; }
190|   "April"      { return  4; }
191|   "Apr"        { return  4; }
192|   "Maio"       { return  5; }
193|   "Mai"        { return  5; }
194|   "May"        { return  5; }
195|   "Mayo"       { return  5; }
196|   "Junho"      { return  6; }
197|   "Jun"        { return  6; }
198|   "June"       { return  6; }
199|   "Junio"      { return  6; }
200|   "Julho"      { return  7; }
201|   "Jul"        { return  7; }
202|   "July"       { return  7; }
203|   "Julio"      { return  7; }
204|   "Agosto"     { return  8; }
205|   "Ago"        { return  8; }
206|   "August"     { return  8; }
207|   "Aug"        { return  8; }
208|   "Setembro"   { return  9; }
209|   "Set"        { return  9; }
210|   "September"  { return  9; }
211|   "Sep"        { return  9; }
212|   "Septiembre" { return  9; }
213|   "Outubro"    { return 10; }
214|   "Out"        { return 10; }
215|   "October"    { return 10; }
216|   "Oct"        { return 10; }
217|   "Octubre"    { return 10; }
218|   "Novembro"   { return 11; }
219|   "Nov"        { return 11; }
220|   "November"   { return 11; }
221|   "Noviembre"  { return 11; }
222|   "Dezembro"   { return 12; }
223|   "Dez"        { return 12; }
224|   "December"   { return 12; }
225|   "Dec"        { return 12; }
226|   "Diciembre"  { return 12; }
227|   "Dic"        { return 12; }
228}
229
230String year() :
231{Token t;}
232{
233    t=<DIGITS> { return t.image; }
234}
235
236int hour() :
237{Token t;}
238{
239    t=<DIGITS> { return parseDigits(t); }
240}
241
242int minute() :
243{Token t;}
244{
245    t=<DIGITS> { return parseDigits(t); }
246}
247
248int second() :
249{Token t;}
250{
251    t=<DIGITS> { return parseDigits(t); }
252}
253
254int zone() :
255{Token u; int z; boolean neg=false; }
256{
257(   ( <PLUS> | <MINUS> {neg=true;} ) u=<DIGITS> { z=parseDigits(u); if(neg) z*=(-1); }
258|   z=obs_zone()
259)+
260    { return z; }
261}
262
263int obs_zone() :
264{Token t; int z;}
265{
266(
267// Dateline (Eniwetok, Kwajalein)
268    "ZW12" { z = -1200; }
269
270// Samoa
271|   "SST"  { z = -1100; }
272
273// Hawaii
274|   "BST"  { z = -1000; }
275
276// Alaskan
277|   "YST"  { z =  -900; }
278|   "YDT"  { z =  -800; }
279
280// Pacific Time (US & Canada, Tijuana)
281|   "PST"  { z =  -800; }
282|   "PDT"  { z =  -700; }
283
284// Mountain Time (US & Canada)
285// US Mountain (Arizona)
286|   "MST"  { z =  -700; }
287|   "MDT"  { z =  -600; }
288
289// Canada Central (Saskatchewan)
290// Central Time (US & Canada)
291// Mexico (Mexico City)
292// Central America
293|   "CST"  { z =  -600; }
294|   "CDT"  { z =  -500; }
295
296// Eastern (US & Canada)
297// US Eastern (Indiana (East))
298// SA Pacific (Bogota, Lima, Quito)
299|   "EST"  { z =  -500; }
300|   "EDT"  { z =  -400; }
301
302// Atlantic (Canada)
303// Pacific SA (Santiago)
304// SA Western (Caracas, La Paz)
305|   "AST"  { z =  -400; }
306|   "ADT"  { z =  -300; }
307
308// Newfoundland
309|   "NST" { z =  -330; }
310|   "NDT" { z =  -230; }
311
312// SA Eastern Standard Time (Brasilia, Buenos Aires, Georgetown)
313// Greenland Standard Time (Greenland)
314|   "ZW3"  { z =  -300; }
315|   "BRT"  { z =  -300; }
316
317// Mid-Atlantic
318|   "ZW2"  { z =  -200; }
319|   "YW2"  { z =  -200; }
320
321// Azores, Cape Verde
322|   "ZW1"  { z =  -100; }
323|   "YW1"  { z =  -100; }
324
325// GMT (Greenwich Mean Time)
326// (Dublin, Edinburgh, Lisbon, London, Casablanca, Monrovia)
327|   "GMT"  { z =   0; }
328|   "GDT"  { z =   0; }
329|   "UT"   { z =   0; }
330
331// Central Europe (Belgrade, Bratislava, Budapest, Ljubljana,
332// Prague, Sarajevo, Skopje, Sofija, Vilnius, Warsaw, Zagreb)
333// Romance (Brussels, Copenhagen, Madrid, Paris)
334// West Central Africa
335// West Europe Standard Time (Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna)
336|   "CET"  { z =   100; }
337|   "CEDT" { z =   200; }
338
339// Eastern Europe (Bucharest)
340// Egypt (Cairo)
341// FLE (Helsinki, Riga, Tallinn)
342// GTB (Athens, Istanbul, Minsk)
343// Jerusalem
344// South Africa (Harare, Pretoria)
345|   "ZE2"  { z =   200; }
346
347// Arabic (Kuwait, Riyadh, Baghdad)
348// Eastern Africa (Nairobi)
349// Russian (Moscow, St. Petersburg, Volgograd)
350|   "ZE3"  { z =   300; }
351
352// Iran (Tehran)
353|   "ZE3B" { z =   330; }
354
355// Arabian (Abu Dhabi, Muscat)
356// Caucasus (Baku, Tbilisi, Yerevan)
357|   "ZE4"  { z =   400; }
358
359// Afghanistan (Kabul)
360|   "ZE4B" { z =   430; }
361
362// Ekaterinburg
363// West Asia (Islamabad, Karachi, Tashkent)
364|   "ZE5"  { z =   500; }
365
366// India Standard Time (Calcutta, Chennai, Mumbai, New Delhi)
367|   "ZE5B"  { z =   530; }
368
369// Nepal (Kathmandu)
370|   "ZE5C"  { z =   545; }
371
372// Central Asia (Astana, Dhaka, Almaty, Novosibirsk)
373// Sri Lanka (Sri Jayawardenepura)
374|   "ZE6"  { z =   600; }
375
376// Myanmar (Rangoon)
377|   "ZE6B"  { z =   630; }
378
379// Asia (Krasnoyarsk Bangkok, Hanoi, Jakarta)
380|   "ZE7"  { z =   700; }
381
382// China (Beijing, Chongqing, Hong Kong, Urumqi)
383// North Asia East (Irkutsk, Ulaan Bataar)
384// Malay Peninsula (Kuala Lumpur, Singapore)
385// Taipei
386// Western Australia (Perth)
387|   "ZE8"  { z =   800; }
388
389// Korea (Seoul)
390// Japan (Osaka, Sapporo, Tokyo)
391// Yakutsk
392|   "ZE9"  { z =   900; }
393
394// Central Australia (Adelaide, Darwin)
395|   "ZE9B"  { z =  930; }
396
397// Eastern Australia (Canberra, Melbourne, Sydney, Brisbane)
398// Tasmania (Hobart)
399// Vladivostok
400// West Pacific (Guam, Port Moresby)
401|   "ZE10" { z =  1000; }
402
403// Central Pacific (Magadan, Solomon Is., New Caledonia)
404|   "ZE11" { z =  1100; }
405
406// Fiji, Kamchatka, Marshall Is.
407// New Zealand (Auckland, Wellington)
408|   "ZE12" { z =  1200; }
409
410// Tonga (Nuku'alofa)
411|   "ZE13" { z =  1300; }
412
413|   t=< MILITARY_ZONE: ["A"-"I","a"-"i","K"-"Z","k"-"z"] > { z=getMilitaryZoneOffset(t.image.charAt(0)); }
414)
415    { return z; }
416}
417
418SPECIAL_TOKEN :
419{
420        < WS: ( [" ", "\t"] )+ >
421}
422
423TOKEN_MGR_DECLS :
424{
425        // Keeps track of how many levels of comment nesting
426        // we've encountered.  This is only used when the 2nd
427        // level is reached, for example ((this)), not (this).
428        // This is because the outermost level must be treated
429        // specially anyway, because the outermost ")" has a
430        // different token type than inner ")" instances.
431        static int commentNest;
432}
433
434MORE :
435{
436        // starts a comment
437        "(" : INCOMMENT
438}
439
440<INCOMMENT>
441SKIP :
442{
443        // ends a comment
444        < COMMENT: ")" > : DEFAULT
445        // if this is ever changed to not be a SKIP, need
446        // to make sure matchedToken.token = token.toString()
447        // is called.
448}
449
450<INCOMMENT>
451MORE :
452{
453        < <QUOTEDPAIR>> { image.deleteCharAt(image.length() - 2); }
454|       "(" { commentNest = 1; } : NESTED_COMMENT
455|       < <ANY>>
456}
457
458<NESTED_COMMENT>
459MORE :
460{
461        < <QUOTEDPAIR>> { image.deleteCharAt(image.length() - 2); }
462|       "(" { ++commentNest; }
463|       ")" { --commentNest; if (commentNest == 0) SwitchTo(INCOMMENT); }
464|       < <ANY>>
465}
466
467TOKEN :
468{
469    < DIGITS: ( ["0"-"9"] )+ >
470|   < PLUS: "+" >
471|   < MINUS: "-" >
472}
473
474// GLOBALS
475
476<*>
477TOKEN :
478{
479        < #QUOTEDPAIR: "\\" <ANY> >
480|       < #ANY: ~[] >
481}
Note: See TracBrowser for help on using the repository browser.