Search in sources :

Example 1 with VTimeZone

use of net.fortuna.ical4j.model.component.VTimeZone in project camel by apache.

the class ICalDataFormatTest method createTestCalendar.

/**
     * Creates test calendar instance.
     * 
     * @return ICal calendar object.
     */
protected Calendar createTestCalendar() throws ParseException {
    // Create a TimeZone
    TimeZoneRegistry registry = TimeZoneRegistryFactory.getInstance().createRegistry();
    TimeZone timezone = registry.getTimeZone("America/New_York");
    VTimeZone tz = timezone.getVTimeZone();
    // Start Date is on: April 1, 2013, 9:00 am
    java.util.Calendar startDate = new GregorianCalendar();
    startDate.setTimeZone(timezone);
    startDate.set(java.util.Calendar.MONTH, java.util.Calendar.APRIL);
    startDate.set(java.util.Calendar.DAY_OF_MONTH, 1);
    startDate.set(java.util.Calendar.YEAR, 2013);
    startDate.set(java.util.Calendar.HOUR_OF_DAY, 17);
    startDate.set(java.util.Calendar.MINUTE, 0);
    startDate.set(java.util.Calendar.SECOND, 0);
    // End Date is on: April 1, 2013, 13:00
    java.util.Calendar endDate = new GregorianCalendar();
    endDate.setTimeZone(timezone);
    endDate.set(java.util.Calendar.MONTH, java.util.Calendar.APRIL);
    endDate.set(java.util.Calendar.DAY_OF_MONTH, 1);
    endDate.set(java.util.Calendar.YEAR, 2013);
    endDate.set(java.util.Calendar.HOUR_OF_DAY, 21);
    endDate.set(java.util.Calendar.MINUTE, 0);
    endDate.set(java.util.Calendar.SECOND, 0);
    // Create the event
    PropertyList propertyList = new PropertyList();
    propertyList.add(new DtStamp("20130324T180000Z"));
    propertyList.add(new DtStart(new DateTime(startDate.getTime())));
    propertyList.add(new DtEnd(new DateTime(endDate.getTime())));
    propertyList.add(new Summary("Progress Meeting"));
    VEvent meeting = new VEvent(propertyList);
    // add timezone info..
    meeting.getProperties().add(tz.getTimeZoneId());
    // generate unique identifier..
    meeting.getProperties().add(new Uid("00000000"));
    // add attendees..
    Attendee dev1 = new Attendee(URI.create("mailto:dev1@mycompany.com"));
    dev1.getParameters().add(Role.REQ_PARTICIPANT);
    dev1.getParameters().add(new Cn("Developer 1"));
    meeting.getProperties().add(dev1);
    Attendee dev2 = new Attendee(URI.create("mailto:dev2@mycompany.com"));
    dev2.getParameters().add(Role.OPT_PARTICIPANT);
    dev2.getParameters().add(new Cn("Developer 2"));
    meeting.getProperties().add(dev2);
    // Create a calendar
    net.fortuna.ical4j.model.Calendar icsCalendar = new net.fortuna.ical4j.model.Calendar();
    icsCalendar.getProperties().add(Version.VERSION_2_0);
    icsCalendar.getProperties().add(new ProdId("-//Events Calendar//iCal4j 1.0//EN"));
    icsCalendar.getProperties().add(CalScale.GREGORIAN);
    // Add the event and print
    icsCalendar.getComponents().add(meeting);
    return icsCalendar;
}
Also used : VEvent(net.fortuna.ical4j.model.component.VEvent) VTimeZone(net.fortuna.ical4j.model.component.VTimeZone) GregorianCalendar(java.util.GregorianCalendar) Calendar(net.fortuna.ical4j.model.Calendar) TimeZoneRegistry(net.fortuna.ical4j.model.TimeZoneRegistry) GregorianCalendar(java.util.GregorianCalendar) Cn(net.fortuna.ical4j.model.parameter.Cn) ProdId(net.fortuna.ical4j.model.property.ProdId) Calendar(net.fortuna.ical4j.model.Calendar) DateTime(net.fortuna.ical4j.model.DateTime) Attendee(net.fortuna.ical4j.model.property.Attendee) Uid(net.fortuna.ical4j.model.property.Uid) VTimeZone(net.fortuna.ical4j.model.component.VTimeZone) TimeZone(net.fortuna.ical4j.model.TimeZone) DtStamp(net.fortuna.ical4j.model.property.DtStamp) PropertyList(net.fortuna.ical4j.model.PropertyList) DtStart(net.fortuna.ical4j.model.property.DtStart) Summary(net.fortuna.ical4j.model.property.Summary) DtEnd(net.fortuna.ical4j.model.property.DtEnd)

