1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.metricshub.wbem.sblim.cimclient.internal.uri;
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42 import org.metricshub.wbem.sblim.cimclient.internal.util.MOF;
43
44
45
46
47 public abstract class Value {
48
49
50
51
52
53
54
55
56
57
58 public static Value parse(boolean pTyped, URIString pUriStr) throws IllegalArgumentException {
59
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
67
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
74 value = strVal;
75 }
76 }
77 pUriStr.set(uriStr);
78 return value;
79 } catch (IllegalArgumentException e) {
80
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
97
98
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 }