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: * @(#)FrameworkStatisticsDataReader.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.FrameworkStatisticsData;
055:
056: /**
057: * @author graj
058: *
059: */
060: public class FrameworkStatisticsDataReader extends DefaultHandler
061: implements FrameworkStatisticsDataXMLConstants, Serializable {
062:
063: static final long serialVersionUID = -1L;
064:
065: // Private members needed to parse the XML document
066:
067: // keep track of parsing
068: private boolean parsingInProgress;
069:
070: // keep track of QName
071: private Stack<String> qNameStack = new Stack<String>();
072:
073: private String frameworkStatisticsDataListVersion;
074:
075: private FrameworkStatisticsData data;
076:
077: private Map<String /* instanceName */, FrameworkStatisticsData> dataMap;
078:
079: /**
080: * Constructor - creates a new instance of
081: * FrameworkStatisticsDataReader
082: */
083: public FrameworkStatisticsDataReader() {
084: }
085:
086: /**
087: * @return the dataMap
088: */
089: public Map<String /* instanceName */, FrameworkStatisticsData> getFrameworkStatisticsDataMap() {
090: return this .dataMap;
091: }
092:
093: /**
094: * Start of document processing.
095: *
096: * @throws org.xml.sax.SAXException
097: * is any SAX exception, possibly wrapping another exception.
098: */
099: public void startDocument() throws SAXException {
100: parsingInProgress = true;
101: qNameStack.removeAllElements();
102: }
103:
104: /**
105: * End of document processing.
106: *
107: * @throws org.xml.sax.SAXException
108: * is any SAX exception, possibly wrapping another exception.
109: */
110: public void endDocument() throws SAXException {
111: parsingInProgress = false;
112: // We have encountered the end of the document. Do any processing that
113: // is desired, for example dump all collected element2 values.
114:
115: }
116:
117: /**
118: * Process the new element.
119: *
120: * @param uri
121: * is the Namespace URI, or the empty string if the element has
122: * no Namespace URI or if Namespace processing is not being
123: * performed.
124: * @param localName
125: * is the The local name (without prefix), or the empty string if
126: * Namespace processing is not being performed.
127: * @param qName
128: * is the qualified name (with prefix), or the empty string if
129: * qualified names are not available.
130: * @param attributes
131: * is the attributes attached to the element. If there are no
132: * attributes, it shall be an empty Attributes object.
133: * @throws org.xml.sax.SAXException
134: * is any SAX exception, possibly wrapping another exception.
135: */
136: public void startElement(String uri, String localName,
137: String qName, Attributes attributes) throws SAXException {
138: if (qName != null) {
139: if (qName.endsWith(FRAMEWORK_STATISTICS_DATA_LIST_KEY)) {
140: // ELEMENT1 has an attribute, get it by name
141: // Do something with the attribute
142: if ((attributes != null)
143: && (attributes.getLength() > 0)) {
144: String namespace = attributes
145: .getValue(NAMESPACE_KEY);
146: // //////////////////////////////////////////////////////
147: // Read performanceDataListVersion attribute and ensure you
148: // store the right
149: // performanceDataListVersion of the report map list
150: // //////////////////////////////////////////////////////
151: this .frameworkStatisticsDataListVersion = attributes
152: .getValue(VERSION_KEY);
153: if ((frameworkStatisticsDataListVersion != null)
154: && (VERSION_VALUE
155: .equals(frameworkStatisticsDataListVersion))) {
156: this .dataMap = new HashMap<String /* instanceName */, FrameworkStatisticsData>();
157: } else {
158: // Invalid frameworkStatisticsDataListVersion.
159: // Not storing it
160: }
161: }
162: } else if (qName.endsWith(FRAMEWORK_STATISTICS_DATA_KEY)) {
163: // ELEMENT1 has an attribute, get it by name
164: // Do something with the attribute
165: if (this .dataMap != null) {
166: this .data = new FrameworkStatisticsData();
167: }
168: }
169: // Keep track of QNames
170: qNameStack.push(qName);
171: }
172: }
173:
174: /**
175: * Process the character report for current tag.
176: *
177: * @param ch
178: * are the element's characters.
179: * @param start
180: * is the start position in the character array.
181: * @param length
182: * is the number of characters to use from the character array.
183: * @throws org.xml.sax.SAXException
184: * is any SAX exception, possibly wrapping another exception.
185: */
186: public void characters(char[] ch, int start, int length)
187: throws SAXException {
188: String qName;
189: String chars = new String(ch, start, length);
190: // Get current QName
191: qName = (String) qNameStack.peek();
192: if (qName.endsWith(INSTANCE_NAME_KEY)) {
193: if (this .data != null) {
194: this .data.setInstanceName(chars);
195: }
196: } else if (qName.endsWith(STARTUP_TIME_KEY)) {
197: if (this .data != null) {
198: long startupTime = Long.valueOf(chars).longValue();
199: this .data.setStartupTime(startupTime);
200: }
201: } else if (qName.endsWith(UP_TIME_KEY)) {
202: if (this .data != null) {
203: long upTime = Long.valueOf(chars).longValue();
204: this .data.setUpTime(upTime);
205: }
206: }
207: }
208:
209: /**
210: * Process the end element tag.
211: *
212: * @param uri
213: * is the Namespace URI, or the empty string if the element has
214: * no Namespace URI or if Namespace processing is not being
215: * performed.
216: * @param localName
217: * is the The local name (without prefix), or the empty string if
218: * Namespace processing is not being performed.
219: * @param qName
220: * is the qualified name (with prefix), or the empty string if
221: * qualified names are not available.
222: * @throws org.xml.sax.SAXException
223: * is any SAX exception, possibly wrapping another exception.
224: */
225: public void endElement(String uri, String localName, String qName)
226: throws SAXException {
227: // Pop QName, since we are done with it
228: qNameStack.pop();
229: if (qName != null) {
230: if (qName.endsWith(FRAMEWORK_STATISTICS_DATA_KEY)) {
231: // We have encountered the end of ELEMENT1
232: // ...
233: if ((this .dataMap != null) && (this .data != null)) {
234: this .dataMap.put(this .data.getInstanceName(),
235: this .data);
236: this .data = null;
237: }
238: }
239: }
240: }
241:
242: /**
243: *
244: * @param rawXMLData
245: * @return
246: * @throws MalformedURLException
247: * @throws ParserConfigurationException
248: * @throws SAXException
249: * @throws URISyntaxException
250: * @throws IOException
251: */
252: public static Map<String /* instanceName */, FrameworkStatisticsData> parseFromXMLData(
253: String rawXMLData) throws MalformedURLException,
254: ParserConfigurationException, SAXException,
255: URISyntaxException, IOException {
256: // System.out.println("Parsing file: "+uriString);
257: // Get an instance of the SAX parser factory
258: SAXParserFactory factory = SAXParserFactory.newInstance();
259:
260: // Get an instance of the SAX parser
261: SAXParser saxParser = factory.newSAXParser();
262:
263: // Initialize the XML Document InputStream
264: Reader reader = new StringReader(rawXMLData);
265:
266: // Create an InputSource from the InputStream
267: InputSource inputSource = new InputSource(reader);
268:
269: // Parse the aspectInput XML document stream, using my event handler
270: FrameworkStatisticsDataReader parser = new FrameworkStatisticsDataReader();
271: saxParser.parse(inputSource, parser);
272:
273: return parser.getFrameworkStatisticsDataMap();
274:
275: }
276:
277: /**
278: *
279: * @param fileName
280: * @return
281: * @throws MalformedURLException
282: * @throws ParserConfigurationException
283: * @throws SAXException
284: * @throws URISyntaxException
285: * @throws IOException
286: */
287: public static Map<String /* instanceName */, FrameworkStatisticsData> parseFromFile(
288: String fileName) throws MalformedURLException,
289: ParserConfigurationException, SAXException,
290: URISyntaxException, IOException {
291: File file = new File(fileName);
292: return parseFromFile(file);
293: }
294:
295: /**
296: *
297: * @param fileName
298: * @return
299: * @throws MalformedURLException
300: * @throws ParserConfigurationException
301: * @throws SAXException
302: * @throws URISyntaxException
303: * @throws IOException
304: */
305: public static Map<String /* instanceName */, FrameworkStatisticsData> parseFromFile(
306: File file) throws MalformedURLException,
307: ParserConfigurationException, SAXException,
308: URISyntaxException, IOException {
309:
310: // Get an instance of the SAX parser factory
311: SAXParserFactory factory = SAXParserFactory.newInstance();
312:
313: // Get an instance of the SAX parser
314: SAXParser saxParser = factory.newSAXParser();
315:
316: // Initialize the URI and XML Document InputStream
317: InputStream inputStream = new FileInputStream(file);
318:
319: // Create an InputSource from the InputStream
320: InputSource inputSource = new InputSource(inputStream);
321:
322: // Parse the aspectInput XML document stream, using my event handler
323: FrameworkStatisticsDataReader parser = new FrameworkStatisticsDataReader();
324: saxParser.parse(inputSource, parser);
325:
326: return parser.getFrameworkStatisticsDataMap();
327: }
328:
329: /**
330: *
331: * @param uriString
332: * @return
333: * @throws MalformedURLException
334: * @throws ParserConfigurationException
335: * @throws SAXException
336: * @throws URISyntaxException
337: * @throws IOException
338: */
339: public static Map<String /* instanceName */, FrameworkStatisticsData> parseFromURI(
340: String uriString) throws MalformedURLException,
341: ParserConfigurationException, SAXException,
342: URISyntaxException, IOException {
343: URI uri = new URI(uriString);
344: return parseFromURI(uri);
345: }
346:
347: /**
348: *
349: * @param uri
350: * @return
351: * @throws MalformedURLException
352: * @throws ParserConfigurationException
353: * @throws SAXException
354: * @throws URISyntaxException
355: * @throws IOException
356: */
357: public static Map<String /* instanceName */, FrameworkStatisticsData> parseFromURI(
358: URI uri) 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 = uri.toURL().openStream();
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: FrameworkStatisticsDataReader parser = new FrameworkStatisticsDataReader();
376: saxParser.parse(inputSource, parser);
377:
378: return parser.getFrameworkStatisticsDataMap();
379: }
380:
381: /**
382: * @param args
383: */
384: public static void main(String[] args) {
385: String uri = "C:/test/schema/frameworkstatistics/FrameworkStatisticsData.xml";
386: try {
387: Map<String /* instanceName */, FrameworkStatisticsData> map = null;
388: map = FrameworkStatisticsDataReader.parseFromFile(uri);
389: for (String instanceName : map.keySet()) {
390: System.out.println(map.get(instanceName)
391: .getDisplayString());
392: }
393: } catch (MalformedURLException e) {
394: e.printStackTrace();
395: } catch (ParserConfigurationException e) {
396: e.printStackTrace();
397: } catch (SAXException e) {
398: e.printStackTrace();
399: } catch (URISyntaxException e) {
400: e.printStackTrace();
401: } catch (IOException e) {
402: e.printStackTrace();
403: }
404: }
405:
406: }
|