001: /*
002: * <copyright>
003: *
004: * Copyright 2001-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.planning.servlet.data.hierarchy;
027:
028: import java.io.IOException;
029: import java.io.Serializable;
030: import java.util.ArrayList;
031: import java.util.Collections;
032: import java.util.List;
033:
034: import org.cougaar.planning.servlet.data.xml.DeXMLable;
035: import org.cougaar.planning.servlet.data.xml.UnexpectedXMLException;
036: import org.cougaar.planning.servlet.data.xml.XMLWriter;
037: import org.cougaar.planning.servlet.data.xml.XMLable;
038: import org.xml.sax.Attributes;
039:
040: /**
041: * Represents the data leaving the Hierarchy PSP
042: *
043: * @since 1/24/01
044: **/
045: public class HierarchyData implements XMLable, DeXMLable, Serializable {
046:
047: //Variables:
048: ////////////
049:
050: public static final String NAME_TAG = "Hierarchy";
051:
052: public static final String ROOT_ORGID_ATTR = "RootID";
053:
054: protected String rootOrgID;
055: protected List organizations;
056:
057: //Constructors:
058: ///////////////
059:
060: public HierarchyData() {
061: organizations = new ArrayList();
062: }
063:
064: //Members:
065: //////////
066:
067: public String getRootOrgID() {
068: return rootOrgID;
069: }
070:
071: public void setRootOrgID(String id) {
072: rootOrgID = id;
073: }
074:
075: public int numOrgs() {
076: return organizations.size();
077: }
078:
079: public void sortOrgs() {
080: Collections.sort(organizations);
081: }
082:
083: public Organization getOrgDataAt(int i) {
084: return (Organization) organizations.get(i);
085: }
086:
087: public void addOrgData(Organization od) {
088: if (!organizations.contains(od))
089: organizations.add(od);
090: }
091:
092: //XMLable members:
093: //----------------
094:
095: /**
096: * Write this class out to the Writer in XML format
097: * @param w output Writer
098: **/
099: public void toXML(XMLWriter w) throws IOException {
100: w.optagln(NAME_TAG, ROOT_ORGID_ATTR, rootOrgID);
101: for (int i = 0; i < numOrgs(); i++)
102: getOrgDataAt(i).toXML(w);
103: w.cltagln(NAME_TAG);
104: }
105:
106: //DeXMLable members:
107: //------------------
108:
109: /**
110: * Report a startElement that pertains to THIS object, not any
111: * sub objects. Call also provides the elements Attributes and data.
112: * Note, that unlike in a SAX parser, data is guaranteed to contain
113: * ALL of this tag's data, not just a 'chunk' of it.
114: * @param name startElement tag
115: * @param attr attributes for this tag
116: * @param data data for this tag
117: **/
118: public void openTag(String name, Attributes attr, String data)
119: throws UnexpectedXMLException {
120: if (name.equals(NAME_TAG)) {
121: rootOrgID = attr.getValue(ROOT_ORGID_ATTR);
122: } else
123: throw new UnexpectedXMLException("Unexpected tag: " + name);
124: }
125:
126: /**
127: * Report an endElement.
128: * @param name endElement tag
129: * @return true iff the object is DONE being DeXMLized
130: **/
131: public boolean closeTag(String name) throws UnexpectedXMLException {
132:
133: return name.equals(NAME_TAG);
134: }
135:
136: /**
137: * This function will be called whenever a subobject has
138: * completed de-XMLizing and needs to be encorporated into
139: * this object.
140: * @param name the startElement tag that caused this subobject
141: * to be created
142: * @param obj the object itself
143: **/
144: public void completeSubObject(String name, DeXMLable obj)
145: throws UnexpectedXMLException {
146: if (!(obj instanceof Organization))
147: throw new UnexpectedXMLException("Unexpected object: "
148: + obj);
149: addOrgData((Organization) obj);
150: }
151:
152: //Inner Classes:
153:
154: /**
155: * Set the serialVersionUID to keep the object serializer from seeing
156: * xerces (org.xml.sax.Attributes).
157: */
158: private static final long serialVersionUID = 1389218378192871398L;
159: }
|