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.xml.parser;
028:
029: import org.cougaar.planning.ldm.LDMServesPlugin;
030: import org.cougaar.planning.ldm.asset.Asset;
031: import org.cougaar.util.log.Logger;
032: import org.w3c.dom.Node;
033: import org.w3c.dom.NodeList;
034:
035: /**
036: * <pre>
037: * Parses prototype tags in an ldm.xml file or other prototype file.
038: * Caches the prototype in the ldm.
039: * </pre>
040: */
041: public class PrototypeParser {
042: /** creates an object parser for parsing the contents of the prototype file */
043: public PrototypeParser(Logger log) {
044: logger = log;
045: objectParser = new ObjectParser(log);
046: }
047:
048: /**
049: * <pre>
050: * Cache the prototype with the <tt>ldm</tt>, give the DOM
051: * document at <tt>node</tt>.
052: *
053: * Calls other cachePrototype with return_first set to false.
054: * </pre>
055: * @param ldm to register the prototype with
056: * @param node that defines the prototype
057: */
058: public void cachePrototype(LDMServesPlugin ldm, Node node) {
059: cachePrototype(ldm, node, false);
060: }
061:
062: /**
063: * <pre>
064: * Cache the prototype with the <tt>ldm</tt>, give the DOM
065: * document at <tt>node</tt>.
066: *
067: * Called from XMLPrototypeProviderPlugin.
068: *
069: * Most of the real work is done in ObjectParser.
070: *
071: * </pre>
072: * @param ldm to register the prototype with
073: * @param node that defines the prototype
074: * @param return_first says whether to return the prototype before cacheing it
075: * @return prototype defined by node
076: * @see org.cougaar.lib.xml.parser.ObjectParser#getObject
077: */
078: public Asset cachePrototype(LDMServesPlugin ldm, Node node,
079: boolean return_first) {
080:
081: Asset prototype = null;
082:
083: if (node.getNodeName().equals("prototype")) {
084: NodeList nlist = node.getChildNodes();
085: int nlength = nlist.getLength();
086:
087: String prototypeName = node.getAttributes().getNamedItem(
088: "name").getNodeValue();
089:
090: // NOTE: we can't do this check, since the DefaultPrototypeProvider
091: // caches some odd (AbstractAsset) values for otherwise normal
092: // (Consumables, Repairables, etc.) assets.
093: //:: Check and see if this prototype is already cached (do nothing).
094: //:: If prototype is not cached then go ahead and create then cache prototype.
095: // if(!myLDMServesPlugin.isPrototypeCached(prototypeName)) {
096: //
097: for (int i = 0; i < nlength; i++) {
098: Node child = nlist.item(i);
099: String childname = child.getNodeName();
100:
101: if (logger.isDebugEnabled())
102: logger
103: .debug("PrototypeParser.cachePrototype - child "
104: + childname);
105:
106: if (child.getNodeType() == Node.ELEMENT_NODE) {
107: if (childname.equals("object")) {
108: prototype = (Asset) objectParser.getObject(ldm,
109: child);
110: if (return_first)
111: return prototype; // return prototype without cacheing
112: ldm.cachePrototype(prototypeName, prototype);
113: if (logger.isDebugEnabled())
114: logger
115: .debug("Caching " + prototypeName
116: + " -> " + prototype
117: + " UID "
118: + prototype.getUID()
119: + " in " + ldm);
120: if (!ldm.isPrototypeCached(prototypeName))
121: logger.error("HUH? " + prototypeName
122: + " not cached?");
123:
124: }
125: }
126: }
127: }
128: return prototype;
129: }
130:
131: protected Logger logger;
132: protected ObjectParser objectParser;
133: }
|