/** * @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 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()); } } }
/** * {@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); } }
@Override public void endComponent(final String name) { assertComponent(bs.getComponent());
@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); }
/** * 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(); }
/** * 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(); }