Example 2 with VTimeZone

use of net.fortuna.ical4j.model.component.VTimeZone in project zm-mailbox by Zimbra.

the class ZoneInfo2iCalendar method getTimeZoneForZoneLines.

/**
     * @param zoneLines - Only the zoneLines related to a time zone that might be relevant from the reference date.
     */
private static String getTimeZoneForZoneLines(String tzid, Set<String> aliases, List<ZoneLine> zoneLines, Params params, Set<String> zoneIDs, Map<String, VTimeZone> oldTimeZones) {
    if ((zoneLines == null) || (zoneLines.isEmpty())) {
        return "";
    }
    boolean isPrimary = sPrimaryTZIDs.contains(tzid);
    Integer matchScore = sMatchScores.get(tzid);
    if (matchScore == null) {
        if (isPrimary) {
            matchScore = Integer.valueOf(TZIDMapper.DEFAULT_MATCH_SCORE_PRIMARY);
        } else {
            matchScore = Integer.valueOf(TZIDMapper.DEFAULT_MATCH_SCORE_NON_PRIMARY);
        }
    }
    Iterator<String> aliasesIter = aliases.iterator();
    while (aliasesIter.hasNext()) {
        String curr = aliasesIter.next();
        if (zoneIDs.contains(curr)) {
            aliasesIter.remove();
        }
    }
    ZoneLine zline = zoneLines.get(0);
    VTimeZone oldVtz = oldTimeZones.get(zline.getName());
    Property oldLastModProp = null;
    if (null != oldVtz) {
        oldLastModProp = oldVtz.getProperties().getProperty(Property.LAST_MODIFIED);
    }
    LastModified newLastModified = getLastModified(params.lastModified);
    LastModified trialLastModified;
    if (null != oldLastModProp && oldLastModProp instanceof LastModified) {
        trialLastModified = (LastModified) oldLastModProp;
    } else {
        trialLastModified = newLastModified;
    }
    VTimeZone vtz = toVTimeZoneComp(params.referenceDate, zoneLines, trialLastModified, aliases, isPrimary, matchScore);
    String asText = vtz.toString();
    if ((null != oldVtz) && (trialLastModified != newLastModified)) {
        String oldText = oldVtz.toString();
        if (!asText.equals(oldText)) {
            /* Work around non-round tripped entries where the original source has:
                 *     X-ZIMBRA-TZ-ALIAS:(GMT+12.00) Anadyr\, Petropavlovsk-Kamchatsky (RTZ 11)
                 * but in this we have:
                 *     X-ZIMBRA-TZ-ALIAS:(GMT+12.00) Anadyr\\\, Petropavlovsk-Kamchatsky (RTZ 11)
                 * suspect that is a bug in libical which may be fixed in a later revision
                 */
            String oldText2 = oldText.replace("\\\\\\,", "\\,");
            if (!asText.equals(oldText2)) {
                LastModified lastModProp = (LastModified) vtz.getProperties().getProperty(Property.LAST_MODIFIED);
                try {
                    lastModProp.setValue(newLastModified.getValue());
                    asText = vtz.toString();
                } catch (ParseException e) {
                    System.err.println("Problem assigning LAST-MODIFIED - " + e.getMessage());
                }
            }
        }
    }
    return asText;
}
Also used : LastModified(net.fortuna.ical4j.model.property.LastModified) ZoneLine(com.zimbra.common.calendar.ZoneInfoParser.ZoneLine) VTimeZone(net.fortuna.ical4j.model.component.VTimeZone) ParseException(java.text.ParseException) TZDataParseException(com.zimbra.common.calendar.ZoneInfoParser.TZDataParseException) XProperty(net.fortuna.ical4j.model.property.XProperty) Property(net.fortuna.ical4j.model.Property)

