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: import org.w3c.dom.Document;
035: import org.w3c.dom.Node;
036: import org.w3c.dom.Element;
037: import org.apache.xerces.dom.DocumentImpl;
038:
039: public class XMLBuilderVisitor implements TreeVisitor {
040:
041: private Document doc;
042: private Node currNode;
043: private boolean verbose;
044:
045: public XMLBuilderVisitor() {
046: this (null);
047: }
048:
049: public XMLBuilderVisitor(Document doc) {
050: if (doc == null) {
051: doc = new DocumentImpl();
052: }
053: this .doc = doc;
054: currNode = doc;
055: }
056:
057: public void initialize() {
058: verbose = false;
059: }
060:
061: public boolean isVerbose() {
062: return verbose;
063: }
064:
065: public void setVerbose(boolean verbose) {
066: this .verbose = verbose;
067: }
068:
069: public void visitEndOfTree() {
070: }
071:
072: public void visitEnd() {
073: currNode = currNode.getParentNode();
074: }
075:
076: public void visitWord(String w) {
077: Element wordElem = doc.createElement(w);
078: currNode.appendChild(wordElem);
079: currNode = wordElem;
080: }
081:
082: public void visitConstant(String type, String value) {
083: Element constElem = doc.createElement("const");
084: if (type != null) {
085: constElem.setAttribute("type", type);
086: }
087: constElem.setAttribute("value", value);
088: currNode.appendChild(constElem);
089: }
090:
091: public void visitConstant(String value) {
092: visitConstant(null, value);
093: }
094:
095: public Element getResult() {
096: // correct usage only adds a single Element child!
097: return (Element) doc.getLastChild();
098: }
099:
100: }
|