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.vishnu.client;
028:
029: import org.cougaar.core.util.UniqueObject;
030:
031: import java.util.Collection;
032: import java.util.Set;
033:
034: import org.w3c.dom.Document;
035: import org.w3c.dom.Element;
036:
037: import org.cougaar.util.log.Logger;
038:
039: /**
040: * Create and return xml for first class log plan objects.
041: * <p>
042: * Element name is extracted from object class, by taking the
043: * last field of the object class, and dropping a trailing "Impl",
044: * if it exists.
045: */
046:
047: public class ALPXMLize extends BaseXMLize {
048:
049: public ALPXMLize(Logger logger) {
050: super (logger);
051: }
052:
053: /** subclass to generate different tag */
054: protected Element createRootNode(Document doc, String tag,
055: boolean isTask, boolean isResource, Object obj,
056: String resourceClass) {
057: return doc.createElement(tag);
058: }
059:
060: /**
061: * Already seen this object or reached maximum depth.
062: * Write the UID if possible, otherwise write the "toString".
063: */
064: protected void generateElementReachedMaxDepth(Document doc,
065: Element parentElement, Object obj) {
066: if (logger.isInfoEnabled())
067: logger.info("Object traversed already/max depth: "
068: + obj.getClass().toString() + " " + obj);
069: if (isUniqueObject(obj)) {
070: Element item = doc.createElement("UID");
071: item.appendChild(doc.createTextNode(((UniqueObject) obj)
072: .getUID().toString()));
073: parentElement.appendChild(item);
074: } else {
075: parentElement.appendChild(doc
076: .createTextNode(obj.toString()));
077: }
078: }
079:
080: protected void generateLeaf(Document doc, Element parentElement,
081: String propertyName, Object propertyValue) {
082: Element item = doc.createElement(propertyName);
083: item.appendChild(doc.createTextNode(propertyValue.toString()));
084: parentElement.appendChild(item);
085: if (logger.isInfoEnabled())
086: logger.info("isLeaf - " + propertyName + " - "
087: + propertyValue);
088: }
089:
090: protected void generateNonLeaf(Document doc, Element parentElement,
091: String propertyName, Object propertyValue, int searchDepth,
092: boolean isList, boolean isFirst, Collection createdNodes) {
093: // this removes the class name following the $ for Locked classes
094: int index = propertyName.indexOf('$');
095: if (index > 0) {
096: propertyName = propertyName.substring(0, index);
097: }
098: Element item = doc.createElement(propertyName);
099: parentElement.appendChild(item);
100:
101: // recurse
102: addNodes(doc, propertyValue, item, (searchDepth - 1),
103: createdNodes);
104: }
105:
106: static Object monitor = new Object();
107: static ALPXMLize instance = null;
108:
109: public static ALPXMLize getInstance(Logger logger) {
110: if (instance == null)
111: instance = new ALPXMLize(logger);
112: return instance;
113: }
114: }
|