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: * @(#)ServiceAssemblyStatisticsDataReader.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.text.ParseException;
042: import java.text.SimpleDateFormat;
043: import java.util.ArrayList;
044: import java.util.Date;
045: import java.util.HashMap;
046: import java.util.List;
047: import java.util.Map;
048: import java.util.Stack;
049:
050: import javax.xml.parsers.ParserConfigurationException;
051: import javax.xml.parsers.SAXParser;
052: import javax.xml.parsers.SAXParserFactory;
053:
054: import org.xml.sax.Attributes;
055: import org.xml.sax.InputSource;
056: import org.xml.sax.SAXException;
057: import org.xml.sax.helpers.DefaultHandler;
058:
059: import com.sun.esb.management.common.data.ServiceAssemblyStatisticsData;
060: import com.sun.esb.management.common.data.ServiceUnitStatisticsData;
061:
062: /**
063: * @author graj
064: *
065: */
066: public class ServiceAssemblyStatisticsDataReader extends DefaultHandler
067: implements ServiceAssemblyStatisticsDataXMLConstants,
068: Serializable {
069: static final long serialVersionUID = -1L;
070:
071: // Private members needed to parse the XML document
072:
073: // keep track of parsing
074: private boolean parsingInProgress;
075:
076: // keep track of QName
077: private Stack<String> qNameStack = new Stack<String>();
078:
079: private String serviceAssemblyStatisticsDataListVersion;
080:
081: private String serviceUnitStatisticsDataListVersion;
082:
083: private ServiceAssemblyStatisticsData data;
084:
085: private Map<String /* instanceName */, ServiceAssemblyStatisticsData> dataMap;
086:
087: private ServiceUnitStatisticsData unitData;
088:
089: private List<ServiceUnitStatisticsData> unitDataList;
090:
091: private List<String /* endpointName */> endpointNameList;
092:
093: /** Constructor - creates a new instance of ServiceAssemblyStatisticsDataReader */
094: public ServiceAssemblyStatisticsDataReader() {
095: }
096:
097: /**
098: * @return the dataMap
099: */
100: public Map<String /* instanceName */, ServiceAssemblyStatisticsData> getServiceAssemblyStatisticsDataMap() {
101: return this .dataMap;
102: }
103:
104: /**
105: * Start of document processing.
106: *
107: * @throws org.xml.sax.SAXException
108: * is any SAX exception, possibly wrapping another exception.
109: */
110: public void startDocument() throws SAXException {
111: parsingInProgress = true;
112: qNameStack.removeAllElements();
113: }
114:
115: /**
116: * End of document processing.
117: *
118: * @throws org.xml.sax.SAXException
119: * is any SAX exception, possibly wrapping another exception.
120: */
121: public void endDocument() throws SAXException {
122: parsingInProgress = false;
123: // We have encountered the end of the document. Do any processing that
124: // is desired, for example dump all collected element2 values.
125: }
126:
127: /**
128: * Process the new element.
129: *
130: * @param uri
131: * is the Namespace URI, or the empty string if the element has
132: * no Namespace URI or if Namespace processing is not being
133: * performed.
134: * @param localName
135: * is the The local name (without prefix), or the empty string if
136: * Namespace processing is not being performed.
137: * @param qName
138: * is the qualified name (with prefix), or the empty string if
139: * qualified names are not available.
140: * @param attributes
141: * is the attributes attached to the element. If there are no
142: * attributes, it shall be an empty Attributes object.
143: * @throws org.xml.sax.SAXException
144: * is any SAX exception, possibly wrapping another exception.
145: */
146: public void startElement(String uri, String localName,
147: String qName, Attributes attributes) throws SAXException {
148: if (qName != null) {
149: if (qName
150: .endsWith(ServiceAssemblyStatisticsDataXMLConstants.ASSEMBLY_STATISTICS_DATA_LIST_KEY)) {
151: // ELEMENT1 has an attribute, get it by name
152: // Do something with the attribute
153: if ((attributes != null)
154: && (attributes.getLength() > 0)) {
155: String namespace = attributes
156: .getValue(NAMESPACE_KEY);
157: // //////////////////////////////////////////////////////
158: // Read serviceAssemblyStatisticsDataListVersion attribute and
159: // ensure you store the right serviceAssemblyStatisticsDataListVersion
160: // of the report map list
161: // //////////////////////////////////////////////////////
162: this .serviceAssemblyStatisticsDataListVersion = attributes
163: .getValue(VERSION_KEY);
164: if ((serviceAssemblyStatisticsDataListVersion != null)
165: && (ServiceAssemblyStatisticsDataXMLConstants.ASSEMBLY_VERSION_VALUE
166: .equals(serviceAssemblyStatisticsDataListVersion))) {
167: this .dataMap = new HashMap<String /* instanceName */, ServiceAssemblyStatisticsData>();
168: } else {
169: // Invalid serviceAssemblyStatisticsDataListVersion.
170: // Not storing it
171: }
172: }
173: } else if (qName
174: .endsWith(ServiceAssemblyStatisticsDataXMLConstants.ASSEMBLY_STATISTICS_DATA_KEY)) {
175: // ELEMENT1 has an attribute, get it by name
176: // Do something with the attribute
177: if (this .dataMap != null) {
178: this .data = new ServiceAssemblyStatisticsData();
179: }
180: } else if (qName
181: .endsWith(ServiceAssemblyStatisticsDataXMLConstants.UNIT_STATISTICS_DATA_LIST_KEY)) {
182: // ELEMENT1 has an attribute, get it by name
183: // Do something with the attribute
184: if ((attributes != null)
185: && (attributes.getLength() > 0)) {
186: String namespace = attributes
187: .getValue(NAMESPACE_KEY);
188: // //////////////////////////////////////////////////////
189: // Read serviceAssemblyStatisticsDataListVersion attribute and
190: // ensure you store the right serviceAssemblyStatisticsDataListVersion
191: // of the report map list
192: // //////////////////////////////////////////////////////
193: this .serviceUnitStatisticsDataListVersion = attributes
194: .getValue(VERSION_KEY);
195: if ((serviceUnitStatisticsDataListVersion != null)
196: && (ServiceAssemblyStatisticsDataXMLConstants.UNIT_VERSION_VALUE
197: .equals(serviceUnitStatisticsDataListVersion))) {
198: if ((this .dataMap != null)
199: && (this .data != null)) {
200: this .unitDataList = new ArrayList<ServiceUnitStatisticsData>();
201: }
202:
203: }
204: } else {
205: // Invalid serviceAssemblyStatisticsDataListVersion.
206: // Not storing it
207: }
208: } else if (qName
209: .endsWith(ServiceAssemblyStatisticsDataXMLConstants.UNIT_STATISTICS_DATA_KEY)) {
210: // ELEMENT1 has an attribute, get it by name
211: // Do something with the attribute
212: if ((this .dataMap != null) && (this .data != null)
213: && (this .unitDataList != null)) {
214: this .unitData = new ServiceUnitStatisticsData();
215: }
216: } else if (qName
217: .endsWith(ServiceAssemblyStatisticsDataXMLConstants.SERVICE_UNIT_ENDPOINT_NAME_LIST_KEY)) {
218: // ELEMENT1 has an attribute, get it by name
219: // Do something with the attribute
220: if ((this .dataMap != null) && (this .data != null)
221: && (this .unitDataList != null)
222: && (this .unitData != null)) {
223: this .endpointNameList = new ArrayList<String /*endpointName*/>();
224: }
225: }
226: // Keep track of QNames
227: qNameStack.push(qName);
228: }
229: }
230:
231: /**
232: * Process the character report for current tag.
233: *
234: * @param ch
235: * are the element's characters.
236: * @param start
237: * is the start position in the character array.
238: * @param length
239: * is the number of characters to use from the character array.
240: * @throws org.xml.sax.SAXException
241: * is any SAX exception, possibly wrapping another exception.
242: */
243: public void characters(char[] ch, int start, int length)
244: throws SAXException {
245: String qName;
246: String chars = new String(ch, start, length);
247: // Get current QName
248: qName = (String) qNameStack.peek();
249: if (qName
250: .endsWith(ServiceAssemblyStatisticsDataXMLConstants.INSTANCE_NAME_KEY)) {
251: if ((this .dataMap != null) && (this .data != null)) {
252: this .data.setInstanceName(chars);
253: }
254: } else if (qName
255: .endsWith(ServiceAssemblyStatisticsDataXMLConstants.SERVICE_ASSEMBLY_NAME_KEY)) {
256: if ((this .dataMap != null) && (this .data != null)) {
257: this .data.setName(chars);
258: }
259: } else if (qName
260: .endsWith(ServiceAssemblyStatisticsDataXMLConstants.SERVICE_ASSEMBLY_LAST_STARTUP_TIME_KEY)) {
261: Date value = null;
262: // Mon Dec 03 02:19:09 PST 2007
263: // Make a SimpleDateFormat for toString()'s output. This
264: // has short (text) date, a space, short (text) month, a space,
265: // 2-digit date, a space, hour (0-23), minute, second, a space,
266: // short timezone, a final space, and a long year.
267: SimpleDateFormat format = new SimpleDateFormat(
268: "EEE MMM dd HH:mm:ss zzz yyyy");
269: try {
270: value = format.parse(chars);
271: } catch (ParseException e) {
272: }
273: if ((this .dataMap != null) && (this .data != null)) {
274: this .data.setLastStartupTime(value);
275: }
276: } else if (qName
277: .endsWith(ServiceAssemblyStatisticsDataXMLConstants.SERVICE_ASSEMBLY_SHUTDOWN_TIME_AVERAGE_KEY)) {
278: long value = Long.valueOf(chars).longValue();
279: if ((this .dataMap != null) && (this .data != null)) {
280: this .data.setShutdownTimeAverage(value);
281: }
282: } else if (qName
283: .endsWith(ServiceAssemblyStatisticsDataXMLConstants.SERVICE_ASSEMBLY_STARTUP_TIME_AVERAGE_KEY)) {
284: long value = Long.valueOf(chars).longValue();
285: if ((this .dataMap != null) && (this .data != null)) {
286: this .data.setStartupTimeAverage(value);
287: }
288: } else if (qName
289: .endsWith(ServiceAssemblyStatisticsDataXMLConstants.SERVICE_ASSEMBLY_STOP_TIME_AVERAGE_KEY)) {
290: Long value = Long.valueOf(chars).longValue();
291: if ((this .dataMap != null) && (this .data != null)) {
292: this .data.setStopTimeAverage(value);
293: }
294: } else if (qName
295: .endsWith(ServiceAssemblyStatisticsDataXMLConstants.SERVICE_ASSEMBLY_UP_TIME_KEY)) {
296: Long value = Long.valueOf(chars).longValue();
297: if ((this .dataMap != null) && (this .data != null)) {
298: this .data.setUpTime(value);
299: }
300: } else if (qName
301: .endsWith(ServiceAssemblyStatisticsDataXMLConstants.SERVICE_UNIT_NAME_KEY)) {
302: if ((this .dataMap != null) && (this .data != null)
303: && (this .unitDataList != null)
304: && (this .unitData != null)) {
305: this .unitData.setName(chars);
306: }
307: } else if (qName
308: .endsWith(ServiceAssemblyStatisticsDataXMLConstants.SERVICE_UNIT_SHUTDOWN_TIME_AVERAGE_KEY)) {
309: long value = Long.valueOf(chars).longValue();
310: if ((this .dataMap != null) && (this .data != null)
311: && (this .unitDataList != null)
312: && (this .unitData != null)) {
313: this .unitData.setShutdownTimeAverage(value);
314: }
315: } else if (qName
316: .endsWith(ServiceAssemblyStatisticsDataXMLConstants.SERVICE_UNIT_STARTUP_TIME_AVERAGE_KEY)) {
317: long value = Long.valueOf(chars).longValue();
318: if ((this .dataMap != null) && (this .data != null)
319: && (this .unitDataList != null)
320: && (this .unitData != null)) {
321: this .unitData.setStartupTimeAverage(value);
322: }
323: } else if (qName
324: .endsWith(ServiceAssemblyStatisticsDataXMLConstants.SERVICE_UNIT_STOP_TIME_AVERAGE_KEY)) {
325: long value = Long.valueOf(chars).longValue();
326: if ((this .dataMap != null) && (this .data != null)
327: && (this .unitDataList != null)
328: && (this .unitData != null)) {
329: this .unitData.setStopTimeAverage(value);
330: }
331: } else if (qName
332: .endsWith(ServiceAssemblyStatisticsDataXMLConstants.SERVICE_UNIT_ENDPOINT_NAME_KEY)) {
333: if ((this .dataMap != null) && (this .data != null)
334: && (this .unitDataList != null)
335: && (this .unitData != null)
336: && (this .endpointNameList != null)) {
337: this .endpointNameList.add(chars);
338: }
339: }
340: }
341:
342: /**
343: * Process the end element tag.
344: *
345: * @param uri
346: * is the Namespace URI, or the empty string if the element has
347: * no Namespace URI or if Namespace processing is not being
348: * performed.
349: * @param localName
350: * is the The local name (without prefix), or the empty string if
351: * Namespace processing is not being performed.
352: * @param qName
353: * is the qualified name (with prefix), or the empty string if
354: * qualified names are not available.
355: * @throws org.xml.sax.SAXException
356: * is any SAX exception, possibly wrapping another exception.
357: */
358: public void endElement(String uri, String localName, String qName)
359: throws SAXException {
360: // Pop QName, since we are done with it
361: qNameStack.pop();
362: if (qName != null) {
363: if (qName
364: .endsWith(ServiceAssemblyStatisticsDataXMLConstants.ASSEMBLY_STATISTICS_DATA_KEY)) {
365: // We have encountered the end of ELEMENT1
366: // ...
367: if ((this .dataMap != null) && (this .data != null)) {
368: this .dataMap.put(this .data.getInstanceName(),
369: this .data);
370: this .data = null;
371: this .unitDataList = null;
372: this .unitData = null;
373: this .endpointNameList = null;
374: }
375: } else if (qName
376: .endsWith(ServiceAssemblyStatisticsDataXMLConstants.UNIT_STATISTICS_DATA_LIST_KEY)) {
377: // We have encountered the end of ELEMENT1
378: // ...
379: if ((this .dataMap != null) && (this .data != null)
380: && (this .unitDataList != null)) {
381: this .data
382: .setServiceUnitStatisticsList(this .unitDataList);
383: this .unitDataList = null;
384: this .unitData = null;
385: this .endpointNameList = null;
386: }
387: } else if (qName
388: .endsWith(ServiceAssemblyStatisticsDataXMLConstants.UNIT_STATISTICS_DATA_KEY)) {
389: // We have encountered the end of ELEMENT1
390: // ...
391: if ((this .dataMap != null) && (this .data != null)
392: && (this .unitDataList != null)
393: && (this .unitData != null)) {
394: this .unitDataList.add(unitData);
395: this .unitData = null;
396: this .endpointNameList = null;
397: }
398: } else if (qName
399: .endsWith(ServiceAssemblyStatisticsDataXMLConstants.SERVICE_UNIT_ENDPOINT_NAME_LIST_KEY)) {
400: // We have encountered the end of ELEMENT1
401: // ...
402: if ((this .dataMap != null) && (this .data != null)
403: && (this .unitDataList != null)
404: && (this .unitData != null)
405: && (this .endpointNameList != null)) {
406: this .unitData
407: .setEndpointNameList(this .endpointNameList);
408: this .endpointNameList = null;
409: }
410: }
411: }
412: }
413:
414: /**
415: *
416: * @param rawXMLData
417: * @return
418: * @throws MalformedURLException
419: * @throws ParserConfigurationException
420: * @throws SAXException
421: * @throws URISyntaxException
422: * @throws IOException
423: */
424: public static Map<String /* instanceName */, ServiceAssemblyStatisticsData> parseFromXMLData(
425: String rawXMLData) throws MalformedURLException,
426: ParserConfigurationException, SAXException,
427: URISyntaxException, IOException {
428: // System.out.println("Parsing file: "+uriString);
429: // Get an instance of the SAX parser factory
430: SAXParserFactory factory = SAXParserFactory.newInstance();
431:
432: // Get an instance of the SAX parser
433: SAXParser saxParser = factory.newSAXParser();
434:
435: // Initialize the XML Document InputStream
436: Reader reader = new StringReader(rawXMLData);
437:
438: // Create an InputSource from the InputStream
439: InputSource inputSource = new InputSource(reader);
440:
441: // Parse the aspectInput XML document stream, using my event handler
442: ServiceAssemblyStatisticsDataReader parser = new ServiceAssemblyStatisticsDataReader();
443: saxParser.parse(inputSource, parser);
444:
445: return parser.getServiceAssemblyStatisticsDataMap();
446:
447: }
448:
449: /**
450: *
451: * @param fileName
452: * @return
453: * @throws MalformedURLException
454: * @throws ParserConfigurationException
455: * @throws SAXException
456: * @throws URISyntaxException
457: * @throws IOException
458: */
459: public static Map<String /* instanceName */, ServiceAssemblyStatisticsData> parseFromFile(
460: String fileName) throws MalformedURLException,
461: ParserConfigurationException, SAXException,
462: URISyntaxException, IOException {
463: File file = new File(fileName);
464: return parseFromFile(file);
465: }
466:
467: /**
468: *
469: * @param fileName
470: * @return
471: * @throws MalformedURLException
472: * @throws ParserConfigurationException
473: * @throws SAXException
474: * @throws URISyntaxException
475: * @throws IOException
476: */
477: public static Map<String /* instanceName */, ServiceAssemblyStatisticsData> parseFromFile(
478: File file) throws MalformedURLException,
479: ParserConfigurationException, SAXException,
480: URISyntaxException, IOException {
481:
482: // Get an instance of the SAX parser factory
483: SAXParserFactory factory = SAXParserFactory.newInstance();
484:
485: // Get an instance of the SAX parser
486: SAXParser saxParser = factory.newSAXParser();
487:
488: // Initialize the URI and XML Document InputStream
489: InputStream inputStream = new FileInputStream(file);
490:
491: // Create an InputSource from the InputStream
492: InputSource inputSource = new InputSource(inputStream);
493:
494: // Parse the aspectInput XML document stream, using my event handler
495: ServiceAssemblyStatisticsDataReader parser = new ServiceAssemblyStatisticsDataReader();
496: saxParser.parse(inputSource, parser);
497:
498: return parser.getServiceAssemblyStatisticsDataMap();
499: }
500:
501: /**
502: *
503: * @param uriString
504: * @return
505: * @throws MalformedURLException
506: * @throws ParserConfigurationException
507: * @throws SAXException
508: * @throws URISyntaxException
509: * @throws IOException
510: */
511: public static Map<String /* instanceName */, ServiceAssemblyStatisticsData> parseFromURI(
512: String uriString) throws MalformedURLException,
513: ParserConfigurationException, SAXException,
514: URISyntaxException, IOException {
515: URI uri = new URI(uriString);
516: return parseFromURI(uri);
517: }
518:
519: /**
520: *
521: * @param uri
522: * @return
523: * @throws MalformedURLException
524: * @throws ParserConfigurationException
525: * @throws SAXException
526: * @throws URISyntaxException
527: * @throws IOException
528: */
529: public static Map<String /* instanceName */, ServiceAssemblyStatisticsData> parseFromURI(
530: URI uri) throws MalformedURLException,
531: ParserConfigurationException, SAXException,
532: URISyntaxException, IOException {
533:
534: // Get an instance of the SAX parser factory
535: SAXParserFactory factory = SAXParserFactory.newInstance();
536:
537: // Get an instance of the SAX parser
538: SAXParser saxParser = factory.newSAXParser();
539:
540: // Initialize the URI and XML Document InputStream
541: InputStream inputStream = uri.toURL().openStream();
542:
543: // Create an InputSource from the InputStream
544: InputSource inputSource = new InputSource(inputStream);
545:
546: // Parse the aspectInput XML document stream, using my event handler
547: ServiceAssemblyStatisticsDataReader parser = new ServiceAssemblyStatisticsDataReader();
548: saxParser.parse(inputSource, parser);
549:
550: return parser.getServiceAssemblyStatisticsDataMap();
551: }
552:
553: /**
554: * @param args
555: */
556: public static void main(String[] args) {
557: String uri = "C:/test/schema/assemblystatistics/ServiceAssemblyStatisticsData.xml";
558: try {
559: Map<String /* instanceName */, ServiceAssemblyStatisticsData> map = null;
560: map = ServiceAssemblyStatisticsDataReader
561: .parseFromFile(uri);
562: for (String instanceName : map.keySet()) {
563: System.out.println(map.get(instanceName)
564: .getDisplayString());
565: }
566: } catch (MalformedURLException e) {
567: e.printStackTrace();
568: } catch (ParserConfigurationException e) {
569: e.printStackTrace();
570: } catch (SAXException e) {
571: e.printStackTrace();
572: } catch (URISyntaxException e) {
573: e.printStackTrace();
574: } catch (IOException e) {
575: e.printStackTrace();
576: }
577: }
578:
579: }
|