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