001: /*
002: * URLProperties.java December 2003
003: *
004: * Copyright (C) 2003, Niall Gallagher <niallg@users.sf.net>
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General
016: * Public License along with this library; if not, write to the
017: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
018: * Boston, MA 02111-1307 USA
019: */
020:
021: package simple.util;
022:
023: import simple.util.PropertyParser;
024: import java.util.Properties;
025: import java.io.IOException;
026: import java.io.InputStream;
027: import java.net.URL;
028:
029: /**
030: * The <code>URLProperties</code> object is used as a convienience
031: * class that is used to create a <code>Properties</code> object
032: * from a URL. This will automatically load the Java properties
033: * file once instantiated. This is used so that the creation and
034: * use of a <code>java.util.Properties</code> file is much cleaner.
035: *
036: * @author Niall Gallagher
037: */
038: public class URLProperties extends Properties {
039:
040: /**
041: * Used to load the properties from the specified file.
042: */
043: private PropertyParser parser;
044:
045: /**
046: * Constructor for the <code>URLProperties</code> object. The
047: * constructor will use the specified URL as the contents of
048: * its properties. The URL source must point to a valid Java
049: * properties file, using a URL scheme such as HTTP or FTP.
050: * For example "http://host/data.properties" will use HTTP.
051: *
052: * @param source this is the URL that contains the properties
053: *
054: * @exception IOException if the properties can not be loaded
055: */
056: public URLProperties(String source) throws IOException {
057: this (new URL(source));
058: }
059:
060: /**
061: * Constructor for the <code>URLProperties</code> object. The
062: * constructor will use the specified URL as the contents of
063: * its properties. The URL source must point to a valid Java
064: * properties file, using a URL scheme such as HTTP or FTP.
065: * For example "http://host/data.properties" will use HTTP.
066: *
067: * @param source this is the URL that contains the properties
068: *
069: * @exception IOException if the properties can not be loaded
070: */
071: public URLProperties(URL source) throws IOException {
072: this (source.openStream());
073: }
074:
075: /**
076: * Constructor for the <code>URLProperties</code> object. The
077: * constructor will use the given <code>InputStream</code> to
078: * load Java properties for this instance. This is used by the
079: * other constructor methods to keep the object simple.
080: *
081: * @param source this is the URL that contains the properties
082: *
083: * @exception IOException if the properties can not be loaded
084: */
085: private URLProperties(InputStream source) throws IOException {
086: this .parser = new PropertyParser(this );
087: this .load(source);
088: }
089:
090: /**
091: * This overloads the <code>Properties.load</code> so that XML
092: * properties can be loaded as well as standard properties.
093: * Once this method is invoked the stream is examined to see
094: * if the properties format is in XML. If the file is a valid
095: * XML file then the properties are loaded int he XML format.
096: * Otherwise they are loaded using the traditional format.
097: *
098: * @param source the stream that contains the properties
099: */
100: public synchronized void load(InputStream source)
101: throws IOException {
102: try {
103: parser.load(source);
104: } catch (Exception e) {
105: throw new PropertyException(e);
106: }
107: }
108: }
|