001    /*
002     * Copyright 2004 Niclas Hedhman
003     *
004     * Licensed  under the  Apache License,  Version 2.0  (the "License");
005     * you may not use  this file  except in  compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *   http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed  under the  License is distributed on an "AS IS" BASIS,
012     * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
013     * implied.
014     *
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    
019    package net.dpml.util;
020    
021    import net.dpml.transit.Transit;
022    
023    import java.util.Properties;
024    import java.util.Iterator;
025    import java.util.Map;
026    import java.util.ArrayList;
027    import java.net.URL;
028    import java.io.IOException;
029    import java.io.InputStream;
030    import java.io.InputStreamReader;
031    import java.io.BufferedReader;
032    
033    /**
034     * Utility class supporting operations related to property retrival.
035     * @author <a href="http://www.dpml.net">Digital Product Meta Library</a>
036     * @version 1.0.3
037     */
038    public final class Util
039    {
040        // ------------------------------------------------------------------------
041        // static
042        // ------------------------------------------------------------------------
043    
044       /**
045        * Read a set of properties from a property file specificed by a url.
046        * Property files may reference symbolic properties in the form ${name}.
047        * @param propsUrl the url of the property file to read
048        * @return the resolved properties
049        * @exception IOException if an io error occurs
050        */
051        public static Properties readProps( URL propsUrl )
052            throws IOException
053        {
054             return readProps( propsUrl, true );
055        }
056    
057       /**
058        * Read a set of properties from a property file specificed by a url.
059        * Property files may reference symbolic properties in the form ${name}.
060        * @param propsUrl the url of the property file to read
061        * @param resolve if TRUE apply property symbol resolution
062        * @return the resolved properties
063        * @exception IOException if an io error occurs
064        */
065        public static Properties readProps( URL propsUrl, boolean resolve )
066            throws IOException
067        {
068            InputStream stream = propsUrl.openStream();
069            try
070            {
071                Properties p = new Properties();
072                p.load( stream );
073                if( resolve )
074                {
075                    p.setProperty( Transit.HOME_KEY, Transit.DPML_HOME.toString() );
076                    Iterator list = p.entrySet().iterator();
077                    while ( list.hasNext() )
078                    {
079                        Map.Entry entry = (Map.Entry) list.next();
080                        String value = (String) entry.getValue();
081                        value = resolveProperty( p, value );
082                        entry.setValue( value );
083                    }
084                }
085                return p;
086            } 
087            finally
088            {
089                stream.close();
090            }
091        }
092    
093       /**
094        * Resolve symbols in a supplied value against supplied known properties.
095        * @param props a set of know properties
096        * @param value the string to parse for tokens
097        * @return the resolved string
098        */
099        public static String resolveProperty( Properties props, String value )
100        {
101            value =  PropertyResolver.resolve( props, value );
102            return value;
103        }
104    
105       /**
106        * Return the value of a property.
107        * @param props the property file
108        * @param key the property key to lookup
109        * @param def the default value
110        * @return the resolve value
111        */
112        public static String getProperty( Properties props, String key, String def )
113        {
114            String value = props.getProperty( key, def );
115            if( value == null )
116            {
117                return null;
118            }
119            if( "".equals( value ) )
120            {
121                return value;
122            }
123            value =  PropertyResolver.resolve( props, value );
124            return value;
125        }
126    
127       /**
128        * Read a file and return the list of lines in an array of strings.
129        * @param listFile the url to read from
130        * @return the lines
131        * @exception IOException if a read error occurs
132        */
133        public static String[] readListFile( URL listFile )
134            throws IOException
135        {
136            ArrayList list = new ArrayList();
137            InputStream stream = openInputStream( listFile );
138            try
139            {
140                InputStreamReader isr = new InputStreamReader( stream, "UTF-8" );
141                BufferedReader reader = new BufferedReader( isr );
142                String line = reader.readLine();
143                while ( line != null )
144                {
145                    list.add( line );
146                    line = reader.readLine();
147                }
148                String[] items = new String[ list.size() ];
149                list.toArray( items );
150                return items;
151            } 
152            finally
153            {
154                stream.close();
155            }
156        }
157    
158        private static InputStream openInputStream( URL url ) throws IOException
159        {
160            try
161            {
162                return url.openStream();
163            }
164            catch( IOException e )
165            {
166                System.out.println( "#URL: " + url );
167                System.out.println( e.toString() );
168                throw e;
169            }
170        }
171    
172        // ------------------------------------------------------------------------
173        // constructor
174        // ------------------------------------------------------------------------
175    
176       /**
177        * Constructor.
178        */
179        private Util()
180        {
181        }
182    }