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.lib.contract.lang.parser;
028:
029: import java.io.*;
030: import java.util.*;
031:
032: import org.cougaar.lib.contract.lang.*;
033:
034: /**
035: * Creates a <code>TreeVisitor</code> that has XML as a <tt>toString</tt>.
036: */
037: public class XMLStringVisitor implements StringVisitor {
038:
039: /**
040: * The <code>String</code> result buffer.
041: */
042: private StringBuffer sb;
043:
044: /**
045: * A stack of words, with <tt>inWord</tt> as the implied top.
046: */
047: private Stack stk;
048:
049: /**
050: * The most current word, or null.
051: * <p>
052: * This is used to trim ">foo<>/foo<" down to ">foo/<"
053: * by recording the "top" of the <tt>stk</tt>.
054: */
055: private String inWord;
056:
057: /**
058: * Verbose flag.
059: */
060: private boolean verbose;
061:
062: /**
063: * Pretty-print flag;
064: */
065: private boolean pretty;
066:
067: /**
068: * Indent depth if <tt>pretty</tt> is <tt>true</tt>.
069: */
070: private int indent;
071:
072: public final void initialize() {
073: sb = new StringBuffer();
074: stk = new Stack();
075: inWord = null;
076: verbose = DEFAULT_VERBOSE;
077: pretty = true;//false;
078: indent = 0;
079: }
080:
081: public final boolean isVerbose() {
082: return verbose;
083: }
084:
085: public final void setVerbose(boolean verbose) {
086: this .verbose = verbose;
087: }
088:
089: public final boolean isPrettyPrint() {
090: return pretty;
091: }
092:
093: public final void setPrettyPrint(boolean pretty) {
094: this .pretty = pretty;
095: this .indent = 0;
096: }
097:
098: public final void visitEnd() {
099: if (inWord != null) {
100: sb.append("/>");
101: inWord = null;
102: if (pretty) {
103: --indent;
104: }
105: } else {
106: String w = (String) stk.pop();
107: if (pretty) {
108: sb.append(IndentFactory.createIndent(--indent));
109: }
110: sb.append("</").append(w).append(">");
111: }
112: }
113:
114: public final void visitWord(String word) {
115: if (inWord != null) {
116: stk.push(inWord);
117: sb.append(">");
118: }
119: inWord = word;
120: // word
121: if (pretty) {
122: sb.append(IndentFactory.createIndent(indent++));
123: }
124: sb.append("<").append(word);
125: }
126:
127: public final void visitConstant(String type, String value) {
128: if (inWord != null) {
129: stk.push(inWord);
130: sb.append(">");
131: inWord = null;
132: }
133: // constant "(const [\"type\"] \"value\")"
134: if (pretty) {
135: sb.append(IndentFactory.createIndent(indent));
136: }
137: sb.append("<const");
138: if (type != null) {
139: sb.append(" type=\"").append(type).append("\"");
140: }
141: sb.append(" value=\"").append(value).append("\"/>");
142: }
143:
144: public final void visitConstant(String value) {
145: visitConstant(null, value);
146: }
147:
148: public final void visitEndOfTree() {
149: }
150:
151: public String toString() {
152: return sb.toString();
153: }
154: }
|