1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.metricshub.wbem.sblim.cimclient.internal.uri;
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 import java.util.regex.Matcher;
45 import java.util.regex.Pattern;
46 import org.metricshub.wbem.sblim.cimclient.internal.util.MOF;
47
48
49
50
51 public class CharValue extends Value implements QuotedValue {
52 private char iChar;
53
54 private static final Pattern pat = Pattern.compile("\\\\x([0-9a-fA-F]+)");
55
56
57
58
59
60
61
62
63
64
65
66
67 public static Value parse(URIString pUriStr, boolean pThrow) throws IllegalArgumentException {
68 URIString uriStr = pUriStr.deepCopy();
69 if (!uriStr.cutStarting('\'')) {
70 if (pThrow) {
71 String msg = "Starting \"'\" is not found!\n" + uriStr.markPosition();
72 throw new IllegalArgumentException(msg);
73 }
74 return null;
75 }
76
77 String charStr = uriStr.removeTill('\'', true);
78 if (charStr == null) {
79 if (pThrow) {
80 String msg = "Closing \"'\" is not found!\n" + uriStr.markPosition();
81 throw new IllegalArgumentException(msg);
82 }
83 return null;
84 }
85
86 if (uriStr.length() != 0 && uriStr.charAt(0) != ',') {
87 if (pThrow) {
88 String msg = "Character should be ',' or end of string!\n" + uriStr.markPosition();
89 throw new IllegalArgumentException(msg);
90 }
91 return null;
92 }
93 if (charStr.length() < 1) {
94 if (pThrow) {
95 String msg = "Empty character is unparseable!\n" + uriStr.markPosition();
96 throw new IllegalArgumentException(msg);
97 }
98 return null;
99 }
100 if (charStr.length() == 1) {
101 pUriStr.set(uriStr);
102 return new CharValue(charStr.charAt(0));
103 }
104 Matcher m = pat.matcher(charStr);
105 if (!m.matches()) {
106 if (pThrow) {
107 String msg = "Unparseable character string!\n" + uriStr.markPosition();
108 throw new IllegalArgumentException(msg);
109 }
110 return null;
111 }
112
113 String hexStr = m.group(1);
114 int charCode = Integer.parseInt(hexStr, 16);
115 pUriStr.set(uriStr);
116 return new CharValue((char) charCode);
117 }
118
119
120
121
122
123
124
125 public static Value parse(URIString pUriStr) {
126 return parse(pUriStr, false);
127 }
128
129 private CharValue(char pChar) {
130 this.iChar = pChar;
131 }
132
133
134
135
136
137
138 public char get() {
139 return this.iChar;
140 }
141
142
143
144
145
146
147 public Character getCharacter() {
148 return Character.valueOf(this.iChar);
149 }
150
151
152
153
154 @Override
155 public String toString() {
156 if (this.iChar < 32) {
157 return "\\x" + (int) this.iChar;
158 }
159 return Character.toString(this.iChar);
160 }
161
162
163
164
165 public String toQuotedString() {
166 return "'" + toString() + '\'';
167 }
168
169
170
171
172 @Override
173 public String getTypeInfo() {
174 return MOF.DT_CHAR16;
175 }
176 }