1 /* |
|
2 * Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the License "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 package com.nokia.helium.metadata.ant.types; |
|
19 |
|
20 import java.io.*; |
|
21 import java.util.*; |
|
22 import org.apache.log4j.Logger; |
|
23 import javax.xml.stream.XMLInputFactory; |
|
24 import javax.xml.stream.XMLStreamReader; |
|
25 import javax.xml.stream.events.XMLEvent; |
|
26 import javax.xml.stream.XMLStreamException; |
|
27 import org.apache.tools.ant.BuildException; |
|
28 |
|
29 |
|
30 /** |
|
31 * This Type abstract base class for all the types based on |
|
32 * XML processing. |
|
33 */ |
|
34 abstract class XMLLogMetaDataInput extends LogMetaDataInput { |
|
35 |
|
36 private Logger log = Logger.getLogger(XMLLogMetaDataInput.class); |
|
37 |
|
38 private XMLInputFactory xmlInputFactory; |
|
39 |
|
40 private XMLStreamReader xmlStreamReader; |
|
41 |
|
42 private boolean inParsing; |
|
43 |
|
44 |
|
45 /** |
|
46 * Constructor |
|
47 */ |
|
48 public XMLLogMetaDataInput() { |
|
49 inParsing = true; |
|
50 xmlInputFactory = XMLInputFactory.newInstance(); |
|
51 xmlInputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES,Boolean.TRUE); |
|
52 xmlInputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES,Boolean.FALSE); |
|
53 xmlInputFactory.setProperty(XMLInputFactory.IS_COALESCING , Boolean.TRUE); |
|
54 |
|
55 } |
|
56 |
|
57 /** |
|
58 * Closes the xml stream |
|
59 */ |
|
60 private void close() { |
|
61 try { |
|
62 if (xmlStreamReader != null) { |
|
63 xmlStreamReader.close(); |
|
64 xmlStreamReader = null; |
|
65 } |
|
66 } catch (XMLStreamException ex) { |
|
67 // We are Ignoring the errors as no need to fail the build. |
|
68 log.debug("Exception while closing xml stream", ex); |
|
69 } |
|
70 |
|
71 } |
|
72 |
|
73 /** |
|
74 * Function to check from the input stream if is there any entries available. |
|
75 * @param file for which the contents needs to be parsed for errors |
|
76 * @return true if there are any entry available otherwise false. |
|
77 */ |
|
78 boolean isEntryCreated(File currentFile) { |
|
79 boolean entryCreated = false; |
|
80 try { |
|
81 if (inParsing ) { |
|
82 if (xmlStreamReader == null) { |
|
83 log.debug("Processing file: " + currentFile); |
|
84 xmlStreamReader = xmlInputFactory.createXMLStreamReader( |
|
85 currentFile.toString(), new BufferedInputStream(new FileInputStream(currentFile))); |
|
86 } |
|
87 int eventType = xmlStreamReader.getEventType(); |
|
88 while (xmlStreamReader.hasNext()) { |
|
89 eventType = xmlStreamReader.next(); |
|
90 switch (eventType) { |
|
91 case XMLEvent.START_ELEMENT: |
|
92 entryCreated = startElement(xmlStreamReader); |
|
93 break; |
|
94 case XMLEvent.END_ELEMENT: |
|
95 entryCreated = endElement(xmlStreamReader); |
|
96 break; |
|
97 case XMLEvent.CHARACTERS: |
|
98 entryCreated = characters(xmlStreamReader); |
|
99 break; |
|
100 default: |
|
101 break; |
|
102 } |
|
103 if ( entryCreated) { |
|
104 return true; |
|
105 } |
|
106 } |
|
107 if (xmlStreamReader != null) { |
|
108 close(); |
|
109 } |
|
110 inParsing = false; |
|
111 } |
|
112 } catch (XMLStreamException ex) { |
|
113 log.debug("XMLStreamException in isEntryCreated" + ex); |
|
114 } catch (FileNotFoundException ex) { |
|
115 log.debug("FileNotFoundException in isEntryCreated" + ex); |
|
116 // CheckStyle:IllegalCatch OFF |
|
117 } catch (RuntimeException ex) { //have to catch this otherwise it crashes other parts of the code |
|
118 throw new BuildException("Failed during writing data to db: ", ex); |
|
119 } |
|
120 // CheckStyle:IllegalCatch ON |
|
121 return false; |
|
122 } |
|
123 |
|
124 |
|
125 /** |
|
126 * Function implemented by the subclasses to process the start event of xml stream callback. |
|
127 * @param streamReader: the input stream reader which contains the xml data to be parsed for recording data. |
|
128 * @return true if there are any element to be added to the database. |
|
129 */ |
|
130 abstract boolean startElement(XMLStreamReader streamReader) ; |
|
131 |
|
132 /** |
|
133 * Function implemented by the subclasses to process the end event of xml stream callback. |
|
134 * @param streamReader: the input stream reader which contains the xml data to be parsed for recording data. |
|
135 * @return true if there are any element to be added to the database. |
|
136 */ |
|
137 abstract boolean endElement(XMLStreamReader streamReader); |
|
138 |
|
139 /** |
|
140 * Function implemented by the subclasses to process the characters event of xml stream callback. |
|
141 * @param streamReader: the input stream reader which contains the xml data to be parsed for recording data. |
|
142 * @return true if there are any element to be added to the database. |
|
143 */ |
|
144 abstract boolean characters(XMLStreamReader streamReader); |
|
145 } |
|