001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)ConfigurationParser.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.ui.runtime;
030:
031: import java.io.ByteArrayInputStream;
032: import java.io.FileInputStream;
033: import java.io.IOException;
034: import java.io.InputStream;
035: import java.io.Serializable;
036: import java.net.MalformedURLException;
037: import java.net.URI;
038: import java.net.URISyntaxException;
039: import java.util.Enumeration;
040: import java.util.Iterator;
041: import java.util.Properties;
042: import java.util.Set;
043: import java.util.Stack;
044: import java.util.Map.Entry;
045:
046: import javax.xml.parsers.ParserConfigurationException;
047: import javax.xml.parsers.SAXParser;
048: import javax.xml.parsers.SAXParserFactory;
049:
050: import org.xml.sax.Attributes;
051: import org.xml.sax.InputSource;
052: import org.xml.sax.SAXException;
053: import org.xml.sax.helpers.DefaultHandler;
054:
055: /**
056: * @author graj
057: *
058: */
059: public class ComponentConfigurationParser extends DefaultHandler
060: implements Serializable {
061:
062: // Private members needed to parse the XML document
063: private boolean parsingInProgress; // keep track of parsing
064: private Stack<String> qNameStack = new Stack<String>(); // keep track of QName
065: private ComponentConfiguration configuration = new ComponentConfiguration(); // keep track of element2
066: private String[] displayKeys;
067: private DisplayInformation[] displayInfo = null;
068:
069: // XML TAGS
070: private static final String CONFIGURATION_KEY = "Configuration";
071: private static final String DISPLAYNAME_KEY = "displayName";
072: private static final String DISPLAYDESCRIPTION_KEY = "displayDescription";
073: private static final String ISPASSWORDFIELD_KEY = "isPasswordField";
074: private static final String NAME_KEY = "name";
075:
076: /**
077: *
078: */
079: public ComponentConfigurationParser(String[] keys) {
080: displayKeys = new String[keys.length];
081: displayInfo = new DisplayInformation[keys.length];
082: for (int index = 0; index < keys.length; index++) {
083: displayKeys[index] = keys[index];
084: displayInfo[index] = new DisplayInformation();
085: }
086: }
087:
088: public ComponentConfiguration getComponentConfiguration() {
089: return this .configuration;
090: }
091:
092: /**
093: * Start of document processing.
094: * @throws org.xml.sax.SAXException is any SAX exception,
095: * possibly wrapping another exception.
096: */
097: public void startDocument() throws SAXException {
098: parsingInProgress = true;
099: qNameStack.removeAllElements();
100: }
101:
102: /**
103: * End of document processing.
104: * @throws org.xml.sax.SAXException is any SAX exception,
105: * possibly wrapping another exception.
106: */
107: public void endDocument() throws SAXException {
108: parsingInProgress = false;
109: // We have encountered the end of the document. Do any processing that is desired,
110: // for example dump all collected element2 values.
111:
112: }
113:
114: /**
115: * Process the new element.
116: * @param uri is the Namespace URI, or the empty string if the element
117: * has no Namespace URI or if Namespace processing is not being performed.
118: * @param localName is the The local name (without prefix), or the empty
119: * string if Namespace processing is not being performed.
120: * @param qName is the qualified name (with prefix), or the empty string
121: * if qualified names are not available.
122: * @param attributes is the attributes attached to the element. If there
123: * are no attributes, it shall be an empty Attributes object.
124: * @throws org.xml.sax.SAXException is any SAX exception,
125: * possibly wrapping another exception.
126: */
127: public void startElement(String uri, String localName,
128: String qName, Attributes attributes) throws SAXException {
129: String displayName = null;
130: String displayDescription = null;
131: String password = null;
132: boolean isPasswordField = false;
133: if (qName.endsWith(CONFIGURATION_KEY)) {
134: // ELEMENT1 has an attribute, get it by name
135: String name = attributes.getValue(NAME_KEY);
136: // Do something with the attribute
137: this .configuration.setName(name);
138: } else {
139: for (int index = 0; index < displayKeys.length; index++) {
140: if ((displayKeys[index] != null)
141: && (qName.endsWith(displayKeys[index]) == true)) {
142: // Keep track of the value of element2
143: isPasswordField = false;
144: displayName = attributes.getValue(DISPLAYNAME_KEY);
145: displayDescription = attributes
146: .getValue(DISPLAYDESCRIPTION_KEY);
147: password = attributes.getValue(ISPASSWORDFIELD_KEY);
148: if ((displayName != null)
149: && (displayDescription != null)
150: && (password != null)) {
151: isPasswordField = Boolean
152: .parseBoolean(password);
153: System.out.println("displayName:" + displayName
154: + " displayDescription:"
155: + displayDescription + " password:"
156: + password + " isPasswordField:"
157: + isPasswordField);
158: displayInfo[index] = new DisplayInformation(
159: displayKeys[index], displayName,
160: displayDescription, isPasswordField);
161: } else {
162: System.out.println("displayName:" + displayName
163: + " displayDescription:"
164: + displayDescription + " password:"
165: + password);
166: }
167: }
168: }
169: }
170: // Keep track of QNames
171: qNameStack.push(qName);
172: }
173:
174: /**
175: * Process the character data for current tag.
176: * @param ch are the element's characters.
177: * @param start is the start position in the character array.
178: * @param length is the number of characters to use from the
179: * character array.
180: * @throws org.xml.sax.SAXException is any SAX exception,
181: * possibly wrapping another exception.
182: */
183: public void characters(char[] ch, int start, int length)
184: throws SAXException {
185: String qName;
186: String chars = new String(ch, start, length);
187: // Get current QName
188: qName = (String) qNameStack.peek();
189: if (qName.endsWith(CONFIGURATION_KEY)) {
190: // Nothing to process
191: } else {
192: for (int index = 0; index < displayKeys.length; index++) {
193: if ((displayKeys[index] != null)
194: && (qName.endsWith(displayKeys[index]) == true)) {
195: // Keep track of the value of element2
196: if (chars != null) {
197: displayInfo[index].setDefaultValue(chars);
198: } else {
199: displayInfo[index].setDefaultValue("");
200: }
201: } else {
202: }
203: }
204: }
205: }
206:
207: /**
208: * Process the end element tag.
209: * @param uri is the Namespace URI, or the empty string if the element
210: * has no Namespace URI or if Namespace processing is not being performed.
211: * @param localName is the The local name (without prefix), or the empty
212: * string if Namespace processing is not being performed.
213: * @param qName is the qualified name (with prefix), or the empty
214: * string if qualified names are not available.
215: * @throws org.xml.sax.SAXException is any SAX exception,
216: * possibly wrapping another exception.
217: */
218: public void endElement(String uri, String localName, String qName)
219: throws SAXException {
220: // Pop QName, since we are done with it
221: qNameStack.pop();
222: if (qName.endsWith(CONFIGURATION_KEY)) {
223: // We have encountered the end of ELEMENT1
224: // ...
225: for (int index = 0; index < displayKeys.length; index++) {
226: if ((displayKeys[index] != null)
227: && (displayInfo[index] != null)) {
228: this .configuration.addDisplayDetail(
229: displayKeys[index], displayInfo[index]);
230: } else {
231: System.out.println("Index " + index
232: + " displayKeys or displayInfo is null.");
233: if (displayKeys[index] != null) {
234: System.out.println("displayKeys[" + index
235: + "] is: " + displayKeys[index]);
236: }
237: if (displayInfo[index] != null) {
238: System.out.println("displayInfo[" + index
239: + "] is: ");
240: displayInfo[index].dump();
241: }
242: }
243: }
244: } else {
245: // We have encountered the end of an ELEMENT2
246: // ...
247: }
248: }
249:
250: /**
251: *
252: * @param uriString
253: * @param keys
254: * @return
255: * @throws MalformedURLException
256: * @throws ParserConfigurationException
257: * @throws SAXException
258: * @throws URISyntaxException
259: * @throws IOException
260: */
261: public static ComponentConfigurationParser parse(String uriString,
262: String[] keys) throws MalformedURLException,
263: ParserConfigurationException, SAXException,
264: URISyntaxException, IOException {
265:
266: // Get an instance of the SAX parser factory
267: SAXParserFactory factory = SAXParserFactory.newInstance();
268:
269: // Get an instance of the SAX parser
270: SAXParser saxParser = factory.newSAXParser();
271:
272: // Initialize the URI and XML Document InputStream
273: URI uri = new URI(uriString);
274: InputStream inputStream = uri.toURL().openStream();
275:
276: // Create an InputSource from the InputStream
277: InputSource inputSource = new InputSource(inputStream);
278:
279: // Parse the input XML document stream, using my event handler
280: ComponentConfigurationParser parser = new ComponentConfigurationParser(
281: keys);
282: saxParser.parse(inputSource, parser);
283:
284: return parser;
285: }
286:
287: /**
288: *
289: * @param xmlData
290: * @param keys
291: * @return
292: * @throws MalformedURLException
293: * @throws ParserConfigurationException
294: * @throws SAXException
295: * @throws URISyntaxException
296: * @throws IOException
297: */
298: public static ComponentConfigurationParser parseFromString(
299: String xmlData, String[] keys)
300: throws MalformedURLException, ParserConfigurationException,
301: SAXException, URISyntaxException, IOException {
302:
303: // Get an instance of the SAX parser factory
304: SAXParserFactory factory = SAXParserFactory.newInstance();
305:
306: // Get an instance of the SAX parser
307: SAXParser saxParser = factory.newSAXParser();
308:
309: // Initialize the XML Document InputStream
310: InputStream inputStream = new ByteArrayInputStream(xmlData
311: .getBytes("UTF-8"));
312:
313: // Create an InputSource from the InputStream
314: InputSource inputSource = new InputSource(inputStream);
315:
316: // Parse the input XML document stream, using my event handler
317: ComponentConfigurationParser parser = new ComponentConfigurationParser(
318: keys);
319: saxParser.parse(inputSource, parser);
320:
321: return parser;
322: }
323:
324: /**
325: * @param args
326: */
327: public static void main(String[] args) {
328: String uri = "file:///${jbicomps_home}/cachese/jbiadapter/componentconfiguration.xml";
329: String[] keys = null;
330: String propertiesFile = System.getProperty("JBICOMPS_HOME")
331: + "/cachese/jbiadapter/config.properties";
332: Properties properties = new Properties();
333: try {
334: properties.load(new FileInputStream(propertiesFile));
335: } catch (IOException e) {
336: }
337: keys = new String[properties.size()];
338: Set set = properties.entrySet();
339: Iterator iterator = set.iterator();
340: for (int index = 0; iterator.hasNext() == true; index++) {
341: Entry entry = (Entry) iterator.next();
342: keys[index] = (String) entry.getKey();
343: }
344: try {
345: ComponentConfigurationParser parser = ComponentConfigurationParser
346: .parse(uri, keys);
347: parser.getComponentConfiguration().dump();
348: } catch (MalformedURLException e) {
349: // TODO Auto-generated catch block
350: e.printStackTrace();
351: } catch (ParserConfigurationException e) {
352: // TODO Auto-generated catch block
353: e.printStackTrace();
354: } catch (SAXException e) {
355: // TODO Auto-generated catch block
356: e.printStackTrace();
357: } catch (URISyntaxException e) {
358: // TODO Auto-generated catch block
359: e.printStackTrace();
360: } catch (IOException e) {
361: // TODO Auto-generated catch block
362: e.printStackTrace();
363: }
364:
365: }
366:
367: }
|