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: * @(#)NMRStatisticsDataReader.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.esb.management.common.data.helper;
030:
031: import java.io.File;
032: import java.io.FileInputStream;
033: import java.io.IOException;
034: import java.io.InputStream;
035: import java.io.Reader;
036: import java.io.Serializable;
037: import java.io.StringReader;
038: import java.net.MalformedURLException;
039: import java.net.URI;
040: import java.net.URISyntaxException;
041: import java.util.ArrayList;
042: import java.util.HashMap;
043: import java.util.List;
044: import java.util.Map;
045: import java.util.Stack;
046:
047: import javax.xml.parsers.ParserConfigurationException;
048: import javax.xml.parsers.SAXParser;
049: import javax.xml.parsers.SAXParserFactory;
050:
051: import org.xml.sax.Attributes;
052: import org.xml.sax.InputSource;
053: import org.xml.sax.SAXException;
054: import org.xml.sax.helpers.DefaultHandler;
055:
056: import com.sun.esb.management.common.data.NMRStatisticsData;
057:
058: /**
059: * @author graj
060: *
061: */
062: public class NMRStatisticsDataReader extends DefaultHandler implements
063: NMRStatisticsDataXMLConstants, Serializable {
064:
065: static final long serialVersionUID = -1L;
066:
067: // Private members needed to parse the XML document
068:
069: // keep track of parsing
070: private boolean parsingInProgress;
071:
072: // keep track of QName
073: private Stack<String> qNameStack = new Stack<String>();
074:
075: private String nmrStatisticsDataListVersion;
076:
077: private List<String /* active channels */> activeChannelsList;
078:
079: private List<String /* active endpoints */> activeEndpointsList;
080:
081: private NMRStatisticsData data;
082:
083: private Map<String /* instanceName */, NMRStatisticsData> dataMap;
084:
085: /** Constructor - creates a new instance of NMRStatisticsDataReader */
086: public NMRStatisticsDataReader() {
087: }
088:
089: /**
090: * @return the dataMap
091: */
092: public Map<String /* instanceName */, NMRStatisticsData> getNMRStatisticsDataMap() {
093: return this .dataMap;
094: }
095:
096: /**
097: * Start of document processing.
098: *
099: * @throws org.xml.sax.SAXException
100: * is any SAX exception, possibly wrapping another exception.
101: */
102: public void startDocument() throws SAXException {
103: parsingInProgress = true;
104: qNameStack.removeAllElements();
105: }
106:
107: /**
108: * End of document processing.
109: *
110: * @throws org.xml.sax.SAXException
111: * is any SAX exception, possibly wrapping another exception.
112: */
113: public void endDocument() throws SAXException {
114: parsingInProgress = false;
115: // We have encountered the end of the document. Do any processing that
116: // is desired, for example dump all collected element2 values.
117:
118: }
119:
120: /**
121: * Process the new element.
122: *
123: * @param uri
124: * is the Namespace URI, or the empty string if the element has
125: * no Namespace URI or if Namespace processing is not being
126: * performed.
127: * @param localName
128: * is the The local name (without prefix), or the empty string if
129: * Namespace processing is not being performed.
130: * @param qName
131: * is the qualified name (with prefix), or the empty string if
132: * qualified names are not available.
133: * @param attributes
134: * is the attributes attached to the element. If there are no
135: * attributes, it shall be an empty Attributes object.
136: * @throws org.xml.sax.SAXException
137: * is any SAX exception, possibly wrapping another exception.
138: */
139: public void startElement(String uri, String localName,
140: String qName, Attributes attributes) throws SAXException {
141: if (qName != null) {
142: if (qName.endsWith(NMR_STATISTICS_DATA_LIST_KEY)) {
143: // ELEMENT1 has an attribute, get it by name
144: // Do something with the attribute
145: if ((attributes != null)
146: && (attributes.getLength() > 0)) {
147: String namespace = attributes
148: .getValue(NAMESPACE_KEY);
149: // //////////////////////////////////////////////////////
150: // Read nmrStatisticsDataListVersion attribute and
151: // ensure you store the right nmrStatisticsDataListVersion
152: // of the report map list
153: // //////////////////////////////////////////////////////
154: this .nmrStatisticsDataListVersion = attributes
155: .getValue(VERSION_KEY);
156: if ((nmrStatisticsDataListVersion != null)
157: && (VERSION_VALUE
158: .equals(nmrStatisticsDataListVersion))) {
159: this .dataMap = new HashMap<String /* instanceName */, NMRStatisticsData>();
160: } else {
161: // Invalid nmrStatisticsDataListVersion.
162: // Not storing it
163: }
164: }
165: } else if (qName.endsWith(ACTIVE_CHANNELS_LIST_KEY)) {
166: // ELEMENT1 has an attribute, get it by name
167: // Do something with the attribute
168: if ((this .dataMap != null) && (this .data != null)) {
169: this .activeChannelsList = new ArrayList<String /*
170: * Active
171: * Channels
172: * List
173: */>();
174: }
175: } else if (qName.endsWith(ACTIVE_ENDPOINTS_LIST_KEY)) {
176: // ELEMENT1 has an attribute, get it by name
177: // Do something with the attribute
178: if ((this .dataMap != null) && (this .data != null)) {
179: this .activeEndpointsList = new ArrayList<String /*
180: * Active
181: * Endpoints
182: * List
183: */>();
184: }
185: } else if (qName.endsWith(NMR_STATISTICS_DATA_KEY)) {
186: // ELEMENT1 has an attribute, get it by name
187: // Do something with the attribute
188: if (this .dataMap != null) {
189: this .data = new NMRStatisticsData();
190: }
191: }
192: // Keep track of QNames
193: qNameStack.push(qName);
194: }
195: }
196:
197: /**
198: * Process the character report for current tag.
199: *
200: * @param ch
201: * are the element's characters.
202: * @param start
203: * is the start position in the character array.
204: * @param length
205: * is the number of characters to use from the character array.
206: * @throws org.xml.sax.SAXException
207: * is any SAX exception, possibly wrapping another exception.
208: */
209: public void characters(char[] ch, int start, int length)
210: throws SAXException {
211: String qName;
212: String chars = new String(ch, start, length);
213: // Get current QName
214: qName = (String) qNameStack.peek();
215: if (qName.endsWith(INSTANCE_NAME_KEY)) {
216: if (this .data != null) {
217: this .data.setInstanceName(chars);
218: }
219: } else if (qName.endsWith(ACTIVE_CHANNEL_KEY)) {
220: if ((this .data != null)
221: && (this .activeChannelsList != null)) {
222: this .activeChannelsList.add(chars);
223: }
224: } else if (qName.endsWith(ACTIVE_ENDPOINT_KEY)) {
225: if ((this .data != null)
226: && (this .activeEndpointsList != null)) {
227: this .activeEndpointsList.add(chars);
228: }
229: }
230: }
231:
232: /**
233: * Process the end element tag.
234: *
235: * @param uri
236: * is the Namespace URI, or the empty string if the element has
237: * no Namespace URI or if Namespace processing is not being
238: * performed.
239: * @param localName
240: * is the The local name (without prefix), or the empty string if
241: * Namespace processing is not being performed.
242: * @param qName
243: * is the qualified name (with prefix), or the empty string if
244: * qualified names are not available.
245: * @throws org.xml.sax.SAXException
246: * is any SAX exception, possibly wrapping another exception.
247: */
248: public void endElement(String uri, String localName, String qName)
249: throws SAXException {
250: // Pop QName, since we are done with it
251: qNameStack.pop();
252: if (qName != null) {
253: if (qName.endsWith(NMR_STATISTICS_DATA_KEY)) {
254: // We have encountered the end of ELEMENT1
255: // ...
256: if ((this .dataMap != null) && (this .data != null)) {
257: this .dataMap.put(this .data.getInstanceName(),
258: this .data);
259: this .data = null;
260: }
261: } else if (qName.endsWith(ACTIVE_CHANNELS_LIST_KEY)) {
262: // We have encountered the end of ELEMENT1
263: // ...
264: if ((this .data != null)
265: && (this .activeChannelsList != null)) {
266: this .data
267: .setActiveChannelsList(this .activeChannelsList);
268: this .activeChannelsList = null;
269: }
270: } else if (qName.endsWith(ACTIVE_ENDPOINTS_LIST_KEY)) {
271: // We have encountered the end of ELEMENT1
272: // ...
273: if ((this .data != null)
274: && (this .activeEndpointsList != null)) {
275: this .data
276: .setActiveEndpointsList(this .activeEndpointsList);
277: this .activeChannelsList = null;
278: }
279: }
280: }
281: }
282:
283: /**
284: *
285: * @param rawXMLData
286: * @return
287: * @throws MalformedURLException
288: * @throws ParserConfigurationException
289: * @throws SAXException
290: * @throws URISyntaxException
291: * @throws IOException
292: */
293: public static Map<String /* instanceName */, NMRStatisticsData> parseFromXMLData(
294: String rawXMLData) throws MalformedURLException,
295: ParserConfigurationException, SAXException,
296: URISyntaxException, IOException {
297: // System.out.println("Parsing file: "+uriString);
298: // Get an instance of the SAX parser factory
299: SAXParserFactory factory = SAXParserFactory.newInstance();
300:
301: // Get an instance of the SAX parser
302: SAXParser saxParser = factory.newSAXParser();
303:
304: // Initialize the XML Document InputStream
305: Reader reader = new StringReader(rawXMLData);
306:
307: // Create an InputSource from the InputStream
308: InputSource inputSource = new InputSource(reader);
309:
310: // Parse the aspectInput XML document stream, using my event handler
311: NMRStatisticsDataReader parser = new NMRStatisticsDataReader();
312: saxParser.parse(inputSource, parser);
313:
314: return parser.getNMRStatisticsDataMap();
315:
316: }
317:
318: /**
319: *
320: * @param fileName
321: * @return
322: * @throws MalformedURLException
323: * @throws ParserConfigurationException
324: * @throws SAXException
325: * @throws URISyntaxException
326: * @throws IOException
327: */
328: public static Map<String /* instanceName */, NMRStatisticsData> parseFromFile(
329: String fileName) throws MalformedURLException,
330: ParserConfigurationException, SAXException,
331: URISyntaxException, IOException {
332: File file = new File(fileName);
333: return parseFromFile(file);
334: }
335:
336: /**
337: *
338: * @param fileName
339: * @return
340: * @throws MalformedURLException
341: * @throws ParserConfigurationException
342: * @throws SAXException
343: * @throws URISyntaxException
344: * @throws IOException
345: */
346: public static Map<String /* instanceName */, NMRStatisticsData> parseFromFile(
347: File file) throws MalformedURLException,
348: ParserConfigurationException, SAXException,
349: URISyntaxException, IOException {
350:
351: // Get an instance of the SAX parser factory
352: SAXParserFactory factory = SAXParserFactory.newInstance();
353:
354: // Get an instance of the SAX parser
355: SAXParser saxParser = factory.newSAXParser();
356:
357: // Initialize the URI and XML Document InputStream
358: InputStream inputStream = new FileInputStream(file);
359:
360: // Create an InputSource from the InputStream
361: InputSource inputSource = new InputSource(inputStream);
362:
363: // Parse the aspectInput XML document stream, using my event handler
364: NMRStatisticsDataReader parser = new NMRStatisticsDataReader();
365: saxParser.parse(inputSource, parser);
366:
367: return parser.getNMRStatisticsDataMap();
368: }
369:
370: /**
371: *
372: * @param uriString
373: * @return
374: * @throws MalformedURLException
375: * @throws ParserConfigurationException
376: * @throws SAXException
377: * @throws URISyntaxException
378: * @throws IOException
379: */
380: public static Map<String /* instanceName */, NMRStatisticsData> parseFromURI(
381: String uriString) throws MalformedURLException,
382: ParserConfigurationException, SAXException,
383: URISyntaxException, IOException {
384: URI uri = new URI(uriString);
385: return parseFromURI(uri);
386: }
387:
388: /**
389: *
390: * @param uri
391: * @return
392: * @throws MalformedURLException
393: * @throws ParserConfigurationException
394: * @throws SAXException
395: * @throws URISyntaxException
396: * @throws IOException
397: */
398: public static Map<String /* instanceName */, NMRStatisticsData> parseFromURI(
399: URI uri) throws MalformedURLException,
400: ParserConfigurationException, SAXException,
401: URISyntaxException, IOException {
402:
403: // Get an instance of the SAX parser factory
404: SAXParserFactory factory = SAXParserFactory.newInstance();
405:
406: // Get an instance of the SAX parser
407: SAXParser saxParser = factory.newSAXParser();
408:
409: // Initialize the URI and XML Document InputStream
410: InputStream inputStream = uri.toURL().openStream();
411:
412: // Create an InputSource from the InputStream
413: InputSource inputSource = new InputSource(inputStream);
414:
415: // Parse the aspectInput XML document stream, using my event handler
416: NMRStatisticsDataReader parser = new NMRStatisticsDataReader();
417: saxParser.parse(inputSource, parser);
418:
419: return parser.getNMRStatisticsDataMap();
420: }
421:
422: /**
423: * @param args
424: */
425: public static void main(String[] args) {
426: String uri = "C:/test/schema/nmrstatistics/NMRStatisticsData.xml";
427: try {
428: Map<String /* instanceName */, NMRStatisticsData> map = null;
429: map = NMRStatisticsDataReader.parseFromFile(uri);
430: for (String instanceName : map.keySet()) {
431: System.out.println(map.get(instanceName)
432: .getDisplayString());
433: }
434: } catch (MalformedURLException e) {
435: e.printStackTrace();
436: } catch (ParserConfigurationException e) {
437: e.printStackTrace();
438: } catch (SAXException e) {
439: e.printStackTrace();
440: } catch (URISyntaxException e) {
441: e.printStackTrace();
442: } catch (IOException e) {
443: e.printStackTrace();
444: }
445: }
446:
447: }
|