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.xml.parser;
028:
029: import org.cougaar.lib.param.BooleanParam;
030: import org.cougaar.lib.param.DoubleParam;
031: import org.cougaar.lib.param.FloatParam;
032: import org.cougaar.lib.param.IntParam;
033: import org.cougaar.lib.param.LongParam;
034: import org.cougaar.lib.param.Param;
035: import org.cougaar.lib.param.ShortParam;
036: import org.cougaar.lib.param.StringParam;
037:
038: /**
039: * Class that create parameters from strings.
040: */
041: public class ParamParser {
042:
043: /**
044: * no constructor
045: */
046: public ParamParser() {
047: }
048:
049: /**
050: * get parameter from parsed "name={type}value"
051: * @param ntv name=[{type}]value
052: * @return param
053: */
054: public Param getParam(String ntv) {
055: Param p = null;
056: if (ntv != null) {
057: int eq = ntv.indexOf("=");
058: if ((eq > 0) && (eq < (ntv.length() - 1))) {
059: p = getParam(ntv.substring(0, eq).trim(), ntv
060: .substring(eq + 1).trim());
061: }
062: }
063: return p;
064: }
065:
066: /**
067: * get parameter from parsed "{type}value"
068: * @param n name of the parameter
069: * @param tv [{type}]value of the parameter: parse "{type}value"
070: * @return param
071: */
072: protected Param getParam(String n, String tv) {
073: Param p = null;
074: if (tv != null) {
075: int endcb = tv.indexOf('}');
076: if (tv.charAt(0) == '{') {
077: if ((endcb > 0) && (endcb < tv.length() - 1))
078: p = getParam(n, tv.substring(1, endcb).trim(), tv
079: .substring(endcb + 1).trim());
080: } else {
081: if (endcb < 0)
082: p = getParam(n, "String", tv);
083: }
084: }
085: return p;
086: }
087:
088: /**
089: * get Parameter
090: * @param n name of the parameter
091: * @param t type of the parameter, null == "String"
092: * @param v String value of the parameter
093: * @return param
094: */
095: protected Param getParam(String n, String t, String v) {
096: Param p = null;
097: try {
098: if ((t == null) || (t.equals("String")))
099: p = new StringParam(n, v);
100: else if (t.equals("Boolean"))
101: p = new BooleanParam(n, v.equalsIgnoreCase("true"));
102: else if (t.equals("Double"))
103: p = new DoubleParam(n, Double.parseDouble(v));
104: else if (t.equals("Float"))
105: p = new FloatParam(n, Float.parseFloat(v));
106: else if (t.equals("Int"))
107: p = new IntParam(n, Integer.parseInt(v));
108: else if (t.equals("Long"))
109: p = new LongParam(n, Long.parseLong(v));
110: else if (t.equals("Short"))
111: p = new ShortParam(n, Short.parseShort(v));
112: } catch (Exception e) {
113: p = null;
114: }
115: return p;
116: }
117: }
|