001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2008 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 Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036: /*
037: * AppConfigXmlUtils.java
038: */
039: package com.sun.jbi.jsf.util;
040:
041: import com.sun.jbi.jsf.bean.AppConfigField;
042: import com.sun.jbi.ui.common.DOMUtil;
043: import java.io.Reader;
044: import java.io.StringReader;
045: import java.util.ArrayList;
046: import java.util.Collections;
047: import java.util.Comparator;
048: import java.util.List;
049: import java.util.logging.Level;
050: import java.util.logging.Logger;
051: import org.w3c.dom.Document;
052: import org.w3c.dom.Element;
053: import org.w3c.dom.NodeList;
054: import org.w3c.dom.Text;
055:
056: /**
057: * This class reads/writes the component configuration metadata list xml.
058: *
059: * @author Sun Microsystems, Inc.
060: */
061: public final class AppConfigXmlUtils {
062:
063: /**
064: * creates list of objects
065: *
066: * @param xmlText xml text
067: * @return list of objects
068: */
069: public static List<AppConfigField> readFromXml(String xmlText) {
070: return readFromXml(new StringReader(xmlText));
071: }
072:
073: /**
074: * return an Application Configuration field
075: *
076: * @param anAppConfigFieldElement XML element describing one field
077: * @return AppConfigField bean representing the
078: * field
079: */
080: private static AppConfigField createAppConfigField(
081: Element anAppConfigFieldElement) {
082:
083: AppConfigField result = new AppConfigField();
084:
085: String displayDescription = anAppConfigFieldElement
086: .getAttribute("displayDescription");
087: result.setDisplayDescription(displayDescription);
088:
089: String displayName = anAppConfigFieldElement
090: .getAttribute("displayName");
091: result.setDisplayName(displayName);
092:
093: String isPasswordFieldValue = anAppConfigFieldElement
094: .getAttribute("isPasswordField");
095: if ("true".equalsIgnoreCase(isPasswordFieldValue)) {
096: result.setIsPassword(true);
097: }
098:
099: String isRequiredFieldValue = anAppConfigFieldElement
100: .getAttribute("isRequiredField");
101:
102: if ("true".equalsIgnoreCase(isRequiredFieldValue)) {
103: result.setIsRequired(true);
104: }
105:
106: String name = anAppConfigFieldElement.getTagName();
107: int offset = "ComponentConfig:".length();
108: name = name.substring(offset);
109: result.setName(name);
110:
111: // TBD set default value...
112: Text text = (Text) anAppConfigFieldElement.getFirstChild();
113: if (null != text) {
114: String defaultValue = text.getData();
115: result.setValue(defaultValue);
116: }
117:
118: return result;
119: }
120:
121: /**
122: * creates list of objects
123: *
124: * @param xmlReader Reader
125: * @return list of objects
126: */
127: private static List<AppConfigField> readFromXml(Reader xmlReader) {
128: ArrayList<AppConfigField> result = new ArrayList();
129: Document xmlDoc = null;
130:
131: try {
132: xmlDoc = DOMUtil.UTIL.buildDOMDocument(xmlReader);
133:
134: } catch (Exception ex) {
135: sLog
136: .log(
137: Level.FINE,
138: ("AppConfigXmlUtils.readFromXml() caught ex=" + ex),
139: ex);
140: }
141:
142: if (null != xmlDoc) {
143:
144: // construct the comp configuration list
145: // TBD change to use namespace aware approach (i.e. strip off "config:" prefix)
146: Element compConfigAppConfigElement = DOMUtil.UTIL
147: .getElement(xmlDoc,
148: "componentConfig:ApplicationConfiguration");
149: if (null != compConfigAppConfigElement) {
150: NodeList compConfigAppConfigsList = compConfigAppConfigElement
151: .getChildNodes();
152: int size = compConfigAppConfigsList.getLength();
153: for (int i = 0; i < size; ++i) {
154: Element componentConfigurationMetadataElement = (Element) compConfigAppConfigsList
155: .item(i);
156: if (null != componentConfigurationMetadataElement) {
157: AppConfigField field = createAppConfigField(componentConfigurationMetadataElement);
158: result.add(field);
159: }
160: }
161: } else {
162: sLog
163: .fine("AppConfigXmlUtils.readFromXml(), found no config:ApplicationConfiguration element");
164: }
165: }
166:
167: sort(result);
168: return result;
169: }
170:
171: /**
172: * sorts the list
173: *
174: * @param compConfigAppConfigsList list
175: */
176: private static void sort(
177: List<AppConfigField> compConfigAppConfigsList) {
178: try {
179: Collections.sort(compConfigAppConfigsList,
180: new Comparator() {
181: public int compare(Object o1, Object o2) {
182: return ((AppConfigField) o1)
183: .getDisplayName().compareTo(
184: ((AppConfigField) o2)
185: .getDisplayName());
186: }
187: });
188: } catch (ClassCastException ccEx) {
189: sLog.log(Level.FINE,
190: ("AppConfigXmlUtils.sort() caught ccEx=" + ccEx),
191: ccEx);
192: } catch (UnsupportedOperationException unsupEx) {
193: sLog
194: .log(
195: Level.FINE,
196: ("AppConfigXmlUtils.sort() caught unsupEx=" + unsupEx),
197: unsupEx);
198: }
199: }
200:
201: /**
202: * prevents instantiation
203: */
204: private AppConfigXmlUtils() {
205: }
206:
207: /**
208: * logger for com.sun.jbi.jsf
209: */
210: private static Logger sLog = JBILogger.getInstance();
211: }
|