Example 3 with VTimeZone

use of net.fortuna.ical4j.model.component.VTimeZone in project zm-mailbox by Zimbra.

the class ZoneInfo2iCalendar method toVTimeZoneComp.

private static VTimeZone toVTimeZoneComp(int hintYear, Observances observances, PropertyList vtzProps) {
    VTimeZone vtz = new VTimeZone(vtzProps);
    vtz.getObservances().add(observances.std);
    if (null != observances.daylight) {
        vtz.getObservances().add(observances.daylight);
    }
    return vtz;
}
Also used : VTimeZone(net.fortuna.ical4j.model.component.VTimeZone)

Example 4 with VTimeZone

use of net.fortuna.ical4j.model.component.VTimeZone in project zm-mailbox by Zimbra.

the class ZoneInfo2iCalendar method makeOldTimeZonesMap.

private static Map<String, VTimeZone> makeOldTimeZonesMap(Params params) {
    Map<String, VTimeZone> oldTimeZones = Maps.newHashMap();
    if (null != params.oldTimezonesFileName) {
        try (FileInputStream fin = new FileInputStream(params.oldTimezonesFileName)) {
            CalendarBuilder builder = new CalendarBuilder();
            net.fortuna.ical4j.model.Calendar calendar = builder.build(fin, "UTF-8");
            for (Iterator i = calendar.getComponents().iterator(); i.hasNext(); ) {
                Component component = (Component) i.next();
                if (Component.VTIMEZONE.equals(component.getName())) {
                    VTimeZone vtz = (VTimeZone) component;
                    Property tzprop = vtz.getProperties().getProperty(Property.TZID);
                    if (null != tzprop) {
                        oldTimeZones.put(tzprop.getValue(), vtz);
                    }
                }
            }
        } catch (IOException | ParserException e) {
            System.err.println("Problem loading old timezones.ics - ignoring it.  " + e.getMessage());
        }
    }
    return oldTimeZones;
}
Also used : ParserException(net.fortuna.ical4j.data.ParserException) CalendarBuilder(net.fortuna.ical4j.data.CalendarBuilder) VTimeZone(net.fortuna.ical4j.model.component.VTimeZone) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Iterator(java.util.Iterator) Component(net.fortuna.ical4j.model.Component) XProperty(net.fortuna.ical4j.model.property.XProperty) Property(net.fortuna.ical4j.model.Property)

Example 5 with VTimeZone

use of net.fortuna.ical4j.model.component.VTimeZone in project zm-mailbox by Zimbra.

the class ZoneInfo2iCalendar method main.

