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: * Implementation of <code>TreeVisitor</code> which will create a
036: * "paren" semi-lisp style <tt>toString</tt>
037: */
038: public class ParenStringVisitor implements StringVisitor {
039:
040: private StringBuffer sb;
041: private boolean pretty;
042: private int indent;
043: private boolean verbose;
044:
045: public void initialize() {
046: sb = new StringBuffer();
047: this .pretty = false;
048: indent = 0;
049: verbose = DEFAULT_VERBOSE;
050: }
051:
052: public final boolean isVerbose() {
053: return verbose;
054: }
055:
056: public final void setVerbose(boolean verbose) {
057: this .verbose = verbose;
058: }
059:
060: public final boolean isPrettyPrint() {
061: return pretty;
062: }
063:
064: public final void setPrettyPrint(boolean pretty) {
065: this .pretty = pretty;
066: this .indent = 0;
067: }
068:
069: public final void visitEnd() {
070: if (pretty) {
071: --indent;
072: }
073: sb.append(")");
074: }
075:
076: public final void visitWord(String word) {
077: if (pretty) {
078: sb.append(IndentFactory.createIndent(indent++));
079: }
080: sb.append("(").append(word);
081: }
082:
083: public final void visitConstant(String type, String value) {
084: if (pretty) {
085: sb.append(IndentFactory.createIndent(indent));
086: }
087: if (type == null) {
088: sb.append("\"").append(value).append("\"");
089: } else {
090: sb.append("(const \"").append(type);
091: sb.append("\" \"").append(value).append("\")");
092: }
093: }
094:
095: /** Short for <tt>visitConstant("java.lang.String", value)</tt>. */
096: public final void visitConstant(String value) {
097: visitConstant(null, value);
098: }
099:
100: public final void visitEndOfTree() {
101: }
102:
103: public String toString() {
104: return sb.toString();
105: }
106: }
|