1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 package org.metricshub.wbem.sblim.cimclient.internal.cimxml.sax;
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 import java.util.ArrayList;
49 import java.util.logging.Level;
50 import org.metricshub.wbem.javax.cim.CIMObjectPath;
51 import org.metricshub.wbem.sblim.cimclient.internal.cimxml.sax.node.CIMNode;
52 import org.metricshub.wbem.sblim.cimclient.internal.cimxml.sax.node.Node;
53 import org.metricshub.wbem.sblim.cimclient.internal.cimxml.sax.node.NonVolatileIf;
54 import org.metricshub.wbem.sblim.cimclient.internal.logging.LogAndTraceBroker;
55 import org.xml.sax.Attributes;
56 import org.xml.sax.SAXException;
57 import org.xml.sax.helpers.DefaultHandler;
58
59
60
61
62
63 public class XMLDefaultHandlerImpl extends DefaultHandler {
64 private Node iRootNode;
65
66 private NodeStack iNodeStack = new NodeStack();
67
68 private NodePool iNodePool = new NodePool();
69
70 private StringBuffer iStrBuf;
71
72 private SAXSession iSession;
73
74 private boolean iAnyRoot;
75
76
77
78
79 static class NodeStack {
80 private ArrayList<Node> iAL = new ArrayList<Node>();
81
82
83
84
85
86
87 public void push(Node pNode) {
88 this.iAL.add(pNode);
89 }
90
91
92
93
94
95
96 public Node pop() {
97 if (this.iAL.size() == 0) return null;
98 return this.iAL.remove(this.iAL.size() - 1);
99 }
100
101
102
103
104
105
106 public Node peek() {
107 if (this.iAL.size() == 0) return null;
108 return this.iAL.get(this.iAL.size() - 1);
109 }
110 }
111
112
113
114
115
116
117
118
119
120
121 public XMLDefaultHandlerImpl(SAXSession pSession, boolean pAnyRoot) {
122 this.iSession = pSession;
123 this.iAnyRoot = pAnyRoot;
124 }
125
126
127
128
129
130
131
132
133
134
135
136 public XMLDefaultHandlerImpl(CIMObjectPath pLocalPath, boolean pAnyRoot) {
137 this(new SAXSession(pLocalPath), pAnyRoot);
138 }
139
140
141
142
143
144
145
146
147 public XMLDefaultHandlerImpl(CIMObjectPath pLocalPath) {
148 this(pLocalPath, false);
149 }
150
151
152
153
154 public XMLDefaultHandlerImpl() {
155 this((CIMObjectPath) null, false);
156 }
157
158
159
160
161
162 @Override
163 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
164 this.iStrBuf = null;
165 String nodeNameEnum = NodeFactory.getEnum(qName);
166 if (nodeNameEnum == null) {
167 LogAndTraceBroker
168 .getBroker()
169 .trace(Level.FINEST, "Ignoring unrecognized starting CIM-XML element found during parsing: " + qName);
170 return;
171 }
172
173 Node parentNode = getPeekNode();
174 if (parentNode == null) {
175 if (!this.iAnyRoot && nodeNameEnum != NodeConstIf.CIM) throw new SAXException(
176 "First node of CIM-XML document must be CIM! " + nodeNameEnum + " is invalid!"
177 );
178 }
179 if (parentNode != null) parentNode.testChild(nodeNameEnum);
180
181 Node node = this.iNodePool.getNode(nodeNameEnum);
182
183 if (node == null) {
184 node = NodeFactory.getNodeInstance(nodeNameEnum);
185 }
186 if (parentNode != null) {
187 if (parentNode instanceof NonVolatileIf) ((NonVolatileIf) parentNode).addChild(node);
188 } else {
189 this.iRootNode = node;
190 }
191 this.iNodeStack.push(node);
192 node.init(attributes, this.iSession);
193 }
194
195 @Override
196 public void characters(char ch[], int start, int length) {
197 String str = new String(ch, start, length);
198
199 if (this.iStrBuf == null) {
200 this.iStrBuf = new StringBuffer(str);
201 } else {
202 this.iStrBuf.append(str);
203 }
204 }
205
206
207
208
209
210 @Override
211 public void endElement(String uri, String localName, String qName) throws SAXException {
212 String nodeNameEnum = NodeFactory.getEnum(qName);
213 if (nodeNameEnum == null) {
214 LogAndTraceBroker
215 .getBroker()
216 .trace(Level.FINEST, "Ignoring unrecognized ending CIM-XML element found during parsing: " + qName);
217 return;
218 }
219 Node peekNode = this.iNodeStack.pop();
220 try {
221
222 if (this.iStrBuf != null) {
223 peekNode.parseData(this.iStrBuf.toString());
224 this.iStrBuf = null;
225 }
226
227 peekNode.testCompletness();
228
229
230 Node parentNode = this.iNodeStack.peek();
231 if (parentNode != null) {
232 parentNode.childParsed(peekNode);
233 }
234 } finally {
235 peekNode.setCompleted();
236
237 if (!(peekNode instanceof NonVolatileIf)) {
238 this.iNodePool.addNode(peekNode);
239 }
240 }
241 }
242
243 @Override
244 public void endDocument() {
245 String msg = "hits : " + getNodePoolHits() + "\nmisses : " + getNodePoolMisses();
246 LogAndTraceBroker.getBroker().trace(Level.FINE, msg);
247 }
248
249
250
251
252
253
254 public CIMNode getCIMNode() {
255 return this.iRootNode instanceof CIMNode ? (CIMNode) this.iRootNode : null;
256 }
257
258
259
260
261
262
263 public Node getRootNode() {
264 return this.iRootNode;
265 }
266
267
268
269
270
271
272 public int getNodePoolHits() {
273 return this.iNodePool.getHitCnt();
274 }
275
276
277
278
279
280
281 public int getNodePoolMisses() {
282 return this.iNodePool.getMissCnt();
283 }
284
285 private Node getPeekNode() {
286 return this.iNodeStack.peek();
287 }
288 }