// main
public static void main(String[] args) throws Exception {
    // command line handling
    CommandLine cl = null;
    Params params = null;
    try {
        cl = parseArgs(args);
        if (cl.hasOption(OPT_HELP)) {
            usage(null);
            System.exit(0);
        }
        params = initParams(cl);
    } catch (Exception e) {
        System.err.println(e.getMessage());
        e.printStackTrace();
        System.exit(1);
    }
    // parse tzdata source
    ZoneInfoParser parser = new ZoneInfoParser();
    for (File tzdataFile : params.tzdataFiles) {
        Reader r = null;
        try {
            r = new InputStreamReader(new FileInputStream(tzdataFile), "UTF-8");
            parser.readTzdata(r);
        } catch (ParseException e) {
            System.err.println(e.getMessage());
            System.err.println("Line: " + e.getErrorOffset());
            System.err.println("File: " + tzdataFile.getAbsolutePath());
            e.printStackTrace();
            System.exit(1);
        } finally {
            if (r != null)
                r.close();
        }
    }
    parser.analyze();
    // read extra data file containing primary TZ list and zone match scores
    if (params.extraDataFile != null) {
        Reader r = null;
        try {
            r = new InputStreamReader(new FileInputStream(params.extraDataFile), "UTF-8");
            readExtraData(r);
        } catch (ParseException e) {
            System.err.println(e.getMessage());
            System.err.println("Line: " + e.getErrorOffset());
            System.err.println("File: " + params.extraDataFile.getAbsolutePath());
            e.printStackTrace();
            System.exit(1);
        } finally {
            if (r != null)
                r.close();
        }
    }
    Writer out;
    if (params.outputFile != null) {
        out = new PrintWriter(params.outputFile, "UTF-8");
    } else {
        out = new PrintWriter(new OutputStreamWriter(System.out, "UTF-8"));
    }
    try {
        StringBuilder hdr = new StringBuilder("BEGIN:VCALENDAR");
        hdr.append(CRLF);
        hdr.append("PRODID:Zimbra-Calendar-Provider").append(CRLF);
        hdr.append("VERSION:2.0").append(CRLF);
        hdr.append("METHOD:PUBLISH").append(CRLF);
        out.write(hdr.toString());
        Map<String, VTimeZone> oldTimeZones = makeOldTimeZonesMap(params);
        Set<Zone> zones = new TreeSet<Zone>(new ZoneComparatorByGmtOffset());
        zones.addAll(parser.getZones());
        Set<String> zoneIDs = new TreeSet<String>();
        for (Zone zone : zones) {
            zoneIDs.add(zone.getName());
        }
        for (Zone zone : zones) {
            out.write(getTimeZoneForZone(zone, params, zoneIDs, oldTimeZones));
        }
        StringBuilder footer = new StringBuilder("END:VCALENDAR");
        footer.append(CRLF);
        out.write(footer.toString());
    } finally {
        out.close();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) Zone(com.zimbra.common.calendar.ZoneInfoParser.Zone) TimeZone(java.util.TimeZone) VTimeZone(net.fortuna.ical4j.model.component.VTimeZone) VTimeZone(net.fortuna.ical4j.model.component.VTimeZone) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) ParseException(java.text.ParseException) ParserException(net.fortuna.ical4j.data.ParserException) FileNotFoundException(java.io.FileNotFoundException) TZDataParseException(com.zimbra.common.calendar.ZoneInfoParser.TZDataParseException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) CommandLine(org.apache.commons.cli.CommandLine) TreeSet(java.util.TreeSet) OutputStreamWriter(java.io.OutputStreamWriter) ParseException(java.text.ParseException) TZDataParseException(com.zimbra.common.calendar.ZoneInfoParser.TZDataParseException) File(java.io.File) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) PrintWriter(java.io.PrintWriter)

Aggregations

VTimeZone (net.fortuna.ical4j.model.component.VTimeZone)8 Property (net.fortuna.ical4j.model.Property)3 PropertyList (net.fortuna.ical4j.model.PropertyList)3 TZDataParseException (com.zimbra.common.calendar.ZoneInfoParser.TZDataParseException)2 ZoneLine (com.zimbra.common.calendar.ZoneInfoParser.ZoneLine)2 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 ParseException (java.text.ParseException)2 ParserException (net.fortuna.ical4j.data.ParserException)2 Component (net.fortuna.ical4j.model.Component)2 TimeZone (net.fortuna.ical4j.model.TimeZone)2 XProperty (net.fortuna.ical4j.model.property.XProperty)2 Zone (com.zimbra.common.calendar.ZoneInfoParser.Zone)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStreamReader (java.io.InputStreamReader)1 OutputStreamWriter (java.io.OutputStreamWriter)1 PrintWriter (java.io.PrintWriter)1 Reader (java.io.Reader)1 Writer (java.io.Writer)1