Tue, 10 Feb 2015 18:12:00 +0100
Import initial revisions of existing project AndroidCaldavSyncAdapater,
forked from upstream repository at 27e8a0f8495c92e0780d450bdf0c7cec77a03a55.
michael@0 | 1 | /** |
michael@0 | 2 | * Copyright (c) 2012, Ben Fortuna |
michael@0 | 3 | * All rights reserved. |
michael@0 | 4 | * |
michael@0 | 5 | * Redistribution and use in source and binary forms, with or without |
michael@0 | 6 | * modification, are permitted provided that the following conditions |
michael@0 | 7 | * are met: |
michael@0 | 8 | * |
michael@0 | 9 | * o Redistributions of source code must retain the above copyright |
michael@0 | 10 | * notice, this list of conditions and the following disclaimer. |
michael@0 | 11 | * |
michael@0 | 12 | * o Redistributions in binary form must reproduce the above copyright |
michael@0 | 13 | * notice, this list of conditions and the following disclaimer in the |
michael@0 | 14 | * documentation and/or other materials provided with the distribution. |
michael@0 | 15 | * |
michael@0 | 16 | * o Neither the name of Ben Fortuna nor the names of any other contributors |
michael@0 | 17 | * may be used to endorse or promote products derived from this software |
michael@0 | 18 | * without specific prior written permission. |
michael@0 | 19 | * |
michael@0 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
michael@0 | 24 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
michael@0 | 25 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
michael@0 | 26 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
michael@0 | 27 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
michael@0 | 28 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
michael@0 | 29 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
michael@0 | 30 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 31 | */ |
michael@0 | 32 | package net.fortuna.ical4j.util; |
michael@0 | 33 | |
michael@0 | 34 | import java.io.FileInputStream; |
michael@0 | 35 | import java.io.IOException; |
michael@0 | 36 | import java.net.URL; |
michael@0 | 37 | import java.nio.charset.Charset; |
michael@0 | 38 | import java.util.HashMap; |
michael@0 | 39 | import java.util.Iterator; |
michael@0 | 40 | import java.util.Map; |
michael@0 | 41 | |
michael@0 | 42 | import net.fortuna.ical4j.data.CalendarBuilder; |
michael@0 | 43 | import net.fortuna.ical4j.data.ParserException; |
michael@0 | 44 | import net.fortuna.ical4j.model.Calendar; |
michael@0 | 45 | import net.fortuna.ical4j.model.Component; |
michael@0 | 46 | import net.fortuna.ical4j.model.ComponentList; |
michael@0 | 47 | import net.fortuna.ical4j.model.ConstraintViolationException; |
michael@0 | 48 | import net.fortuna.ical4j.model.IndexedComponentList; |
michael@0 | 49 | import net.fortuna.ical4j.model.Parameter; |
michael@0 | 50 | import net.fortuna.ical4j.model.Property; |
michael@0 | 51 | import net.fortuna.ical4j.model.component.VTimeZone; |
michael@0 | 52 | import net.fortuna.ical4j.model.parameter.TzId; |
michael@0 | 53 | import net.fortuna.ical4j.model.property.Method; |
michael@0 | 54 | import net.fortuna.ical4j.model.property.Uid; |
michael@0 | 55 | |
michael@0 | 56 | /** |
michael@0 | 57 | * $Id$ |
michael@0 | 58 | * |
michael@0 | 59 | * Created on 10/11/2006 |
michael@0 | 60 | * |
michael@0 | 61 | * Utility method for working with {@link Calendar}s. |
michael@0 | 62 | * @author Ben Fortuna |
michael@0 | 63 | */ |
michael@0 | 64 | public final class Calendars { |
michael@0 | 65 | |
michael@0 | 66 | /** |
michael@0 | 67 | * Constructor made private to enforce static nature. |
michael@0 | 68 | */ |
michael@0 | 69 | private Calendars() { |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | /** |
michael@0 | 73 | * Loads a calendar from the specified file. |
michael@0 | 74 | * @param filename the name of the file from which to load calendar data |
michael@0 | 75 | * @return returns a new calendar instance initialised from the specified file |
michael@0 | 76 | * @throws IOException occurs when there is an error reading the specified file |
michael@0 | 77 | * @throws ParserException occurs when the data in the specified file is invalid |
michael@0 | 78 | */ |
michael@0 | 79 | public static Calendar load(final String filename) throws IOException, ParserException { |
michael@0 | 80 | final FileInputStream fin = new FileInputStream(filename); |
michael@0 | 81 | final CalendarBuilder builder = new CalendarBuilder(); |
michael@0 | 82 | return builder.build(fin); |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | /** |
michael@0 | 86 | * Loads a calendar from the specified URL. |
michael@0 | 87 | * @param url the URL from which to load calendar data |
michael@0 | 88 | * @return returns a new calendar instance initialised from the specified URL |
michael@0 | 89 | * @throws IOException occurs when there is an error reading from the specified URL |
michael@0 | 90 | * @throws ParserException occurs when the data in the specified URL is invalid |
michael@0 | 91 | */ |
michael@0 | 92 | public static Calendar load(final URL url) throws IOException, ParserException { |
michael@0 | 93 | final CalendarBuilder builder = new CalendarBuilder(); |
michael@0 | 94 | return builder.build(url.openStream()); |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | /** |
michael@0 | 98 | * Merge all properties and components from two specified calendars into one instance. |
michael@0 | 99 | * Note that the merge process is not very sophisticated, and may result in invalid calendar |
michael@0 | 100 | * data (e.g. multiple properties of a type that should only be specified once). |
michael@0 | 101 | * @param c1 the first calendar to merge |
michael@0 | 102 | * @param c2 the second calendar to merge |
michael@0 | 103 | * @return a Calendar instance containing all properties and components from both of the specified calendars |
michael@0 | 104 | */ |
michael@0 | 105 | public static Calendar merge(final Calendar c1, final Calendar c2) { |
michael@0 | 106 | final Calendar result = new Calendar(); |
michael@0 | 107 | result.getProperties().addAll(c1.getProperties()); |
michael@0 | 108 | for (final Iterator i = c2.getProperties().iterator(); i.hasNext();) { |
michael@0 | 109 | final Property p = (Property) i.next(); |
michael@0 | 110 | if (!result.getProperties().contains(p)) { |
michael@0 | 111 | result.getProperties().add(p); |
michael@0 | 112 | } |
michael@0 | 113 | } |
michael@0 | 114 | result.getComponents().addAll(c1.getComponents()); |
michael@0 | 115 | for (final Iterator i = c2.getComponents().iterator(); i.hasNext();) { |
michael@0 | 116 | final Component c = (Component) i.next(); |
michael@0 | 117 | if (!result.getComponents().contains(c)) { |
michael@0 | 118 | result.getComponents().add(c); |
michael@0 | 119 | } |
michael@0 | 120 | } |
michael@0 | 121 | return result; |
michael@0 | 122 | } |
michael@0 | 123 | |
michael@0 | 124 | /** |
michael@0 | 125 | * Wraps a component in a calendar. |
michael@0 | 126 | * @param component the component to wrap with a calendar |
michael@0 | 127 | * @return a calendar containing the specified component |
michael@0 | 128 | */ |
michael@0 | 129 | public static Calendar wrap(final Component component) { |
michael@0 | 130 | final ComponentList components = new ComponentList(); |
michael@0 | 131 | components.add(component); |
michael@0 | 132 | return new Calendar(components); |
michael@0 | 133 | } |
michael@0 | 134 | |
michael@0 | 135 | /** |
michael@0 | 136 | * Splits a calendar object into distinct calendar objects for unique |
michael@0 | 137 | * identifers (UID). |
michael@0 | 138 | * @param calendar a calendar instance |
michael@0 | 139 | * @return an array of calendar objects |
michael@0 | 140 | */ |
michael@0 | 141 | public static Calendar[] split(final Calendar calendar) { |
michael@0 | 142 | // if calendar contains one component or less, or is composed entirely of timezone |
michael@0 | 143 | // definitions, return the original calendar unmodified.. |
michael@0 | 144 | if (calendar.getComponents().size() <= 1 |
michael@0 | 145 | || calendar.getComponents(Component.VTIMEZONE).size() == calendar.getComponents().size()) { |
michael@0 | 146 | return new Calendar[] {calendar}; |
michael@0 | 147 | } |
michael@0 | 148 | |
michael@0 | 149 | final IndexedComponentList timezones = new IndexedComponentList(calendar.getComponents(Component.VTIMEZONE), |
michael@0 | 150 | Property.TZID); |
michael@0 | 151 | |
michael@0 | 152 | final Map calendars = new HashMap(); |
michael@0 | 153 | for (final Iterator i = calendar.getComponents().iterator(); i.hasNext();) { |
michael@0 | 154 | final Component c = (Component) i.next(); |
michael@0 | 155 | if (c instanceof VTimeZone) { |
michael@0 | 156 | continue; |
michael@0 | 157 | } |
michael@0 | 158 | |
michael@0 | 159 | final Uid uid = (Uid) c.getProperty(Property.UID); |
michael@0 | 160 | |
michael@0 | 161 | Calendar uidCal = (Calendar) calendars.get(uid); |
michael@0 | 162 | if (uidCal == null) { |
michael@0 | 163 | uidCal = new Calendar(calendar.getProperties(), new ComponentList()); |
michael@0 | 164 | // remove METHOD property for split calendars.. |
michael@0 | 165 | for (final Iterator mp = uidCal.getProperties(Property.METHOD).iterator(); mp.hasNext();) { |
michael@0 | 166 | uidCal.getProperties().remove(mp.next()); |
michael@0 | 167 | } |
michael@0 | 168 | calendars.put(uid, uidCal); |
michael@0 | 169 | } |
michael@0 | 170 | |
michael@0 | 171 | for (final Iterator j = c.getProperties().iterator(); j.hasNext();) { |
michael@0 | 172 | final Property p = (Property) j.next(); |
michael@0 | 173 | final TzId tzid = (TzId) p.getParameter(Parameter.TZID); |
michael@0 | 174 | if (tzid != null) { |
michael@0 | 175 | final VTimeZone timezone = (VTimeZone) timezones.getComponent(tzid.getValue()); |
michael@0 | 176 | if (!uidCal.getComponents().contains(timezone)) { |
michael@0 | 177 | uidCal.getComponents().add(timezone); |
michael@0 | 178 | } |
michael@0 | 179 | } |
michael@0 | 180 | } |
michael@0 | 181 | uidCal.getComponents().add(c); |
michael@0 | 182 | } |
michael@0 | 183 | return (Calendar[]) calendars.values().toArray(new Calendar[calendars.values().size()]); |
michael@0 | 184 | } |
michael@0 | 185 | |
michael@0 | 186 | /** |
michael@0 | 187 | * Returns a unique identifier as specified by components in the provided calendar. |
michael@0 | 188 | * @param calendar a calendar instance |
michael@0 | 189 | * @return the UID property |
michael@0 | 190 | * @throws ConstraintViolationException if zero or more than one unique identifer is found in the specified calendar |
michael@0 | 191 | */ |
michael@0 | 192 | public static Uid getUid(final Calendar calendar) throws ConstraintViolationException { |
michael@0 | 193 | Uid uid = null; |
michael@0 | 194 | for (final Iterator i = calendar.getComponents().iterator(); i.hasNext();) { |
michael@0 | 195 | final Component c = (Component) i.next(); |
michael@0 | 196 | for (final Iterator j = c.getProperties(Property.UID).iterator(); j.hasNext();) { |
michael@0 | 197 | final Uid foundUid = (Uid) j.next(); |
michael@0 | 198 | if (uid != null && !uid.equals(foundUid)) { |
michael@0 | 199 | throw new ConstraintViolationException("More than one UID found in calendar"); |
michael@0 | 200 | } |
michael@0 | 201 | uid = foundUid; |
michael@0 | 202 | } |
michael@0 | 203 | } |
michael@0 | 204 | if (uid == null) { |
michael@0 | 205 | throw new ConstraintViolationException("Calendar must specify a single unique identifier (UID)"); |
michael@0 | 206 | } |
michael@0 | 207 | return uid; |
michael@0 | 208 | } |
michael@0 | 209 | |
michael@0 | 210 | /** |
michael@0 | 211 | * Returns an appropriate MIME Content-Type for the specified calendar object. |
michael@0 | 212 | * @param calendar a calendar instance |
michael@0 | 213 | * @param charset an optional encoding |
michael@0 | 214 | * @return a content type string |
michael@0 | 215 | */ |
michael@0 | 216 | public static String getContentType(Calendar calendar, Charset charset) { |
michael@0 | 217 | final StringBuffer b = new StringBuffer("text/calendar"); |
michael@0 | 218 | |
michael@0 | 219 | final Method method = (Method) calendar.getProperty(Property.METHOD); |
michael@0 | 220 | if (method != null) { |
michael@0 | 221 | b.append("; method="); |
michael@0 | 222 | b.append(method.getValue()); |
michael@0 | 223 | } |
michael@0 | 224 | |
michael@0 | 225 | if (charset != null) { |
michael@0 | 226 | b.append("; charset="); |
michael@0 | 227 | b.append(charset); |
michael@0 | 228 | } |
michael@0 | 229 | return b.toString(); |
michael@0 | 230 | } |
michael@0 | 231 | } |