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