1.1 --- a/src/net/fortuna/ical4j/data/CalendarParserImpl.java Thu Feb 12 18:02:00 2015 +0100 1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 1.3 @@ -1,531 +0,0 @@ 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, in); 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 = nextToken(tokeniser, in); 1.235 - 1.236 - while (nextToken != StreamTokenizer.TT_EOL) { 1.237 - 1.238 - if (tokeniser.ttype == StreamTokenizer.TT_WORD) { 1.239 - value.append(tokeniser.sval); 1.240 - } 1.241 - else { 1.242 - value.append((char) tokeniser.ttype); 1.243 - } 1.244 - 1.245 - nextToken = nextToken(tokeniser, in); 1.246 - } 1.247 - 1.248 - // reset DQUOTE to be quote char 1.249 - tokeniser.quoteChar('"'); 1.250 - 1.251 - try { 1.252 - handler.propertyValue(value.toString()); 1.253 - } 1.254 - catch (ParseException e) { 1.255 - final ParseException eNew = new ParseException("[" + name + "] " 1.256 - + e.getMessage(), e.getErrorOffset()); 1.257 - eNew.initCause(e); 1.258 - throw eNew; 1.259 - } 1.260 - 1.261 - handler.endProperty(name); 1.262 - 1.263 - } 1.264 - } 1.265 - 1.266 - /** 1.267 - * Parses a list of iCalendar parameters by parsing the specified stream tokeniser. 1.268 - * @param tokeniser 1.269 - * @throws IOException 1.270 - * @throws ParserException 1.271 - * @throws URISyntaxException 1.272 - */ 1.273 - private class ParameterListParser { 1.274 - 1.275 - public void parse(final StreamTokenizer tokeniser, Reader in, 1.276 - final ContentHandler handler) throws IOException, ParserException, 1.277 - URISyntaxException { 1.278 - 1.279 - while (nextToken(tokeniser, in) == ';') { 1.280 - paramParser.parse(tokeniser, in, handler); 1.281 - } 1.282 - } 1.283 - } 1.284 - 1.285 - /** 1.286 - * @param tokeniser 1.287 - * @param handler 1.288 - * @throws IOException 1.289 - * @throws ParserException 1.290 - * @throws URISyntaxException 1.291 - */ 1.292 - private class ParameterParser { 1.293 - 1.294 - private void parse(final StreamTokenizer tokeniser, Reader in, 1.295 - final ContentHandler handler) throws IOException, ParserException, 1.296 - URISyntaxException { 1.297 - 1.298 - assertToken(tokeniser, in, StreamTokenizer.TT_WORD); 1.299 - 1.300 - final String paramName = tokeniser.sval; 1.301 - 1.302 - // debugging.. 1.303 - if (log.isDebugEnabled()) { 1.304 - log.debug("Parameter [" + paramName + "]"); 1.305 - } 1.306 - 1.307 - assertToken(tokeniser, in, '='); 1.308 - 1.309 - final StringBuffer paramValue = new StringBuffer(); 1.310 - 1.311 - // preserve quote chars.. 1.312 - if (nextToken(tokeniser, in) == '"') { 1.313 - paramValue.append('"'); 1.314 - paramValue.append(tokeniser.sval); 1.315 - paramValue.append('"'); 1.316 - } 1.317 - else if (tokeniser.sval != null) { 1.318 - paramValue.append(tokeniser.sval); 1.319 - // check for additional words to account for equals (=) in param-value 1.320 - int nextToken = nextToken(tokeniser, in); 1.321 - 1.322 - while (nextToken != ';' && nextToken != ':' && nextToken != ',') { 1.323 - 1.324 - if (tokeniser.ttype == StreamTokenizer.TT_WORD) { 1.325 - paramValue.append(tokeniser.sval); 1.326 - } 1.327 - else { 1.328 - paramValue.append((char) tokeniser.ttype); 1.329 - } 1.330 - 1.331 - nextToken = nextToken(tokeniser, in); 1.332 - } 1.333 - tokeniser.pushBack(); 1.334 - } else if(tokeniser.sval == null) { 1.335 - tokeniser.pushBack(); 1.336 - } 1.337 - 1.338 - try { 1.339 - handler.parameter(paramName, paramValue.toString()); 1.340 - } 1.341 - catch (ClassCastException cce) { 1.342 - throw new ParserException("Error parsing parameter", getLineNumber(tokeniser, in), cce); 1.343 - } 1.344 - } 1.345 - } 1.346 - 1.347 - /** 1.348 - * Parses an iCalendar component list from the specified stream tokeniser. 1.349 - * @param tokeniser 1.350 - * @throws IOException 1.351 - * @throws ParseException 1.352 - * @throws URISyntaxException 1.353 - * @throws ParserException 1.354 - */ 1.355 - private class ComponentListParser { 1.356 - 1.357 - private void parse(final StreamTokenizer tokeniser, Reader in, 1.358 - final ContentHandler handler) throws IOException, ParseException, 1.359 - URISyntaxException, ParserException { 1.360 - 1.361 - while (Component.BEGIN.equals(tokeniser.sval)) { 1.362 - componentParser.parse(tokeniser, in, handler); 1.363 - absorbWhitespace(tokeniser, in); 1.364 - // assertToken(tokeniser, StreamTokenizer.TT_WORD); 1.365 - } 1.366 - } 1.367 - } 1.368 - 1.369 - /** 1.370 - * Parses an iCalendar component from the specified stream tokeniser. 1.371 - * @param tokeniser 1.372 - * @throws IOException 1.373 - * @throws ParseException 1.374 - * @throws URISyntaxException 1.375 - * @throws ParserException 1.376 - */ 1.377 - private class ComponentParser { 1.378 - 1.379 - private void parse(final StreamTokenizer tokeniser, Reader in, 1.380 - final ContentHandler handler) throws IOException, ParseException, 1.381 - URISyntaxException, ParserException { 1.382 - 1.383 - assertToken(tokeniser, in, ':'); 1.384 - 1.385 - assertToken(tokeniser, in, StreamTokenizer.TT_WORD); 1.386 - 1.387 - final String name = tokeniser.sval; 1.388 - 1.389 - handler.startComponent(name); 1.390 - 1.391 - assertToken(tokeniser, in, StreamTokenizer.TT_EOL); 1.392 - 1.393 - propertyListParser.parse(tokeniser, in, handler); 1.394 - 1.395 - /* 1.396 - * // a special case for VTIMEZONE component which contains 1.397 - * // sub-components.. 1.398 - * if (Component.VTIMEZONE.equals(name)) { 1.399 - * parseComponentList(tokeniser, handler); 1.400 - * } 1.401 - * // VEVENT/VTODO components may optionally have embedded VALARM 1.402 - * // components.. 1.403 - * else if ((Component.VEVENT.equals(name) || Component.VTODO.equals(name)) 1.404 - * && Component.BEGIN.equals(tokeniser.sval)) { 1.405 - * parseComponentList(tokeniser, handler); 1.406 - * } 1.407 - */ 1.408 - 1.409 - assertToken(tokeniser, in, ':'); 1.410 - 1.411 - assertToken(tokeniser, in, name); 1.412 - 1.413 - assertToken(tokeniser, in, StreamTokenizer.TT_EOL); 1.414 - 1.415 - handler.endComponent(name); 1.416 - } 1.417 - } 1.418 - 1.419 - /** 1.420 - * Asserts that the next token in the stream matches the specified token. 1.421 - * @param tokeniser stream tokeniser to perform assertion on 1.422 - * @param token expected token 1.423 - * @throws IOException when unable to read from stream 1.424 - * @throws ParserException when next token in the stream does not match the expected token 1.425 - */ 1.426 - private void assertToken(final StreamTokenizer tokeniser, Reader in, final int token) 1.427 - throws IOException, ParserException { 1.428 - 1.429 - if (nextToken(tokeniser, in) != token) { 1.430 - throw new ParserException(MessageFormat.format(UNEXPECTED_TOKEN_MESSAGE, new Object[] { 1.431 - new Integer(token), new Integer(tokeniser.ttype), 1.432 - }), getLineNumber(tokeniser, in)); 1.433 - } 1.434 - 1.435 - if (log.isDebugEnabled()) { 1.436 - log.debug("[" + token + "]"); 1.437 - } 1.438 - } 1.439 - 1.440 - /** 1.441 - * Asserts that the next token in the stream matches the specified token. This method is case-sensitive. 1.442 - * @param tokeniser 1.443 - * @param token 1.444 - * @throws IOException 1.445 - * @throws ParserException 1.446 - */ 1.447 - private void assertToken(final StreamTokenizer tokeniser, Reader in, final String token) 1.448 - throws IOException, ParserException { 1.449 - assertToken(tokeniser, in, token, false); 1.450 - } 1.451 - 1.452 - /** 1.453 - * Asserts that the next token in the stream matches the specified token. 1.454 - * @param tokeniser stream tokeniser to perform assertion on 1.455 - * @param token expected token 1.456 - * @throws IOException when unable to read from stream 1.457 - * @throws ParserException when next token in the stream does not match the expected token 1.458 - */ 1.459 - private void assertToken(final StreamTokenizer tokeniser, Reader in, 1.460 - final String token, final boolean ignoreCase) throws IOException, 1.461 - ParserException { 1.462 - 1.463 - // ensure next token is a word token.. 1.464 - assertToken(tokeniser, in, StreamTokenizer.TT_WORD); 1.465 - 1.466 - if (ignoreCase) { 1.467 - if (!token.equalsIgnoreCase(tokeniser.sval)) { 1.468 - throw new ParserException(MessageFormat.format(UNEXPECTED_TOKEN_MESSAGE, new Object[] { 1.469 - token, tokeniser.sval, 1.470 - }), getLineNumber(tokeniser, in)); 1.471 - } 1.472 - } 1.473 - else if (!token.equals(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 - if (log.isDebugEnabled()) { 1.480 - log.debug("[" + token + "]"); 1.481 - } 1.482 - } 1.483 - 1.484 - /** 1.485 - * Absorbs extraneous newlines. 1.486 - * @param tokeniser 1.487 - * @throws IOException 1.488 - */ 1.489 - private void absorbWhitespace(final StreamTokenizer tokeniser, Reader in) throws IOException, ParserException { 1.490 - // HACK: absorb extraneous whitespace between components (KOrganizer).. 1.491 - while (nextToken(tokeniser, in) == StreamTokenizer.TT_EOL) { 1.492 - if (log.isTraceEnabled()) { 1.493 - log.trace("Absorbing extra whitespace.."); 1.494 - } 1.495 - } 1.496 - if (log.isTraceEnabled()) { 1.497 - log.trace("Aborting: absorbing extra whitespace complete"); 1.498 - } 1.499 - } 1.500 - 1.501 - /** 1.502 - * @param tokeniser 1.503 - * @param in 1.504 - * @return 1.505 - */ 1.506 - private int getLineNumber(StreamTokenizer tokeniser, Reader in) { 1.507 - int line = tokeniser.lineno(); 1.508 - if (tokeniser.ttype == StreamTokenizer.TT_EOL) { 1.509 - line -= 1; 1.510 - } 1.511 - if (in instanceof UnfoldingReader) { 1.512 - // need to take unfolded lines into account 1.513 - final int unfolded = ((UnfoldingReader) in).getLinesUnfolded(); 1.514 - line += unfolded; 1.515 - } 1.516 - return line; 1.517 - } 1.518 - 1.519 - /** 1.520 - * Reads the next token from the tokeniser. 1.521 - * This method throws a ParseException when reading EOF. 1.522 - * @param tokeniser 1.523 - * @param in 1.524 - * @return 1.525 - * @throws ParseException When reading EOF. 1.526 - */ 1.527 - private int nextToken(StreamTokenizer tokeniser, Reader in) throws IOException, ParserException { 1.528 - int token = tokeniser.nextToken(); 1.529 - if (token == StreamTokenizer.TT_EOF) { 1.530 - throw new ParserException("Unexpected end of file", getLineNumber(tokeniser, in)); 1.531 - } 1.532 - return token; 1.533 - } 1.534 -}