001: /*
002: *
003: * <copyright>
004: *
005: * Copyright 1997-2004 BBNT Solutions, LLC
006: * under sponsorship of the Defense Advanced Research Projects
007: * Agency (DARPA).
008: *
009: * You can redistribute this software and/or modify it under the
010: * terms of the Cougaar Open Source License as published on the
011: * Cougaar Open Source Website (www.cougaar.org).
012: *
013: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
014: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
015: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
016: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
017: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
018: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
019: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
020: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
021: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
022: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
023: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
024: *
025: * </copyright>
026: */
027: package org.cougaar.planning.servlet;
028:
029: import java.io.BufferedReader;
030: import java.io.IOException;
031: import java.io.InputStream;
032: import java.io.InputStreamReader;
033: import java.io.Reader;
034: import java.io.StringReader;
035:
036: import org.cougaar.core.servlet.ServletUtil;
037: import org.cougaar.util.PropertyTree;
038:
039: /**
040: * Parser for the <code>PlanViewServlet</code>'s "Advanced Search"
041: * loading of the built-in predicates.
042: *
043: * @see #parse(BufferedReader) for input stream format
044: */
045: public class PredTableParser {
046:
047: /** @see #parse(BufferedReader) */
048: public static final PropertyTree parse(InputStream in) {
049: return parse(new BufferedReader(new InputStreamReader(in)));
050: }
051:
052: /** @see #parse(BufferedReader) */
053: public static final PropertyTree parse(Reader in) {
054: return parse(new BufferedReader(in));
055: }
056:
057: /**
058: * Parse the input to build a PropertyTree of encoded predicates
059: * for the <code>PlanViewServlet</code>'s "Advanced Search" page.
060: * <pre>
061: * Expected file format is:
062: *
063: * Entry separator lines start with "***"
064: *
065: * Comment lines start with "**" and then can contain additional
066: * characters (except having the third char be "*", since this
067: * would confuse things with the entry separator)
068: *
069: * Entries must have at least two lines or they are ignored. They
070: * must be prefixed AND followed by "***" lines.
071: *
072: * The first line is the "key", and the following lines make the
073: * "value". These are encoded with
074: * <tt>ServletUtil.encodeForHTML</tt> and
075: * <tt>ServletUtil.encodeForJava</tt>
076: * to let the PlanViewServlet print them in HTML and Javascript.
077: * </pre>
078: * @see #main(String[]) for an example
079: */
080: public static final PropertyTree parse(BufferedReader in) {
081: PropertyTree pt = new PropertyTree();
082: try {
083: // read entries
084: readEntries: while (true) {
085: // read the key line
086: String rawKey = in.readLine();
087: if (rawKey == null) {
088: // end of input
089: break readEntries;
090: }
091: if (rawKey.startsWith("**")) {
092: // ignore comment or empty entry
093: continue readEntries;
094: }
095: // read the value lines
096: String rawValue = null;
097: readValue: while (true) {
098: String s = in.readLine();
099: if (s == null) {
100: // end of input
101: break readEntries;
102: }
103: if (!(s.startsWith("**"))) {
104: // value line, typical case
105: if (rawValue != null) {
106: rawValue += "\n" + s;
107: } else {
108: rawValue = s;
109: }
110: } else {
111: // control line
112: if ((s.length() <= 2) || (s.charAt(2) != '*')) {
113: // comment ("**?");
114: continue readValue;
115: } else {
116: // end entry ("***");
117: if (rawValue != null) {
118: break readValue;
119: } else {
120: // ignore
121: break readEntries;
122: }
123: }
124: }
125: }
126: // encode the key and value for javascript use
127: String encKey = ServletUtil.encodeForHTML(rawKey);
128: String encValue = ServletUtil.encodeForJava(rawValue);
129: // add to property tree
130: pt.put(encKey, encValue);
131: }
132: in.close();
133: } catch (IOException ioe) {
134: System.err
135: .println("Unable to parse PlanViewServlet's predicates: "
136: + ioe);
137: }
138: return pt;
139: }
140:
141: /** testing utility, plus illustrates file format. */
142: public static void main(String[] args) {
143: String s = "******" + "\n** comment " + "\n**" + "\n***"
144: + "\n******" + "\nkey" + "\nvalue" + "\n******"
145: + "\ncomplex (x < \"foo\" > z) \\ endKey"
146: + "\n**comment" + "\n// java comment" + "\n//"
147: + "\n/* another java comment \"here\"*/" + "\n"
148: + "\nvalue" + "\n(y < \"z\")"
149: + "\n ** not a comment \\ here"
150: + "\n *** not the end of the entry"
151: + "\n more junk > blah"
152: + "\nkeep double-encoded: \\\" \\n" + "\n" + "\nend..."
153: + "\n***";
154: System.out.println("Test with:\n" + s);
155: // can replace this deprecated input with something newer...
156: StringReader srin = new StringReader(s);
157: // parse!
158: PropertyTree pt = parse(srin);
159: int n = pt.size();
160: System.out.println("Parsed[" + n + "]");
161: for (int i = 0; i < n; i++) {
162: String ki = (String) pt.getKey(i);
163: String vi = (String) pt.getValue(i);
164: System.out.println(i + ")");
165: System.out.println(" key=|" + ki + "|");
166: System.out.println(" value=|" + vi + "|");
167: }
168: }
169:
170: }
|