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: * @(#)PerformanceDataMapReader.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.HashMap;
042: import java.util.Map;
043: import java.util.Stack;
044:
045: import javax.xml.parsers.ParserConfigurationException;
046: import javax.xml.parsers.SAXParser;
047: import javax.xml.parsers.SAXParserFactory;
048:
049: import org.xml.sax.Attributes;
050: import org.xml.sax.InputSource;
051: import org.xml.sax.SAXException;
052: import org.xml.sax.helpers.DefaultHandler;
053:
054: import com.sun.esb.management.common.data.PerformanceData;
055:
056: /**
057: * Parses Performance Measurement data from an XML String or file
058: *
059: * @author graj
060: */
061: public class PerformanceDataMapReader extends DefaultHandler implements
062: PerformanceDataXMLConstants, Serializable {
063:
064: static final long serialVersionUID = -1L;
065:
066: // Private members needed to parse the XML document
067:
068: // keep track of parsing
069: private boolean parsingInProgress;
070:
071: // keep track of QName
072: private Stack<String> qNameStack = new Stack<String>();
073:
074: private PerformanceData data;
075:
076: private Map<String /* category */, PerformanceData> dataMap;
077:
078: private String performanceDataListVersion;
079:
080: /** Constructor - creates a new instance of PerformanceDataMapReader */
081: public PerformanceDataMapReader() {
082: }
083:
084: /**
085: * @return the dataMap
086: */
087: public Map<String /* category */, PerformanceData> getPerformanceDataMap() {
088: return this .dataMap;
089: }
090:
091: /**
092: * Start of document processing.
093: *
094: * @throws org.xml.sax.SAXException
095: * is any SAX exception, 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: *
105: * @throws org.xml.sax.SAXException
106: * is any SAX exception, possibly wrapping another exception.
107: */
108: public void endDocument() throws SAXException {
109: parsingInProgress = false;
110: // We have encountered the end of the document. Do any processing that
111: // is desired, for example dump all collected element2 values.
112:
113: }
114:
115: /**
116: * Process the new element.
117: *
118: * @param uri
119: * is the Namespace URI, or the empty string if the element has
120: * no Namespace URI or if Namespace processing is not being
121: * performed.
122: * @param localName
123: * is the The local name (without prefix), or the empty string if
124: * Namespace processing is not being performed.
125: * @param qName
126: * is the qualified name (with prefix), or the empty string if
127: * qualified names are not available.
128: * @param attributes
129: * is the attributes attached to the element. If there are no
130: * attributes, it shall be an empty Attributes object.
131: * @throws org.xml.sax.SAXException
132: * is any SAX exception, possibly wrapping another exception.
133: */
134: public void startElement(String uri, String localName,
135: String qName, Attributes attributes) throws SAXException {
136: if (qName != null) {
137: if (qName.endsWith(PERFORMANCE_MEASUREMENT_DATA_LIST_KEY)) {
138: // ELEMENT1 has an attribute, get it by name
139: // Do something with the attribute
140: if ((attributes != null)
141: && (attributes.getLength() > 0)) {
142: String namespace = attributes
143: .getValue(NAMESPACE_KEY);
144: ////////////////////////////////////////////////////////
145: // Read performanceDataListVersion attribute and ensure you store the right
146: // performanceDataListVersion of the data map list
147: ////////////////////////////////////////////////////////
148: this .performanceDataListVersion = attributes
149: .getValue(VERSION_KEY);
150: if ((performanceDataListVersion != null)
151: && (VERSION_VALUE
152: .equals(performanceDataListVersion))) {
153: this .dataMap = new HashMap<String /* category */, PerformanceData>();
154: } else {
155: // Invalid performanceDataListVersion. Not storing it
156: }
157: }
158:
159: } else if (qName.endsWith(PERFORMANCE_MEASUREMENT_DATA_KEY)) {
160: // ELEMENT1 has an attribute, get it by name
161: // Do something with the attribute
162: if (this .dataMap != null) {
163: this .data = new PerformanceData();
164: }
165: }
166: // Keep track of QNames
167: qNameStack.push(qName);
168: }
169: }
170:
171: /**
172: * Process the character data for current tag.
173: *
174: * @param ch
175: * are the element's characters.
176: * @param start
177: * is the start position in the character array.
178: * @param length
179: * is the number of characters to use from the character array.
180: * @throws org.xml.sax.SAXException
181: * is any SAX exception, 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(CATEGORY_KEY)) {
190: if (this .data != null) {
191: this .data.setCategory(chars);
192: }
193: } else if (qName.endsWith(ENDPOINT_KEY)) {
194: if (this .data != null) {
195: this .data.setEndpoint(chars);
196: }
197: } else if (qName.endsWith(SOURCE_CLASS_NAME_KEY)) {
198: if (this .data != null) {
199: this .data.setSourceClassName(chars);
200: }
201: } else if (qName.endsWith(NUMBER_OF_MEASUREMENT_OBJECTS_KEY)) {
202: if (this .data != null) {
203: int numberOfMeasurementObjects = Integer.valueOf(chars)
204: .intValue();
205: this .data
206: .setNumberOfMeasurementObjects(numberOfMeasurementObjects);
207: }
208: } else if (qName.endsWith(NUMBER_OF_MEASUREMENTS_KEY)) {
209: if (this .data != null) {
210: int numberOfMeasurements = Integer.valueOf(chars)
211: .intValue();
212: this .data.setNumberOfMeasurements(numberOfMeasurements);
213: }
214: } else if (qName.endsWith(AVERAGE_KEY)) {
215: if (this .data != null) {
216: double average = Double.valueOf(chars).doubleValue();
217: this .data.setAverage(average);
218: }
219: } else if (qName
220: .endsWith(AVERAGE_WITHOUT_FIRST_MEASUREMENT_KEY)) {
221: if (this .data != null) {
222: double averageWithoutFirstMeasurement = Double.valueOf(
223: chars).doubleValue();
224: this .data
225: .setAverageWithoutFirstMeasurement(averageWithoutFirstMeasurement);
226: }
227: } else if (qName.endsWith(FIRST_MEASUREMENT_TIME_KEY)) {
228: if (this .data != null) {
229: double firstMeasurementTime = Double.valueOf(chars)
230: .doubleValue();
231: this .data.setFirstMeasurementTime(firstMeasurementTime);
232: }
233: } else if (qName.endsWith(LOAD_KEY)) {
234: if (this .data != null) {
235: double load = Double.valueOf(chars).doubleValue();
236: this .data.setLoad(load);
237: }
238: } else if (qName.endsWith(MEDIAN_KEY)) {
239: if (this .data != null) {
240: double median = Double.valueOf(chars).doubleValue();
241: this .data.setMedian(median);
242: }
243: } else if (qName.endsWith(THROUGHPUT_KEY)) {
244: if (this .data != null) {
245: double throughput = Double.valueOf(chars).doubleValue();
246: this .data.setThroughput(throughput);
247: }
248: } else if (qName.endsWith(TIME_TAKEN_KEY)) {
249: if (this .data != null) {
250: double timeTaken = Double.valueOf(chars).doubleValue();
251: this .data.setTimeTaken(timeTaken);
252: }
253: } else if (qName.endsWith(TOTAL_TIME_KEY)) {
254: if (this .data != null) {
255: double totalTime = Double.valueOf(chars).doubleValue();
256: this .data.setTotalTime(totalTime);
257: }
258: }
259: }
260:
261: /**
262: * Process the end element tag.
263: *
264: * @param uri
265: * is the Namespace URI, or the empty string if the element has
266: * no Namespace URI or if Namespace processing is not being
267: * performed.
268: * @param localName
269: * is the The local name (without prefix), or the empty string if
270: * Namespace processing is not being performed.
271: * @param qName
272: * is the qualified name (with prefix), or the empty string if
273: * qualified names are not available.
274: * @throws org.xml.sax.SAXException
275: * is any SAX exception, possibly wrapping another exception.
276: */
277: public void endElement(String uri, String localName, String qName)
278: throws SAXException {
279: // Pop QName, since we are done with it
280: qNameStack.pop();
281: if (qName != null) {
282: if (qName.endsWith(PERFORMANCE_MEASUREMENT_DATA_KEY)) {
283: // We have encountered the end of ELEMENT1
284: // ...
285: if ((this .dataMap != null) && (this .data != null)) {
286: this .dataMap
287: .put(this .data.getCategory(), this .data);
288: this .data = null;
289: }
290: }
291: }
292: }
293:
294: /**
295: *
296: * @param rawXMLData
297: * @return
298: * @throws MalformedURLException
299: * @throws ParserConfigurationException
300: * @throws SAXException
301: * @throws URISyntaxException
302: * @throws IOException
303: */
304: public static Map<String /* category */, PerformanceData> parseFromXMLData(
305: String rawXMLData) throws MalformedURLException,
306: ParserConfigurationException, SAXException,
307: URISyntaxException, IOException {
308: // System.out.println("Parsing file: "+uriString);
309: // Get an instance of the SAX parser factory
310: SAXParserFactory factory = SAXParserFactory.newInstance();
311:
312: // Get an instance of the SAX parser
313: SAXParser saxParser = factory.newSAXParser();
314:
315: // Initialize the XML Document InputStream
316: Reader reader = new StringReader(rawXMLData);
317:
318: // Create an InputSource from the InputStream
319: InputSource inputSource = new InputSource(reader);
320:
321: // Parse the aspectInput XML document stream, using my event handler
322: PerformanceDataMapReader parser = new PerformanceDataMapReader();
323: saxParser.parse(inputSource, parser);
324:
325: return parser.getPerformanceDataMap();
326:
327: }
328:
329: /**
330: *
331: * @param fileName
332: * @return
333: * @throws MalformedURLException
334: * @throws ParserConfigurationException
335: * @throws SAXException
336: * @throws URISyntaxException
337: * @throws IOException
338: */
339: public static Map<String /* category */, PerformanceData> parseFromFile(
340: String fileName) throws MalformedURLException,
341: ParserConfigurationException, SAXException,
342: URISyntaxException, IOException {
343: File file = new File(fileName);
344: return parseFromFile(file);
345: }
346:
347: /**
348: *
349: * @param fileName
350: * @return
351: * @throws MalformedURLException
352: * @throws ParserConfigurationException
353: * @throws SAXException
354: * @throws URISyntaxException
355: * @throws IOException
356: */
357: public static Map<String /* category */, PerformanceData> parseFromFile(
358: File file) throws MalformedURLException,
359: ParserConfigurationException, SAXException,
360: URISyntaxException, IOException {
361:
362: // Get an instance of the SAX parser factory
363: SAXParserFactory factory = SAXParserFactory.newInstance();
364:
365: // Get an instance of the SAX parser
366: SAXParser saxParser = factory.newSAXParser();
367:
368: // Initialize the URI and XML Document InputStream
369: InputStream inputStream = new FileInputStream(file);
370:
371: // Create an InputSource from the InputStream
372: InputSource inputSource = new InputSource(inputStream);
373:
374: // Parse the aspectInput XML document stream, using my event handler
375: PerformanceDataMapReader parser = new PerformanceDataMapReader();
376: saxParser.parse(inputSource, parser);
377:
378: return parser.getPerformanceDataMap();
379: }
380:
381: /**
382: *
383: * @param uriString
384: * @return
385: * @throws MalformedURLException
386: * @throws ParserConfigurationException
387: * @throws SAXException
388: * @throws URISyntaxException
389: * @throws IOException
390: */
391: public static Map<String /* category */, PerformanceData> parseFromURI(
392: String uriString) throws MalformedURLException,
393: ParserConfigurationException, SAXException,
394: URISyntaxException, IOException {
395: URI uri = new URI(uriString);
396: return parseFromURI(uri);
397: }
398:
399: /**
400: *
401: * @param uri
402: * @return
403: * @throws MalformedURLException
404: * @throws ParserConfigurationException
405: * @throws SAXException
406: * @throws URISyntaxException
407: * @throws IOException
408: */
409: public static Map<String /* category */, PerformanceData> parseFromURI(
410: URI uri) throws MalformedURLException,
411: ParserConfigurationException, SAXException,
412: URISyntaxException, IOException {
413:
414: // Get an instance of the SAX parser factory
415: SAXParserFactory factory = SAXParserFactory.newInstance();
416:
417: // Get an instance of the SAX parser
418: SAXParser saxParser = factory.newSAXParser();
419:
420: // Initialize the URI and XML Document InputStream
421: InputStream inputStream = uri.toURL().openStream();
422:
423: // Create an InputSource from the InputStream
424: InputSource inputSource = new InputSource(inputStream);
425:
426: // Parse the aspectInput XML document stream, using my event handler
427: PerformanceDataMapReader parser = new PerformanceDataMapReader();
428: saxParser.parse(inputSource, parser);
429:
430: return parser.getPerformanceDataMap();
431: }
432:
433: /**
434: * @param args
435: */
436: public static void main(String[] args) {
437: // String uri = "C:/test/performance/instance.xml";
438: String uri = "C:/test/performance/PerformanceMeasurementDataList.xml";
439: try {
440: Map<String, PerformanceData> map = PerformanceDataMapReader
441: .parseFromFile(uri);
442: for (String category : map.keySet()) {
443: System.out
444: .println(map.get(category).getDisplayString());
445: }
446: } catch (MalformedURLException e) {
447: // TODO Auto-generated catch block
448: e.printStackTrace();
449: } catch (ParserConfigurationException e) {
450: // TODO Auto-generated catch block
451: e.printStackTrace();
452: } catch (SAXException e) {
453: // TODO Auto-generated catch block
454: e.printStackTrace();
455: } catch (URISyntaxException e) {
456: // TODO Auto-generated catch block
457: e.printStackTrace();
458: } catch (IOException e) {
459: // TODO Auto-generated catch block
460: e.printStackTrace();
461: }
462: }
463:
464: }
|