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