001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * If you wish your version of this file to be governed by only the CDDL
025: * or only the GPL Version 2, indicate your decision by adding
026: * "[Contributor] elects to include this software in this distribution
027: * under the [CDDL or GPL Version 2] license." If you do not indicate a
028: * single choice of license, a recipient has the option to distribute
029: * your version of this file under either the CDDL, the GPL Version 2 or
030: * to extend the choice of license to its licensees as provided above.
031: * However, if you add GPL Version 2 code and therefore, elected the GPL
032: * Version 2 license, then the option applies only if the new code is
033: * made subject to such option by the copyright holder.
034: *
035: * Contributor(s):
036: *
037: * Portions Copyrighted 2008 Sun Microsystems, Inc.
038: */
039: package org.netbeans.modules.sun.manager.jbi.management.model;
040:
041: import org.netbeans.modules.sun.manager.jbi.management.model.constraint.JBIComponentConfigurationConstraint;
042: import org.netbeans.modules.sun.manager.jbi.management.model.constraint.CompositeConstraint;
043: import org.netbeans.modules.sun.manager.jbi.management.model.constraint.JBIComponentConfigurationConstraintFactory;
044: import java.io.IOException;
045: import java.io.StringReader;
046: import java.util.ArrayList;
047: import java.util.List;
048: import javax.xml.namespace.QName;
049: import javax.xml.parsers.DocumentBuilder;
050: import javax.xml.parsers.DocumentBuilderFactory;
051: import javax.xml.parsers.ParserConfigurationException;
052: import org.netbeans.modules.sun.manager.jbi.util.XmlUtil;
053: import org.w3c.dom.Document;
054: import org.w3c.dom.Element;
055: import org.w3c.dom.NodeList;
056: import org.xml.sax.InputSource;
057: import org.xml.sax.SAXException;
058:
059: /**
060: * JBI Component configuration parser.
061: *
062: * @author jqian
063: */
064: public class JBIComponentConfigurationParser {
065:
066: private static final String CONFIGURATION_NAMESPACE = "http://www.sun.com/jbi/Configuration/V1.0"; // NOI18N
067: // element names
068: private static final String PROPERTY = "Property";
069: private static final String PROPERTY_GROUP = "PropertyGroup";
070: private static final String APPLICATION_CONFIGURATION = "ApplicationConfiguration";
071: private static final String APPLICATION_VARIABLE = "ApplicationVariable";
072: private static final String CONFIGURATION = "Configuration"; // NOI18N
073: private static final String CONSTRAINT = "Constraint"; // NOI18N
074: // attribute names
075: private static final String NAME = "name"; // NOI18N
076: private static final String DISPLAY_NAME = "displayName"; // NOI18N
077: private static final String DISPLAY_DESCRIPTION = "displayDescription"; // NOI18N
078: private static final String ENCRYPTED = "encrypted"; // NOI18N
079: private static final String IS_APPLICATION_RESTART_REQUIRED = "isApplicationRestartRequired"; // NOI18N
080: private static final String IS_COMPONENT_RESTART_REQUIRED = "isComponentRestartRequired"; // NOI18N
081: private static final String IS_SERVER_RESTART_REQUIRED = "isServerRestartRequired"; // NOI18N
082: private static final String REQUIRED = "required"; // NOI18N
083: private static final String SHOW_DISPLAY = "showDisplay"; // NOI18N
084: private static final String ON_CHANGE_MESSAGE = "onChangeMessage"; // NOI18N
085: private static final String TYPE = "type"; // NOI18N
086: private static final String DEFAULT_VALUE = "defaultValue"; // NOI18N
087: private static final String FACET = "facet"; // NOI18N
088: private static final String FACET_VALUE = "value"; // NOI18N
089:
090: /**
091: * Parses component jbi.xml and gets the component configuration
092: * meta data.
093: *
094: * @return the top level configuration descriptor
095: *
096: * @throws javax.xml.parsers.ParserConfigurationException
097: * @throws java.io.IOException
098: * @throws org.xml.sax.SAXException
099: */
100: public static JBIComponentConfigurationDescriptor parse(
101: String configXmlData) throws ParserConfigurationException,
102: IOException, SAXException {
103:
104: JBIComponentConfigurationDescriptor ret = null;
105:
106: if (configXmlData != null) {
107: DocumentBuilderFactory factory = DocumentBuilderFactory
108: .newInstance();
109: factory.setNamespaceAware(true);
110: DocumentBuilder builder = factory.newDocumentBuilder();
111: Document jbiDoc = builder.parse(new InputSource(
112: new StringReader(configXmlData)));
113: ret = parse(jbiDoc);
114: }
115:
116: return ret;
117: }
118:
119: public static JBIComponentConfigurationDescriptor parse(
120: Document jbiDoc) throws ParserConfigurationException,
121: IOException, SAXException {
122:
123: JBIComponentConfigurationDescriptor ret = null;
124:
125: NodeList configNodeList = jbiDoc.getElementsByTagNameNS(
126: CONFIGURATION_NAMESPACE, CONFIGURATION);
127: if (configNodeList != null && configNodeList.getLength() == 1) {
128: Element configRootElement = (Element) configNodeList
129: .item(0);
130: ret = new JBIComponentConfigurationDescriptor();
131: parseConfigurationElement(ret, configRootElement);
132: }
133:
134: return ret;
135: }
136:
137: private static void parseConfigurationElement(
138: JBIComponentConfigurationDescriptor descriptor,
139: Element element) {
140:
141: NodeList children = element.getChildNodes();
142: for (int i = 0; i < children.getLength(); i++) {
143: if (children.item(i) instanceof Element) {
144: Element childElement = (Element) children.item(i);
145:
146: String elementName = childElement.getLocalName();
147: if (!elementName.equals(PROPERTY)
148: && !elementName.equals(PROPERTY_GROUP)
149: && !elementName.equals(APPLICATION_VARIABLE)
150: && !elementName
151: .equals(APPLICATION_CONFIGURATION)) {
152: continue;
153: }
154:
155: JBIComponentConfigurationDescriptor childDescriptor;
156:
157: if (elementName.equals(APPLICATION_VARIABLE)) {
158: childDescriptor = new JBIComponentConfigurationDescriptor.ApplicationVariable();
159: } else if (elementName
160: .equals(APPLICATION_CONFIGURATION)) {
161: childDescriptor = new JBIComponentConfigurationDescriptor.ApplicationConfiguration();
162: } else { // Property or Property Group
163: childDescriptor = new JBIComponentConfigurationDescriptor();
164:
165: // The following are equired fields.
166: childDescriptor.setName(childElement
167: .getAttribute(NAME));
168: childDescriptor.setDisplayName(childElement
169: .getAttribute(DISPLAY_NAME));
170: childDescriptor.setDescription(childElement
171: .getAttribute(DISPLAY_DESCRIPTION));
172: childDescriptor.setTypeQName(XmlUtil
173: .getAttributeNSName(childElement, TYPE));
174: childDescriptor.setPropertyType(elementName); // not really required, but mandatory
175:
176: // The following are not required.
177: String required = childElement
178: .getAttribute(REQUIRED);
179: if (required.length() != 0) {
180: childDescriptor.setRequired(Boolean
181: .valueOf(required));
182: }
183: String showDisplay = childElement
184: .getAttribute(SHOW_DISPLAY);
185: if (showDisplay.length() != 0) {
186: childDescriptor.setShowDisplay(showDisplay);
187: }
188: String encrypted = childElement
189: .getAttribute(ENCRYPTED);
190: if (encrypted.length() != 0) {
191: childDescriptor.setEncrypted(Boolean
192: .valueOf(encrypted));
193: }
194: /*
195: String isApplicationRestartRequired =
196: childElement.getAttribute(IS_APPLICATION_RESTART_REQUIRED);
197: if (isApplicationRestartRequired.length() != 0) {
198: childDescriptor.setApplicationRestartRequired(
199: Boolean.valueOf(isApplicationRestartRequired));
200: }
201: String isComponentRestartRequired =
202: childElement.getAttribute(IS_COMPONENT_RESTART_REQUIRED);
203: if (isComponentRestartRequired.length() != 0) {
204: childDescriptor.setComponentRestartRequired(
205: Boolean.valueOf(isComponentRestartRequired));
206: }
207: String isServerRestartRequired =
208: childElement.getAttribute(IS_SERVER_RESTART_REQUIRED);
209: if (isServerRestartRequired.length() != 0) {
210: childDescriptor.setServerRestartRequired(
211: Boolean.valueOf(isServerRestartRequired));
212: }
213: */
214:
215: String onChangeMessage = childElement
216: .getAttribute(ON_CHANGE_MESSAGE);
217: if (onChangeMessage.length() != 0) {
218: childDescriptor
219: .setOnChangeMessage(onChangeMessage);
220: }
221:
222: String defaultValue = childElement
223: .getAttribute(DEFAULT_VALUE);
224: if (defaultValue.length() != 0) {
225: childDescriptor.setDefaultValue(defaultValue);
226: }
227:
228: CompositeConstraint compositeConstraint = new CompositeConstraint();
229: List<String> enumerationOptions = null;
230:
231: NodeList constraintNodes = childElement
232: .getElementsByTagNameNS(
233: CONFIGURATION_NAMESPACE, CONSTRAINT);
234:
235: for (int j = 0; j < constraintNodes.getLength(); j++) {
236: Element constraintElement = (Element) constraintNodes
237: .item(j);
238: String facet = constraintElement
239: .getAttribute(FACET);
240: String facetValue = constraintElement
241: .getAttribute(FACET_VALUE);
242: if (facet
243: .equals(JBIComponentConfigurationConstraintFactory.ENUMERATION)) {
244: if (enumerationOptions == null) {
245: enumerationOptions = new ArrayList<String>();
246: }
247: enumerationOptions.add(facetValue);
248: } else {
249: JBIComponentConfigurationConstraint constraint = JBIComponentConfigurationConstraintFactory
250: .newConstraint(facet, facetValue);
251: compositeConstraint
252: .addConstraint(constraint);
253: }
254: }
255:
256: if (enumerationOptions != null) {
257: JBIComponentConfigurationConstraint constraint = JBIComponentConfigurationConstraintFactory
258: .newConstraint(
259: JBIComponentConfigurationConstraintFactory.ENUMERATION,
260: enumerationOptions);
261: compositeConstraint.addConstraint(constraint);
262: }
263:
264: childDescriptor.setConstraint(compositeConstraint);
265: }
266:
267: String isApplicationRestartRequired = childElement
268: .getAttribute(IS_APPLICATION_RESTART_REQUIRED);
269: if (isApplicationRestartRequired.length() != 0) {
270: childDescriptor
271: .setApplicationRestartRequired(Boolean
272: .valueOf(isApplicationRestartRequired));
273: }
274: String isComponentRestartRequired = childElement
275: .getAttribute(IS_COMPONENT_RESTART_REQUIRED);
276: if (isComponentRestartRequired.length() != 0) {
277: childDescriptor.setComponentRestartRequired(Boolean
278: .valueOf(isComponentRestartRequired));
279: }
280: String isServerRestartRequired = childElement
281: .getAttribute(IS_SERVER_RESTART_REQUIRED);
282: if (isServerRestartRequired.length() != 0) {
283: childDescriptor.setServerRestartRequired(Boolean
284: .valueOf(isServerRestartRequired));
285: }
286:
287: descriptor.addChild(childDescriptor);
288:
289: parseConfigurationElement(childDescriptor, childElement);
290: }
291: }
292: }
293: }
|