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.ArrayList;
030: import java.util.List;
031: import java.util.Iterator;
032:
033: import org.cougaar.lib.util.UTILAsset;
034: import org.cougaar.planning.ldm.LDMServesPlugin;
035: import org.cougaar.planning.ldm.asset.Asset;
036: import org.cougaar.planning.ldm.plan.NewRoleSchedule;
037: import org.cougaar.planning.ldm.plan.Schedule;
038: import org.cougaar.util.log.Logger;
039: import org.w3c.dom.Node;
040: import org.w3c.dom.NodeList;
041:
042: /**
043: * Creates instances specified in ldm.xml files.
044: *
045: * Expects a base prototype and optionally a quantity of n instances of the
046: * new asset.
047: */
048: public class InstanceParser {
049:
050: public InstanceParser(Logger logger) {
051: scheduleParser = new ScheduleParser();
052: fieldParser = new FieldParser(logger, new ObjectParser(logger));
053: assetHelper = new UTILAsset(logger);
054: }
055:
056: public List getInstance(LDMServesPlugin ldm, Node node) {
057:
058: Schedule newSchedule = null;
059: List newAssets = new ArrayList();
060: List fields = new ArrayList();
061:
062: if (node.getNodeName().equals("instance")) {
063: NodeList nlist = node.getChildNodes();
064: int nlength = nlist.getLength();
065:
066: String id = node.getAttributes().getNamedItem("id")
067: .getNodeValue();
068: String prototype = node.getAttributes().getNamedItem(
069: "prototype").getNodeValue();
070:
071: // Only expect one schedule per instance but what the heck.
072: for (int i = 0; i < nlength; i++) {
073: Node child = nlist.item(i);
074: if (child.getNodeType() == Node.ELEMENT_NODE) {
075: if (child.getNodeName().equals("schedule")) {
076: newSchedule = scheduleParser.getSchedule(ldm,
077: child);
078: } else if (child.getNodeName().equals("field")) {
079: // We may have to set the field on a bunch of instances.
080: // So, we just save a list for now.
081: fields.add(child);
082: }
083: }
084: }
085:
086: Integer intQ = new Integer(1);
087:
088: if (node.getAttributes().getNamedItem("quantity") != null) {
089: String quantity = node.getAttributes().getNamedItem(
090: "quantity").getNodeValue();
091: intQ = new Integer(quantity);
092: }
093:
094: for (int j = 0; j < intQ.intValue(); j++) {
095: String newID = id;
096: String prefix = "";
097: // we want id's less than 10 to be 01, 02, etc. instead of 0,1,2
098: if (intQ.intValue() > 1) {
099: prefix = (j < 9) ? "0" : "";
100: newID = newID + "-" + prefix + (j + 1);
101: }
102: newAssets.add(makeNewAsset(ldm, prototype, newID,
103: newSchedule, fields));
104: }
105:
106: return newAssets;
107: }
108: // Should not get here should throw a runtimeexception
109: return null;
110: }
111:
112: /**
113: * Make a new asset of the specified prototype and bumper #.
114: * Also gives it a default schedule.
115: *
116: * @param ldm place to get new instances
117: * @param prototype what kind of asset is it
118: * @param id unique identifier within kind
119: * @param newSchedule initial availability
120: * @param fields A List of "Node", extracted from fields,
121: * probably property groups. Set to null to
122: * not set any additional fields.
123: * @return the new asset!
124: */
125: protected Asset makeNewAsset(LDMServesPlugin ldm, String prototype,
126: String id, Schedule newSchedule, List fields) {
127: // Create the instance
128: Asset newAsset = assetHelper.createInstance(ldm, prototype, id);
129:
130: // Schedule copySchedule = ldm.getFactory().newSimpleSchedule(new Date(newSchedule.getStartTime()),
131: // new Date(newSchedule.getEndTime()));
132: // Set the Schedule
133: ((NewRoleSchedule) newAsset.getRoleSchedule())
134: .setAvailableSchedule(newSchedule);
135:
136: if (null != fields) {
137: for (Iterator it = fields.iterator(); it.hasNext();) {
138: Node n = (Node) it.next();
139: // FieldParser seems to handle overwriting propertygroupps
140: // just fine, though it may merit further investigation.
141: fieldParser.setField(ldm, n, newAsset);
142: }
143: }
144:
145: //logger.debug ("Making new asset " + newAsset + " with UID " +
146: //newAsset.getUID());
147: return newAsset;
148: }
149:
150: protected ScheduleParser scheduleParser;
151: protected FieldParser fieldParser;
152: protected UTILAsset assetHelper;
153: }
|