KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > protomatter > util > ProtoProperties


1 package com.protomatter.util;
2
3 /**
4  * {{{ The Protomatter Software License, Version 1.0
5  * derived from The Apache Software License, Version 1.1
6  *
7  * Copyright (c) 1998-2002 Nate Sammons. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed for the
24  * Protomatter Software Project
25  * (http://protomatter.sourceforge.net/)."
26  * Alternately, this acknowledgment may appear in the software itself,
27  * if and wherever such third-party acknowledgments normally appear.
28  *
29  * 4. The names "Protomatter" and "Protomatter Software Project" must
30  * not be used to endorse or promote products derived from this
31  * software without prior written permission. For written
32  * permission, please contact support@protomatter.com.
33  *
34  * 5. Products derived from this software may not be called "Protomatter",
35  * nor may "Protomatter" appear in their name, without prior written
36  * permission of the Protomatter Software Project
37  * (support@protomatter.com).
38  *
39  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
40  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
41  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42  * DISCLAIMED. IN NO EVENT SHALL THE PROTOMATTER SOFTWARE PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
46  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
47  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
48  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
49  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE. }}}
51  */

52
53 import java.io.*;
54 import java.util.*;
55
56 /**
57  * A Properties object that allows in-line file imports.
58  * This is just like java.util.Properties, except that
59  * it honors the following directives:<P>
60  *
61  * <blockquote><tt>Import <i>filename</i></tt></blockquote><P>
62  *
63  * Which imports the given filename in place (imported files can
64  * also have imports, but circular refs are <b>NOT</b> checked).
65  */

66 public class ProtoProperties
67 extends Properties
68 {
69   /**
70    * Create a blank properties object with nothing defined.
71    *
72    * @see java.util.Properties
73    */

74   public ProtoProperties()
75   {
76     super();
77   }
78
79   /**
80    * Create a properties object with the given default settings.
81    *
82    * @see java.util.Properties
83    */

84   public ProtoProperties(Properties props)
85   {
86     super(props);
87   }
88
89   public synchronized void load(InputStream in)
90   throws IOException
91   {
92     read(new BufferedReader(new InputStreamReader(in)));
93   }
94
95   /**
96    * Read lines from the given buffered reader and append them
97    * to the given vector.
98    */

99   private void read(BufferedReader reader)
100   throws IOException
101   {
102     String JavaDoc line = null;
103     while (true)
104     {
105       if (line == null)
106         line = reader.readLine();
107       if (line == null)
108         return;
109
110       if ((line.length() > 0) && (line.charAt(0) != '#'))
111       {
112         if (line.startsWith("Import "))
113         {
114           String JavaDoc file = line.substring(7); // "Import ".length()
115
read(new BufferedReader(new FileReader(new File(file))));
116           line = null; // don't re-use this line.
117
}
118         else if (line.charAt(line.length() -1) == '\\')
119         {
120           line = line.substring(0, line.length() -1);
121           StringBuffer JavaDoc b = new StringBuffer JavaDoc();
122           b.append(line);
123
124           boolean done = false;
125           while (!done)
126           {
127             line = reader.readLine();
128             if (line != null)
129               line = line.trim();
130             if (line == null || line.length() == 0)
131             {
132               line = null; // don't re-use this line
133
done = true;
134             }
135             else if (line.charAt(0) == '#') // comment (skip)
136
{
137               ; // no-op
138
}
139             else
140             {
141               if (line.charAt(line.length() -1) == '\\')
142               {
143                 line = line.substring(0, line.length() -1);
144                 b.append(line);
145               }
146               else
147               {
148                 b.append(line);
149                 line = null; // don't re-use this line
150
done = true;
151               }
152             }
153           }
154
155           addProp(b.toString());
156         }
157         else // "normal" line
158
{
159           addProp(line);
160           line = null;
161         }
162       }
163       else
164       {
165         line = null; // want to read the next line.
166
}
167     }
168   }
169
170   private void addProp(String JavaDoc line)
171   {
172     int index = line.indexOf("=");
173     if (index != -1)
174     {
175       String JavaDoc key = line.substring(0, index).trim();
176       String JavaDoc val = line.substring(index +1).trim();
177       put(key, val);
178     }
179   }
180
181   /**
182    * A simple test program that reads a properties file defined as the
183    * first command-line argument, and then lists the properties to
184    * Standard Out.
185    */

186   public static void main(String JavaDoc args[])
187   {
188     if (args.length != 1)
189     {
190       System.out.println("Usage: com.protomatter.util.ProtoProperties file");
191       System.exit(0);
192     }
193     try
194     {
195       Properties p = new ProtoProperties();
196       p.load(new FileInputStream(new File(args[0])));
197
198       Enumeration e = p.keys();
199       while (e.hasMoreElements())
200       {
201         String JavaDoc key = (String JavaDoc)e.nextElement();
202         String JavaDoc val = p.getProperty(key);
203         System.out.println("'" + key + "' = '" + val + "'");
204         System.out.println("");
205       }
206     }
207     catch (Exception JavaDoc x)
208     {
209       x.printStackTrace();
210     }
211   }
212 }
213
Popular Tags