/** * Build an iCalendar model by parsing data from the specified reader. * * @param in an unfolding reader to read data from * @return a calendar parsed from the specified reader * @throws java.io.IOException where an error occurs reading data from the specified reader * @throws net.fortuna.ical4j.data.ParserException where an error occurs parsing data from the reader */ public Calendar build(final Reader in) throws IOException, ParserException { BuildState bs = new BuildState(tzRegistry); bs.setContentHandler(new ContentHandlerImpl(bs)); try { final JsonParser parser = jsonFactory.createParser(in); process(parser, bs); } catch (Throwable t) { throw new ParserException(t.getMessage(), 0, t); } if ((bs.getDatesMissingTimezones().size() > 0) && (tzRegistry != null)) { resolveTimezones(bs); } return bs.getCalendars().iterator().next(); }
@Override public void endProperty(final String name) { assertProperty(bs.getProperty()); // replace with a constant instance if applicable.. bs.setProperty(Constants.forProperty(bs.getProperty())); if (bs.getComponent() != null) { bs.getComponent().getProperties().add(bs.getProperty()); } else if (bs.getCalendar() != null) { bs.getCalendar().getProperties().add(bs.getProperty()); } bs.setProperty(null); }
private void propVal(final BuildState bs, final String val) throws Throwable { bs.getContentHandler().propertyValue(val); }
/** * @param ical * @return Calendar object or null for no data * @throws Throwable */ public Calendar fromXcal(final IcalendarType ical) throws Throwable { BuildState bs = new BuildState(tzRegistry); bs.setContentHandler(new ContentHandlerImpl(bs)); List<VcalendarType> vcts = ical.getVcalendar(); if (vcts.size() == 0) { return null; } if (vcts.size() > 1) { throw new Exception("More than one vcalendar"); } processVcalendar(vcts.get(0), bs); return bs.getCalendar(); }
@Override public void endComponent(final String name) { assertComponent(bs.getComponent()); final Component component = bs.getComponent(); bs.endComponent(); final Component parent = bs.getComponent(); bs.getCalendar().getComponents().add((CalendarComponent)component); if ((component instanceof VTimeZone) && (bs.getTzRegistry() != null)) { bs.getTzRegistry().register(new TimeZone((VTimeZone) component));
private void process(final Document doc, final BuildState bs) throws ParserException { // start = element icalendar { vcalendar+ } Element root = doc.getDocumentElement(); if (!XmlUtil.nodeMatches(root, XcalTags.icalendar)) { // error throw new ParserException("Expected " + XcalTags.icalendar + " found " + root, 0); } for (Element el: getChildren(root)) { // Expect vcalendar if (!XmlUtil.nodeMatches(el, XcalTags.vcalendar)) { // error throw new ParserException("Expected " + XcalTags.vcalendar + " found " + el, 0); } bs.setCalendar(null); processVcalendar(el, bs); if (bs.getCalendar() != null) { bs.getCalendars().add(bs.getCalendar()); } } }
@Override public void parameter(final String name, final String value) throws URISyntaxException { assertProperty(bs.getProperty()); // parameter names are case-insensitive, but convert to upper case to simplify further processing final Parameter param = parameterFactory.createParameter(name.toUpperCase(), value); bs.getProperty().getParameters().add(param); if ((param instanceof TzId) && (bs.getTzRegistry() != null)) { final TimeZone timezone = bs.getTzRegistry().getTimeZone(param.getValue()); if (timezone != null) { updateTimeZone(bs.getProperty(), timezone); /* Bedework - for the moment switch ids if they differ */ if (!timezone.getID().equals(param.getValue())) { /* Fetched timezone has a different id */ final ParameterList pl = bs.getProperty().getParameters(); pl.replace(ParameterFactoryImpl.getInstance(). createParameter(Parameter.TZID, timezone.getID())); } } else { // VTIMEZONE may be defined later, so so keep // track of dates until all components have been // parsed, and then try again later bs.getDatesMissingTimezones().add(bs.getProperty()); } } }
final String type) throws ParserException { try { if (bs.getProperty() instanceof Geo) { bs.getContentHandler().propertyValue(sb.toString()); if (bs.getProperty() instanceof RequestStatus) { arrayStart(parser); bs.getContentHandler().propertyValue(sb.toString()); bs.getContentHandler().propertyValue(sb.toString()); bs.getContentHandler().propertyValue(String.valueOf( booleanField(parser))); bs.getContentHandler().propertyValue( XcalUtil.getIcalUtcOffset(textField(parser))); bs.getContentHandler().propertyValue(res.toString()); bs.getContentHandler().propertyValue(String.valueOf(intField( parser))); bs.getContentHandler().propertyValue(String.valueOf(intField( parser))); type.equals("date-time") || type.equals("time")) {
private void resolveTimezones(final BuildState bs) throws IOException { for (final Property property: bs.getDatesMissingTimezones()) { final Parameter tzParam = property.getParameter(Parameter.TZID);
/** * {@inheritDoc} */ @Override public void startCalendar() { bs.setCalendar(new Calendar()); }
/** * {@inheritDoc} */ @Override public void propertyValue(final String value) throws URISyntaxException, ParseException, IOException { assertProperty(bs.getProperty()); if (bs.getProperty() instanceof Escapable) { bs.getProperty().setValue(Strings.unescape(value)); } else { bs.getProperty().setValue(value); } }
private void process(final JsonParser parser, final BuildState bs) throws ParserException { /* ["vcalendar", [ <properties> ], [ <components> ] ] */ try { arrayStart(parser); final String ctype = textField(parser); if (!ctype.equals("vcalendar")) { // error throwException("Expected vcalendar: found " + ctype, parser); } lastComponent = "vcalendar"; bs.setCalendar(null); processVcalendar(parser, bs); if (bs.getCalendar() != null) { bs.getCalendars().add(bs.getCalendar()); } arrayEnd(parser); } catch (final Throwable t) { handleException(t, parser); } }
private void resolveTimezones(final BuildState bs) throws IOException { for (final Property property: bs.getDatesMissingTimezones()) { final Parameter tzParam = property.getParameter(Parameter.TZID);
/** * Build an iCalendar model by parsing data from the specified reader. * * @param in an unfolding reader to read data from * @return a calendar parsed from the specified reader * @throws IOException where an error occurs reading data from the specified reader * @throws ParserException where an error occurs parsing data from the reader */ public Calendar build(final Reader in) throws IOException, ParserException { BuildState bs = new BuildState(tzRegistry); bs.setContentHandler(new ContentHandlerImpl(bs)); try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(new InputSource(in)); process(doc, bs); } catch (SAXException e) { throw new ParserException(e.getMessage(), 0, e); } catch (Throwable t) { throw new ParserException(t.getMessage(), 0, t); } if ((bs.getDatesMissingTimezones().size() > 0) && (tzRegistry != null)) { resolveTimezones(bs); } return bs.getCalendars().iterator().next(); }
private void processComponent(final BaseComponentType comp, final BuildState bs) throws Throwable { final ComponentInfoIndex cii = ComponentInfoIndex.fromXmlClass(comp.getClass()); if (cii == null) { throw new Exception("Unknown component " + comp.getClass()); } final String name = cii.getPname(); bs.getContentHandler().startComponent(name); processProperties(comp.getProperties(), bs); processCalcomps(comp, bs); bs.getContentHandler().endComponent(name); }
private void processVcalendar(final JsonParser parser, final BuildState bs) throws ParserException { bs.getContentHandler().startCalendar(); /* Properties first */ processProperties(parser, bs); /* Now components */ processCalcomps(parser, bs); }
private void processComponent(final JsonParser parser, final BuildState bs) throws ParserException { currentArrayStart(parser); final String cname = textField(parser).toUpperCase(); lastComponent = cname; bs.getContentHandler().startComponent(cname); /* Properties first */ processProperties(parser, bs); /* Now components */ processCalcomps(parser, bs); bs.getContentHandler().endComponent(cname); arrayEnd(parser); }
private void processProperty(final Element el, final BuildState bs) throws ParserException { try { bs.getContentHandler().startProperty(el.getLocalName()); for (Element e: XmlUtil.getElements(el)) { if (XmlUtil.nodeMatches(e, XcalTags.parameters)) { for (Element par: XmlUtil.getElements(e)) { bs.getContentHandler().parameter(par.getLocalName(), XmlUtil.getElementContent(par)); } } if (!processValue(e, bs)) { throw new ParserException("Bad property " + el, 0); } } bs.getContentHandler().endProperty(el.getLocalName()); } catch (SAXException e) { throw new ParserException(e.getMessage(), 0, e); } catch (URISyntaxException e) { throw new ParserException(e.getMessage(), 0, e); } }
bs.getContentHandler().startProperty(name); bs.getContentHandler().parameter(parName, getParValue(e.getValue())); bs.getContentHandler().endProperty(name);
private void processComponent(final Element el, final BuildState bs) throws ParserException { try { bs.getContentHandler().startComponent(el.getLocalName().toUpperCase()); for (Element e: XmlUtil.getElements(el)) { if (XmlUtil.nodeMatches(e, XcalTags.properties)) { processProperties(e, bs); } else if (XmlUtil.nodeMatches(e, XcalTags.components)) { for (Element ce: XmlUtil.getElements(e)) { processComponent(ce, bs); } } else { throw new ParserException("Unexpected element: found " + e, 0); } } bs.getContentHandler().endComponent(el.getLocalName().toUpperCase()); } catch (SAXException e) { throw new ParserException(e.getMessage(), 0, e); } }