View Javadoc

1   /*
2    * Copyright 2003,2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.pluto.portalImpl.util;
17  
18  import java.io.BufferedReader;
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.io.InputStreamReader;
22  import java.util.Iterator;
23  
24  /***
25   ** This class loads name/value pairs from a properties
26   ** file. The properties file is specified as input stream
27   ** and follows the usual convention for Java properties files.
28   **/
29  
30  public class Properties extends NameValuePairs
31  {
32  
33      /***
34       ** Loads name/value pairs from the given input stream.
35       **/
36  
37      public void load (InputStream aInputStream) throws IOException
38      {
39          if (aInputStream == null)
40              return;
41  
42          BufferedReader reader = new BufferedReader (new InputStreamReader (aInputStream, "UTF-8"));
43  
44          String line;
45  
46          while ((line = reader.readLine ()) != null)
47          {
48              if (line.startsWith ("#") ||
49                  line.startsWith ("//"))
50              {
51                  continue;
52              }
53  
54              int index = line.indexOf ('=');
55  
56              if (index > 0)
57              {
58                  String name  = line.substring (0, index).trim ();
59  
60                  if (name.length () == 0)
61                      name = null;
62  
63                  String value = null;
64  
65                  if (index + 1 < line.length ())
66                  {
67                      value = line.substring (index + 1).trim ();
68  
69                      if (value.length () == 0)
70                          value = null;
71                  }
72  
73                  if (name != null && value != null)
74                  {
75                      add (name, value);
76                  }
77              }
78          }
79      }
80  
81  
82      /***
83       ** Returns a subset of the NameValuePairs where the names are starting with the specified prefix.
84       ** As keys of the result set all start with the same prefix, this prfix is cut off.</br>
85       ** E.g:</br>
86       ** Given following set: {[A1,x], [A2,y], [A3,z], [B1,x], [B2,y]]}</br>
87       ** getSubSet("A") returns {[1,x], [2,y], [3,z]}</p>
88       **
89       ** Please note: the implementation of this method is <b>slow</b>, to be used only for initialisation tasks or alike!
90       **
91       ** @param   aNamePrefix
92       **          the prefix that all returned pair names have in common.
93       **/
94  
95      public Properties getSubSet (String aNamePrefix)
96      {
97          Properties subset = new Properties();
98          int prefixLength = aNamePrefix.length();
99          String name;
100 
101         for (Iterator iter = this.names (); iter.hasNext () ; )
102         {
103             name = (String) iter.next ();
104 
105             if (name.startsWith(aNamePrefix))
106             {
107                 subset.add( name.substring(prefixLength), getStrings(name));
108             }
109         }
110         return (subset);
111     }
112 }