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 | * RFC2045 Content-Type parser. |
---|
23 | * |
---|
24 | * Created 10/2/2004 |
---|
25 | * by Joe Cheng <code@joecheng.com> |
---|
26 | */ |
---|
27 | |
---|
28 | options { |
---|
29 | STATIC=false; |
---|
30 | LOOKAHEAD=1; |
---|
31 | JDK_VERSION = "1.5"; |
---|
32 | OUTPUT_DIRECTORY = "../../../../../../../../../target/generated-sources/javacc"; |
---|
33 | //DEBUG_PARSER=true; |
---|
34 | //DEBUG_TOKEN_MANAGER=true; |
---|
35 | } |
---|
36 | |
---|
37 | PARSER_BEGIN(ContentTypeParser) |
---|
38 | /**************************************************************** |
---|
39 | * Licensed to the Apache Software Foundation (ASF) under one * |
---|
40 | * or more contributor license agreements. See the NOTICE file * |
---|
41 | * distributed with this work for additional information * |
---|
42 | * regarding copyright ownership. The ASF licenses this file * |
---|
43 | * to you under the Apache License, Version 2.0 (the * |
---|
44 | * "License"); you may not use this file except in compliance * |
---|
45 | * with the License. You may obtain a copy of the License at * |
---|
46 | * * |
---|
47 | * http://www.apache.org/licenses/LICENSE-2.0 * |
---|
48 | * * |
---|
49 | * Unless required by applicable law or agreed to in writing, * |
---|
50 | * software distributed under the License is distributed on an * |
---|
51 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * |
---|
52 | * KIND, either express or implied. See the License for the * |
---|
53 | * specific language governing permissions and limitations * |
---|
54 | * under the License. * |
---|
55 | ****************************************************************/ |
---|
56 | package org.apache.james.mime4j.field.contenttype.parser; |
---|
57 | |
---|
58 | import java.util.List; |
---|
59 | import java.util.ArrayList; |
---|
60 | |
---|
61 | public class ContentTypeParser { |
---|
62 | |
---|
63 | private String type; |
---|
64 | private String subtype; |
---|
65 | private List<String> paramNames = new ArrayList<String>(); |
---|
66 | private List<String> paramValues = new ArrayList<String>(); |
---|
67 | |
---|
68 | public String getType() { return type; } |
---|
69 | public String getSubType() { return subtype; } |
---|
70 | public List<String> getParamNames() { return paramNames; } |
---|
71 | public List<String> getParamValues() { return paramValues; } |
---|
72 | |
---|
73 | public static void main(String args[]) throws ParseException { |
---|
74 | while (true) { |
---|
75 | try { |
---|
76 | ContentTypeParser parser = new ContentTypeParser(System.in); |
---|
77 | parser.parseLine(); |
---|
78 | } catch (Exception x) { |
---|
79 | x.printStackTrace(); |
---|
80 | return; |
---|
81 | } |
---|
82 | } |
---|
83 | } |
---|
84 | } |
---|
85 | |
---|
86 | PARSER_END(ContentTypeParser) |
---|
87 | |
---|
88 | void parseLine() : |
---|
89 | {} |
---|
90 | { |
---|
91 | parse() ["\r"] "\n" |
---|
92 | } |
---|
93 | |
---|
94 | void parseAll() : |
---|
95 | {} |
---|
96 | { |
---|
97 | parse() <EOF> |
---|
98 | } |
---|
99 | |
---|
100 | void parse() : |
---|
101 | { |
---|
102 | Token type; |
---|
103 | Token subtype; |
---|
104 | } |
---|
105 | { |
---|
106 | type=<ATOKEN> "/" subtype=<ATOKEN> |
---|
107 | { |
---|
108 | this.type = type.image; |
---|
109 | this.subtype = subtype.image; |
---|
110 | } |
---|
111 | ( ";" parameter() )* |
---|
112 | } |
---|
113 | |
---|
114 | void parameter() : |
---|
115 | { |
---|
116 | Token attrib; |
---|
117 | String val; |
---|
118 | } |
---|
119 | { |
---|
120 | attrib=<ATOKEN> "=" val=value() |
---|
121 | { |
---|
122 | paramNames.add(attrib.image); |
---|
123 | paramValues.add(val); |
---|
124 | } |
---|
125 | } |
---|
126 | |
---|
127 | String value() : |
---|
128 | {Token t;} |
---|
129 | { |
---|
130 | ( t=<ATOKEN> |
---|
131 | | t=<DIGITS> |
---|
132 | | t=<QUOTEDSTRING> |
---|
133 | ) |
---|
134 | { return t.image; } |
---|
135 | } |
---|
136 | |
---|
137 | |
---|
138 | |
---|
139 | SPECIAL_TOKEN : |
---|
140 | { |
---|
141 | < WS: ( [" ", "\t"] )+ > |
---|
142 | } |
---|
143 | |
---|
144 | TOKEN_MGR_DECLS : |
---|
145 | { |
---|
146 | // Keeps track of how many levels of comment nesting |
---|
147 | // we've encountered. This is only used when the 2nd |
---|
148 | // level is reached, for example ((this)), not (this). |
---|
149 | // This is because the outermost level must be treated |
---|
150 | // specially anyway, because the outermost ")" has a |
---|
151 | // different token type than inner ")" instances. |
---|
152 | static int commentNest; |
---|
153 | } |
---|
154 | |
---|
155 | MORE : |
---|
156 | { |
---|
157 | // starts a comment |
---|
158 | "(" : INCOMMENT |
---|
159 | } |
---|
160 | |
---|
161 | <INCOMMENT> |
---|
162 | SKIP : |
---|
163 | { |
---|
164 | // ends a comment |
---|
165 | < COMMENT: ")" > : DEFAULT |
---|
166 | // if this is ever changed to not be a SKIP, need |
---|
167 | // to make sure matchedToken.token = token.toString() |
---|
168 | // is called. |
---|
169 | } |
---|
170 | |
---|
171 | <INCOMMENT> |
---|
172 | MORE : |
---|
173 | { |
---|
174 | < <QUOTEDPAIR>> { image.deleteCharAt(image.length() - 2); } |
---|
175 | | "(" { commentNest = 1; } : NESTED_COMMENT |
---|
176 | | < <ANY>> |
---|
177 | } |
---|
178 | |
---|
179 | <NESTED_COMMENT> |
---|
180 | MORE : |
---|
181 | { |
---|
182 | < <QUOTEDPAIR>> { image.deleteCharAt(image.length() - 2); } |
---|
183 | | "(" { ++commentNest; } |
---|
184 | | ")" { --commentNest; if (commentNest == 0) SwitchTo(INCOMMENT); } |
---|
185 | | < <ANY>> |
---|
186 | } |
---|
187 | |
---|
188 | |
---|
189 | |
---|
190 | // QUOTED STRINGS |
---|
191 | |
---|
192 | MORE : |
---|
193 | { |
---|
194 | "\"" { image.deleteCharAt(image.length() - 1); } : INQUOTEDSTRING |
---|
195 | } |
---|
196 | |
---|
197 | <INQUOTEDSTRING> |
---|
198 | MORE : |
---|
199 | { |
---|
200 | < <QUOTEDPAIR>> { image.deleteCharAt(image.length() - 2); } |
---|
201 | | < (~["\"", "\\"])+ > |
---|
202 | } |
---|
203 | |
---|
204 | <INQUOTEDSTRING> |
---|
205 | TOKEN : |
---|
206 | { |
---|
207 | < QUOTEDSTRING: "\"" > { matchedToken.image = image.substring(0, image.length() - 1); } : DEFAULT |
---|
208 | } |
---|
209 | |
---|
210 | |
---|
211 | TOKEN : |
---|
212 | { |
---|
213 | < DIGITS: ( ["0"-"9"] )+ > |
---|
214 | } |
---|
215 | |
---|
216 | TOKEN : |
---|
217 | { |
---|
218 | < ATOKEN: ( ~[" ", "\t", "(", ")", "<", ">", "@", ",", ";", ":", "\\", "\"", "/", "[", "]", "?", "="] )+ > |
---|
219 | } |
---|
220 | |
---|
221 | |
---|
222 | // GLOBALS |
---|
223 | |
---|
224 | <*> |
---|
225 | TOKEN : |
---|
226 | { |
---|
227 | < #QUOTEDPAIR: "\\" <ANY> > |
---|
228 | | < #ANY: ~[] > |
---|
229 | } |
---|