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   * 3293248    2011-05-03  blaschke-oss Support for CIM_ERROR instances within ERROR
24   *    2604    2013-07-01  blaschke-oss SAXException messages should contain node name
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.CIMInstance;
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 ERROR (INSTANCE*)
59   * ATTLIST ERROR
60   *   CODE CDATA #REQUIRED
61   *   DESCRIPTION CDATA #IMPLIED
62   * </pre>
63   */
64  public class ErrorNode extends Node implements ErrorIf {
65  	private int iCode;
66  
67  	private String iDesc;
68  
69  	private ArrayList<CIMInstance> iCIMInstAL;
70  
71  	/**
72  	 * Ctor.
73  	 */
74  	public ErrorNode() {
75  		super(ERROR);
76  	}
77  
78  	/**
79  	 * @param pSession
80  	 */
81  	@Override
82  	public void init(Attributes pAttribs, SAXSession pSession) throws SAXException {
83  		this.iCIMInstAL = null;
84  		String code = pAttribs.getValue("CODE");
85  		if (code == null) throw new SAXException(getNodeName() + " node must have a CODE attribute!");
86  		try {
87  			this.iCode = Integer.parseInt(code);
88  		} catch (NumberFormatException e) {
89  			throw new SAXException("Failed to parse CODE attribute in " + getNodeName() + " node!", e);
90  		}
91  		this.iDesc = pAttribs.getValue("DESCRIPTION");
92  	}
93  
94  	/**
95  	 * @param pData
96  	 */
97  	@Override
98  	public void parseData(String pData) {
99  		// no data
100 	}
101 
102 	@Override
103 	public void testChild(String pNodeNameEnum) throws SAXException {
104 		if (pNodeNameEnum != INSTANCE) throw new SAXException(
105 			getNodeName() + " node cannot have " + pNodeNameEnum + " child node!"
106 		);
107 	}
108 
109 	@Override
110 	public void childParsed(Node pChild) {
111 		if (this.iCIMInstAL == null) this.iCIMInstAL = new ArrayList<CIMInstance>();
112 		this.iCIMInstAL.add(((InstanceNode) pChild).getCIMInstance());
113 	}
114 
115 	@Override
116 	public void testCompletness() {
117 		// no mandatory child nodes
118 	}
119 
120 	private static final CIMInstance[] EMPTY_IA = new CIMInstance[0];
121 
122 	public CIMError getCIMError() {
123 		if (this.iCIMInstAL != null) {
124 			return new CIMError(this.iCode, this.iDesc, this.iCIMInstAL.toArray(EMPTY_IA));
125 		}
126 		return new CIMError(this.iCode, this.iDesc);
127 	}
128 }