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.util;
028:
029: import java.io.BufferedReader;
030: import java.io.FileInputStream;
031: import java.io.IOException;
032: import java.io.InputStream;
033: import java.io.InputStreamReader;
034: import java.util.List;
035: import java.util.ArrayList;
036: import java.util.Iterator;
037:
038: /** Utility for reading MSWindows-style .ini files.
039: **/
040:
041: public class INIParser {
042:
043: private boolean isVerbose = false;
044:
045: public boolean isVerbose() {
046: return isVerbose;
047: }
048:
049: public void setVerbose(boolean v) {
050: isVerbose = v;
051: }
052:
053: private char commentChar = '#';
054: private String filename = "<stream>";
055:
056: public char getCommentChar() {
057: return commentChar;
058: }
059:
060: public void setCommentChar(char c) {
061: commentChar = c;
062: }
063:
064: public static class SlotHolder {
065: /** List<Slot> **/
066: private List slots = new ArrayList(5);
067:
068: public List getSlots() {
069: return slots;
070: }
071:
072: public void addSlot(Slot s) {
073: slots.add(s);
074: }
075:
076: public Slot getSlot(String name) {
077: int ix = slots.indexOf(name);
078: if (ix < 0)
079: return null;
080: return (Slot) slots.get(ix);
081: }
082: }
083:
084: public static class Group extends SlotHolder {
085: private List sections = new ArrayList(5);
086:
087: public List getSections() {
088: return sections;
089: }
090:
091: public void addSection(Section s) {
092: sections.add(s);
093: }
094: }
095:
096: public static class Section extends SlotHolder {
097: /** name of Section **/
098: private String name = null;
099:
100: public String getName() {
101: return name;
102: }
103:
104: /** List<String> **/
105: private List parameters = new ArrayList();
106:
107: public List getParameters() {
108: return parameters;
109: }
110:
111: public void addParameter(String p) {
112: parameters.add(p);
113: }
114:
115: public Section(String name) {
116: this .name = name;
117: }
118:
119: public String toString() {
120: return "Section " + name;
121: }
122: }
123:
124: public static class Slot {
125: private String name;
126:
127: public String getName() {
128: return name;
129: }
130:
131: private List values = new ArrayList(1);
132:
133: public List getValues() {
134: return values;
135: }
136:
137: public String getValue() {
138: if (values.size() < 1)
139: return null;
140: return (String) values.get(0);
141: }
142:
143: public void setValues(List v) {
144: values = v;
145: }
146:
147: public void addValue(String v) {
148: values.add(v);
149: }
150:
151: public Slot(String name) {
152: this .name = name;
153: }
154:
155: public String toString() {
156: return "Slot " + name;
157: }
158:
159: public boolean equals(Object obj) {
160: if (obj instanceof String) {
161: return name.equals(obj);
162: }
163: if (obj instanceof Slot) {
164: return name.equals(((Slot) obj).name);
165: }
166: return false;
167: }
168: }
169:
170: public Group parse(String filename) throws IOException {
171: this .filename = filename;
172: InputStream stream = null;
173:
174: if (filename.equals("-")) {
175: if (isVerbose)
176: System.err.println("Reading from standard input.");
177: stream = new java.io.DataInputStream(System.in);
178: } else {
179: if (isVerbose)
180: System.err.println("Reading \"" + filename + "\".");
181: stream = new FileInputStream(filename);
182: }
183:
184: Group g = parse(stream);
185: stream.close();
186: return g;
187: }
188:
189: public Section parse(String filename, String section)
190: throws IOException {
191: this .filename = filename;
192: InputStream stream = null;
193:
194: if (filename.equals("-")) {
195: if (isVerbose)
196: System.err.println("Reading from standard input.");
197: stream = new java.io.DataInputStream(System.in);
198: } else {
199: if (isVerbose)
200: System.err.println("Reading \"" + filename + "\".");
201: stream = new FileInputStream(filename);
202: }
203:
204: Section s = parse(stream, section);
205: stream.close();
206: return s;
207: }
208:
209: public Section parse(InputStream in, String section)
210: throws IOException {
211: InputStreamReader isr = new InputStreamReader(in);
212: BufferedReader br = new BufferedReader(isr);
213:
214: Group g = runParser(br);
215: if (g != null) {
216: for (Iterator it = g.getSections().iterator(); it.hasNext();) {
217: Section s = (Section) it.next();
218: if (section.equals(s.getName())) {
219: return s;
220: }
221: }
222: }
223:
224: return null;
225: }
226:
227: public Group parse(InputStream in) throws IOException {
228: InputStreamReader isr = new InputStreamReader(in);
229: BufferedReader br = new BufferedReader(isr);
230: return runParser(br);
231: }
232:
233: private Group runParser(BufferedReader br) throws IOException {
234: Group g = new Group();
235: Section section = null;
236:
237: String line;
238: int ln = 0;
239: for (line = br.readLine(); line != null; line = br.readLine()) {
240: int i, j;
241:
242: ln++;
243:
244: // ignore comments
245: if ((i = line.indexOf(commentChar)) >= 0)
246: line = line.substring(0, i);
247:
248: // zap extra whitespace
249: line = line.trim();
250:
251: // ignore empty lines
252: if (line.length() <= 0)
253: continue;
254:
255: int l;
256: while (line.charAt((l = line.length()) - 1) == '\\') {
257: line = line.substring(0, l - 1) + br.readLine().trim();
258: ln++;
259: }
260:
261: if (line.charAt(0) == '[') {
262: // classd line
263: j = line.indexOf(']');
264: String stuff = line.substring(1, j).trim();
265:
266: String[] v = stuff.split("\\s*");
267:
268: if (v.length < 1) {
269: throw new RuntimeException("Empty Section at line "
270: + ln);
271: }
272:
273: section = new Section(v[0]);
274: for (i = 1; i < v.length; i++) {
275: section.addParameter(v[i]);
276: }
277:
278: g.addSection(section);
279: } else if ((j = line.indexOf('=')) >= 0) {
280: // a param line
281:
282: String name = line.substring(0, j).trim();
283: List v = CSVUtility.parseToList(line.substring(j + 1));
284:
285: Slot slot = new Slot(name);
286: slot.setValues(v);
287: if (section == null) {
288: g.addSlot(slot);
289: } else {
290: section.addSlot(slot);
291: }
292: } else {
293: System.err
294: .println("Bad line " + ln + " in " + filename);
295: }
296: }
297: return g;
298: }
299: }
|