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.param;
028:
029: import org.xml.sax.Attributes;
030: import org.xml.sax.SAXException;
031: import org.xml.sax.helpers.DefaultHandler;
032:
033: /**
034: * Class that handles what to do when we encouter a parameter in a
035: * parameter file. This class has a pointer to the parameter table
036: * so that it can add the parameters when it encouters them in
037: * the file.
038: *
039: */
040: class ParamHandler extends DefaultHandler {
041:
042: /**
043: * Constructor
044: * @param ptable the associated parameter table
045: */
046: ParamHandler(ParamTable ptable) {
047: this .paramTable = ptable;
048: }
049:
050: /**
051: * Overriden from base class. This is the function that actually
052: * knows what to do when we encouter a parameter.
053: * @param name the name of the xml element (parameter name)
054: * @param atts the attribute list of the xml element
055: */
056: public void startElement(String uri, String local, String name,
057: Attributes atts) throws SAXException {
058: String attributeName = null;
059: String attributeType = null;
060: String attributeValue = null;
061: Param param = null;
062: int size = atts.getLength();
063:
064: if (name.equals("Parameter")) {
065: for (int i = 0; i < size; i++) {
066: // String attribute = atts.getName(i);
067: String attribute = atts.getLocalName(i);
068:
069: if (attribute.equals("name")) {
070: attributeName = atts.getValue(i);
071: } else if (attribute.equals("type")) {
072: attributeType = atts.getValue(i);
073: } else if (attribute.equals("value")) {
074: attributeValue = atts.getValue(i);
075: }
076: }
077:
078: if (attributeName == null) {
079: throw new ParamRuntimeException(
080: "parameter missing NAME");
081: }
082: if (attributeType == null) {
083: throw new ParamRuntimeException(
084: "parameter missing TYPE");
085: }
086: if (attributeValue == null) {
087: throw new ParamRuntimeException(
088: "parameter missing VALUE");
089: }
090:
091: if (attributeType.equals("boolean")) {
092: Boolean b = new Boolean(attributeValue);
093: param = new BooleanParam(attributeName, b
094: .booleanValue());
095: } else if (attributeType.equals("double")) {
096: Double d = new Double(attributeValue);
097: param = new DoubleParam(attributeName, d.doubleValue());
098: } else if (attributeType.equals("float")) {
099: Float f = new Float(attributeValue);
100: param = new FloatParam(attributeName, f.floatValue());
101: } else if (attributeType.equals("int")) {
102: Integer i = new Integer(attributeValue);
103: param = new IntParam(attributeName, i.intValue());
104: } else if (attributeType.equals("long")) {
105: Long l = new Long(attributeValue);
106: param = new LongParam(attributeName, l.longValue());
107: } else if (attributeType.equals("short")) {
108: Short s = new Short(attributeValue);
109: param = new ShortParam(attributeName, s.shortValue());
110: } else if (attributeType.equals("String")) {
111: String s = new String(attributeValue);
112: param = new StringParam(attributeName, s);
113: } else {
114: throw new RuntimeException("unknown type in parameter");
115: }
116:
117: // finally add the parameter in the table
118: paramTable.addParam(attributeName, param);
119:
120: /*
121: * logger.debug(" Name = " + attributeName +
122: * " Type = " + attributeType +
123: * " Value = " + attributeValue);
124: */
125: }
126: }
127:
128: private ParamTable paramTable = null;
129: }
|