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   * 2763216    2009-04-14  blaschke-oss Code cleanup: visible spelling/grammar errors
22   * 2845211    2009-08-27  raman_arora  Pull Enumeration Feature (SAX Parser)
23   * 3511454    2012-03-27  blaschke-oss SAX nodes not reinitialized properly
24   *    2697    2012-10-30  blaschke-oss (I)MethodResponseNode allows ERROR with PARAMVALUE
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.CIMArgument;
51  import org.metricshub.wbem.sblim.cimclient.internal.cimxml.sax.SAXSession;
52  import org.metricshub.wbem.sblim.cimclient.internal.wbem.CIMError;
53  import org.xml.sax.Attributes;
54  import org.xml.sax.SAXException;
55  
56  /**
57   * <pre>
58   * ELEMENT IMETHODRESPONSE (ERROR|IRETURNVALUE?)
59   * ATTLIST IMETHODRESPONSE
60   *   %CIMName;
61   * </pre>
62   */
63  /**
64   * Class IMethodResponseNode is responsible for
65   *
66   */
67  public class IMethodResponseNode extends Node implements ErrorIf, RetValPipeIf, NonVolatileIf {
68  	private String iName;
69  
70  	private ErrorNode iErrorNode;
71  
72  	private IReturnValueNode iRetValNode;
73  
74  	private ArrayList<CIMArgument<Object>> iCIMArgAL;
75  
76  	private static final CIMArgument<?>[] EMPTY_ARG_A = new CIMArgument[0];
77  
78  	private boolean iHasError;
79  
80  	private boolean iHasRetVal;
81  
82  	/**
83  	 * Ctor.
84  	 */
85  	public IMethodResponseNode() {
86  		super(IMETHODRESPONSE);
87  	}
88  
89  	public void addChild(Node pChild) {
90  		if (pChild instanceof IReturnValueNode) {
91  			this.iRetValNode = (IReturnValueNode) pChild;
92  		} else if (pChild instanceof ErrorNode) {
93  			this.iErrorNode = (ErrorNode) pChild;
94  		}
95  	}
96  
97  	/**
98  	 * @param pSession
99  	 */
100 	@Override
101 	public void init(Attributes pAttribs, SAXSession pSession) throws SAXException {
102 		this.iName = getCIMName(pAttribs);
103 		this.iErrorNode = null;
104 		this.iRetValNode = null;
105 		if (this.iCIMArgAL != null) this.iCIMArgAL.clear();
106 		this.iHasError = false;
107 		this.iHasRetVal = false;
108 	}
109 
110 	/**
111 	 * @param pData
112 	 */
113 	@Override
114 	public void parseData(String pData) {
115 		// no data
116 	}
117 
118 	@Override
119 	public void testChild(String pNodeNameEnum) throws SAXException {
120 		if (pNodeNameEnum == ERROR) {
121 			String ownedNodeName;
122 			if (this.iHasRetVal) ownedNodeName = IRETURNVALUE; else if (this.iHasError) ownedNodeName = ERROR; else if (
123 				this.iCIMArgAL != null && this.iCIMArgAL.size() > 0
124 			) ownedNodeName = PARAMVALUE; else ownedNodeName = null;
125 			if (ownedNodeName != null) throw new SAXException(
126 				pNodeNameEnum +
127 				" child node is invalid for " +
128 				getNodeName() +
129 				" node, since it already has a " +
130 				ownedNodeName +
131 				" child node!"
132 			);
133 		} else if (pNodeNameEnum == IRETURNVALUE) {
134 			String ownedNodeName;
135 			if (this.iHasRetVal) ownedNodeName = IRETURNVALUE; else if (this.iHasError) ownedNodeName =
136 				ERROR; else ownedNodeName = null;
137 			if (ownedNodeName != null) throw new SAXException(
138 				pNodeNameEnum +
139 				" child node is invalid for " +
140 				getNodeName() +
141 				" node, since it already has a " +
142 				ownedNodeName +
143 				" child node!"
144 			);
145 		} else if (pNodeNameEnum == PARAMVALUE) {
146 			if (this.iHasError) throw new SAXException(
147 				pNodeNameEnum +
148 				" child node is invalid for " +
149 				getNodeName() +
150 				" node, since it already has an ERROR child node!"
151 			);
152 		} else throw new SAXException(getNodeName() + " node cannot have " + pNodeNameEnum + " child node!");
153 	}
154 
155 	/**
156 	 * @param pChild
157 	 */
158 	@Override
159 	public void childParsed(Node pChild) {
160 		if (pChild instanceof ErrorNode) {
161 			this.iHasError = true;
162 			this.iErrorNode = (ErrorNode) pChild;
163 		} else if (pChild instanceof IReturnValueNode) {
164 			this.iHasRetVal = true;
165 			this.iRetValNode = (IReturnValueNode) pChild;
166 		} else // Values of parameters should be stored in array
167 		if (pChild instanceof ParamValueNode) {
168 			if (this.iCIMArgAL == null) this.iCIMArgAL = new ArrayList<CIMArgument<Object>>();
169 			this.iCIMArgAL.add(((ParamValueNode) pChild).getCIMArgument());
170 		}
171 	}
172 
173 	@Override
174 	public void testCompletness() {
175 		// no mandatory child nodes
176 	}
177 
178 	public CIMError getCIMError() {
179 		return this.iErrorNode == null ? null : this.iErrorNode.getCIMError();
180 	}
181 
182 	public int getReturnValueCount() {
183 		return this.iRetValNode == null ? 0 : this.iRetValNode.getReturnValueCount();
184 	}
185 
186 	public Object readReturnValue() {
187 		return this.iRetValNode.readReturnValue();
188 	}
189 
190 	/**
191 	 * getName
192 	 *
193 	 * @return String
194 	 */
195 	public String getName() {
196 		return this.iName;
197 	}
198 
199 	/**
200 	 * getCIMArguments : returns the array of parsed parameters and their values
201 	 * : String name, CIMDataType type, Object value
202 	 *
203 	 * @return CIMArgument&lt;?&gt;[]
204 	 */
205 	public CIMArgument<?>[] getCIMArguments() {
206 		if (this.iCIMArgAL == null || this.iCIMArgAL.size() == 0) return null;
207 		return this.iCIMArgAL.toArray(EMPTY_ARG_A);
208 	}
209 }