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   *    2707    2013-11-12  blaschke-oss INSTANCENAME ignores KEYVALUE and VALUE.REFERENCE children
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.CIMObjectPath;
50  import org.metricshub.wbem.javax.cim.CIMProperty;
51  import org.metricshub.wbem.sblim.cimclient.internal.cimxml.LocalPathBuilder;
52  import org.metricshub.wbem.sblim.cimclient.internal.cimxml.sax.SAXSession;
53  import org.xml.sax.Attributes;
54  import org.xml.sax.SAXException;
55  
56  /**
57   * <pre>
58   * perhaps it's a documentation mistake: ELEMENT INSTANCENAME (KEYBINDING* | KEYVALUE? | VALUE.REFERENCE?)
59   * ELEMENT INSTANCENAME (KEYBINDING*)
60   * ATTLIST INSTANCENAME
61   *  %ClassName;
62   * </pre>
63   */
64  public class InstanceNameNode extends AbstractPathNode {
65  	private String iClassName;
66  
67  	private ArrayList<CIMProperty<?>> iCIMPropAL;
68  
69  	private CIMObjectPath iLocalPath;
70  
71  	private String iNodeName;
72  
73  	/**
74  	 * Ctor.
75  	 */
76  	public InstanceNameNode() {
77  		super(INSTANCENAME);
78  	}
79  
80  	@Override
81  	public void init(Attributes pAttribs, SAXSession pSession) throws SAXException {
82  		this.iLocalPath = pSession.getDefLocalPath();
83  		if (this.iCIMPropAL != null) this.iCIMPropAL.clear();
84  		this.iClassName = getClassName(pAttribs);
85  		this.iNodeName = null;
86  	}
87  
88  	/**
89  	 * @param pData
90  	 */
91  	@Override
92  	public void parseData(String pData) {
93  		// no data
94  	}
95  
96  	private static final String[] ALLOWED_CHILDREN = { KEYBINDING, KEYVALUE, VALUE_REFERENCE };
97  
98  	@Override
99  	public void testChild(String pNodeNameEnum) throws SAXException {
100 		for (int i = 0; i < ALLOWED_CHILDREN.length; i++) if (ALLOWED_CHILDREN[i] == pNodeNameEnum) {
101 			if (this.iNodeName != null && this.iNodeName != pNodeNameEnum) throw new SAXException(
102 				getNodeName() + " node cannot have " + pNodeNameEnum + " child node, it already has a " + this.iNodeName + "!"
103 			);
104 			if (pNodeNameEnum != KEYBINDING) {
105 				if (this.iNodeName != null) throw new SAXException(
106 					getNodeName() + " node can have only one " + pNodeNameEnum + " child node!"
107 				);
108 			}
109 			this.iNodeName = pNodeNameEnum;
110 			return;
111 		}
112 		throw new SAXException(getNodeName() + " node cannot have " + pNodeNameEnum + " child node!");
113 	}
114 
115 	@Override
116 	public void childParsed(Node pChild) {
117 		if (this.iCIMPropAL == null) this.iCIMPropAL = new ArrayList<CIMProperty<?>>();
118 		if (pChild instanceof KeyBindingNode) this.iCIMPropAL.add(
119 				((KeyBindingNode) pChild).getCIMProperty()
120 			); else this.iCIMPropAL.add(
121 				new CIMProperty<Object>(
122 					"",
123 					((AbstractScalarValueNode) pChild).getType(),
124 					((AbstractScalarValueNode) pChild).getValue(),
125 					true,
126 					false,
127 					null
128 				)
129 			);
130 	}
131 
132 	@Override
133 	public void testCompletness() {
134 		// no mandatory child nodes
135 	}
136 
137 	/**
138 	 * getClassName
139 	 *
140 	 * @return String
141 	 */
142 	public String getClassName() {
143 		return this.iClassName;
144 	}
145 
146 	private static final CIMProperty<?>[] EMPTY_PA = new CIMProperty[0];
147 
148 	/**
149 	 * getKeys
150 	 *
151 	 * @return CIMProperty[]
152 	 */
153 	public CIMProperty<?>[] getKeys() {
154 		if (this.iCIMPropAL == null || this.iCIMPropAL.size() == 0) return null;
155 		return this.iCIMPropAL.toArray(EMPTY_PA);
156 	}
157 
158 	public CIMObjectPath getCIMObjectPath() {
159 		/*
160 		 * CIMObjectPath(String objectName, String namespace, CIMProperty[]
161 		 * keys)
162 		 */
163 		return LocalPathBuilder.build(this.iLocalPath, this.iClassName, null, getKeys());
164 	}
165 }