001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.tools.build;
028:
029: import java.io.BufferedReader;
030: import java.io.IOException;
031: import java.io.InputStream;
032: import java.io.InputStreamReader;
033: import java.util.Collection;
034: import java.util.Collections;
035: import java.util.HashMap;
036: import java.util.Map;
037:
038: class PGParser {
039:
040: public static final String DEFAULT_FILENAME = "properties.def";
041: public static final String TIMEPHASED = "timephased";
042: public static final String HAS_RELATIONSHIPS = "hasrelationships";
043: public static final String LOCAL = "local";
044:
045: public static final String PG_SOURCE = "source";
046: public static final String PRIMARY = "primary";
047: public static final String INCLUDED = "included";
048:
049: //private boolean isVerbose = false;
050: private boolean isVerbose = true;
051:
052: private InputStream inputStream = null;
053: private boolean modifiable = true;
054:
055: public PGParser(boolean isVerbose) {
056: this .isVerbose = isVerbose;
057: }
058:
059: public PGParser(InputStream input, boolean isVerbose) {
060: this .isVerbose = isVerbose;
061: inputStream = input;
062: }
063:
064: Map<String, Map<String, String>> table = new HashMap<String, Map<String, String>>();
065:
066: public boolean hasContext(String context) {
067: return (table.get(context) != null);
068: }
069:
070: public Map<String, String> getContext(String context) {
071: Map<String, String> ct = table.get(context);
072: if (ct == null) {
073: ct = new HashMap<String, String>();
074: putIntoContext(context, ct);
075: }
076: return ct;
077: }
078:
079: private void putIntoContext(String context, Map<String, String> ct) {
080: if (!isModifiable()) {
081: throw new IllegalStateException("Can't put " + context
082: + " into context table when read only.");
083: }
084: table.put(context, ct);
085: }
086:
087: public void put(String context, String key, String value) {
088: Map<String, String> ct = getContext(context);
089: ct.put(key, value);
090: }
091:
092: public String get(String context, String key) {
093: Map<String, String> ct = getContext(context);
094: return ct.get(key);
095: }
096:
097: public Collection<String> getContexts() {
098: return Collections.unmodifiableSet(table.keySet());
099: }
100:
101: public void parse() throws IOException {
102: parse(inputStream);
103: }
104:
105: public void parse(InputStream s) throws IOException {
106: parse(s, true);
107: }
108:
109: public void parse(InputStream s, boolean top) throws IOException {
110: InputStreamReader isr = new InputStreamReader(s);
111: BufferedReader br = new BufferedReader(isr);
112: String context = "global";
113: int i;
114:
115: getContext(context);
116: for (String line = br.readLine(); line != null; line = br
117: .readLine()) {
118: line = line.trim();
119: if (line.length() == 0 || line.startsWith(";")) {
120: // empty or comment line
121: } else {
122: while (line.endsWith("\\")) {
123: String more = br.readLine();
124: if (more == null)
125: throw new IOException("Unexpected EOF");
126: line = line.substring(0, line.length() - 1)
127: + more.trim();
128: }
129: if (line.startsWith("includedefs")) {
130: i = line.indexOf("=");
131: if (i > 0) {
132: String fileName = line.substring(i + 1,
133: line.length()).trim();
134: InputStream stream = getClass()
135: .getClassLoader().getResourceAsStream(
136: fileName);
137: try {
138: parse(stream, false);
139: } catch (Exception e) {
140: System.err
141: .println("Could not find resource "
142: + fileName
143: + " referenced by includedefs on classpath. Aborting.");
144: System.exit(-1); // something better to do here than exit? should we continue?
145: }
146: if (stream != null)
147: stream.close();
148: } else {
149: debug("Bad line \"" + line + "\"");
150: }
151: }
152:
153: if (line.startsWith("[") && line.endsWith("]")) { // new context
154: context = line.substring(1, line.length() - 1);
155: debug("Parsing PropertyGroup \"" + context + "\"");
156: if (hasContext(context)) {
157: System.err
158: .println("Multiple definition of PropertyGroup "
159: + context);
160: }
161: getContext(context); // setup the new context
162: if (top) {
163: put(context, PG_SOURCE, PRIMARY);
164: } else {
165: put(context, PG_SOURCE, INCLUDED);
166: }
167: } else if ((i = line.indexOf("=")) >= 0) { // key/value pair
168: //Don't include global values from included files.
169: if (top || !(context.equals("global"))) {
170: String key = line.substring(0, i);
171: String value = line.substring(i + 1, line
172: .length());
173:
174: put(context, key, value);
175: }
176: } else {
177: debug("Bad line \"" + line + "\"");
178: }
179: }
180: }
181: }
182:
183: public void debug(String s) {
184: if (isVerbose)
185: System.err.println(s);
186: }
187:
188: public Object getValue(String context, String key,
189: boolean lookGlobal, Object defaultValue) {
190: Object value = get(context, key);
191: if ((value == null) && (lookGlobal)) {
192: value = get("global", key);
193: }
194: if (value == null) {
195: return defaultValue;
196: } else {
197: return value;
198: }
199: }
200:
201: public boolean getValueFlag(String context, String key,
202: boolean lookGlobal, boolean defaultValue) {
203: Object value = getValue(context, key, lookGlobal, new Boolean(
204: defaultValue));
205:
206: if (value instanceof Boolean) {
207: return ((Boolean) value).booleanValue();
208: } else {
209: return Boolean.valueOf((String) value).booleanValue();
210: }
211: }
212:
213: public boolean isModifiable() {
214: return modifiable;
215: }
216:
217: public void setModifiable(boolean modifiable) {
218: this.modifiable = modifiable;
219: }
220: }
|