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 java.util.Date;
030: import java.util.Vector;
031:
032: import org.cougaar.lib.util.UTILAsset;
033: import org.cougaar.planning.ldm.LDMServesPlugin;
034: import org.cougaar.planning.ldm.asset.AggregateAsset;
035: import org.cougaar.planning.ldm.asset.Asset;
036: import org.cougaar.planning.ldm.asset.AssetGroup;
037: import org.cougaar.util.log.Logger;
038: import org.w3c.dom.Node;
039: import org.w3c.dom.NodeList;
040:
041: /**
042: * Creates an asset group -- primarily used in parsing test input files.
043: */
044: public class AssetGroupParser {
045: public AssetGroupParser(Logger log) {
046: logger = log;
047: assetHelper = new UTILAsset(log);
048: assetParser = new AssetParser(log);
049: aggregateAssetParser = new AggregateAssetParser(log);
050: }
051:
052: public AssetGroup getAssetGroup(LDMServesPlugin ldm, Node node) {
053: AssetGroup ag = null;
054: try {
055: NodeList nlist = node.getChildNodes();
056: int nlength = nlist.getLength();
057: Node idNode = node.getAttributes().getNamedItem("id");
058: String id = null;
059:
060: if (idNode == null)
061: id = "" + new Date().getTime();
062: else
063: id = idNode.getNodeValue();
064:
065: Vector assets = new Vector();
066:
067: for (int i = 0; i < nlength; i++) {
068: Node child = nlist.item(i);
069: String childname = child.getNodeName();
070:
071: if (childname.equals("asset")) {
072: Asset asset = assetParser.getAsset(ldm, child);
073: assets.addElement(asset);
074: } else if (childname.equals("aggregateasset")) {
075: AggregateAsset asset = aggregateAssetParser
076: .getAggregate(ldm, child);
077: assets.addElement(asset);
078: } else if (childname.equals("assetgroup")) {
079: AssetGroup asset = getAssetGroup(ldm, child); // RECURSE
080: assets.addElement(asset);
081: } else if (!childname.equals("#text")) {
082: logger
083: .error("AssetGroupParser - XML Syntax error : "
084: + "expecting one of <asset>, <aggregateasset>, or <assetgroup> but got <"
085: + childname + ">");
086: }
087: }
088: if (ldm.getFactory() == null)
089: logger
090: .error("WARNING: LDM Factory is null in xmlparser!");
091: ag = assetHelper.makeAssetGroup(ldm.getFactory(), id);
092: ag.setAssets(assets);
093: } catch (Exception e) {
094: logger.error(e.getMessage(), e);
095: }
096:
097: return ag;
098: }
099:
100: protected Logger logger;
101: protected UTILAsset assetHelper;
102: protected AssetParser assetParser;
103: protected AggregateAssetParser aggregateAssetParser;
104: }
|