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: * @(#)XMLReader.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.binding.jms.config;
030:
031: import com.sun.jbi.StringTranslator;
032:
033: import com.sun.jbi.binding.jms.JMSBindingContext;
034:
035: import com.sun.org.apache.xpath.internal.XPathAPI;
036:
037: import java.io.InputStream;
038:
039: import java.util.ArrayList;
040: import java.util.Collection;
041:
042: import java.util.logging.Logger;
043:
044: import javax.jbi.JBIException;
045:
046: import javax.xml.parsers.DocumentBuilder;
047: import javax.xml.parsers.DocumentBuilderFactory;
048:
049: import org.w3c.dom.Document;
050: import org.w3c.dom.Element;
051:
052: import org.w3c.dom.NodeList;
053: import org.w3c.dom.Text;
054:
055: /**
056: * This configuration reader implementation maps a physical XML file
057: * representation to a ConfigReader's tree representation. Users instantiate
058: * an instance of XMLReader to read a XML configuration file and use the
059: * ConfigElement and ConfigReader's interfaces to read the configuration data.
060: * For e.g: To read the loglevel from XML file containing
061: * <pre> <code>
062: * <jmsbinding>
063: * <loglevel>INFO</loglevel>
064: * </jmsbinding>
065: * </code> </pre>
066: * the user would have to write the following code
067: * <pre> <code>
068: * String logLevel = xmlReader.getConfigurationValue("/jmsbinding/loglevel");
069: * </code> </pre>
070: *
071: * @author Sun Microsystems Inc.
072: */
073: public class XMLReader {
074: /**
075: * Representation of the configuration file as a DOM object.
076: */
077: private Element mRootElement;
078:
079: /**
080: * Handle to the logger instance.
081: */
082: private Logger mLogger;
083:
084: /**
085: * Internal handle to the string translator.
086: */
087: private StringTranslator mStringTranslator;
088:
089: /**
090: * Creates a new instance of XMLReader.
091: */
092: public XMLReader() {
093: mLogger = JMSBindingContext.getInstance().getLogger();
094: mStringTranslator = JMSBindingContext.getInstance()
095: .getStringTranslator();
096: }
097:
098: /**
099: * This returns the configuration element value as a config element.
100: *
101: * @param elementName - Configuration element name.
102: *
103: * @return the configuration element value as a config element.
104: */
105: public ConfigElement getConfigurationElement(String elementName) {
106: ConfigElement result = null;
107:
108: if (mRootElement != null) {
109: try {
110: Element configElement = (Element) XPathAPI
111: .selectSingleNode(mRootElement, elementName);
112:
113: if (configElement != null) {
114: result = new XMLConfigElement(configElement);
115: }
116: } catch (Exception exp) {
117: exp.printStackTrace();
118: result = null;
119: }
120: }
121:
122: return result;
123: }
124:
125: /**
126: * This returns the configuration element value as a config element array.
127: *
128: * @param elementName - Configuration element name.
129: *
130: * @return the configuration element values as a config element array.
131: */
132: public Collection getConfigurationElementList(String elementName) {
133: Collection result = new ArrayList();
134:
135: if (mRootElement != null) {
136: try {
137: NodeList configElementList = XPathAPI.selectNodeList(
138: mRootElement, elementName);
139:
140: for (int i = 0; i < configElementList.getLength(); i++) {
141: result.add(new XMLConfigElement(
142: (Element) configElementList.item(i)));
143: }
144: } catch (Exception exp) {
145: ;
146: }
147: }
148:
149: return result;
150: }
151:
152: /**
153: * This returns the configuration element value as a config element array.
154: *
155: * @param base is the base element from where the search is to begin.
156: * @param elementName - Configuration element name.
157: *
158: * @return the configuration element values as a config element array.
159: */
160: public Collection getConfigurationElementList(Element base,
161: String elementName) {
162: Collection result = new ArrayList();
163:
164: if (mRootElement != null) {
165: try {
166: NodeList configElementList = XPathAPI.selectNodeList(
167: base, elementName);
168:
169: for (int i = 0; i < configElementList.getLength(); i++) {
170: result.add(new XMLConfigElement(
171: (Element) configElementList.item(i)));
172: }
173: } catch (Exception exp) {
174: ;
175: }
176: }
177:
178: return result;
179: }
180:
181: /**
182: * This returns the configuration element value as a string. It returns the
183: * first node value if there are more than one.
184: *
185: * @param elementName - Configuration element name.
186: *
187: * @return the configuration element value as a string.
188: */
189: public String getConfigurationValue(String elementName) {
190: String result = null;
191:
192: if (mRootElement != null) {
193: try {
194: Element configElement = (Element) XPathAPI
195: .selectSingleNode(mRootElement, elementName);
196:
197: if (configElement != null) {
198: Text textNode = (Text) configElement
199: .getFirstChild();
200: result = textNode.getData();
201: }
202: } catch (Exception exp) {
203: ;
204: }
205: }
206:
207: return result;
208: }
209:
210: /**
211: * This returns a configuration element values as a string array.
212: *
213: * @param elementName - Configuration element name.
214: *
215: * @return the configuration element values as a string array.
216: */
217: public Collection getConfigurationValueList(String elementName) {
218: Collection result = new ArrayList();
219:
220: if (mRootElement != null) {
221: try {
222: NodeList configElementList = XPathAPI.selectNodeList(
223: mRootElement, elementName);
224:
225: for (int i = 0; i < configElementList.getLength(); i++) {
226: Element textElement = (Element) configElementList
227: .item(i);
228: Text textNode = (Text) textElement.getFirstChild();
229: result.add(textNode.getData());
230: }
231: } catch (Exception exp) {
232: result = null;
233: }
234: }
235:
236: return result;
237: }
238:
239: /**
240: * Reads the configuration file and stores the content as a DOM object.
241: *
242: * @param doc - configuration file name.
243: *
244: * @throws JBIException - if the reader cannot be initialized.
245: */
246: void init(Document doc) throws JBIException {
247: try {
248: mRootElement = doc.getDocumentElement();
249: } catch (Exception exception) {
250: exception.printStackTrace();
251:
252: JBIException jbiException = new JBIException(exception
253: .getMessage());
254: jbiException.initCause(exception);
255: throw jbiException;
256: }
257: }
258:
259: /**
260: * Reads the input stream and stores the content as a DOM object.
261: *
262: * @param fileStream - input file stream.
263: *
264: * @throws JBIException jbi exception.
265: */
266: void init(InputStream fileStream) throws JBIException {
267: try {
268: DocumentBuilderFactory builderFactory = DocumentBuilderFactory
269: .newInstance();
270: DocumentBuilder documentBuilder = builderFactory
271: .newDocumentBuilder();
272: Document configDocument = documentBuilder.parse(fileStream);
273: mRootElement = configDocument.getDocumentElement();
274: } catch (Exception exception) {
275: JBIException jbiException = new JBIException(exception
276: .getMessage());
277: jbiException.initCause(exception);
278: throw jbiException;
279: }
280: }
281: }
|