001: /*
002: * <copyright>
003: *
004: * Copyright 2003-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: package org.cougaar.lib.vishnu.client;
027:
028: import java.io.File;
029: import java.io.FileNotFoundException;
030: import java.io.FileOutputStream;
031: import java.io.InputStream;
032: import java.io.IOException;
033: import java.io.StringReader;
034:
035: import java.text.ParseException;
036: import java.text.SimpleDateFormat;
037:
038: import java.util.Collection;
039: import java.util.Date;
040: import java.util.ArrayList;
041: import java.util.List;
042: import java.util.Map;
043: import java.util.Vector;
044:
045: import org.apache.xerces.dom.DocumentImpl;
046: import org.apache.xerces.parsers.DOMParser;
047: import org.apache.xerces.parsers.SAXParser;
048:
049: import org.cougaar.planning.ldm.asset.Asset;
050: import org.cougaar.planning.ldm.plan.Task;
051: import org.cougaar.lib.param.ParamMap;
052: import org.cougaar.util.StringKey;
053: import org.cougaar.util.log.Logger;
054:
055: import org.w3c.dom.Document;
056:
057: import org.xml.sax.helpers.DefaultHandler;
058: import org.xml.sax.Attributes;
059: import org.xml.sax.InputSource;
060: import org.xml.sax.SAXException;
061:
062: /**
063: * Result handler used by VishnuAggregatorPlugin <p>
064: *
065: * Interacts with ResultListener, which the plugin implements
066: *
067: * @see ResultListener
068: */
069: public class AggregateXMLResultHandler extends XMLResultHandler {
070: /** called by VishnuAggregatorPlugin#createXMLResultHandler */
071: public AggregateXMLResultHandler(ModeListener parent,
072: VishnuComm comm, XMLProcessor xmlProcessor,
073: VishnuDomUtil domUtil, VishnuConfig config,
074: ParamMap myParamTable, Logger logger) {
075: super (parent, comm, xmlProcessor, domUtil, config,
076: myParamTable, logger);
077: }
078:
079: /**
080: * Given the XML that indicates assignments, parse it <p>
081: *
082: * Asks the ResultListener to lookup the keys returned in the xml to figure out <br>
083: * which task was assigned to which asset. These were set in processTasks using <br>
084: * setUIDToObjectMap.
085: *
086: * @param name the tag name
087: * @param atts the tag's attributes
088: * @see org.cougaar.lib.vishnu.client.VishnuPlugin#processTasks
089: * @see org.cougaar.lib.vishnu.client.VishnuPlugin#setUIDToObjectMap
090: */
091: protected void parseStartElement(String name, Attributes atts) {
092: try {
093: if (logger.isDebugEnabled())
094: logger.debug(getName() + ".parseStartElement got "
095: + name);
096:
097: if (name.equals("MULTITASK")) {
098: if (logger.isInfoEnabled() || debugParseAnswer) {
099: logger.info(getName()
100: + ".parseStartElement -\nAssignment: "
101: + " resource = "
102: + atts.getValue("resource") + " start = "
103: + atts.getValue("start") + " end = "
104: + atts.getValue("end") + " setup = "
105: + atts.getValue("setup") + " wrapup = "
106: + atts.getValue("wrapup"));
107: }
108: String resourceUID = atts.getValue("resource");
109: String startTime = atts.getValue("start");
110: String endTime = atts.getValue("end");
111: String setupTime = atts.getValue("setup");
112: String wrapupTime = atts.getValue("wrapup");
113: start = format.parse(startTime);
114: end = format.parse(endTime);
115: setup = format.parse(setupTime);
116: wrapup = format.parse(wrapupTime);
117:
118: assignedAsset = resultListener
119: .getAssetForKey(new StringKey(resourceUID));
120: assetWasUsedBefore = false;
121: if (assignedAsset == null)
122: logger
123: .debug(getName()
124: + ".parseStartElement - no asset found with "
125: + resourceUID);
126: } else if (name.equals("TASK")) {
127: if (logger.isInfoEnabled() || debugParseAnswer) {
128: logger.info(getName()
129: + ".parseStartElement -\nTask: "
130: + " task = " + atts.getValue("task"));
131: }
132: if (atts.getValue("task") == null)
133: logger
134: .error(getName()
135: + ".parseStartElement - ERROR, no task attribute on TASK element? "
136: + "Element attributes were " + atts);
137:
138: String taskUID = atts.getValue("task");
139:
140: StringKey taskKey = new StringKey(taskUID);
141: Task handledTask = resultListener
142: .getTaskForKey(taskKey);
143: if (handledTask == null) {
144: // Already handled before
145: assetWasUsedBefore = true;
146: return;
147: // debug (getName () + ".parseStartElement - no task found with " + taskUID + " uid.");
148: // debug ("\tmap keys were " + myTaskUIDtoObject.keySet());
149: } else {
150: alpTasks.add(handledTask);
151: }
152:
153: // this is absolutely critical, otherwise VishnuPlugin will make a failed disposition
154: resultListener.removeTask(taskKey);
155: if (debugParseAnswer)
156: logger.debug(getName()
157: + ".parseStartElement - removing task key "
158: + taskKey);
159: } else if (debugParseAnswer) {
160: logger.debug(getName()
161: + ".parseStartElement - ignoring tag " + name);
162: }
163: } catch (ParseException pe) {
164: logger
165: .error(
166: getName()
167: + ".parseStartElement - start or end time is in bad format "
168: + pe + "\ndates were : "
169: + " start = "
170: + atts.getValue("start")
171: + " end = " + atts.getValue("end")
172: + " setup = "
173: + atts.getValue("setup")
174: + " wrapup = "
175: + atts.getValue("wrapup"), pe);
176: } catch (Exception npe) {
177: logger.error(getName()
178: + ".parseStartElement - got bogus assignment "
179: + npe.getMessage(), npe);
180: }
181: }
182:
183: /** Calls resultListener (= the plugin) handleMultiAssignment when sees a MULTITASK end tag */
184: protected void parseEndElement(String name) {
185: try {
186: if (name.equals("MULTITASK")) {
187: if (debugParseAnswer) {
188: logger
189: .debug(getName()
190: + ".parseEndElement - got ending MULTITASK.");
191: }
192: if (!alpTasks.isEmpty()) {
193: resultListener.handleMultiAssignment(alpTasks,
194: assignedAsset, start, end, setup, wrapup,
195: assetWasUsedBefore);
196: assetWasUsedBefore = false;
197: alpTasks.clear();
198: }
199: } else if (name.equals("TASK")) {
200: }
201: // else if (debugParseAnswer) {
202: // debug (getName () + ".parseEndElement - ignoring tag " + name);
203: // }
204: } catch (Exception npe) {
205: logger.error(getName()
206: + ".parseEndElement - got bogus assignment "
207: + npe.getMessage(), npe);
208: }
209: }
210:
211: protected String getName() {
212: return "AggregateXMLResultHandler";
213: }
214:
215: protected boolean assetWasUsedBefore;
216: protected boolean debugParseAnswer = false;
217:
218: private final SimpleDateFormat format = new SimpleDateFormat(
219: "yyyy-MM-dd HH:mm:ss");
220: }
|