1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/net/fortuna/ical4j/data/CalendarParserImpl.java Tue Feb 10 18:12:00 2015 +0100 1.3 @@ -0,0 +1,521 @@ 1.4 +/** 1.5 + * Copyright (c) 2012, Ben Fortuna 1.6 + * All rights reserved. 1.7 + * 1.8 + * Redistribution and use in source and binary forms, with or without 1.9 + * modification, are permitted provided that the following conditions 1.10 + * are met: 1.11 + * 1.12 + * o Redistributions of source code must retain the above copyright 1.13 + * notice, this list of conditions and the following disclaimer. 1.14 + * 1.15 + * o Redistributions in binary form must reproduce the above copyright 1.16 + * notice, this list of conditions and the following disclaimer in the 1.17 + * documentation and/or other materials provided with the distribution. 1.18 + * 1.19 + * o Neither the name of Ben Fortuna nor the names of any other contributors 1.20 + * may be used to endorse or promote products derived from this software 1.21 + * without specific prior written permission. 1.22 + * 1.23 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.24 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.25 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.26 + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 1.27 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 1.28 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 1.29 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 1.30 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 1.31 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 1.32 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 1.33 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.34 + */ 1.35 +package net.fortuna.ical4j.data; 1.36 + 1.37 +import java.io.IOException; 1.38 +import java.io.InputStream; 1.39 +import java.io.InputStreamReader; 1.40 +import java.io.Reader; 1.41 +import java.io.StreamTokenizer; 1.42 +import java.net.URISyntaxException; 1.43 +import java.text.MessageFormat; 1.44 +import java.text.ParseException; 1.45 + 1.46 +import net.fortuna.ical4j.model.Calendar; 1.47 +import net.fortuna.ical4j.model.Component; 1.48 + 1.49 +import org.apache.commons.logging.Log; 1.50 +import org.apache.commons.logging.LogFactory; 1.51 + 1.52 +/** 1.53 + * <pre> 1.54 + * $Id$ 1.55 + * 1.56 + * Created [Nov 5, 2004] 1.57 + * </pre> 1.58 + * 1.59 + * The default implementation of a calendar parser. 1.60 + * @author Ben Fortuna 1.61 + */ 1.62 +public class CalendarParserImpl implements CalendarParser { 1.63 + 1.64 + private static final int WORD_CHAR_START = 32; 1.65 + 1.66 + private static final int WORD_CHAR_END = 255; 1.67 + 1.68 + private static final int WHITESPACE_CHAR_START = 0; 1.69 + 1.70 + private static final int WHITESPACE_CHAR_END = 20; 1.71 + 1.72 + private static final String UNEXPECTED_TOKEN_MESSAGE = "Expected [{0}], read [{1}]"; 1.73 + 1.74 + private Log log = LogFactory.getLog(CalendarParserImpl.class); 1.75 + 1.76 + private final ComponentListParser componentListParser = new ComponentListParser(); 1.77 + 1.78 + private final ComponentParser componentParser = new ComponentParser(); 1.79 + 1.80 + private final PropertyListParser propertyListParser = new PropertyListParser(); 1.81 + 1.82 + private final PropertyParser propertyParser = new PropertyParser(); 1.83 + 1.84 + private final ParameterListParser paramListParser = new ParameterListParser(); 1.85 + 1.86 + private final ParameterParser paramParser = new ParameterParser(); 1.87 + 1.88 + /** 1.89 + * {@inheritDoc} 1.90 + */ 1.91 + public final void parse(final InputStream in, final ContentHandler handler) 1.92 + throws IOException, ParserException { 1.93 + parse(new InputStreamReader(in), handler); 1.94 + } 1.95 + 1.96 + /** 1.97 + * {@inheritDoc} 1.98 + */ 1.99 + public final void parse(final Reader in, final ContentHandler handler) 1.100 + throws IOException, ParserException { 1.101 + 1.102 + final StreamTokenizer tokeniser = new StreamTokenizer(in); 1.103 + try { 1.104 + tokeniser.resetSyntax(); 1.105 + tokeniser.wordChars(WORD_CHAR_START, WORD_CHAR_END); 1.106 + tokeniser.whitespaceChars(WHITESPACE_CHAR_START, 1.107 + WHITESPACE_CHAR_END); 1.108 + tokeniser.ordinaryChar(':'); 1.109 + tokeniser.ordinaryChar(';'); 1.110 + tokeniser.ordinaryChar('='); 1.111 + tokeniser.ordinaryChar('\t'); 1.112 + tokeniser.eolIsSignificant(true); 1.113 + tokeniser.whitespaceChars(0, 0); 1.114 + tokeniser.quoteChar('"'); 1.115 + 1.116 + // BEGIN:VCALENDAR 1.117 + assertToken(tokeniser, in, Calendar.BEGIN); 1.118 + 1.119 + assertToken(tokeniser, in, ':'); 1.120 + 1.121 + assertToken(tokeniser, in, Calendar.VCALENDAR, true); 1.122 + 1.123 + assertToken(tokeniser, in, StreamTokenizer.TT_EOL); 1.124 + 1.125 + handler.startCalendar(); 1.126 + 1.127 + // parse calendar properties.. 1.128 + propertyListParser.parse(tokeniser, in, handler); 1.129 + 1.130 + // parse components.. 1.131 + componentListParser.parse(tokeniser, in, handler); 1.132 + 1.133 + // END:VCALENDAR 1.134 + // assertToken(tokeniser,Calendar.END); 1.135 + 1.136 + assertToken(tokeniser, in, ':'); 1.137 + 1.138 + assertToken(tokeniser, in, Calendar.VCALENDAR, true); 1.139 + 1.140 + handler.endCalendar(); 1.141 + } 1.142 + catch (Exception e) { 1.143 + 1.144 + if (e instanceof IOException) { 1.145 + throw (IOException) e; 1.146 + } 1.147 + if (e instanceof ParserException) { 1.148 + throw (ParserException) e; 1.149 + } 1.150 + else { 1.151 + throw new ParserException(e.getMessage(), getLineNumber(tokeniser, in), e); 1.152 + } 1.153 + } 1.154 + } 1.155 + 1.156 + /** 1.157 + * Parses an iCalendar property list from the specified stream tokeniser. 1.158 + * @param tokeniser 1.159 + * @throws IOException 1.160 + * @throws ParseException 1.161 + * @throws URISyntaxException 1.162 + * @throws URISyntaxException 1.163 + * @throws ParserException 1.164 + */ 1.165 + private class PropertyListParser { 1.166 + 1.167 + public void parse(final StreamTokenizer tokeniser, Reader in, 1.168 + final ContentHandler handler) throws IOException, ParseException, 1.169 + URISyntaxException, ParserException { 1.170 + 1.171 + assertToken(tokeniser, in, StreamTokenizer.TT_WORD); 1.172 + 1.173 + while (/* 1.174 + * !Component.BEGIN.equals(tokeniser.sval) && 1.175 + */!Component.END.equals(tokeniser.sval)) { 1.176 + // check for timezones observances or vevent/vtodo alarms.. 1.177 + if (Component.BEGIN.equals(tokeniser.sval)) { 1.178 + componentParser.parse(tokeniser, in, handler); 1.179 + } 1.180 + else { 1.181 + propertyParser.parse(tokeniser, in, handler); 1.182 + } 1.183 + absorbWhitespace(tokeniser); 1.184 + // assertToken(tokeniser, StreamTokenizer.TT_WORD); 1.185 + } 1.186 + } 1.187 + } 1.188 + 1.189 + /** 1.190 + * Parses an iCalendar property from the specified stream tokeniser. 1.191 + * @param tokeniser 1.192 + * @throws IOException 1.193 + * @throws ParserException 1.194 + * @throws URISyntaxException 1.195 + * @throws ParseException 1.196 + */ 1.197 + private class PropertyParser { 1.198 + 1.199 + private static final String PARSE_DEBUG_MESSAGE = "Property [{0}]"; 1.200 + 1.201 + private static final String PARSE_EXCEPTION_MESSAGE = "Property [{0}]"; 1.202 + 1.203 + private void parse(final StreamTokenizer tokeniser, Reader in, 1.204 + final ContentHandler handler) throws IOException, ParserException, 1.205 + URISyntaxException, ParseException { 1.206 + 1.207 + final String name = tokeniser.sval; 1.208 + 1.209 + // debugging.. 1.210 + if (log.isDebugEnabled()) { 1.211 + log.debug(MessageFormat.format(PARSE_DEBUG_MESSAGE, new Object[] {name})); 1.212 + } 1.213 + 1.214 + handler.startProperty(name); 1.215 + 1.216 + paramListParser.parse(tokeniser, in, handler); 1.217 + 1.218 + // it appears that control tokens (ie. ':') are allowed 1.219 + // after the first instance on a line is used.. as such 1.220 + // we must continue appending to value until EOL is 1.221 + // reached.. 1.222 + // assertToken(tokeniser, StreamTokenizer.TT_WORD); 1.223 + 1.224 + // String value = tokeniser.sval; 1.225 + final StringBuffer value = new StringBuffer(); 1.226 + 1.227 + // assertToken(tokeniser,StreamTokenizer.TT_EOL); 1.228 + 1.229 + // DQUOTE is ordinary char for property value 1.230 + // From sec 4.3.11 of rfc-2445: 1.231 + // text = *(TSAFE-CHAR / ":" / DQUOTE / ESCAPED-CHAR) 1.232 + // 1.233 + tokeniser.ordinaryChar('"'); 1.234 + int nextToken = tokeniser.nextToken(); 1.235 + 1.236 + while (nextToken != StreamTokenizer.TT_EOL 1.237 + && nextToken != StreamTokenizer.TT_EOF) { 1.238 + 1.239 + if (tokeniser.ttype == StreamTokenizer.TT_WORD) { 1.240 + value.append(tokeniser.sval); 1.241 + } 1.242 + else { 1.243 + value.append((char) tokeniser.ttype); 1.244 + } 1.245 + 1.246 + nextToken = tokeniser.nextToken(); 1.247 + } 1.248 + 1.249 + // reset DQUOTE to be quote char 1.250 + tokeniser.quoteChar('"'); 1.251 + 1.252 + if (nextToken == StreamTokenizer.TT_EOF) { 1.253 + throw new ParserException("Unexpected end of file", 1.254 + getLineNumber(tokeniser, in)); 1.255 + } 1.256 + 1.257 + try { 1.258 + handler.propertyValue(value.toString()); 1.259 + } 1.260 + catch (ParseException e) { 1.261 + final ParseException eNew = new ParseException("[" + name + "] " 1.262 + + e.getMessage(), e.getErrorOffset()); 1.263 + eNew.initCause(e); 1.264 + throw eNew; 1.265 + } 1.266 + 1.267 + handler.endProperty(name); 1.268 + 1.269 + } 1.270 + } 1.271 + 1.272 + /** 1.273 + * Parses a list of iCalendar parameters by parsing the specified stream tokeniser. 1.274 + * @param tokeniser 1.275 + * @throws IOException 1.276 + * @throws ParserException 1.277 + * @throws URISyntaxException 1.278 + */ 1.279 + private class ParameterListParser { 1.280 + 1.281 + public void parse(final StreamTokenizer tokeniser, Reader in, 1.282 + final ContentHandler handler) throws IOException, ParserException, 1.283 + URISyntaxException { 1.284 + 1.285 + while (tokeniser.nextToken() == ';') { 1.286 + paramParser.parse(tokeniser, in, handler); 1.287 + } 1.288 + } 1.289 + } 1.290 + 1.291 + /** 1.292 + * @param tokeniser 1.293 + * @param handler 1.294 + * @throws IOException 1.295 + * @throws ParserException 1.296 + * @throws URISyntaxException 1.297 + */ 1.298 + private class ParameterParser { 1.299 + 1.300 + private void parse(final StreamTokenizer tokeniser, Reader in, 1.301 + final ContentHandler handler) throws IOException, ParserException, 1.302 + URISyntaxException { 1.303 + 1.304 + assertToken(tokeniser, in, StreamTokenizer.TT_WORD); 1.305 + 1.306 + final String paramName = tokeniser.sval; 1.307 + 1.308 + // debugging.. 1.309 + if (log.isDebugEnabled()) { 1.310 + log.debug("Parameter [" + paramName + "]"); 1.311 + } 1.312 + 1.313 + assertToken(tokeniser, in, '='); 1.314 + 1.315 + final StringBuffer paramValue = new StringBuffer(); 1.316 + 1.317 + // preserve quote chars.. 1.318 + if (tokeniser.nextToken() == '"') { 1.319 + paramValue.append('"'); 1.320 + paramValue.append(tokeniser.sval); 1.321 + paramValue.append('"'); 1.322 + } 1.323 + else if (tokeniser.sval != null) { 1.324 + paramValue.append(tokeniser.sval); 1.325 + // check for additional words to account for equals (=) in param-value 1.326 + int nextToken = tokeniser.nextToken(); 1.327 + 1.328 + while (nextToken != ';' && nextToken != ':' && nextToken != ',') { 1.329 + 1.330 + if (tokeniser.ttype == StreamTokenizer.TT_WORD) { 1.331 + paramValue.append(tokeniser.sval); 1.332 + } 1.333 + else { 1.334 + paramValue.append((char) tokeniser.ttype); 1.335 + } 1.336 + 1.337 + nextToken = tokeniser.nextToken(); 1.338 + } 1.339 + tokeniser.pushBack(); 1.340 + } else if(tokeniser.sval == null) { 1.341 + tokeniser.pushBack(); 1.342 + } 1.343 + 1.344 + try { 1.345 + handler.parameter(paramName, paramValue.toString()); 1.346 + } 1.347 + catch (ClassCastException cce) { 1.348 + throw new ParserException("Error parsing parameter", getLineNumber(tokeniser, in), cce); 1.349 + } 1.350 + } 1.351 + } 1.352 + 1.353 + /** 1.354 + * Parses an iCalendar component list from the specified stream tokeniser. 1.355 + * @param tokeniser 1.356 + * @throws IOException 1.357 + * @throws ParseException 1.358 + * @throws URISyntaxException 1.359 + * @throws ParserException 1.360 + */ 1.361 + private class ComponentListParser { 1.362 + 1.363 + private void parse(final StreamTokenizer tokeniser, Reader in, 1.364 + final ContentHandler handler) throws IOException, ParseException, 1.365 + URISyntaxException, ParserException { 1.366 + 1.367 + while (Component.BEGIN.equals(tokeniser.sval)) { 1.368 + componentParser.parse(tokeniser, in, handler); 1.369 + absorbWhitespace(tokeniser); 1.370 + // assertToken(tokeniser, StreamTokenizer.TT_WORD); 1.371 + } 1.372 + } 1.373 + } 1.374 + 1.375 + /** 1.376 + * Parses an iCalendar component from the specified stream tokeniser. 1.377 + * @param tokeniser 1.378 + * @throws IOException 1.379 + * @throws ParseException 1.380 + * @throws URISyntaxException 1.381 + * @throws ParserException 1.382 + */ 1.383 + private class ComponentParser { 1.384 + 1.385 + private void parse(final StreamTokenizer tokeniser, Reader in, 1.386 + final ContentHandler handler) throws IOException, ParseException, 1.387 + URISyntaxException, ParserException { 1.388 + 1.389 + assertToken(tokeniser, in, ':'); 1.390 + 1.391 + assertToken(tokeniser, in, StreamTokenizer.TT_WORD); 1.392 + 1.393 + final String name = tokeniser.sval; 1.394 + 1.395 + handler.startComponent(name); 1.396 + 1.397 + assertToken(tokeniser, in, StreamTokenizer.TT_EOL); 1.398 + 1.399 + propertyListParser.parse(tokeniser, in, handler); 1.400 + 1.401 + /* 1.402 + * // a special case for VTIMEZONE component which contains 1.403 + * // sub-components.. 1.404 + * if (Component.VTIMEZONE.equals(name)) { 1.405 + * parseComponentList(tokeniser, handler); 1.406 + * } 1.407 + * // VEVENT/VTODO components may optionally have embedded VALARM 1.408 + * // components.. 1.409 + * else if ((Component.VEVENT.equals(name) || Component.VTODO.equals(name)) 1.410 + * && Component.BEGIN.equals(tokeniser.sval)) { 1.411 + * parseComponentList(tokeniser, handler); 1.412 + * } 1.413 + */ 1.414 + 1.415 + assertToken(tokeniser, in, ':'); 1.416 + 1.417 + assertToken(tokeniser, in, name); 1.418 + 1.419 + assertToken(tokeniser, in, StreamTokenizer.TT_EOL); 1.420 + 1.421 + handler.endComponent(name); 1.422 + } 1.423 + } 1.424 + 1.425 + /** 1.426 + * Asserts that the next token in the stream matches the specified token. 1.427 + * @param tokeniser stream tokeniser to perform assertion on 1.428 + * @param token expected token 1.429 + * @throws IOException when unable to read from stream 1.430 + * @throws ParserException when next token in the stream does not match the expected token 1.431 + */ 1.432 + private void assertToken(final StreamTokenizer tokeniser, Reader in, final int token) 1.433 + throws IOException, ParserException { 1.434 + 1.435 + if (tokeniser.nextToken() != token) { 1.436 + throw new ParserException(MessageFormat.format(UNEXPECTED_TOKEN_MESSAGE, new Object[] { 1.437 + new Integer(token), new Integer(tokeniser.ttype), 1.438 + }), getLineNumber(tokeniser, in)); 1.439 + } 1.440 + 1.441 + if (log.isDebugEnabled()) { 1.442 + log.debug("[" + token + "]"); 1.443 + } 1.444 + } 1.445 + 1.446 + /** 1.447 + * Asserts that the next token in the stream matches the specified token. This method is case-sensitive. 1.448 + * @param tokeniser 1.449 + * @param token 1.450 + * @throws IOException 1.451 + * @throws ParserException 1.452 + */ 1.453 + private void assertToken(final StreamTokenizer tokeniser, Reader in, final String token) 1.454 + throws IOException, ParserException { 1.455 + assertToken(tokeniser, in, token, false); 1.456 + } 1.457 + 1.458 + /** 1.459 + * Asserts that the next token in the stream matches the specified token. 1.460 + * @param tokeniser stream tokeniser to perform assertion on 1.461 + * @param token expected token 1.462 + * @throws IOException when unable to read from stream 1.463 + * @throws ParserException when next token in the stream does not match the expected token 1.464 + */ 1.465 + private void assertToken(final StreamTokenizer tokeniser, Reader in, 1.466 + final String token, final boolean ignoreCase) throws IOException, 1.467 + ParserException { 1.468 + 1.469 + // ensure next token is a word token.. 1.470 + assertToken(tokeniser, in, StreamTokenizer.TT_WORD); 1.471 + 1.472 + if (ignoreCase) { 1.473 + if (!token.equalsIgnoreCase(tokeniser.sval)) { 1.474 + throw new ParserException(MessageFormat.format(UNEXPECTED_TOKEN_MESSAGE, new Object[] { 1.475 + token, tokeniser.sval, 1.476 + }), getLineNumber(tokeniser, in)); 1.477 + } 1.478 + } 1.479 + else if (!token.equals(tokeniser.sval)) { 1.480 + throw new ParserException(MessageFormat.format(UNEXPECTED_TOKEN_MESSAGE, new Object[] { 1.481 + token, tokeniser.sval, 1.482 + }), getLineNumber(tokeniser, in)); 1.483 + } 1.484 + 1.485 + if (log.isDebugEnabled()) { 1.486 + log.debug("[" + token + "]"); 1.487 + } 1.488 + } 1.489 + 1.490 + /** 1.491 + * Absorbs extraneous newlines. 1.492 + * @param tokeniser 1.493 + * @throws IOException 1.494 + */ 1.495 + private void absorbWhitespace(final StreamTokenizer tokeniser) throws IOException { 1.496 + // HACK: absorb extraneous whitespace between components (KOrganizer).. 1.497 + while (tokeniser.nextToken() == StreamTokenizer.TT_EOL) { 1.498 + if (log.isTraceEnabled()) { 1.499 + log.trace("Absorbing extra whitespace.."); 1.500 + } 1.501 + } 1.502 + if (log.isTraceEnabled()) { 1.503 + log.trace("Aborting: absorbing extra whitespace complete"); 1.504 + } 1.505 + } 1.506 + 1.507 + /** 1.508 + * @param tokeniser 1.509 + * @param in 1.510 + * @return 1.511 + */ 1.512 + private int getLineNumber(StreamTokenizer tokeniser, Reader in) { 1.513 + int line = tokeniser.lineno(); 1.514 + if (tokeniser.ttype == StreamTokenizer.TT_EOL) { 1.515 + line -= 1; 1.516 + } 1.517 + if (in instanceof UnfoldingReader) { 1.518 + // need to take unfolded lines into account 1.519 + final int unfolded = ((UnfoldingReader) in).getLinesUnfolded(); 1.520 + line += unfolded; 1.521 + } 1.522 + return line; 1.523 + } 1.524 +}