001: /*
002: * (C) Copyright 2000 - 2005 Nabh Information Systems, Inc.
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU General Public License
006: * as published by the Free Software Foundation; either version 2
007: * of the License, or (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: *
018: */
019:
020: package com.nabhinc.ws.core;
021:
022: import java.io.IOException;
023: import java.io.Writer;
024:
025: import org.w3c.dom.Element;
026:
027: import com.nabhinc.util.StringUtil;
028: import com.nabhinc.util.XMLUtil;
029:
030: /**
031: * Encapsulates a Web service framework object property information
032: * including the property name and value, description, etc.
033: *
034: * @author Padmanabh Dabke
035: * (c) 2005 Nabh Information Systems, Inc. All Rights Reserved.
036: */
037: public class PropertyInfo {
038:
039: public static final int SPEC_TYPE_LITERAL = 0;
040: public static final int SPEC_TYPE_COIL = 1;
041:
042: /**
043: * Property name
044: */
045: public String name = null;
046:
047: /**
048: * Property description
049: */
050: public String description = null;
051:
052: /**
053: * Prompt to be used in a form corresponding to this property
054: */
055: public String prompt = null;
056:
057: /**
058: * Property value
059: */
060: public String value = null;
061:
062: /**
063: * A flag that provides a hint for editor UI. If true, editor
064: * should provide a text area for specifying this property.
065: */
066: public boolean isMemo = false;
067: /**
068: * Indicates how the property value is specified. If
069: * the spec type is SPEC_TYPE_COIL, the property
070: * setting mechanism will attempt to evaluate the value
071: * as COIL expression.
072: */
073: public int specType = SPEC_TYPE_LITERAL;
074:
075: /**
076: * Initializes property meta-data from XML configuration
077: * @param config XML configuration
078: * @throws IllegalArgumentException When spec-type is other than "coil" or "literal".
079: */
080: public void init(Element config) {
081: String paramName = XMLUtil.getSubElementText(config, "name");
082: if (paramName == null || paramName.trim().equals("")) {
083: throw new IllegalArgumentException(
084: "Missing init parameter name.");
085: }
086: String paramValue = XMLUtil.getSubElementText(config, "value");
087:
088: name = paramName;
089: value = paramValue;
090: description = XMLUtil.getSubElementText(config, "description");
091: prompt = XMLUtil.getSubElementText(config, "prompt");
092: String sType = XMLUtil.getSubElementText(config, "spec-type");
093: if (sType != null) {
094: if ("coil".equals(sType.toLowerCase())) {
095: specType = PropertyInfo.SPEC_TYPE_COIL;
096: } else if (!"literal".equals(sType)) {
097: throw new IllegalArgumentException(
098: "Invalid spec-type value. Must be coil or literal.");
099: }
100: }
101:
102: if (XMLUtil.getSubElement(config, "is-memo") != null)
103: isMemo = true;
104:
105: }
106:
107: /**
108: * Serializes property metadata as XML.
109: * @param indent Starting indent
110: * @param delta Indentation for each level
111: * @param w Writer
112: * @param tag Enclosing tag used for the element.
113: * @throws IOException
114: */
115: public void serialize(String indent, String delta, Writer w,
116: String tag) throws IOException {
117: String indent1 = indent + delta;
118: XMLUtil.writeElementStart(indent, tag, w);
119: XMLUtil.writeElement(indent1, "name", name, w);
120: XMLUtil.writeElement(indent1, "value", StringUtil
121: .encodeXML(value), w);
122: XMLUtil.writeElement(indent1, "prompt", prompt, w);
123: XMLUtil.writeElement(indent1, "description", description, w);
124: if (specType == PropertyInfo.SPEC_TYPE_COIL) {
125: XMLUtil.writeElement(indent1, "spec-type", "coil", w);
126: }
127: if (isMemo)
128: XMLUtil.writeEmptyElement(indent1, "is-memo", w);
129: XMLUtil.writeElementEnd(indent, tag, w);
130:
131: }
132:
133: }
|