src/net/fortuna/ical4j/util/Calendars.java

branch
ICAL4J_EMBED_1
changeset 15
cc93757aeca3
parent 14
5ae3e5665a0b
child 18
6dcaece8ec41
     1.1 --- a/src/net/fortuna/ical4j/util/Calendars.java	Thu Feb 12 18:02:00 2015 +0100
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,231 +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.util;
    1.36 -
    1.37 -import java.io.FileInputStream;
    1.38 -import java.io.IOException;
    1.39 -import java.net.URL;
    1.40 -import java.nio.charset.Charset;
    1.41 -import java.util.HashMap;
    1.42 -import java.util.Iterator;
    1.43 -import java.util.Map;
    1.44 -
    1.45 -import net.fortuna.ical4j.data.CalendarBuilder;
    1.46 -import net.fortuna.ical4j.data.ParserException;
    1.47 -import net.fortuna.ical4j.model.Calendar;
    1.48 -import net.fortuna.ical4j.model.Component;
    1.49 -import net.fortuna.ical4j.model.ComponentList;
    1.50 -import net.fortuna.ical4j.model.ConstraintViolationException;
    1.51 -import net.fortuna.ical4j.model.IndexedComponentList;
    1.52 -import net.fortuna.ical4j.model.Parameter;
    1.53 -import net.fortuna.ical4j.model.Property;
    1.54 -import net.fortuna.ical4j.model.component.VTimeZone;
    1.55 -import net.fortuna.ical4j.model.parameter.TzId;
    1.56 -import net.fortuna.ical4j.model.property.Method;
    1.57 -import net.fortuna.ical4j.model.property.Uid;
    1.58 -
    1.59 -/**
    1.60 - * $Id$
    1.61 - *
    1.62 - * Created on 10/11/2006
    1.63 - *
    1.64 - * Utility method for working with {@link Calendar}s.
    1.65 - * @author Ben Fortuna
    1.66 - */
    1.67 -public final class Calendars {
    1.68 -
    1.69 -    /**
    1.70 -     * Constructor made private to enforce static nature.
    1.71 -     */
    1.72 -    private Calendars() {
    1.73 -    }
    1.74 -
    1.75 -    /**
    1.76 -     * Loads a calendar from the specified file.
    1.77 -     * @param filename the name of the file from which to load calendar data
    1.78 -     * @return returns a new calendar instance initialised from the specified file
    1.79 -     * @throws IOException occurs when there is an error reading the specified file
    1.80 -     * @throws ParserException occurs when the data in the specified file is invalid
    1.81 -     */
    1.82 -    public static Calendar load(final String filename) throws IOException, ParserException {
    1.83 -        final FileInputStream fin = new FileInputStream(filename);
    1.84 -        final CalendarBuilder builder = new CalendarBuilder();
    1.85 -        return builder.build(fin);
    1.86 -    }
    1.87 -
    1.88 -    /**
    1.89 -     * Loads a calendar from the specified URL.
    1.90 -     * @param url the URL from which to load calendar data
    1.91 -     * @return returns a new calendar instance initialised from the specified URL
    1.92 -     * @throws IOException occurs when there is an error reading from the specified URL
    1.93 -     * @throws ParserException occurs when the data in the specified URL is invalid
    1.94 -     */
    1.95 -    public static Calendar load(final URL url) throws IOException, ParserException {
    1.96 -        final CalendarBuilder builder = new CalendarBuilder();
    1.97 -        return builder.build(url.openStream());
    1.98 -    }
    1.99 -
   1.100 -    /**
   1.101 -     * Merge all properties and components from two specified calendars into one instance.
   1.102 -     * Note that the merge process is not very sophisticated, and may result in invalid calendar
   1.103 -     * data (e.g. multiple properties of a type that should only be specified once).
   1.104 -     * @param c1 the first calendar to merge
   1.105 -     * @param c2 the second calendar to merge
   1.106 -     * @return a Calendar instance containing all properties and components from both of the specified calendars
   1.107 -     */
   1.108 -    public static Calendar merge(final Calendar c1, final Calendar c2) {
   1.109 -        final Calendar result = new Calendar();
   1.110 -        result.getProperties().addAll(c1.getProperties());
   1.111 -        for (final Iterator i = c2.getProperties().iterator(); i.hasNext();) {
   1.112 -            final Property p = (Property) i.next();
   1.113 -            if (!result.getProperties().contains(p)) {
   1.114 -                result.getProperties().add(p);
   1.115 -            }
   1.116 -        }
   1.117 -        result.getComponents().addAll(c1.getComponents());
   1.118 -        for (final Iterator i = c2.getComponents().iterator(); i.hasNext();) {
   1.119 -            final Component c = (Component) i.next();
   1.120 -            if (!result.getComponents().contains(c)) {
   1.121 -                result.getComponents().add(c);
   1.122 -            }
   1.123 -        }
   1.124 -        return result;
   1.125 -    }
   1.126 -
   1.127 -    /**
   1.128 -     * Wraps a component in a calendar.
   1.129 -     * @param component the component to wrap with a calendar
   1.130 -     * @return a calendar containing the specified component
   1.131 -     */
   1.132 -    public static Calendar wrap(final Component component) {
   1.133 -        final ComponentList components = new ComponentList();
   1.134 -        components.add(component);
   1.135 -        return new Calendar(components);
   1.136 -    }
   1.137 -    
   1.138 -    /**
   1.139 -     * Splits a calendar object into distinct calendar objects for unique
   1.140 -     * identifers (UID).
   1.141 -     * @param calendar a calendar instance
   1.142 -     * @return an array of calendar objects
   1.143 -     */
   1.144 -    public static Calendar[] split(final Calendar calendar) {
   1.145 -        // if calendar contains one component or less, or is composed entirely of timezone
   1.146 -        // definitions, return the original calendar unmodified..
   1.147 -        if (calendar.getComponents().size() <= 1
   1.148 -                || calendar.getComponents(Component.VTIMEZONE).size() == calendar.getComponents().size()) {
   1.149 -            return new Calendar[] {calendar};
   1.150 -        }
   1.151 -        
   1.152 -        final IndexedComponentList timezones = new IndexedComponentList(calendar.getComponents(Component.VTIMEZONE),
   1.153 -                Property.TZID);
   1.154 -        
   1.155 -        final Map calendars = new HashMap();
   1.156 -        for (final Iterator i = calendar.getComponents().iterator(); i.hasNext();) {
   1.157 -            final Component c = (Component) i.next();
   1.158 -            if (c instanceof VTimeZone) {
   1.159 -                continue;
   1.160 -            }
   1.161 -            
   1.162 -            final Uid uid = (Uid) c.getProperty(Property.UID);
   1.163 -            
   1.164 -            Calendar uidCal = (Calendar) calendars.get(uid);
   1.165 -            if (uidCal == null) {
   1.166 -                uidCal = new Calendar(calendar.getProperties(), new ComponentList());
   1.167 -                // remove METHOD property for split calendars..
   1.168 -                for (final Iterator mp = uidCal.getProperties(Property.METHOD).iterator(); mp.hasNext();) {
   1.169 -                    uidCal.getProperties().remove(mp.next());
   1.170 -                }
   1.171 -                calendars.put(uid, uidCal);
   1.172 -            }
   1.173 -            
   1.174 -            for (final Iterator j = c.getProperties().iterator(); j.hasNext();) {
   1.175 -                final Property p = (Property) j.next();
   1.176 -                final TzId tzid = (TzId) p.getParameter(Parameter.TZID);
   1.177 -                if (tzid != null) {
   1.178 -                    final VTimeZone timezone = (VTimeZone) timezones.getComponent(tzid.getValue());
   1.179 -                    if (!uidCal.getComponents().contains(timezone)) {
   1.180 -                        uidCal.getComponents().add(timezone);
   1.181 -                    }
   1.182 -                }
   1.183 -            }
   1.184 -            uidCal.getComponents().add(c);
   1.185 -        }
   1.186 -        return (Calendar[]) calendars.values().toArray(new Calendar[calendars.values().size()]);
   1.187 -    }
   1.188 -    
   1.189 -    /**
   1.190 -     * Returns a unique identifier as specified by components in the provided calendar.
   1.191 -     * @param calendar a calendar instance
   1.192 -     * @return the UID property
   1.193 -     * @throws ConstraintViolationException if zero or more than one unique identifer is found in the specified calendar
   1.194 -     */
   1.195 -    public static Uid getUid(final Calendar calendar) throws ConstraintViolationException {
   1.196 -        Uid uid = null;
   1.197 -        for (final Iterator i = calendar.getComponents().iterator(); i.hasNext();) {
   1.198 -            final Component c = (Component) i.next();
   1.199 -            for (final Iterator j = c.getProperties(Property.UID).iterator(); j.hasNext();) {
   1.200 -                final Uid foundUid = (Uid) j.next();
   1.201 -                if (uid != null && !uid.equals(foundUid)) {
   1.202 -                    throw new ConstraintViolationException("More than one UID found in calendar");
   1.203 -                }
   1.204 -                uid = foundUid;
   1.205 -            }
   1.206 -        }
   1.207 -        if (uid == null) {
   1.208 -            throw new ConstraintViolationException("Calendar must specify a single unique identifier (UID)");
   1.209 -        }
   1.210 -        return uid;
   1.211 -    }
   1.212 -    
   1.213 -    /**
   1.214 -     * Returns an appropriate MIME Content-Type for the specified calendar object.
   1.215 -     * @param calendar a calendar instance
   1.216 -     * @param charset an optional encoding
   1.217 -     * @return a content type string
   1.218 -     */
   1.219 -    public static String getContentType(Calendar calendar, Charset charset) {
   1.220 -        final StringBuffer b = new StringBuffer("text/calendar");
   1.221 -        
   1.222 -        final Method method = (Method) calendar.getProperty(Property.METHOD);
   1.223 -        if (method != null) {
   1.224 -            b.append("; method=");
   1.225 -            b.append(method.getValue());
   1.226 -        }
   1.227 -        
   1.228 -        if (charset != null) {
   1.229 -            b.append("; charset=");
   1.230 -            b.append(charset);
   1.231 -        }
   1.232 -        return b.toString();
   1.233 -    }
   1.234 -}

mercurial