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 | options { |
---|
21 | static=false; |
---|
22 | JDK_VERSION = "1.5"; |
---|
23 | OUTPUT_DIRECTORY = "../../../../../../../../../target/generated-sources/javacc"; |
---|
24 | //DEBUG_PARSER = true; |
---|
25 | //DEBUG_TOKEN_MANAGER = true; |
---|
26 | } |
---|
27 | |
---|
28 | PARSER_BEGIN(StructuredFieldParser) |
---|
29 | /**************************************************************** |
---|
30 | * Licensed to the Apache Software Foundation (ASF) under one * |
---|
31 | * or more contributor license agreements. See the NOTICE file * |
---|
32 | * distributed with this work for additional information * |
---|
33 | * regarding copyright ownership. The ASF licenses this file * |
---|
34 | * to you under the Apache License, Version 2.0 (the * |
---|
35 | * "License"); you may not use this file except in compliance * |
---|
36 | * with the License. You may obtain a copy of the License at * |
---|
37 | * * |
---|
38 | * http://www.apache.org/licenses/LICENSE-2.0 * |
---|
39 | * * |
---|
40 | * Unless required by applicable law or agreed to in writing, * |
---|
41 | * software distributed under the License is distributed on an * |
---|
42 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * |
---|
43 | * KIND, either express or implied. See the License for the * |
---|
44 | * specific language governing permissions and limitations * |
---|
45 | * under the License. * |
---|
46 | ****************************************************************/ |
---|
47 | package org.apache.james.mime4j.field.structured.parser; |
---|
48 | |
---|
49 | /** |
---|
50 | * Parses generic structure fields. |
---|
51 | * Unfolds and removes comments. |
---|
52 | */ |
---|
53 | public class StructuredFieldParser { |
---|
54 | |
---|
55 | private boolean preserveFolding = false; |
---|
56 | |
---|
57 | /** |
---|
58 | * Should the \r\n folding sequence be preserved? |
---|
59 | */ |
---|
60 | public boolean isFoldingPreserved() { |
---|
61 | return preserveFolding; |
---|
62 | } |
---|
63 | |
---|
64 | /** |
---|
65 | * Sets whether the \r\n folding sequence should be preserved. |
---|
66 | */ |
---|
67 | public void setFoldingPreserved(boolean preserveFolding) { |
---|
68 | this.preserveFolding = preserveFolding; |
---|
69 | } |
---|
70 | |
---|
71 | /** |
---|
72 | * Unfolds the input and removes comments. |
---|
73 | * @return unfolded header value with comments removed |
---|
74 | */ |
---|
75 | public String parse() throws ParseException { |
---|
76 | try { |
---|
77 | return doParse(); |
---|
78 | } catch (TokenMgrError e) { |
---|
79 | // An issue with the TOKENiser |
---|
80 | // but it's not polite to throw an Error |
---|
81 | // when executing on a server |
---|
82 | throw new ParseException(e); |
---|
83 | } |
---|
84 | } |
---|
85 | } |
---|
86 | PARSER_END(StructuredFieldParser) |
---|
87 | |
---|
88 | private String doParse() : |
---|
89 | { |
---|
90 | Token t; |
---|
91 | StringBuffer buffer = new StringBuffer(50); |
---|
92 | boolean whitespace = false; |
---|
93 | boolean first = true; |
---|
94 | } |
---|
95 | { |
---|
96 | ( |
---|
97 | t = <CONTENT> |
---|
98 | { |
---|
99 | if (first) { |
---|
100 | first = false; |
---|
101 | } else if (whitespace) { |
---|
102 | buffer.append(" "); |
---|
103 | whitespace = false; |
---|
104 | } |
---|
105 | buffer.append(t.image); |
---|
106 | } |
---|
107 | | |
---|
108 | t = <STRING_CONTENT> |
---|
109 | { |
---|
110 | buffer.append(t.image); |
---|
111 | } |
---|
112 | | |
---|
113 | t = <QUOTEDSTRING> |
---|
114 | { |
---|
115 | if (first) { |
---|
116 | first = false; |
---|
117 | } else if (whitespace) { |
---|
118 | buffer.append(" "); |
---|
119 | whitespace = false; |
---|
120 | } |
---|
121 | buffer.append(t.image); |
---|
122 | } |
---|
123 | | |
---|
124 | t = <FOLD> |
---|
125 | { |
---|
126 | if (preserveFolding) buffer.append("\r\n"); |
---|
127 | } |
---|
128 | | |
---|
129 | t = <WS> |
---|
130 | { |
---|
131 | whitespace = true; |
---|
132 | } |
---|
133 | )* |
---|
134 | {return buffer.toString();} |
---|
135 | } |
---|
136 | |
---|
137 | TOKEN_MGR_DECLS : |
---|
138 | { |
---|
139 | // Keeps track of how many levels of comment nesting |
---|
140 | // we've encountered. This is only used when the 2nd |
---|
141 | // level is reached, for example ((this)), not (this). |
---|
142 | // This is because the outermost level must be treated |
---|
143 | // specially anyway, because the outermost ")" has a |
---|
144 | // different token type than inner ")" instances. |
---|
145 | int commentNest; |
---|
146 | } |
---|
147 | |
---|
148 | |
---|
149 | SKIP : |
---|
150 | { |
---|
151 | // starts a comment |
---|
152 | "(" : INCOMMENT |
---|
153 | } |
---|
154 | |
---|
155 | <INCOMMENT> SKIP : |
---|
156 | { |
---|
157 | // ends a comment |
---|
158 | ")" : DEFAULT |
---|
159 | // if this is ever changed to not be a SKIP, need |
---|
160 | // to make sure matchedToken.token = token.toString() |
---|
161 | // is called. |
---|
162 | } |
---|
163 | |
---|
164 | <INCOMMENT> SKIP : |
---|
165 | { |
---|
166 | "(" { commentNest = 1; } : NESTED_COMMENT |
---|
167 | } |
---|
168 | |
---|
169 | <INCOMMENT> SKIP : |
---|
170 | { |
---|
171 | <~[ "(", ")" ]> |
---|
172 | } |
---|
173 | |
---|
174 | <NESTED_COMMENT> SKIP: |
---|
175 | { |
---|
176 | "(" { ++commentNest; } |
---|
177 | | ")" { --commentNest; if (commentNest == 0) SwitchTo(INCOMMENT); } |
---|
178 | } |
---|
179 | |
---|
180 | <NESTED_COMMENT> SKIP : |
---|
181 | { |
---|
182 | < <QUOTEDPAIR>> { image.deleteCharAt(image.length() - 2); } |
---|
183 | } |
---|
184 | |
---|
185 | <NESTED_COMMENT> SKIP: |
---|
186 | { |
---|
187 | <~[ "(", ")" ]> |
---|
188 | } |
---|
189 | |
---|
190 | // QUOTED STRINGS |
---|
191 | |
---|
192 | SKIP : |
---|
193 | { |
---|
194 | "\"" : INQUOTEDSTRING |
---|
195 | } |
---|
196 | |
---|
197 | <INQUOTEDSTRING> MORE : |
---|
198 | { |
---|
199 | < <QUOTEDPAIR> > { image.deleteCharAt(image.length() - 2); } |
---|
200 | } |
---|
201 | |
---|
202 | <INQUOTEDSTRING> TOKEN : |
---|
203 | { |
---|
204 | < STRING_CONTENT : (~["\"", "\\", "\r"])+ > |
---|
205 | // Preserve line break within quotes but not trailing white space |
---|
206 | | < FOLD: "\r\n" ( [" ", "\t"] )* > |
---|
207 | | < QUOTEDSTRING: "\"" > { matchedToken.image = image.substring(0, image.length() - 1); } : DEFAULT |
---|
208 | } |
---|
209 | |
---|
210 | <DEFAULT> |
---|
211 | TOKEN : |
---|
212 | { |
---|
213 | < WS: ( [" ", "\t", "\r", "\n"] )+ > |
---|
214 | } |
---|
215 | |
---|
216 | <DEFAULT> |
---|
217 | TOKEN : |
---|
218 | { |
---|
219 | < CONTENT: (~[" ", "\t", "\r", "\n", "(", "\""])+ > |
---|
220 | } |
---|
221 | |
---|
222 | <*> |
---|
223 | TOKEN : |
---|
224 | { |
---|
225 | < #QUOTEDPAIR: "\\" <ANY> > |
---|
226 | | < #ANY: (~[])+ > |
---|
227 | } |
---|