KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > interop > properties > PropertyMap


1 /**
2  *
3  * Copyright 2004-2005 The Apache Software Foundation
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 implied.
14  *
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

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