View Javadoc
1   /*
2     (C) Copyright IBM Corp. 2006, 2009
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-11-05  ebak         Make SBLIM client JSR48 compliant
16   * 2003590    2008-06-30  blaschke-oss Change licensing from CPL to EPL
17   * 2524131    2009-01-21  raman_arora  Upgrade client to JDK 1.5 (Phase 1)
18   */
19  
20  package org.metricshub.wbem.sblim.cimclient.internal.uri;
21  
22  /*-
23   * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
24   * WBEM Java Client
25   * ჻჻჻჻჻჻
26   * Copyright 2023 - 2025 MetricsHub
27   * ჻჻჻჻჻჻
28   * Licensed under the Apache License, Version 2.0 (the "License");
29   * you may not use this file except in compliance with the License.
30   * You may obtain a copy of the License at
31   *
32   *      http://www.apache.org/licenses/LICENSE-2.0
33   *
34   * Unless required by applicable law or agreed to in writing, software
35   * distributed under the License is distributed on an "AS IS" BASIS,
36   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
37   * See the License for the specific language governing permissions and
38   * limitations under the License.
39   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
40   */
41  
42  import org.metricshub.wbem.sblim.cimclient.internal.util.MOF;
43  
44  /**
45   * This class is responsible for parsing key values.
46   */
47  public abstract class Value {
48  
49  	/**
50  	 * Factory method which tries to parse an untyped value.
51  	 *
52  	 * @param pTyped
53  	 * @param pUriStr
54  	 * @return <code>Value</code> instance
55  	 * @throws IllegalArgumentException
56  	 *             if parsing failed.
57  	 */
58  	public static Value parse(boolean pTyped, URIString pUriStr) throws IllegalArgumentException {
59  		// TODO: tracing TRC.debug(uriStr.toString());
60  		if (pTyped) return parseTypedValue(pUriStr);
61  		URIString uriStr = pUriStr.deepCopy();
62  		Value value;
63  		try {
64  			StringValue strVal = (StringValue) StringValue.parse(uriStr);
65  
66  			// string like value
67  			// is this string an instance reference?
68  			try {
69  				URI ref = URI.parseRef(new URIString(strVal.toString()), false);
70  				value = new ReferenceValue(ref);
71  			} catch (IllegalArgumentException e) {
72  				if ((value = DateTimeValue.parse(strVal.toString())) == null) {
73  					// if not dateTimeValue, it is stringvalue
74  					value = strVal;
75  				}
76  			}
77  			pUriStr.set(uriStr);
78  			return value;
79  		} catch (IllegalArgumentException e) {
80  			// non string like value
81  			if (
82  				(value = IntegerValue.parse(uriStr)) != null ||
83  				(value = RealValue.parse(uriStr)) != null ||
84  				(value = BooleanValue.parse(uriStr)) != null ||
85  				(value = CharValue.parse(uriStr)) != null
86  			) {
87  				pUriStr.set(uriStr);
88  				return value;
89  			}
90  			String msg = "Failed to parse untyped value!\n" + uriStr.markPosition();
91  			throw new IllegalArgumentException(msg);
92  		}
93  	}
94  
95  	/**
96  	 * getTypeInfo
97  	 *
98  	 * @return the type description string of the Value.
99  	 */
100 	public abstract String getTypeInfo();
101 
102 	private static Value parseTypedValue(URIString pUriStr) throws IllegalArgumentException {
103 		URIString uriStr = pUriStr.deepCopy();
104 		int typeInfoPos = uriStr.getPos();
105 		String typeInfo = parseTypeInfo(uriStr);
106 		if (typeInfo == null) {
107 			String msg = "typeInfo expected!\n" + uriStr.markPosition();
108 			throw new IllegalArgumentException(msg);
109 		}
110 		int valuePos = uriStr.getPos();
111 		Value val;
112 		try {
113 			if (typeInfo.equalsIgnoreCase(MOF.DT_STR)) {
114 				val = StringValue.parse(uriStr);
115 			} else if (typeInfo.equalsIgnoreCase(MOF.REFERENCE)) {
116 				val = parseTypedReference(uriStr);
117 			} else if (typeInfo.equalsIgnoreCase(MOF.DT_DATETIME)) {
118 				val = parseTypedDateTime(uriStr);
119 			} else if (typeInfo.equalsIgnoreCase(MOF.DT_CHAR16)) {
120 				val = CharValue.parse(uriStr);
121 			} else if (typeInfo.equalsIgnoreCase(MOF.DT_BOOL)) {
122 				val = BooleanValue.parse(uriStr);
123 			} else if (typeInfo.equalsIgnoreCase(MOF.DT_SINT8)) {
124 				val = IntegerValue.parseSigned(uriStr, 8);
125 			} else if (typeInfo.equalsIgnoreCase(MOF.DT_SINT16)) {
126 				val = IntegerValue.parseSigned(uriStr, 16);
127 			} else if (typeInfo.equalsIgnoreCase(MOF.DT_SINT32)) {
128 				val = IntegerValue.parseSigned(uriStr, 32);
129 			} else if (typeInfo.equalsIgnoreCase(MOF.DT_SINT64)) {
130 				val = IntegerValue.parseSigned(uriStr, 64);
131 			} else if (typeInfo.equalsIgnoreCase(MOF.DT_UINT8)) {
132 				val = IntegerValue.parseUnsigned(uriStr, 8);
133 			} else if (typeInfo.equalsIgnoreCase(MOF.DT_UINT16)) {
134 				val = IntegerValue.parseUnsigned(uriStr, 16);
135 			} else if (typeInfo.equalsIgnoreCase(MOF.DT_UINT32)) {
136 				val = IntegerValue.parseUnsigned(uriStr, 32);
137 			} else if (typeInfo.equalsIgnoreCase(MOF.DT_UINT64)) {
138 				val = IntegerValue.parseUnsigned(uriStr, 64);
139 			} else if (typeInfo.equalsIgnoreCase(MOF.DT_REAL32)) {
140 				val = RealValue.parseFloat(uriStr);
141 			} else if (typeInfo.equalsIgnoreCase(MOF.DT_REAL64)) {
142 				val = RealValue.parseDouble(uriStr);
143 			} else {
144 				val = null;
145 			}
146 		} catch (IllegalArgumentException e) {
147 			String msg =
148 				"Failed to parse " +
149 				typeInfo +
150 				" value!\n" +
151 				uriStr.markPosition(valuePos) +
152 				"Nested message:\n" +
153 				e.getMessage();
154 			throw new IllegalArgumentException(msg);
155 		}
156 		if (val == null) {
157 			String msg = "Unknown type:" + typeInfo + "!\n" + uriStr.markPosition(typeInfoPos);
158 			throw new IllegalArgumentException(msg);
159 		}
160 		pUriStr.set(uriStr);
161 		return val;
162 	}
163 
164 	private static Value parseTypedReference(URIString pUriStr) throws IllegalArgumentException {
165 		Value strVal;
166 		int pos = pUriStr.getPos();
167 		try {
168 			strVal = StringValue.parse(pUriStr);
169 		} catch (IllegalArgumentException e) {
170 			String msg =
171 				"Failed to retrieve typed reference string!\n" +
172 				pUriStr.markPosition() +
173 				"Nested message is:\n" +
174 				e.getMessage();
175 			throw new IllegalArgumentException(msg);
176 		}
177 		URIString refUriStr = new URIString(strVal.toString());
178 		try {
179 			URI ref = URI.parseRef(refUriStr, true);
180 			return new ReferenceValue(ref);
181 		} catch (IllegalArgumentException e) {
182 			String msg =
183 				"Failed to parse typed reference value!\n" +
184 				pUriStr.markPosition(pos) +
185 				"Nested message is:\n" +
186 				e.getMessage();
187 			throw new IllegalArgumentException(msg);
188 		}
189 	}
190 
191 	private static Value parseTypedDateTime(URIString pUriStr) throws IllegalArgumentException {
192 		Value strVal;
193 		try {
194 			strVal = StringValue.parse(pUriStr);
195 		} catch (IllegalArgumentException e) {
196 			String msg =
197 				"Failed to retrieve typed datetime string!\n" +
198 				pUriStr.markPosition() +
199 				"Nested message is:\n" +
200 				e.getMessage();
201 			throw new IllegalArgumentException(msg);
202 		}
203 		return DateTimeValue.parse(strVal.toString(), true);
204 	}
205 
206 	private static String parseTypeInfo(URIString pUriStr) {
207 		URIString uriStr = pUriStr.deepCopy();
208 		if (!uriStr.cutStarting('(')) return null;
209 		String typeInfo = uriStr.removeTill(')', true, true);
210 		if (typeInfo == null) return null;
211 		pUriStr.set(uriStr);
212 		return typeInfo;
213 	}
214 }