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
27
28
29
30
31
32 package org.sentrysoftware.wbem.sblim.cimclient.internal.cimxml.sax;
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56 import java.io.ByteArrayInputStream;
57 import java.util.ArrayList;
58 import java.util.HashMap;
59
60 import org.sentrysoftware.wbem.javax.cim.CIMClass;
61 import org.sentrysoftware.wbem.javax.cim.CIMDataType;
62 import org.sentrysoftware.wbem.javax.cim.CIMDateTime;
63 import org.sentrysoftware.wbem.javax.cim.CIMDateTimeAbsolute;
64 import org.sentrysoftware.wbem.javax.cim.CIMDateTimeInterval;
65 import org.sentrysoftware.wbem.javax.cim.CIMInstance;
66 import org.sentrysoftware.wbem.javax.cim.CIMObjectPath;
67 import org.sentrysoftware.wbem.javax.cim.UnsignedInteger16;
68 import org.sentrysoftware.wbem.javax.cim.UnsignedInteger32;
69 import org.sentrysoftware.wbem.javax.cim.UnsignedInteger64;
70 import org.sentrysoftware.wbem.javax.cim.UnsignedInteger8;
71 import javax.xml.parsers.SAXParser;
72 import javax.xml.parsers.SAXParserFactory;
73
74 import org.sentrysoftware.wbem.sblim.cimclient.internal.cimxml.sax.node.ClassNode;
75 import org.sentrysoftware.wbem.sblim.cimclient.internal.cimxml.sax.node.InstanceNode;
76 import org.sentrysoftware.wbem.sblim.cimclient.internal.cimxml.sax.node.Node;
77 import org.sentrysoftware.wbem.sblim.cimclient.internal.cimxml.sax.node.ValueArrayNode;
78 import org.sentrysoftware.wbem.sblim.cimclient.internal.cimxml.sax.node.ValueNode;
79 import org.sentrysoftware.wbem.sblim.cimclient.internal.util.MOF;
80 import org.sentrysoftware.wbem.sblim.cimclient.internal.util.Util;
81 import org.sentrysoftware.wbem.sblim.cimclient.internal.util.WBEMConfiguration;
82 import org.xml.sax.SAXException;
83
84
85
86
87
88 public class CIMObjectFactory {
89
90
91
92
93
94
95
96
97
98
99 public static Object[] getEmbeddedObjA(CIMDataType pType, ValueArrayNode pValueArrayNode,
100 SAXSession pSession) throws SAXException {
101 if (pValueArrayNode == null) return null;
102 return getEmbeddedObjA(pType, (String[]) pValueArrayNode.getValue(), pSession);
103 }
104
105
106
107
108
109
110
111
112
113
114 public static Object[] getEmbeddedObjA(CIMDataType pType, String[] pValueStrA,
115 SAXSession pSession) throws SAXException {
116 embeddedObjTypeCheck(pType);
117 if (pValueStrA == null || pValueStrA.length == 0) return null;
118 CIMDataType type = null;
119 ArrayList<Object> objAL = new ArrayList<Object>(pValueStrA.length);
120 for (int i = 0; i < pValueStrA.length; i++) {
121 Object obj = parseEmbeddedObj(pValueStrA[i], pSession);
122 if (type == null) {
123 type = getCIMObjScalarType(obj, false);
124 } else {
125 CIMDataType type2 = getCIMObjScalarType(obj, false);
126 if (type2 != null && type != type2) throw new SAXException(
127 "Embedded Object array contains both Instance and Class objects. "
128 + "This is not handled!");
129 }
130 objAL.add(obj);
131 }
132 if (type == CIMDataType.OBJECT_T) {
133 return objAL.toArray(EMPTY_INST_A);
134 } else if (type == CIMDataType.CLASS_T) { return objAL.toArray(EMPTY_CLASS_A); }
135 return objAL.toArray(EMPTY_STR_A);
136 }
137
138
139
140
141
142
143
144
145
146
147 public static Object getEmbeddedObj(CIMDataType pType, String pValueStr, SAXSession pSession)
148 throws SAXException {
149 embeddedObjTypeCheck(pType);
150 return parseEmbeddedObj(pValueStr, pSession);
151 }
152
153
154
155
156
157
158
159
160
161
162
163 public static Object getEmbeddedObj(CIMDataType pType, Object pValObj, SAXSession pSession)
164 throws SAXException {
165 if (pValObj instanceof String) { return getEmbeddedObj(pType, (String) pValObj, pSession); }
166 return getEmbeddedObjA(pType, (String[]) pValObj, pSession);
167 }
168
169
170
171
172
173
174 public static Object[] getObjectArray(CIMDataType pType, ArrayList<Object> pAL) {
175 createValFactoryA();
176
177 ValueFactory factory = cValFactoryA[pType.getType()];
178 return factory.make(pAL);
179 }
180
181
182
183
184
185
186
187
188
189 public static Object getObject(CIMDataType pType, String pValueStr) throws SAXException {
190 if (pValueStr == null) return null;
191 createValFactoryA();
192 ValueFactory factory = cValFactoryA[pType.getType()];
193 try {
194 return factory.make(pValueStr);
195 } catch (NumberFormatException e) {
196 throw new SAXException(e);
197 }
198 }
199
200
201
202
203
204
205
206
207
208 public static Object getObject(CIMDataType pType, ValueNode pValueNode) throws SAXException {
209 if (pValueNode == null) return null;
210 return getObject(pType, (String) pValueNode.getValue());
211 }
212
213
214
215
216
217
218
219
220
221 public static Object getObject(CIMDataType pType, ValueArrayNode pValueArrayNode)
222 throws SAXException {
223 if (pValueArrayNode == null) return null;
224 ArrayList<Object> objAL = new ArrayList<Object>(pValueArrayNode.size());
225 for (int i = 0; i < pValueArrayNode.size(); i++)
226 objAL.add(getObject(pType, (String) pValueArrayNode.elementAt(i)));
227 return getObjectArray(pType, objAL);
228 }
229
230
231
232
233
234
235
236
237
238 public static CIMDataType getCIMObjScalarType(Object pObj, boolean pNullToString)
239 throws SAXException {
240 if (pObj == null) return pNullToString ? CIMDataType.STRING_T : null;
241 if (pObj instanceof CIMInstance) {
242 return CIMDataType.OBJECT_T;
243 } else if (pObj instanceof CIMClass) {
244 return CIMDataType.CLASS_T;
245 } else if (pObj instanceof String) { return CIMDataType.STRING_T; }
246 throw new SAXException(pObj.getClass().getName() + " is not a CIMObject!");
247 }
248
249
250
251
252
253
254
255
256 public static CIMDataType getCIMObjScalarType(Object pObj) throws SAXException {
257 return getCIMObjScalarType(pObj, true);
258 }
259
260
261
262
263
264
265
266
267 public static CIMDataType getCIMObjArrayType(Object pObj) throws SAXException {
268 return getCIMObjArrayType(pObj, true);
269 }
270
271
272
273
274
275
276
277
278
279
280 public static CIMDataType getCIMObjArrayType(Object pObj, boolean pNullToString)
281 throws SAXException {
282 if (pObj == null) return pNullToString ? CIMDataType.STRING_ARRAY_T : null;
283 if (pObj instanceof CIMInstance[]) {
284 return CIMDataType.OBJECT_ARRAY_T;
285 } else if (pObj instanceof CIMClass[]) {
286 return CIMDataType.CLASS_ARRAY_T;
287 } else if (pObj instanceof String[]) { return CIMDataType.STRING_ARRAY_T; }
288 throw new SAXException(pObj.getClass().getName() + " is not a CIMObject array!");
289 }
290
291
292
293
294
295
296
297
298 public static CIMDataType getType(String pTypeStr) throws SAXException {
299 if (pTypeStr == null) return null;
300 createTypeStrMap();
301 CIMDataType type = cTypeStrMap.get(pTypeStr);
302 if (type == null && !cTypeStrMap.containsKey(pTypeStr)) throw new SAXException(pTypeStr
303 + " is invalid PARAMTYPE!");
304 return type;
305 }
306
307 static final CIMInstance[] EMPTY_INST_A = new CIMInstance[0];
308
309 static final CIMClass[] EMPTY_CLASS_A = new CIMClass[0];
310
311 static final String[] EMPTY_STR_A = new String[0];
312
313 static final UnsignedInteger8[] EMPTY_UINT8_A = new UnsignedInteger8[0];
314
315 static final UnsignedInteger16[] EMPTY_UINT16_A = new UnsignedInteger16[0];
316
317 static final UnsignedInteger32[] EMPTY_UINT32_A = new UnsignedInteger32[0];
318
319 static final UnsignedInteger64[] EMPTY_UINT64_A = new UnsignedInteger64[0];
320
321 static final Byte[] EMPTY_BYTE_A = new Byte[0];
322
323 static final Short[] EMPTY_SHORT_A = new Short[0];
324
325 static final Integer[] EMPTY_INT_A = new Integer[0];
326
327 static final Long[] EMPTY_LONG_A = new Long[0];
328
329 static final Float[] EMPTY_FLOAT_A = new Float[0];
330
331 static final Double[] EMPTY_DOUBLE_A = new Double[0];
332
333 static final Character[] EMPTY_CHAR_A = new Character[0];
334
335 static final Boolean[] EMPTY_BOOL_A = new Boolean[0];
336
337 static final CIMDateTime[] EMPTY_DT_A = new CIMDateTime[0];
338
339 static final CIMObjectPath[] EMPTY_OP_A = new CIMObjectPath[0];
340
341 private static void embeddedObjTypeCheck(CIMDataType pType) throws SAXException {
342 if (pType.getType() != CIMDataType.STRING) throw new SAXException(
343 "TYPE attribute should be 'string' for EmbeddedObjects!");
344 }
345
346 private static Object parseEmbeddedObj(String pValueStr, SAXSession pSession)
347 throws SAXException {
348 if (pValueStr == null || pValueStr.length() == 0) return null;
349 XMLDefaultHandlerImpl ourHandler = new XMLDefaultHandlerImpl(pSession, true);
350
351 SAXParserFactory factory = SAXParserFactory.newInstance();
352 try {
353 SAXParser saxParser = factory.newSAXParser();
354 saxParser.parse(new ByteArrayInputStream(pValueStr.getBytes()), ourHandler);
355 } catch (SAXException se) {
356 throw se;
357 } catch (Exception e) {
358 throw new SAXException("Exception occurred during embedded object parsing!", e);
359 }
360 Node node = ourHandler.getRootNode();
361 if (node instanceof InstanceNode) { return ((InstanceNode) node).getCIMInstance(); }
362 if (node instanceof ClassNode) { return ((ClassNode) node).getCIMClass(); }
363 throw new SAXException(node.getNodeName()
364 + " root element is unexpected for Embedded Object XML String!");
365 }
366
367 private static HashMap<String, CIMDataType> cTypeStrMap;
368
369 private synchronized static void createTypeStrMap() {
370 if (cTypeStrMap != null) return;
371 cTypeStrMap = new HashMap<String, CIMDataType>();
372 cTypeStrMap.put(MOF.DT_UINT8, CIMDataType.UINT8_T);
373 cTypeStrMap.put(MOF.DT_UINT16, CIMDataType.UINT16_T);
374 cTypeStrMap.put(MOF.DT_UINT32, CIMDataType.UINT32_T);
375 cTypeStrMap.put(MOF.DT_UINT64, CIMDataType.UINT64_T);
376 cTypeStrMap.put(MOF.DT_SINT8, CIMDataType.SINT8_T);
377 cTypeStrMap.put(MOF.DT_SINT16, CIMDataType.SINT16_T);
378 cTypeStrMap.put(MOF.DT_SINT32, CIMDataType.SINT32_T);
379 cTypeStrMap.put(MOF.DT_SINT64, CIMDataType.SINT64_T);
380 cTypeStrMap.put(MOF.DT_REAL32, CIMDataType.REAL32_T);
381 cTypeStrMap.put(MOF.DT_REAL64, CIMDataType.REAL64_T);
382 cTypeStrMap.put(MOF.DT_CHAR16, CIMDataType.CHAR16_T);
383 cTypeStrMap.put(MOF.DT_STR, CIMDataType.STRING_T);
384 cTypeStrMap.put(MOF.DT_BOOL, CIMDataType.BOOLEAN_T);
385 cTypeStrMap.put(MOF.DT_DATETIME, CIMDataType.DATETIME_T);
386 cTypeStrMap.put(MOF.REFERENCE, new CIMDataType(""));
387 }
388
389 private interface ValueFactory {
390
391
392
393
394
395
396
397 Object make(String pStr);
398
399
400
401
402
403
404
405 Object[] make(ArrayList<Object> pAL);
406 }
407
408 private static ValueFactory[] cValFactoryA;
409
410 private static void putFactory(int pTypeCode, ValueFactory pFactory) {
411 cValFactoryA[pTypeCode] = pFactory;
412 }
413
414 private synchronized static void createValFactoryA() {
415 if (cValFactoryA != null) return;
416 cValFactoryA = new ValueFactory[64];
417
418 putFactory(CIMDataType.UINT8, new ValueFactory() {
419
420 public Object make(String pStr) {
421 return new UnsignedInteger8(pStr);
422 }
423
424 public Object[] make(ArrayList<Object> pAL) {
425 return pAL.toArray(EMPTY_UINT8_A);
426 }
427 });
428 putFactory(CIMDataType.UINT16, new ValueFactory() {
429
430 public Object make(String pStr) {
431 return new UnsignedInteger16(pStr);
432 }
433
434 public Object[] make(ArrayList<Object> pAL) {
435 return pAL.toArray(EMPTY_UINT16_A);
436 }
437 });
438 putFactory(CIMDataType.UINT32, new ValueFactory() {
439
440 public Object make(String pStr) {
441 return new UnsignedInteger32(pStr);
442 }
443
444 public Object[] make(ArrayList<Object> pAL) {
445 return pAL.toArray(EMPTY_UINT32_A);
446 }
447 });
448 putFactory(CIMDataType.UINT64, new ValueFactory() {
449
450 public Object make(String pStr) {
451 return new UnsignedInteger64(pStr);
452 }
453
454 public Object[] make(ArrayList<Object> pAL) {
455 return pAL.toArray(EMPTY_UINT64_A);
456 }
457 });
458
459 putFactory(CIMDataType.SINT8, new ValueFactory() {
460
461 public Object make(String pStr) {
462 return new Byte(pStr);
463 }
464
465 public Object[] make(ArrayList<Object> pAL) {
466 return pAL.toArray(EMPTY_BYTE_A);
467 }
468 });
469 putFactory(CIMDataType.SINT16, new ValueFactory() {
470
471 public Object make(String pStr) {
472 return new Short(pStr);
473 }
474
475 public Object[] make(ArrayList<Object> pAL) {
476 return pAL.toArray(EMPTY_SHORT_A);
477 }
478 });
479 putFactory(CIMDataType.SINT32, new ValueFactory() {
480
481 public Object make(String pStr) {
482 return new Integer(pStr);
483 }
484
485 public Object[] make(ArrayList<Object> pAL) {
486 return pAL.toArray(EMPTY_INT_A);
487 }
488 });
489 putFactory(CIMDataType.SINT64, new ValueFactory() {
490
491 public Object make(String pStr) {
492 return new Long(pStr);
493 }
494
495 public Object[] make(ArrayList<Object> pAL) {
496 return pAL.toArray(EMPTY_LONG_A);
497 }
498 });
499
500 putFactory(CIMDataType.REAL32, new ValueFactory() {
501
502 public Object make(String pStr) {
503 return new Float(pStr);
504 }
505
506 public Object[] make(ArrayList<Object> pAL) {
507 return pAL.toArray(EMPTY_FLOAT_A);
508 }
509 });
510 putFactory(CIMDataType.REAL64, new ValueFactory() {
511
512 public Object make(String pStr) {
513 if (WBEMConfiguration.getGlobalConfiguration().verifyJavaLangDoubleStrings()) {
514 if (Util.isBadDoubleString(pStr)) throw new IllegalArgumentException(
515 "Double value string hangs older JVMs!\n" + pStr);
516 }
517 return new Double(pStr);
518 }
519
520 public Object[] make(ArrayList<Object> pAL) {
521 return pAL.toArray(EMPTY_DOUBLE_A);
522 }
523 });
524
525 putFactory(CIMDataType.CHAR16, new ValueFactory() {
526
527 public Object make(String pStr) {
528 if (pStr == null || pStr.length() == 0) throw new IllegalArgumentException(
529 "Cannot make Character from empty String!");
530 return Character.valueOf(pStr.charAt(0));
531 }
532
533 public Object[] make(ArrayList<Object> pAL) {
534 return pAL.toArray(EMPTY_CHAR_A);
535 }
536 });
537
538 putFactory(CIMDataType.STRING, new ValueFactory() {
539
540 public Object make(String pStr) {
541 return pStr;
542 }
543
544 public Object[] make(ArrayList<Object> pAL) {
545 return pAL.toArray(EMPTY_STR_A);
546 }
547 });
548
549 putFactory(CIMDataType.BOOLEAN, new ValueFactory() {
550
551 public Object make(String pStr) {
552 return Boolean.valueOf(pStr);
553 }
554
555 public Object[] make(ArrayList<Object> pAL) {
556 return pAL.toArray(EMPTY_BOOL_A);
557 }
558 });
559
560 putFactory(CIMDataType.DATETIME, new ValueFactory() {
561
562 public Object make(String pStr) {
563 try {
564 return new CIMDateTimeAbsolute(pStr);
565 } catch (IllegalArgumentException eA) {
566 try {
567 return new CIMDateTimeInterval(pStr);
568 } catch (IllegalArgumentException eI) {
569 throw new IllegalArgumentException("CIMDataTimeAbsolute:" + eA.getMessage()
570 + "\nCIMDateTimeInterval:" + eI.getMessage());
571 }
572 }
573 }
574
575 public Object[] make(ArrayList<Object> pAL) {
576 return pAL.toArray(EMPTY_DT_A);
577 }
578 });
579
580 putFactory(CIMDataType.REFERENCE, new ValueFactory() {
581
582 public Object make(String pStr) {
583 return new CIMObjectPath(pStr);
584 }
585
586 public Object[] make(ArrayList<Object> pAL) {
587 return pAL.toArray(EMPTY_OP_A);
588 }
589 });
590 }
591
592 }