View Javadoc
1   /*
2     (C) Copyright IBM Corp. 2006, 2013
3   
4     THIS FILE IS PROVIDED UNDER THE TERMS OF THE ECLIPSE PUBLIC LICENSE
5     ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE
6     CONSTITUTES RECIPIENTS ACCEPTANCE OF THE AGREEMENT.
7   
8     You can obtain a current copy of the Eclipse Public License from
9     http://www.opensource.org/licenses/eclipse-1.0.php
10  
11    @author : Endre Bak, ebak@de.ibm.com
12   * 
13   * Flag       Date        Prog         Description
14   * -------------------------------------------------------------------------------
15   * 1565892    2006-12-04  ebak         Make SBLIM client JSR48 compliant
16   * 1663270    2007-02-19  ebak         Minor performance problems
17   * 1660756    2007-02-22  ebak         Embedded object support
18   * 1720707    2007-05-17  ebak         Conventional Node factory for CIM-XML SAX parser
19   * 2003590    2008-06-30  blaschke-oss Change licensing from CPL to EPL
20   * 2524131    2009-01-21  raman_arora  Upgrade client to JDK 1.5 (Phase 1)
21   * 2531371    2009-02-10  raman_arora  Upgrade client to JDK 1.5 (Phase 2)
22   * 2797550    2009-06-01  raman_arora  JSR48 compliance - add Java Generics
23   * 3598613    2013-01-11  blaschke-oss different data type in cim instance and cim object path
24   */
25  
26  package org.metricshub.wbem.sblim.cimclient.internal.cimxml.sax.node;
27  
28  /*-
29   * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
30   * WBEM Java Client
31   * ჻჻჻჻჻჻
32   * Copyright 2023 - 2025 MetricsHub
33   * ჻჻჻჻჻჻
34   * Licensed under the Apache License, Version 2.0 (the "License");
35   * you may not use this file except in compliance with the License.
36   * You may obtain a copy of the License at
37   *
38   *      http://www.apache.org/licenses/LICENSE-2.0
39   *
40   * Unless required by applicable law or agreed to in writing, software
41   * distributed under the License is distributed on an "AS IS" BASIS,
42   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
43   * See the License for the specific language governing permissions and
44   * limitations under the License.
45   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
46   */
47  
48  import java.util.ArrayList;
49  import org.metricshub.wbem.javax.cim.CIMInstance;
50  import org.metricshub.wbem.javax.cim.CIMObjectPath;
51  import org.metricshub.wbem.javax.cim.CIMProperty;
52  import org.metricshub.wbem.sblim.cimclient.internal.cim.CIMHelper;
53  import org.metricshub.wbem.sblim.cimclient.internal.cimxml.LocalPathBuilder;
54  import org.metricshub.wbem.sblim.cimclient.internal.cimxml.sax.SAXSession;
55  import org.metricshub.wbem.sblim.cimclient.internal.util.WBEMConfiguration;
56  import org.xml.sax.Attributes;
57  import org.xml.sax.SAXException;
58  
59  /**
60   * <pre>
61   * ELEMENT INSTANCE (QUALIFIER*, (PROPERTY | PROPERTY.ARRAY | PROPERTY.REFERENCE)*)
62   * ATTLIST INSTANCE
63   *   %ClassName;
64   *   xml:lang   NMTOKEN      #IMPLIED
65   * </pre>
66   */
67  public class InstanceNode extends AbstractObjectNode {
68  	private String iClassName;
69  
70  	/**
71  	 * FIXME: What to do with the qualifiers? JSR48 doesn't specify qualifiers
72  	 * for CIMInstance!
73  	 */
74  	private QualifiedNodeHandler iQualiHandler;
75  
76  	private ArrayList<CIMProperty<?>> iCIMPropAL;
77  
78  	/**
79  	 * Ctor.
80  	 */
81  	public InstanceNode() {
82  		super(INSTANCE);
83  	}
84  
85  	@Override
86  	public void init(Attributes pAttribs, SAXSession pSession) throws SAXException {
87  		this.iLocalPath = pSession.getDefLocalPath();
88  		this.iQualiHandler = new QualifiedNodeHandler();
89  		this.iCIMPropAL = null;
90  		this.iClassName = getClassName(pAttribs);
91  	}
92  
93  	/**
94  	 * @param pData
95  	 */
96  	@Override
97  	public void parseData(String pData) {
98  		// no data
99  	}
100 
101 	private static final String[] ALLOWED_CHILDREN = { QUALIFIER, PROPERTY, PROPERTY_ARRAY, PROPERTY_REFERENCE };
102 
103 	@Override
104 	public void testChild(String pNodeNameEnum) throws SAXException {
105 		for (int i = 0; i < ALLOWED_CHILDREN.length; i++) if (ALLOWED_CHILDREN[i] == pNodeNameEnum) return;
106 		throw new SAXException(getNodeName() + " node cannot have " + pNodeNameEnum + " child node!");
107 	}
108 
109 	@Override
110 	public void childParsed(Node pChild) {
111 		if (this.iQualiHandler.addQualifierNode(pChild)) return;
112 		if (this.iCIMPropAL == null) this.iCIMPropAL = new ArrayList<CIMProperty<?>>();
113 		this.iCIMPropAL.add(((AbstractPropertyNode) pChild).getCIMProperty());
114 	}
115 
116 	@Override
117 	public void testCompletness() {
118 		// all child nodes are optional
119 	}
120 
121 	/**
122 	 * getCIMInstance
123 	 *
124 	 * @return CIMInstance
125 	 */
126 	public CIMInstance getCIMInstance() {
127 		return new CIMInstance(LocalPathBuilder.build(this.iLocalPath, this.iClassName, null), getProps());
128 	}
129 
130 	/**
131 	 * getCIMInstance
132 	 *
133 	 * @param pObjPath
134 	 * @return CIMInstance with the provided object path
135 	 */
136 	public CIMInstance getCIMInstance(CIMObjectPath pObjPath) {
137 		if (
138 			WBEMConfiguration.getGlobalConfiguration().synchronizeNumericKeyDataTypes()
139 		) return CIMHelper.CIMInstanceWithSynchonizedNumericKeyDataTypes(pObjPath, getProps());
140 		return new CIMInstance(pObjPath, getProps());
141 	}
142 
143 	/**
144 	 * @see ValueIf#getValue()
145 	 * @return CIMInstance
146 	 */
147 	public Object getValue() {
148 		return getCIMInstance();
149 	}
150 
151 	private static final CIMProperty<?>[] EMPTY_PA = new CIMProperty[0];
152 
153 	private CIMProperty<?>[] getProps() {
154 		if (this.iCIMPropAL == null) return null;
155 		return this.iCIMPropAL.toArray(EMPTY_PA);
156 	}
157 }