michael@18: /** michael@18: * Copyright (c) 2012, Ben Fortuna michael@18: * All rights reserved. michael@18: * michael@18: * Redistribution and use in source and binary forms, with or without michael@18: * modification, are permitted provided that the following conditions michael@18: * are met: michael@18: * michael@18: * o Redistributions of source code must retain the above copyright michael@18: * notice, this list of conditions and the following disclaimer. michael@18: * michael@18: * o Redistributions in binary form must reproduce the above copyright michael@18: * notice, this list of conditions and the following disclaimer in the michael@18: * documentation and/or other materials provided with the distribution. michael@18: * michael@18: * o Neither the name of Ben Fortuna nor the names of any other contributors michael@18: * may be used to endorse or promote products derived from this software michael@18: * without specific prior written permission. michael@18: * michael@18: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@18: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@18: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@18: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR michael@18: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, michael@18: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, michael@18: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR michael@18: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF michael@18: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING michael@18: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS michael@18: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@18: */ michael@18: package net.fortuna.ical4j.model; michael@18: michael@18: import java.io.IOException; michael@18: import java.net.URL; michael@18: import java.util.Map; michael@18: import java.util.Properties; michael@18: import java.util.regex.Matcher; michael@18: import java.util.regex.Pattern; michael@18: michael@18: import net.fortuna.ical4j.data.CalendarBuilder; michael@18: import net.fortuna.ical4j.data.ParserException; michael@18: import net.fortuna.ical4j.model.component.VTimeZone; michael@18: import net.fortuna.ical4j.model.property.TzUrl; michael@18: import net.fortuna.ical4j.util.CompatibilityHints; michael@18: import net.fortuna.ical4j.util.Configurator; michael@18: import net.fortuna.ical4j.util.ResourceLoader; michael@18: michael@18: import android.util.Log; michael@18: michael@18: import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap; michael@18: michael@18: /** michael@18: * $Id$ michael@18: * michael@18: * Created on 18/09/2005 michael@18: * michael@18: * The default implementation of a TimeZoneRegistry. This implementation will search the classpath for michael@18: * applicable VTimeZone definitions used to back the provided TimeZone instances. michael@18: * @author Ben Fortuna michael@18: */ michael@18: public class TimeZoneRegistryImpl implements TimeZoneRegistry { michael@18: michael@18: private static final String DEFAULT_RESOURCE_PREFIX = "zoneinfo/"; michael@18: michael@18: private static final Pattern TZ_ID_SUFFIX = Pattern.compile("(?<=/)[^/]*/[^/]*$"); michael@18: michael@18: private static final String UPDATE_ENABLED = "net.fortuna.ical4j.timezone.update.enabled"; michael@18: michael@18: private static final Map DEFAULT_TIMEZONES = new ConcurrentHashMap(); michael@18: michael@18: private static final Properties ALIASES = new Properties(); michael@18: static { michael@18: try { michael@18: ALIASES.load(ResourceLoader.getResourceAsStream("tz.alias")); michael@18: } michael@18: catch (IOException ioe) { michael@18: Log.w("MSvB-Hack", "Error loading timezone aliases: " + ioe.getMessage()); michael@18: } michael@18: try { michael@18: ALIASES.load(ResourceLoader.getResourceAsStream("/tz.alias")); michael@18: } michael@18: catch (Exception e) { michael@18: Log.d("MSvB-Hack", "Error loading custom timezone aliases: " + e.getMessage()); michael@18: } michael@18: } michael@18: michael@18: private Map timezones; michael@18: michael@18: private String resourcePrefix; michael@18: michael@18: /** michael@18: * Default constructor. michael@18: */ michael@18: public TimeZoneRegistryImpl() { michael@18: this(DEFAULT_RESOURCE_PREFIX); michael@18: } michael@18: michael@18: /** michael@18: * Creates a new instance using the specified resource prefix. michael@18: * @param resourcePrefix a prefix prepended to classpath resource lookups for default timezones michael@18: */ michael@18: public TimeZoneRegistryImpl(final String resourcePrefix) { michael@18: this.resourcePrefix = resourcePrefix; michael@18: timezones = new ConcurrentHashMap(); michael@18: } michael@18: michael@18: /** michael@18: * {@inheritDoc} michael@18: */ michael@18: public final void register(final TimeZone timezone) { michael@18: // for now we only apply updates to included definitions by default.. michael@18: register(timezone, false); michael@18: } michael@18: michael@18: /** michael@18: * {@inheritDoc} michael@18: */ michael@18: public final void register(final TimeZone timezone, boolean update) { michael@18: if (update) { michael@18: // load any available updates for the timezone.. michael@18: timezones.put(timezone.getID(), new TimeZone(updateDefinition(timezone.getVTimeZone()))); michael@18: } michael@18: else { michael@18: timezones.put(timezone.getID(), timezone); michael@18: } michael@18: } michael@18: michael@18: /** michael@18: * {@inheritDoc} michael@18: */ michael@18: public final void clear() { michael@18: timezones.clear(); michael@18: } michael@18: michael@18: /** michael@18: * {@inheritDoc} michael@18: */ michael@18: public final TimeZone getTimeZone(final String id) { michael@18: TimeZone timezone = (TimeZone) timezones.get(id); michael@18: if (timezone == null) { michael@18: timezone = (TimeZone) DEFAULT_TIMEZONES.get(id); michael@18: if (timezone == null) { michael@18: // if timezone not found with identifier, try loading an alias.. michael@18: final String alias = ALIASES.getProperty(id); michael@18: if (alias != null) { michael@18: return getTimeZone(alias); michael@18: } michael@18: else { michael@18: synchronized (DEFAULT_TIMEZONES) { michael@18: // check again as it may be loaded now.. michael@18: timezone = (TimeZone) DEFAULT_TIMEZONES.get(id); michael@18: if (timezone == null) { michael@18: try { michael@18: final VTimeZone vTimeZone = loadVTimeZone(id); michael@18: if (vTimeZone != null) { michael@18: // XXX: temporary kludge.. michael@18: // ((TzId) vTimeZone.getProperties().getProperty(Property.TZID)).setValue(id); michael@18: timezone = new TimeZone(vTimeZone); michael@18: DEFAULT_TIMEZONES.put(timezone.getID(), timezone); michael@18: } michael@18: else if (CompatibilityHints.isHintEnabled(CompatibilityHints.KEY_RELAXED_PARSING)) { michael@18: // strip global part of id and match on default tz.. michael@18: Matcher matcher = TZ_ID_SUFFIX.matcher(id); michael@18: if (matcher.find()) { michael@18: return getTimeZone(matcher.group()); michael@18: } michael@18: } michael@18: } michael@18: catch (Exception e) { michael@18: Log.w("MSvB-Hack", "Error occurred loading VTimeZone", e); michael@18: } michael@18: } michael@18: } michael@18: } michael@18: } michael@18: } michael@18: return timezone; michael@18: } michael@18: michael@18: /** michael@18: * Loads an existing VTimeZone from the classpath corresponding to the specified Java timezone. michael@18: */ michael@18: private VTimeZone loadVTimeZone(final String id) throws IOException, ParserException { michael@18: final URL resource = ResourceLoader.getResource(resourcePrefix + id + ".ics"); michael@18: if (resource != null) { michael@18: final CalendarBuilder builder = new CalendarBuilder(); michael@18: final Calendar calendar = builder.build(resource.openStream()); michael@18: final VTimeZone vTimeZone = (VTimeZone) calendar.getComponent(Component.VTIMEZONE); michael@18: // load any available updates for the timezone.. can be explicility disabled via configuration michael@18: if (!"false".equals(Configurator.getProperty(UPDATE_ENABLED))) { michael@18: return updateDefinition(vTimeZone); michael@18: } michael@18: return vTimeZone; michael@18: } michael@18: return null; michael@18: } michael@18: michael@18: /** michael@18: * @param vTimeZone michael@18: * @return michael@18: */ michael@18: private VTimeZone updateDefinition(VTimeZone vTimeZone) { michael@18: final TzUrl tzUrl = vTimeZone.getTimeZoneUrl(); michael@18: if (tzUrl != null) { michael@18: try { michael@18: final CalendarBuilder builder = new CalendarBuilder(); michael@18: final Calendar calendar = builder.build(tzUrl.getUri().toURL().openStream()); michael@18: final VTimeZone updatedVTimeZone = (VTimeZone) calendar.getComponent(Component.VTIMEZONE); michael@18: if (updatedVTimeZone != null) { michael@18: return updatedVTimeZone; michael@18: } michael@18: } michael@18: catch (Exception e) { michael@18: Log.w("MSvB-Hack", "Unable to retrieve updates for timezone: " + vTimeZone.getTimeZoneId().getValue(), e); michael@18: } michael@18: } michael@18: return vTimeZone; michael@18: } michael@18: }