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   * 2763216    2009-04-14  blaschke-oss Code cleanup: visible spelling/grammar errors
23   * 2797550    2009-06-01  raman_arora  JSR48 compliance - add Java Generics
24   * 2845211    2009-08-27  raman_arora  Pull Enumeration Feature (SAX Parser)
25   * 3511454    2012-03-27  blaschke-oss SAX nodes not reinitialized properly
26   *    2697    2012-10-30  blaschke-oss (I)MethodResponseNode allows ERROR with PARAMVALUE
27   */
28  
29  package org.metricshub.wbem.sblim.cimclient.internal.cimxml.sax.node;
30  
31  /*-
32   * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
33   * WBEM Java Client
34   * ჻჻჻჻჻჻
35   * Copyright 2023 - 2025 MetricsHub
36   * ჻჻჻჻჻჻
37   * Licensed under the Apache License, Version 2.0 (the "License");
38   * you may not use this file except in compliance with the License.
39   * You may obtain a copy of the License at
40   *
41   *      http://www.apache.org/licenses/LICENSE-2.0
42   *
43   * Unless required by applicable law or agreed to in writing, software
44   * distributed under the License is distributed on an "AS IS" BASIS,
45   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
46   * See the License for the specific language governing permissions and
47   * limitations under the License.
48   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
49   */
50  
51  import java.util.ArrayList;
52  import org.metricshub.wbem.javax.cim.CIMArgument;
53  import org.metricshub.wbem.sblim.cimclient.internal.cimxml.sax.SAXSession;
54  import org.metricshub.wbem.sblim.cimclient.internal.wbem.CIMError;
55  import org.xml.sax.Attributes;
56  import org.xml.sax.SAXException;
57  
58  /**
59   * <pre>
60   * ELEMENT METHODRESPONSE (ERROR|(RETURNVALUE?,PARAMVALUE*))
61   * ATTLIST METHODRESPONSE
62   *   %CIMName;
63   * </pre>
64   */
65  public class MethodResponseNode extends Node implements ErrorIf, RetValPipeIf, NonVolatileIf {
66  	private String iName;
67  
68  	private CIMError iError;
69  
70  	private Object iRetVal;
71  
72  	private ArrayList<CIMArgument<Object>> iCIMArgAL;
73  
74  	private boolean iHasError;
75  
76  	private boolean iHasRetVal;
77  
78  	/**
79  	 * Ctor.
80  	 */
81  	public MethodResponseNode() {
82  		super(METHODRESPONSE);
83  	}
84  
85  	/**
86  	 * @param pChild
87  	 */
88  	public void addChild(Node pChild) {
89  		// nothing to do
90  	}
91  
92  	/**
93  	 * @param pSession
94  	 */
95  	@Override
96  	public void init(Attributes pAttribs, SAXSession pSession) throws SAXException {
97  		this.iName = getCIMName(pAttribs);
98  		this.iError = null;
99  		this.iRetVal = null;
100 		if (this.iCIMArgAL != null) this.iCIMArgAL.clear();
101 		this.iHasError = false;
102 		this.iHasRetVal = false;
103 	}
104 
105 	/**
106 	 * @param pData
107 	 */
108 	@Override
109 	public void parseData(String pData) {
110 		// no data
111 	}
112 
113 	@Override
114 	public void testChild(String pNodeNameEnum) throws SAXException {
115 		if (pNodeNameEnum == ERROR) {
116 			String ownedNodeName;
117 			if (this.iHasRetVal) ownedNodeName = RETURNVALUE; else if (this.iHasError) ownedNodeName = ERROR; else if (
118 				this.iCIMArgAL != null && this.iCIMArgAL.size() > 0
119 			) ownedNodeName = PARAMVALUE; else ownedNodeName = null;
120 			if (ownedNodeName != null) throw new SAXException(
121 				pNodeNameEnum +
122 				" child node is invalid for " +
123 				getNodeName() +
124 				" node, since it already has a " +
125 				ownedNodeName +
126 				" child node!"
127 			);
128 		} else if (pNodeNameEnum == RETURNVALUE) {
129 			String ownedNodeName;
130 			if (this.iHasRetVal) ownedNodeName = RETURNVALUE; else if (this.iHasError) ownedNodeName =
131 				ERROR; else ownedNodeName = null;
132 			if (ownedNodeName != null) throw new SAXException(
133 				pNodeNameEnum +
134 				" child node is invalid for " +
135 				getNodeName() +
136 				" node, since it already has a " +
137 				ownedNodeName +
138 				" child node!"
139 			);
140 		} else if (pNodeNameEnum == PARAMVALUE) {
141 			if (this.iHasError) throw new SAXException(
142 				pNodeNameEnum +
143 				" child node is invalid for " +
144 				getNodeName() +
145 				" node, since it already has an ERROR child node!"
146 			);
147 		} else throw new SAXException(getNodeName() + " node cannot have " + pNodeNameEnum + " child node!");
148 	}
149 
150 	@Override
151 	public void childParsed(Node pChild) {
152 		if (pChild instanceof ErrorNode) {
153 			this.iHasError = true;
154 			this.iError = ((ErrorNode) pChild).getCIMError();
155 		} else if (pChild instanceof ReturnValueNode) {
156 			this.iHasRetVal = true;
157 			this.iRetVal = ((ReturnValueNode) pChild).getValue();
158 		} else if (pChild instanceof ParamValueNode) {
159 			if (this.iCIMArgAL == null) this.iCIMArgAL = new ArrayList<CIMArgument<Object>>();
160 			this.iCIMArgAL.add(((ParamValueNode) pChild).getCIMArgument());
161 		}
162 	}
163 
164 	@Override
165 	public void testCompletness() {
166 		// no mandatory child
167 	}
168 
169 	public CIMError getCIMError() {
170 		return this.iError;
171 	}
172 
173 	private static final CIMArgument<?>[] EMPTY_ARG_A = new CIMArgument[0];
174 
175 	/**
176 	 * getCIMArguments : returns the array of parsed parameters and their values
177 	 * : String name, CIMDataType type, Object value
178 	 *
179 	 * @return CIMArgument&lt;?&gt;[]
180 	 */
181 	public CIMArgument<?>[] getCIMArguments() {
182 		if (this.iCIMArgAL == null || this.iCIMArgAL.size() == 0) return null;
183 		return this.iCIMArgAL.toArray(EMPTY_ARG_A);
184 	}
185 
186 	public int getReturnValueCount() {
187 		return this.iRetVal == null ? 0 : 1;
188 	}
189 
190 	public Object readReturnValue() {
191 		Object val = this.iRetVal;
192 		this.iRetVal = null;
193 		return val;
194 	}
195 
196 	/**
197 	 * getName
198 	 *
199 	 * @return String
200 	 */
201 	public String getName() {
202 		return this.iName;
203 	}
204 }