KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gcc > properties > PropertyMap


1 /*
2  * Copyright 2004 The Apache Software Foundation or its licensors, as
3  * applicable.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19 package gcc.properties;
20
21 import gcc.*;
22 import java.io.*;
23 import java.util.*;
24
25 public class PropertyMap extends HashMap
26 {
27     public PropertyMap()
28     {
29         super();
30     }
31
32     public PropertyMap(Map map)
33     {
34         super(map);
35     }
36
37     public Object put(Object key, Object value)
38     {
39         if (key == null)
40         {
41             throw new NullPointerException("key");
42         }
43         if (value == null)
44         {
45             // Avoid exception in getProperties (Properties can't take null value)
46
throw new NullPointerException("value");
47         }
48         return super.put(key, value);
49     }
50
51     public String getProperty(String name)
52     {
53         return (String)super.get(name);
54     }
55
56     public String getProperty(String name, String defaultValue)
57     {
58         String value = getProperty(name);
59         if (value == null || value.length() == 0)
60         {
61             value = defaultValue;
62         }
63         return value;
64     }
65
66     public Properties getProperties()
67     {
68         Properties props = new Properties();
69         for (Iterator i = entrySet().iterator(); i.hasNext();)
70         {
71             Map.Entry entry = (Map.Entry)i.next();
72             String key = (String)entry.getKey();
73             String value = (String)entry.getValue();
74             props.put(key, value);
75         }
76         return props;
77     }
78
79     public static PropertyMap readFile(String fileName)
80     {
81         try
82         {
83             Properties props = new Properties();
84             BufferedInputStream input = new BufferedInputStream(new FileInputStream(fileName));
85             props.load(input);
86             input.close();
87             PropertyMap map = new PropertyMap();
88             for (Iterator i = props.entrySet().iterator(); i.hasNext();)
89             {
90                 Map.Entry entry = (Map.Entry)i.next();
91                 String key = entry.getKey().toString().trim();
92                 String value = entry.getValue().toString().trim();
93                 map.put(key, value);
94             }
95             return map;
96         }
97         catch (Exception ex)
98         {
99             throw new SystemException(ex);
100         }
101     }
102 }
103
Popular Tags