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  /* 
17  
18   */
19  
20  package org.apache.pluto.portalImpl.util;
21  
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.io.InputStreamReader;
25  import java.io.BufferedReader;
26  
27  import java.util.Iterator;
28  
29  /***
30   ** This class loads name/value pairs from a properties
31   ** file. The properties file is specified as input stream
32   ** and follows the usual convention for Java properties files.
33   **/
34  
35  public class Properties extends NameValuePairs
36  {
37  
38      /***
39       ** Loads name/value pairs from the given input stream.
40       **/
41  
42      public void load (InputStream aInputStream) throws IOException
43      {
44          if (aInputStream == null)
45              return;
46  
47          BufferedReader reader = new BufferedReader (new InputStreamReader (aInputStream, "UTF-8"));
48  
49          String line;
50  
51          while ((line = reader.readLine ()) != null)
52          {
53              if (line.startsWith ("#") ||
54                  line.startsWith ("//"))
55              {
56                  continue;
57              }
58  
59              int index = line.indexOf ('=');
60  
61              if (index > 0)
62              {
63                  String name  = line.substring (0, index).trim ();
64  
65                  if (name.length () == 0)
66                      name = null;
67  
68                  String value = null;
69  
70                  if (index + 1 < line.length ())
71                  {
72                      value = line.substring (index + 1).trim ();
73  
74                      if (value.length () == 0)
75                          value = null;
76                  }
77  
78                  if (name != null && value != null)
79                  {
80                      add (name, value);
81                  }
82              }
83          }
84      }
85  
86  
87      /***
88       ** Returns a subset of the NameValuePairs where the names are starting with the specified prefix.
89       ** As keys of the result set all start with the same prefix, this prfix is cut off.</br>
90       ** E.g:</br>
91       ** Given following set: {[A1,x], [A2,y], [A3,z], [B1,x], [B2,y]]}</br>
92       ** getSubSet("A") returns {[1,x], [2,y], [3,z]}</p>
93       **
94       ** Please note: the implementation of this method is <b>slow</b>, to be used only for initialisation tasks or alike!
95       **
96       ** @param   aNamePrefix
97       **          the prefix that all returned pair names have in common.
98       **/
99  
100     public Properties getSubSet (String aNamePrefix)
101     {
102         Properties subset = new Properties();
103         int prefixLength = aNamePrefix.length();
104         String name;
105 
106         for (Iterator iter = this.names (); iter.hasNext () ; )
107         {
108             name = (String) iter.next ();
109 
110             if (name.startsWith(aNamePrefix))
111             {
112                 subset.add( name.substring(prefixLength), getStrings(name));
113             }
114         }
115         return (subset);
116     }
117 }