View Javadoc
1   /*
2     (C) Copyright IBM Corp. 2006, 2012
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   * 1783288    2007-09-10  ebak         CIMClass.isAssociation() not working for retrieved classes.
20   * 2003590    2008-06-30  blaschke-oss Change licensing from CPL to EPL
21   * 2524131    2009-01-21  raman_arora  Upgrade client to JDK 1.5 (Phase 1)
22   * 2531371    2009-02-10  raman_arora  Upgrade client to JDK 1.5 (Phase 2)
23   * 2797550    2009-06-01  raman_arora  JSR48 compliance - add Java Generics
24   * 3500619    2012-03-16  blaschke-oss JSR48 1.0.0: CIMClass association/key clean up
25   */
26  
27  package org.metricshub.wbem.sblim.cimclient.internal.cimxml.sax.node;
28  
29  /*-
30   * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
31   * WBEM Java Client
32   * ჻჻჻჻჻჻
33   * Copyright 2023 - 2025 MetricsHub
34   * ჻჻჻჻჻჻
35   * Licensed under the Apache License, Version 2.0 (the "License");
36   * you may not use this file except in compliance with the License.
37   * You may obtain a copy of the License at
38   *
39   *      http://www.apache.org/licenses/LICENSE-2.0
40   *
41   * Unless required by applicable law or agreed to in writing, software
42   * distributed under the License is distributed on an "AS IS" BASIS,
43   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
44   * See the License for the specific language governing permissions and
45   * limitations under the License.
46   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
47   */
48  
49  import java.util.ArrayList;
50  import org.metricshub.wbem.javax.cim.CIMClass;
51  import org.metricshub.wbem.javax.cim.CIMClassProperty;
52  import org.metricshub.wbem.javax.cim.CIMMethod;
53  import org.metricshub.wbem.javax.cim.CIMObjectPath;
54  import org.metricshub.wbem.sblim.cimclient.GenericExts;
55  import org.metricshub.wbem.sblim.cimclient.internal.cimxml.LocalPathBuilder;
56  import org.metricshub.wbem.sblim.cimclient.internal.cimxml.sax.SAXSession;
57  import org.xml.sax.Attributes;
58  import org.xml.sax.SAXException;
59  
60  /**
61   * <pre>
62   * ELEMENT CLASS (QUALIFIER*, (PROPERTY | PROPERTY.ARRAY | PROPERTY.REFERENCE)*, METHOD*)
63   * ATTLIST CLASS
64   * %CIMName;
65   * %SuperClass;
66   * </pre>
67   */
68  public class ClassNode extends AbstractObjectNode {
69  	private String iName;
70  
71  	private String iSuperClass;
72  
73  	private QualifiedNodeHandler iQualiHandler;
74  
75  	private ArrayList<CIMClassProperty<?>> iCIMClassPropAL;
76  
77  	private boolean iKeyed;
78  
79  	private ArrayList<CIMMethod<?>> iCIMMethodAL;
80  
81  	/**
82  	 * Ctor.
83  	 */
84  	public ClassNode() {
85  		super(CLASS);
86  	}
87  
88  	@Override
89  	public void init(Attributes pAttribs, SAXSession pSession) throws SAXException {
90  		this.iLocalPath = pSession.getDefLocalPath();
91  		this.iQualiHandler = QualifiedNodeHandler.init(this.iQualiHandler);
92  		this.iCIMClassPropAL = GenericExts.initClearArrayList(this.iCIMClassPropAL);
93  		this.iKeyed = false;
94  		this.iCIMMethodAL = GenericExts.initClearArrayList(this.iCIMMethodAL);
95  		this.iName = getCIMName(pAttribs);
96  		this.iSuperClass = pAttribs.getValue("SUPERCLASS"); // not mandatory
97  	}
98  
99  	/**
100 	 * @param pData
101 	 */
102 	@Override
103 	public void parseData(String pData) {
104 		// no data
105 	}
106 
107 	private static final String[] ALLOWED_CHILDREN = { QUALIFIER, PROPERTY, PROPERTY_ARRAY, PROPERTY_REFERENCE, METHOD };
108 
109 	@Override
110 	public void testChild(String pNodeNameEnum) throws SAXException {
111 		for (int i = 0; i < ALLOWED_CHILDREN.length; i++) if (ALLOWED_CHILDREN[i] == pNodeNameEnum) return;
112 		throw new SAXException(getNodeName() + " node cannot have " + pNodeNameEnum + " child node!");
113 	}
114 
115 	@Override
116 	public void childParsed(Node pChild) {
117 		if (this.iQualiHandler.addQualifierNode(pChild)) return;
118 		if (pChild instanceof AbstractPropertyNode) {
119 			if (this.iCIMClassPropAL == null) this.iCIMClassPropAL = new ArrayList<CIMClassProperty<?>>();
120 			CIMClassProperty<Object> prop = ((AbstractPropertyNode) pChild).getCIMClassProperty();
121 			if (prop.isKey()) this.iKeyed = true;
122 			this.iCIMClassPropAL.add(prop);
123 		} else {
124 			if (this.iCIMMethodAL == null) this.iCIMMethodAL = new ArrayList<CIMMethod<?>>();
125 			this.iCIMMethodAL.add(((MethodNode) pChild).getCIMMethod());
126 		}
127 	}
128 
129 	@Override
130 	public void testCompletness() {
131 		// all child nodes are optional
132 	}
133 
134 	/**
135 	 * getCIMClass
136 	 *
137 	 * @return CIMClass
138 	 */
139 	public CIMClass getCIMClass() {
140 		/*
141 		 * ebak: this constructor can add localPath info to the class CIMClass(
142 		 * CIMObjectPath path, String superclass, CIMQualifier[] qualifiers,
143 		 * CIMClassProperty[] props, CIMMethod[] pMethods, boolean
144 		 * pIsAssociation, boolean pIsKeyed )
145 		 */
146 		return new CIMClass(
147 			LocalPathBuilder.build(this.iLocalPath, this.iName, null),
148 			this.iSuperClass,
149 			this.iQualiHandler.getQualis(),
150 			this.iCIMClassPropAL.toArray(EMPTY_PA),
151 			this.iCIMMethodAL.toArray(EMPTY_MA),
152 			this.iQualiHandler.isAssociation(),
153 			this.iKeyed
154 		);
155 	}
156 
157 	private static final CIMMethod<?>[] EMPTY_MA = new CIMMethod[0];
158 
159 	private static final CIMClassProperty<?>[] EMPTY_PA = new CIMClassProperty[0];
160 
161 	/**
162 	 * getCIMClass
163 	 *
164 	 * @param pObjPath
165 	 * @return CIMClass with the provided object path
166 	 */
167 	public CIMClass getCIMClass(CIMObjectPath pObjPath) {
168 		return new CIMClass(
169 			pObjPath,
170 			this.iSuperClass,
171 			this.iQualiHandler.getQualis(),
172 			this.iCIMClassPropAL.toArray(EMPTY_PA),
173 			this.iCIMMethodAL.toArray(EMPTY_MA),
174 			this.iQualiHandler.isAssociation(),
175 			this.iKeyed
176 		);
177 	}
178 
179 	/**
180 	 * @see ValueIf#getValue()
181 	 * @return CIMClass
182 	 */
183 	public Object getValue() {
184 		return getCIMClass();
185 	}
186 }