KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > forrest > conf > AntProperties


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

17 package org.apache.forrest.conf;
18
19 import java.io.BufferedReader JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.io.InputStreamReader JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Properties JavaDoc;
27 import java.util.StringTokenizer JavaDoc;
28
29 import org.apache.commons.lang.StringUtils;
30
31 /**
32  * Class for accessing properties in a properties file roughly compatible with
33  * Ant property files, where ${name} is replaced with the value of the property
34  * if declared beforehand.
35  */

36 public class AntProperties extends Properties JavaDoc
37 {
38
39     public AntProperties() {
40         super();
41     }
42
43     public AntProperties(Properties JavaDoc arg0) {
44         super(arg0);
45         putAllProperties(arg0);
46     }
47
48     public synchronized void load(InputStream JavaDoc arg0) throws IOException JavaDoc {
49         super.load(arg0);
50
51         BufferedReader JavaDoc in = null;
52         try {
53
54             in = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(arg0));
55
56             String JavaDoc currentLine, name, value;
57             int splitIndex;
58
59             while ((currentLine = in.readLine()) != null) {
60                 // # == comment
61
if (!currentLine.startsWith("#")
62                                 && !(currentLine.trim().length() == 0)) {
63                     splitIndex = currentLine.indexOf('=');
64                     name = currentLine.substring(0, splitIndex).trim();
65                     value = currentLine.substring(splitIndex + 1).trim();
66                     this.put(name, value);
67                 }
68             }
69         } finally {
70             if (in != null) {
71                 try {
72                     in.close();
73                 } catch (IOException JavaDoc e) {}
74             }
75         }
76     }
77
78     public synchronized Object JavaDoc put(Object JavaDoc name, Object JavaDoc value) {
79         //if the property is already there don't overwrite, as in Ant
80
//properties defined first take precedence
81
if (!super.containsKey(name)) {
82             Enumeration JavaDoc names = super.propertyNames();
83             while (names.hasMoreElements()) {
84                 String JavaDoc currentName = (String JavaDoc) names.nextElement();
85                 String JavaDoc valueToSearchFor = "${" + currentName + "}";
86                 String JavaDoc valueToReplaceWith = (String JavaDoc) super.get(currentName);
87                 value = StringUtils.replace(value.toString(), valueToSearchFor,
88                                 valueToReplaceWith);
89             }
90             return super.put(name, value);
91         }
92
93         return null;
94     }
95
96     public synchronized void putAllProperties(Map JavaDoc arg0) {
97         Iterator JavaDoc i = arg0.entrySet().iterator();
98         while (i.hasNext()) {
99             Map.Entry JavaDoc me = (Map.Entry JavaDoc)i.next();
100             this.put(me.getKey(), me.getValue());
101         }
102     }
103
104     public synchronized Object JavaDoc setProperty(String JavaDoc name, String JavaDoc value) {
105         return this.put(name, value);
106     }
107
108     public String JavaDoc filter(String JavaDoc stringToFilter) {
109
110         StringBuffer JavaDoc resultStringBuffer = new StringBuffer JavaDoc();
111
112         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(stringToFilter, "@", true);
113
114         STATE state;
115
116         if (stringToFilter.startsWith("@")) {
117             state = new STATE(STATE.START_TOKEN);
118         } else {
119             state = new STATE(STATE.STRING_NOT_TO_FILTER);
120         }
121
122         for (String JavaDoc currentToken; st.hasMoreTokens(); state.increment()) {
123
124             currentToken = st.nextToken();
125
126             if (state.isStringToFilter()) {
127                 resultStringBuffer.append(getProperty(currentToken, "@"
128                                 + currentToken + "@"));
129             } else if (state.isStringNOTToFilter()) {
130                 resultStringBuffer.append(currentToken);
131             }
132         }
133
134         return resultStringBuffer.toString();
135     }
136
137     private static class STATE
138     {
139
140         final static int STRING_NOT_TO_FILTER = 0;
141         final static int START_TOKEN = 1;
142         final static int STRING_TO_FILTER = 2;
143         final static int END_TOKEN = 3;
144
145         private int currentState;
146
147         STATE(int startState) {
148             this.currentState = startState;
149         }
150
151         void increment() {
152             currentState++;
153             if (currentState > 3) {
154                 currentState = 0;
155             }
156
157         }
158
159         boolean isStringToFilter() {
160             return (currentState == STRING_TO_FILTER);
161         }
162
163         boolean isStringNOTToFilter() {
164             return (currentState == STRING_NOT_TO_FILTER);
165         }
166     }
167
168 }
169
Popular Tags