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: package org.cougaar.mlm.plugin.ldm;
027:
028: import java.io.File;
029: import java.text.ParseException;
030: import java.text.SimpleDateFormat;
031: import java.util.ArrayList;
032: import java.util.Date;
033: import java.util.HashMap;
034:
035: import org.cougaar.core.mts.MessageAddress;
036: import org.cougaar.core.service.UIDServer;
037: import org.cougaar.core.util.UID;
038: import org.cougaar.glm.ldm.oplan.DFSP;
039: import org.cougaar.glm.ldm.oplan.ForcePackage;
040: import org.cougaar.glm.ldm.oplan.Oplan;
041: import org.cougaar.glm.ldm.oplan.OplanContributor;
042: import org.cougaar.glm.ldm.oplan.OplanFactory;
043: import org.cougaar.glm.ldm.oplan.OrgActivity;
044: import org.cougaar.glm.ldm.oplan.OrgActivityImpl;
045: import org.cougaar.glm.ldm.oplan.OrgRelation;
046: import org.cougaar.glm.ldm.oplan.POD;
047: import org.cougaar.glm.ldm.oplan.TimeSpan;
048: import org.cougaar.glm.ldm.plan.GeolocLocationImpl;
049: import org.cougaar.planning.ldm.ClusterServesPlugin;
050: import org.cougaar.planning.ldm.PlanningFactory;
051: import org.cougaar.planning.ldm.measure.Latitude;
052: import org.cougaar.planning.ldm.measure.Longitude;
053: import org.cougaar.planning.ldm.policy.Policy;
054: import org.w3c.dom.Document;
055: import org.w3c.dom.Element;
056: import org.w3c.dom.NamedNodeMap;
057: import org.w3c.dom.Node;
058: import org.w3c.dom.NodeList;
059:
060: /**
061: This class is used to parse the given oplan xml file and instantiates oplan objects
062: accordingly. The oplan xml file is retrieved from the Oplan object. Once retrieved
063: The OplanFileReader parses the Nodes and attributes of the xml file.
064:
065: */
066:
067: public class OplanFileReader {
068:
069: private String xmlfilename;
070: private File XMLFile;
071: private Document doc;
072:
073: // Reference back to the Oplan object
074: private Oplan thePlan_;
075: private ArrayList theOrgActivities_ = new ArrayList();
076: private ArrayList theOrgRelations_ = new ArrayList();
077: private ArrayList theForcePackages_ = new ArrayList();
078: private ArrayList thePolicies_ = new ArrayList();
079:
080: private static final String OPLANID = "oplanID";
081: private static final String OPERATIONNAME = "operationName";
082: private static final String PRIORITY = "priority";
083: private static final String CDAY = "cDay";
084: private static final String ENDDAY = "endDay";
085:
086: // TimeSpan attributesth
087: private static final String TIMESPAN = "timespan";
088: private static final String TS_STARTTIME = "startTime";
089: private static final String TS_THRUTIME = "thruTime";
090:
091: // Policy attributes
092: private static final String OPLANPOLICY = "Policies";
093: private static final String POLICY = "Policy";
094: private static final String KEYVALUE = "keyvalue";
095: private static final String POLICYNAME = "policyname";
096:
097: // TheaterInfo
098: //private static final String THEATERINFO = "theaterinfo";
099: private static final String THEATERID = "theaterId";
100: private static final String TERRAINTYPE = "terrainType";
101: private static final String SEASON = "season";
102: private static final String ENEMYFORCETYPE = "enemyForceType";
103: private static final String HOSTNATIONPOL = "hostNationPOLSupport";
104: private static final String HOSTNATIONPOLCAPABILITY = "hostNationPOLCapability";
105: private static final String HOSTNATIONWATERSUPPORT = "hostNationWaterSupport";
106: private static final String HOSTNATIONWATERCAPABILITY = "hostNationWaterCapability";
107:
108: // DFSP
109: private static final String DFSP = "dfsp";
110: private static final String DFSPNAME = "name";
111:
112: private static final String GEOLOC = "geoloc";
113: private static final String GEOLOCNAME = "name";
114: private static final String GEOLOCCODE = "code";
115: private static final String GEOLOCLAT = "lat";
116: private static final String GEOLOCLONG = "long";
117:
118: // TransportInfo
119: //private static final String TRANSPORTINFO = "transportinfo";
120:
121: // POD
122: private static final String POD = "pod";
123: private static final String PODTYPE = "type";
124: private static final String PODVALUE = "value";
125:
126: // Organization
127: private static final String ORGANIZATION = "organization";
128: private static final String ORGID = "OrgId";
129: private static final String ORGDEPLOYREQ = "orgdeploymentrequirements";
130: private static final String ORGACTIVITY = "orgactivity";
131: private static final String ORGRELATION = "orgrelation";
132:
133: // OrgActivity
134: private static final String ACTIVITYTYPE = "ActivityType";
135: private static final String ACTIVITYNAME = "ActivityName";
136: private static final String OPTEMPO = "OpTempo";
137: private static final String KEY = "key";
138: private static final String VALUE = "value";
139: // private static final String TAALOC = "TAALoc";
140: // private static final String TAAEAD = "TAAEAD";
141: // private static final String TAALAD = "TAALAD";
142:
143: // ForcePackage
144: private static final String FORCEPACKAGE = "forcepackage";
145: private static final String FORCEPACKAGEID = "ForcePackageId";
146: private static final String ASSIGNEDROLE = "AssignedRole";
147:
148: // OrgRelation
149: private static final String RELATIONTYPE = "RelationType";
150: private static final String OTHERORGID = "OtherOrgId";
151:
152: private String oplanID;
153: private UID oplanUID;
154: private String orgID;
155: private PlanningFactory ldmF;
156: private UIDServer uids;
157: private MessageAddress cid;
158: private ClusterServesPlugin theCluster;
159:
160: private OplanFileReader() {
161: } //OplanFileReader
162:
163: public OplanFileReader(String xmlfilename, PlanningFactory ldmF,
164: ClusterServesPlugin theCluster) {
165: this .xmlfilename = xmlfilename;
166: this .ldmF = ldmF;
167: this .theCluster = theCluster;
168: this .uids = theCluster.getUIDServer();
169: this .cid = theCluster.getMessageAddress();
170: //getParams();
171:
172: }//OplanFileReader
173:
174: public Oplan readOplan() {
175: theForcePackages_.clear();
176: theOrgActivities_.clear();
177: theOrgRelations_.clear();
178: thePolicies_.clear();
179:
180: thePlan_ = new Oplan(xmlfilename);
181: uids.registerUniqueObject(thePlan_);
182: thePlan_.setOwner(cid);
183: oplanID = thePlan_.getOplanId();
184: oplanUID = thePlan_.getUID();
185: readOplanData();
186: return thePlan_;
187: }
188:
189: public ArrayList getForcePackages(Oplan oplan) {
190: if ((thePlan_ != null) && (oplan.getUID() == thePlan_.getUID())) {
191: return theForcePackages_;
192: } else {
193: throw new IllegalArgumentException("Oplan - " + oplan
194: + " does not match the current oplan - " + thePlan_);
195: }
196: }
197:
198: public ArrayList getOrgActivities(Oplan oplan) {
199: if ((thePlan_ != null) && (oplan.getUID() == thePlan_.getUID())) {
200: return theOrgActivities_;
201: } else {
202: throw new IllegalArgumentException("Oplan - " + oplan
203: + " does not match the current oplan - " + thePlan_);
204: }
205: }
206:
207: public ArrayList getOrgRelations(Oplan oplan) {
208: if ((thePlan_ != null) && (oplan.getUID() == thePlan_.getUID())) {
209: return theOrgRelations_;
210: } else {
211: throw new IllegalArgumentException("Oplan - " + oplan
212: + " does not match the current oplan - " + thePlan_);
213: }
214: }
215:
216: public ArrayList getPolicies(Oplan oplan) {
217: if ((thePlan_ != null) && (oplan.getUID() == thePlan_.getUID())) {
218: return thePolicies_;
219: } else {
220: throw new IllegalArgumentException("Oplan - " + oplan
221: + " does not match the current oplan - " + thePlan_);
222: }
223: }
224:
225: /**
226: get the XMLFileName
227: */
228: private void getParams() {
229: /* Need to add back!
230: Vector pv = getParameters();
231: if ( pv == null ) {
232: throw new RuntimeException( "Oplan requires a parameter" );
233: } else {
234: try {
235: Enumeration ps = pv.elements();
236: String p = (String) ps.nextElement();
237: globalParameters.put( "XMLFile", p );
238: xmlfilename = p;
239: } catch( Exception e ) {
240: e.printStackTrace();
241: }
242: }
243:
244: xmlfilename= "oplan.xml";
245: */
246:
247: //xmlfilename = thePlan_.getXMLFileName();
248: }
249:
250: public void readOplanData() {
251: try {
252: doc = theCluster.getConfigFinder().parseXMLConfigFile(
253: xmlfilename);
254: parseFileData(doc);
255: } catch (Exception e) {
256: e.printStackTrace();
257: }// trycatch block
258: }// readOplanData
259:
260: protected void parseFileData(Node node) {
261: if (node != null) {
262: int type = node.getNodeType();
263:
264: if (type == Node.DOCUMENT_NODE)
265: node = ((Document) node).getDocumentElement();
266: else
267: System.err.println("\nErrror: not DOCUMENT NODE\n");
268:
269: setOplanAttrs(node);
270:
271: // Get the remaining child notes.
272: NodeList theNodes = ((Element) node).getChildNodes();
273: int orgNum = 0;
274: int theaterNum = 0;
275:
276: if (theNodes != null) {
277: // Parse each of the subNodes.
278: for (int i = 0; i < theNodes.getLength(); i++) {
279: // From DTD
280: // pod,dfsp,cinc+,timespan?,organization+,forcepackage*,OplanPolicy*
281:
282: String nodeName = theNodes.item(i).getNodeName();
283:
284: if (nodeName.equals(TIMESPAN))
285: setTimeSpan(thePlan_, theNodes.item(i));
286:
287: if (nodeName.equals(ORGANIZATION)) {
288: addOrganization(theNodes.item(i));
289: orgNum++;
290: }
291: if (nodeName.equals(FORCEPACKAGE))
292: addForcePackage(theNodes.item(i));
293:
294: if (nodeName.equals(OPLANPOLICY))
295: setPolicy(theNodes.item(i));
296:
297: if (nodeName.equals(POD))
298: addPOD(theNodes.item(i));
299:
300: if (nodeName.equals(DFSP))
301: addDFSP(theNodes.item(i));
302:
303: }// for loop
304: }// if theNodes != null
305:
306: } // node != null
307: else
308: System.err
309: .println("OplanFileReader:parseFileData Unable to parse "
310: + xmlfilename + " please check the file.");
311: }// parseFileData
312:
313: private static SimpleDateFormat datefmt = new SimpleDateFormat(
314: "MM/dd/yyyy");
315:
316: private void setOplanAttrs(Node data) {
317: NamedNodeMap attributes = data.getAttributes();
318: Date cDaydate = new Date();
319: Date endDaydate = null;
320:
321: // Oplan Attributes
322: /* cDay
323: endDay
324: priority
325: operationName
326: oplanID */
327:
328: if (attributes.getNamedItem(OPLANID) != null) {
329: oplanID = attributes.getNamedItem(OPLANID).getNodeValue();
330: thePlan_.setOplanId(oplanID);
331: }
332: if (attributes.getNamedItem(OPERATIONNAME) != null)
333: thePlan_.setOperationName(attributes.getNamedItem(
334: OPERATIONNAME).getNodeValue());
335: if (attributes.getNamedItem(PRIORITY) != null)
336: thePlan_.setPriority(attributes.getNamedItem(PRIORITY)
337: .getNodeValue());
338: if (attributes.getNamedItem(CDAY) != null) {
339: try {
340: SimpleDateFormat formatter = datefmt;
341: cDaydate = formatter.parse(attributes
342: .getNamedItem(CDAY).getNodeValue());
343: } catch (ParseException e) {
344: System.out.println(e.getMessage());
345: }// try catch
346: thePlan_.setCday(cDaydate);
347: }// if CDAY
348: if (attributes.getNamedItem(ENDDAY) != null) {
349: try {
350: SimpleDateFormat formatter = datefmt;
351: endDaydate = formatter.parse(attributes.getNamedItem(
352: ENDDAY).getNodeValue());
353: } catch (ParseException e) {
354: System.out.println(e.getMessage());
355: }// try catch
356: thePlan_.setEndDay(endDaydate);
357: }// if ENDDAY
358: if (attributes.getNamedItem(THEATERID) != null)
359: thePlan_.setTheaterID(attributes.getNamedItem(THEATERID)
360: .getNodeValue());
361: if (attributes.getNamedItem(TERRAINTYPE) != null)
362: thePlan_.setTerrainType(attributes
363: .getNamedItem(TERRAINTYPE).getNodeValue());
364: if (attributes.getNamedItem(SEASON) != null)
365: thePlan_.setSeason(attributes.getNamedItem(SEASON)
366: .getNodeValue());
367: if (attributes.getNamedItem(ENEMYFORCETYPE) != null)
368: thePlan_.setEnemyForceType(attributes.getNamedItem(
369: ENEMYFORCETYPE).getNodeValue());
370: if (attributes.getNamedItem(HOSTNATIONPOL) != null)
371: thePlan_.setHNSPOL(new Boolean(attributes.getNamedItem(
372: HOSTNATIONPOL).getNodeValue()).booleanValue());
373: if (attributes.getNamedItem(HOSTNATIONPOLCAPABILITY) != null)
374: thePlan_.setHNSPOLCapacity(attributes.getNamedItem(
375: HOSTNATIONPOLCAPABILITY).getNodeValue());
376: if (attributes.getNamedItem(HOSTNATIONWATERSUPPORT) != null)
377: thePlan_.setHNSForWater(new Boolean(attributes
378: .getNamedItem(HOSTNATIONWATERSUPPORT)
379: .getNodeValue()).booleanValue());
380: if (attributes.getNamedItem(HOSTNATIONWATERCAPABILITY) != null)
381: thePlan_.setHNSWaterCapability(attributes.getNamedItem(
382: HOSTNATIONWATERCAPABILITY).getNodeValue());
383: }// setOplanAttrs
384:
385: public void addPOD(Node data) {
386: POD pod = new POD();
387: NamedNodeMap attributes = data.getAttributes();
388: if (attributes.getNamedItem(PODTYPE) != null)
389: pod
390: .setType(attributes.getNamedItem(PODTYPE)
391: .getNodeValue());
392: if (attributes.getNamedItem(PODVALUE) != null)
393: pod.setValue(attributes.getNamedItem(PODVALUE)
394: .getNodeValue());
395:
396: thePlan_.addPOD(pod);
397: }// addPOD
398:
399: public void addOrganization(Node data) {
400: // Organization org = new Organization(oplanID);
401: int orgActivityNum = 0;
402: int orgDeployNum = 0;
403: NamedNodeMap attributes = data.getAttributes();
404:
405: // DTD requirements
406: // OrgId
407:
408: if (attributes.getNamedItem(ORGID) != null) {
409: orgID = attributes.getNamedItem(ORGID).getNodeValue();
410: // org.setOrgId(orgID);
411: }
412: // get the subNodes
413: NodeList theNodes = ((Element) data).getChildNodes();
414: if (theNodes != null) {
415: int len = theNodes.getLength();
416: for (int i = 0; i < len; i++) {
417: // <!ELEMENT organization (orgactivity+,orgrelation*)>
418: String nodeName = theNodes.item(i).getNodeName();
419:
420: if (nodeName.equals(ORGACTIVITY)) {
421: setOrgActivity(theNodes.item(i), theNodes.item(i)
422: .getAttributes());
423: orgActivityNum++;
424: }// if orgactivity
425: if (nodeName.equals(ORGRELATION)) {
426: addOrgRelation(null, theNodes.item(i)); // more than one
427: }
428:
429: }// for loop
430: }// if statement
431:
432: if (orgDeployNum > 1)
433: System.err
434: .println("OplanFileReader:addOrganization Only one deployment requirement can be specified per organization.");
435:
436: if (orgActivityNum < 1)
437: System.out
438: .println("OplanFileReader:addOrganization At least one org activity must be specified per organization.");
439:
440: }// addOrganization
441:
442: public void addForcePackage(Node data) {
443: ForcePackage fp = new ForcePackage(oplanUID);
444: uids.registerUniqueObject(fp);
445: fp.setOwner(cid);
446: NamedNodeMap attributes = data.getAttributes();
447: // DTD Requirements
448: // forcepackage.ForcePackageId
449:
450: if (attributes.getNamedItem(FORCEPACKAGEID) != null)
451: fp.setForcePackageId(attributes
452: .getNamedItem(FORCEPACKAGEID).getNodeValue());
453:
454: NodeList theNodes = ((Element) data).getChildNodes();
455:
456: // More DTD Requirements
457: /* <!ELEMENT forcepackage (timespan?,orgrelation+)> */
458: if (theNodes != null) {
459: int len = theNodes.getLength();
460: for (int i = 0; i < len; i++) {
461: String nodeName = theNodes.item(i).getNodeName();
462: if (nodeName.equals(TIMESPAN)) {
463: //setTimeSpan(fp, theNodes.item(i).getAttributes());
464: setTimeSpan(fp, theNodes.item(i));
465: }
466: if (nodeName.equals(ORGRELATION))
467: addOrgRelation(fp, theNodes.item(i)); // more than one
468: }// for loop
469: }// if statement
470:
471: theForcePackages_.add(fp);
472:
473: }// setForcePackages
474:
475: public void setOrgActivity(Node data, NamedNodeMap attributes) {
476: OrgActivityImpl oa = OplanFactory.newOrgActivity(orgID,
477: oplanUID);
478: uids.registerUniqueObject(oa);
479: oa.setOwner(cid);
480:
481: if (attributes.getNamedItem(ACTIVITYTYPE) != null)
482: oa.setActivityType(attributes.getNamedItem(ACTIVITYTYPE)
483: .getNodeValue());
484: if (attributes.getNamedItem(ACTIVITYNAME) != null)
485: oa.setActivityName(attributes.getNamedItem(ACTIVITYNAME)
486: .getNodeValue());
487: if (attributes.getNamedItem(OPTEMPO) != null)
488: oa.setOpTempo(attributes.getNamedItem(OPTEMPO)
489: .getNodeValue());
490:
491: addGeoLocs(oa, data);
492:
493: // DTD Requirements (keyvalue*,timespan?)>
494: NodeList subNodes = ((Element) data).getChildNodes();
495: if (subNodes != null) {
496: int len = subNodes.getLength();
497: for (int i = 0; i < len; i++) {
498: String nodeName = subNodes.item(i).getNodeName();
499: // Create a has table for OrgActivity stuff
500: if (nodeName.equals(KEYVALUE)) {
501: NamedNodeMap subattributes = subNodes.item(i)
502: .getAttributes();
503: if ((subattributes.getNamedItem(KEY) != null)
504: && (subattributes.getNamedItem(VALUE) != null))
505: oa.addActivityItem(subattributes.getNamedItem(
506: KEY).getNodeValue(), subattributes
507: .getNamedItem(VALUE).getNodeValue());
508: }// if statement
509: // if Timespan
510: if (nodeName.equals(TIMESPAN))
511: setTimeSpan(oa, subNodes.item(i));
512: }// for loop
513: }// if statement
514:
515: theOrgActivities_.add(oa);
516: }//setOrgActivity
517:
518: public void addOrgRelation(Object obj, Node node) {
519: OrgRelation or;
520:
521: if (obj instanceof ForcePackage) {
522: or = new OrgRelation("", oplanUID);
523: or.setForcePackageId(((ForcePackage) obj)
524: .getForcePackageId());
525: } else {
526: or = new OrgRelation(orgID, oplanUID);
527: or.setForcePackageId("");
528: }
529: uids.registerUniqueObject(or);
530: or.setOwner(cid);
531:
532: NamedNodeMap attributes = node.getAttributes();
533:
534: /* <!ELEMENT orgrelation (timespan?)>
535: <!--
536: orgrelation.OtherOrgId: E
537: orgrelation.AssignedRole: N S
538: orgrelation.RelationType: E
539: --> */
540:
541: if (attributes.getNamedItem(ASSIGNEDROLE) != null)
542: or.setAssignedRole(attributes.getNamedItem(ASSIGNEDROLE)
543: .getNodeValue());
544: if (attributes.getNamedItem(RELATIONTYPE) != null)
545: or.setRelationType(attributes.getNamedItem(RELATIONTYPE)
546: .getNodeValue());
547: if (attributes.getNamedItem(OTHERORGID) != null)
548: or.setOtherOrgId(attributes.getNamedItem(OTHERORGID)
549: .getNodeValue());
550:
551: // Set a unique Identifier
552: //UID uid = ldmF.getNextUID();
553: //or.setOrgRelationId(uid);
554: uids.registerUniqueObject(or);
555: or.setOwner(cid);
556:
557: NodeList subNodes = ((Element) node).getChildNodes();
558: if (subNodes != null) {
559: int len = subNodes.getLength();
560: for (int i = 0; i < len; i++) {
561: String nodeName = subNodes.item(i).getNodeName();
562: if (nodeName.equals(TIMESPAN)) {
563: // setTimeSpan(oa, subNodes.item(i).getAttributes());
564: setTimeSpan(or, subNodes.item(i));
565: }// if statement
566: } // for loop
567: }// if there are subNodes
568:
569: // if (obj instanceof ForcePackage)
570: // ((ForcePackage)obj).addOrgRelation(or);
571:
572: theOrgRelations_.add(or);
573:
574: }//addOrgRelation
575:
576: public void setTimeSpan(Object object, Node theNode) {
577:
578: NamedNodeMap attributes = theNode.getAttributes();
579:
580: int startDelta = 0;
581: int thruDelta = 0;
582:
583: // Parse for the integer value and store it.
584: if (attributes.getNamedItem(TS_STARTTIME) != null) {
585:
586: String startTime = attributes.getNamedItem(TS_STARTTIME)
587: .getNodeValue();
588: int index = startTime.indexOf('+');
589: try {
590: startDelta = (new Integer(startTime
591: .substring(index + 1).trim())).intValue();
592: } catch (NumberFormatException e) {
593: System.err
594: .println("startTime is not in the correct Format");
595: }// catch
596: }// if startTime
597: // Parse for the integer value and store it.
598: if (attributes.getNamedItem(TS_THRUTIME) != null) {
599: String thruTime = attributes.getNamedItem(TS_THRUTIME)
600: .getNodeValue();
601: int index = thruTime.indexOf('+');
602: try {
603: thruDelta = (new Integer(thruTime.substring(index + 1)
604: .trim())).intValue();
605: } catch (NumberFormatException e) {
606: System.err
607: .println("thruTime is not in the correct Format");
608: }// catch
609: }// thruTime
610:
611: TimeSpan ts = new org.cougaar.glm.ldm.oplan.TimeSpan(thePlan_
612: .getCday(), startDelta, thruDelta);
613:
614: if (object instanceof OplanContributor)
615: ((OplanContributor) object).setTimeSpan(ts);
616:
617: }//setTimeSpan
618:
619: public void addDFSP(Node node) {
620: DFSP dfsp = new DFSP();
621: uids.registerUniqueObject(dfsp);
622: dfsp.setOwner(cid);
623:
624: NamedNodeMap attributes = node.getAttributes();
625:
626: if (attributes.getNamedItem(DFSPNAME) != null)
627: dfsp.setName(attributes.getNamedItem(DFSPNAME)
628: .getNodeValue());
629:
630: addGeoLocs(dfsp, node);
631:
632: thePlan_.addDFSP(dfsp);
633:
634: }//setDFSP
635:
636: public void addGeoLocs(Object obj, Node node) {
637: NodeList subNodes = ((Element) node).getChildNodes();
638:
639: if (subNodes != null) {
640: int len = subNodes.getLength();
641: for (int i = 0; i < len; i++) {
642: String nodeName = subNodes.item(i).getNodeName();
643: if (nodeName.equals(GEOLOC)) {
644:
645: setGeoLoc(obj, subNodes.item(i));
646: }//if statement
647: }//for loop
648: }//if
649: }//addGeoLocs
650:
651: private HashMap geolocCache = new HashMap(89);
652:
653: public void setGeoLoc(Object obj, Node node) {
654: Latitude lat = null;
655: Longitude lon = null;
656: String name = null;
657: NamedNodeMap subattributes = node.getAttributes();
658:
659: try {
660:
661: // Create a newGeolocLocation
662: GeolocLocationImpl geoLoc = null;
663:
664: // get the geoLoc Code from the XML
665: Node gcn = subattributes.getNamedItem(GEOLOCCODE);
666: String geoLocCode = null;
667: if (gcn != null
668: && (geoLocCode = gcn.getNodeValue()) != null) {
669: geoLocCode = geoLocCode.intern();
670: geoLoc = (GeolocLocationImpl) geolocCache
671: .get(geoLocCode);
672: }
673:
674: if (geoLoc == null) {
675: if (geoLocCode == null)
676: geoLocCode = new String("");
677:
678: geoLoc = new GeolocLocationImpl();
679: // Set the value in NewGeoLocation accordingly.
680: geoLoc.setGeolocCode(geoLocCode);
681:
682: // Get the GeoLoc Name from the XML
683: String geoLocName;
684: if (subattributes.getNamedItem(GEOLOCNAME) == null)
685: geoLocName = "";
686: else
687: geoLocName = subattributes.getNamedItem(GEOLOCNAME)
688: .getNodeValue();
689:
690: // Set the value in NewGeoLocation accordingly.
691: geoLoc.setName(geoLocName);
692:
693: // get the Latitude and Longitude values.
694: if ((subattributes.getNamedItem(GEOLOCLAT) != null)
695: && (subattributes.getNamedItem(GEOLOCLONG) != null)) {
696: lat = Latitude.newLatitude(subattributes
697: .getNamedItem(GEOLOCLAT).getNodeValue());
698: lon = Longitude.newLongitude(subattributes
699: .getNamedItem(GEOLOCLONG).getNodeValue());
700:
701: geoLoc.setLatitude(lat);
702: geoLoc.setLongitude(lon);
703: }// if the latitude long
704: // cache the instance
705: geolocCache.put(geoLocCode, geoLoc);
706: }
707:
708: if (geoLoc != null) {
709: if (obj instanceof DFSP) {
710: ((DFSP) obj).setGeoLoc(geoLoc);
711: } else if (obj instanceof OrgActivity) {
712: ((OrgActivity) obj).setGeoLoc(geoLoc);
713: } else {
714: Class clazz = (obj == null) ? null : obj.getClass();
715: System.err
716: .println("OplanFileReader.setGeoLoc - Couldn't figure out how to set geoloc of target class "
717: + clazz + " " + obj);
718: }
719: } else {
720: System.err
721: .println("OplanFileReader:setGeoLoc - geoloc not specifed correctly in xml file.");
722: }
723: } catch (Exception e) {
724: System.err.println("OplanFileReader Got exception: " + e);
725: e.printStackTrace();
726: //System.exit(-1);
727: }
728: }
729:
730: public void setPolicy(Node data) {
731: XMLPolicyCreator pc = new XMLPolicyCreator(xmlfilename,
732: theCluster.getConfigFinder(), ldmF);
733: // New functionality to create an OplanPolicy Object
734: // 9-13-99
735:
736: NodeList subNodes = ((Element) data).getChildNodes();
737:
738: if (subNodes != null) {
739: int len = subNodes.getLength();
740:
741: for (int i = 0; i < len; i++) {
742: String nodeName = subNodes.item(i).getNodeName();
743: if (nodeName.equals(POLICY)) {
744: Policy thePolicy = (Policy) pc.getPolicy(subNodes
745: .item(i));
746: if (thePolicy != null) {
747: //String policyName = subNodes.item(i).getAttributes().getNamedItem("name").getNodeValue();
748: thePolicies_.add(thePolicy);
749: }
750:
751: }// if statement
752: }// for loop
753: }// if statement
754:
755: }//setPolicy
756:
757: }// OPlanFileReader
|