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.mlm.plugin.perturbation;
028:
029: import java.io.File;
030: import java.util.Vector;
031:
032: import org.cougaar.core.blackboard.Subscriber;
033: import org.cougaar.util.ConfigFinder;
034: import org.w3c.dom.Document;
035: import org.w3c.dom.Element;
036: import org.w3c.dom.Node;
037: import org.w3c.dom.NodeList;
038:
039: /**
040: * The PerturbationReader class reads the perturbation
041: * data ( which is in XML format ) and parses that data
042: * using an XML Parser to create the specified
043: * Perturbations.
044: */
045: public class PerturbationReader {
046: private String xmlfilename_; //default filename
047: private File XMLFile;
048: private Document doc;
049: private Vector perturbations_;
050: private Subscriber assetSubscriber_;
051: private PerturbationPlugin pertPlugin_;
052:
053: /**
054: * @param subscriber Asset Subscriber
055: */
056:
057: public PerturbationReader(String fileName, Subscriber subscriber,
058: PerturbationPlugin pertPlugin) {
059: setFileName(fileName);
060: setSubscriber(subscriber);
061: setPerturbationPlugin(pertPlugin);
062: createPerturbations();
063: readPerturbationData();
064: }
065:
066: /**
067: * Sets the name of the XML file to be read.
068: * @param String XML File Name.
069: */
070: private void setFileName(String filename) {
071: this .xmlfilename_ = filename;
072: }
073:
074: /**
075: * Sets the PerturbationPlugin.
076: * @param PerturbationPlugin The calling plugin.
077: */
078: private void setPerturbationPlugin(PerturbationPlugin pertPlugin_) {
079: this .pertPlugin_ = pertPlugin_;
080: }
081:
082: /**
083: * Sets the asset subscriber.
084: * @param obj Asset Subscriber
085: */
086: private void setSubscriber(Subscriber obj) {
087: this .assetSubscriber_ = obj;
088: }
089:
090: /**
091: * Returns a refernce to the asset subscriber.
092: * @returns Returns the asset Subscriber.
093: */
094: private Subscriber getSubscriber() {
095: return this .assetSubscriber_;
096: }
097:
098: /**
099: * Reads the perturbation data from the XML file.
100: */
101: public void readPerturbationData() {
102: try {
103: doc = ConfigFinder.getInstance().parseXMLConfigFile(
104: xmlfilename_);
105: parseFileData(doc);
106: } catch (Exception e) {
107: e.printStackTrace();
108: }
109: }
110:
111: /**
112: * Parses the perturbation data read in from the XML file.
113: * @param node XML Node
114: */
115: protected void parseFileData(Node node) {
116: if (node != null) {
117: int type = node.getNodeType();
118: Node nextTodo = null;
119: if (type == Node.DOCUMENT_NODE) {
120: node = ((Document) node).getDocumentElement();
121: } else {
122: System.err.println("\nErrror: not DOCUMENT NODE\n");
123: }
124: NodeList theNodes = ((Element) node)
125: .getElementsByTagName("perturbation");
126: if (theNodes != null) {
127: int len = theNodes.getLength();
128: for (int i = 0; i < len; i++) {
129: perturbations_.addElement(new PerturbationNode(
130: theNodes.item(i), getSubscriber(),
131: pertPlugin_));
132: }
133: }
134: }
135: }
136:
137: /**
138: * Creates the perturbations.
139: */
140: protected void createPerturbations() {
141: Vector perturbations;
142:
143: perturbations = new Vector();
144:
145: setPerturbations(perturbations);
146: }
147:
148: /**
149: * Sets the perturbations.
150: * @param pertDataIn Vector of perturbations.
151: */
152: protected void setPerturbations(Vector pertDataIn) {
153: this .perturbations_ = pertDataIn;
154: }
155:
156: /**
157: * Returns the perturbations.
158: * @return Returns the Perturbations.
159: */
160: public Vector getPerturbations() {
161: return perturbations_;
162: }
163: }
|