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.AggregateAsset;
031: import org.cougaar.planning.ldm.asset.Asset;
032: import org.cougaar.planning.ldm.plan.NewRoleSchedule;
033: import org.cougaar.planning.ldm.plan.Schedule;
034: import org.cougaar.util.log.Logger;
035: import org.w3c.dom.NamedNodeMap;
036: import org.w3c.dom.Node;
037: import org.w3c.dom.NodeList;
038:
039: /**
040: * Creates asset from AggregateAsset xml node. <p>
041: *
042: * Called from UTILLdmXMLPlugin.
043: */
044: public class AggregateAssetParser {
045: private static final boolean testing = false;
046:
047: public AggregateAssetParser(Logger log) {
048: logger = log;
049: scheduleParser = new ScheduleParser();
050: }
051:
052: /**
053: * This function is for use with the UTILLdmXMLPlugin. <p>
054: *
055: * Insensitive to the case of the <aggregateasset> tag. (So <AggregateAsset> is OK too.)
056: *
057: * @param ldm the factory to ask to create instances
058: * @param node the AggregateAsset document node itself
059: * @return an aggregate asset that corresponds to the node
060: */
061: public AggregateAsset getAggregate(LDMServesPlugin ldm, Node node) {
062: AggregateAsset newAsset = null;
063:
064: if (node.getNodeName().toLowerCase().equals("aggregateasset")) {
065: NamedNodeMap map = node.getAttributes();
066:
067: Node prototypeNode = map.getNamedItem("prototype");
068: if (prototypeNode == null) {
069: prototypeNode = getNodeNamed(node.getChildNodes(),
070: "prototype");
071: prototypeNode = prototypeNode.getChildNodes().item(0);
072: }
073:
074: Node quantityNode = map.getNamedItem("quantity");
075: if (quantityNode == null) {
076: quantityNode = getNodeNamed(node.getChildNodes(),
077: "quantity");
078: quantityNode = quantityNode.getChildNodes().item(0);
079: }
080:
081: String prototype;
082: int quantity;
083: try {
084: String quantityNodeValue = quantityNode.getNodeValue();
085: quantity = (new Integer(quantityNodeValue)).intValue();
086: } catch (NullPointerException npe) {
087: logger
088: .error(classname
089: + ".getAggregate - XML syntax error : expecting *quantity* attribute on tag <"
090: + node.getNodeName()
091: + "> or child node.");
092: return null;
093: }
094:
095: try {
096: prototype = prototypeNode.getNodeValue();
097: } catch (NullPointerException npe) {
098: logger
099: .error(classname
100: + ".getAggregate - XML syntax error : expecting *prototype* attribute on tag <"
101: + node.getNodeName()
102: + "> or child node.");
103: return null;
104: }
105:
106: if (logger.isDebugEnabled())
107: logger.debug("Creating " + quantity + " of "
108: + prototype);
109:
110: Asset proto = ldm.getFactory().getPrototype(prototype);
111:
112: if (proto == null)
113: logger
114: .error(classname
115: + ".getAggregate - XML syntax error : no prototype "
116: + prototype
117: + " known. Check cluster's ldm.xml file to make sure "
118: + prototype
119: + "'s prototype file is included.");
120:
121: else {
122: newAsset = (AggregateAsset) ldm.getFactory()
123: .createAggregate(proto, quantity);
124: if (logger.isDebugEnabled())
125: logger.debug(classname
126: + ".getAggregate - Creating agg asset "
127: + newAsset);
128:
129: Schedule sched = getSchedule(ldm, node.getChildNodes());
130: if (sched != null)
131: setSchedule(ldm, newAsset, sched);
132: }
133: } else {
134: logger
135: .error(classname
136: + ".getAggregate - XML syntax error : expecting <AggregateAsset> instead got <"
137: + node.getNodeName() + ">");
138: }
139: return newAsset;
140: }
141:
142: protected Node getNodeNamed(NodeList nlist, String name) {
143: for (int i = 0; i < nlist.getLength(); i++) {
144: Node child = nlist.item(i);
145: String childname = child.getNodeName();
146: if (childname.equals(name)) {
147: return child;
148: }
149: }
150: return null;
151: }
152:
153: protected Schedule getSchedule(LDMServesPlugin ldm, NodeList nlist) {
154: // Only expect one schedule per instance
155: for (int i = 0; i < nlist.getLength(); i++) {
156: Node child = nlist.item(i);
157: if (child.getNodeType() == Node.ELEMENT_NODE) {
158: if (child.getNodeName().equals("schedule")) {
159: return scheduleParser.getSchedule(ldm, child);
160: }
161: }
162: }
163: return null;
164: }
165:
166: /**
167: * Give the aggregate asset a default schedule.
168: *
169: * @param ldm - place to get new instances
170: * @param asset - to modify
171: * @param newSchedule - initial availability
172: */
173: protected void setSchedule(LDMServesPlugin ldm, Asset asset,
174: Schedule newSchedule) {
175: if (logger.isDebugEnabled())
176: logger.debug("setSchedule");
177: // Schedule copySchedule = ldm.getFactory().newSimpleSchedule(new Date(newSchedule.getStartTime()),
178: // new Date(newSchedule.getEndTime()));
179: // Set the Schedule
180: ((NewRoleSchedule) asset.getRoleSchedule())
181: .setAvailableSchedule(newSchedule);
182: }
183:
184: private static String classname = AggregateAssetParser.class
185: .getName();
186: protected Logger logger;
187: protected ScheduleParser scheduleParser;
188: }
|