1 /*
2 (C) Copyright IBM Corp. 2006, 2012
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-10-12 ebak Make SBLIM client JSR48 compliant
16 * 1678807 2007-03-12 ebak Minor CIMDateTime suggestions
17 * 2003590 2008-06-30 blaschke-oss Change licensing from CPL to EPL
18 * 2524131 2009-01-21 raman_arora Upgrade client to JDK 1.5 (Phase 1)
19 * 2763216 2009-04-14 blaschke-oss Code cleanup: visible spelling/grammar errors
20 * 3526675 2012-05-14 blaschke-oss Unit test fails on Java 7
21 */
22
23 package org.metricshub.wbem.sblim.cimclient.internal.cim;
24
25 /*-
26 * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
27 * WBEM Java Client
28 * ჻჻჻჻჻჻
29 * Copyright 2023 - 2025 MetricsHub
30 * ჻჻჻჻჻჻
31 * Licensed under the Apache License, Version 2.0 (the "License");
32 * you may not use this file except in compliance with the License.
33 * You may obtain a copy of the License at
34 *
35 * http://www.apache.org/licenses/LICENSE-2.0
36 *
37 * Unless required by applicable law or agreed to in writing, software
38 * distributed under the License is distributed on an "AS IS" BASIS,
39 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
40 * See the License for the specific language governing permissions and
41 * limitations under the License.
42 * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
43 */
44
45 import java.io.IOException;
46 import java.io.StringReader;
47
48 /**
49 * Class DTString helps parsing CIMDateTime Strings.
50 *
51 */
52 public class DTStringReader {
53 private String iDateTimeStr;
54
55 private StringReader iReader;
56
57 private int iPos = 0;
58
59 private boolean iUnsignificant;
60
61 /**
62 * Ctor.
63 *
64 * @param pDateTimeStr
65 */
66 public DTStringReader(String pDateTimeStr) {
67 this.iDateTimeStr = pDateTimeStr;
68 this.iReader = new StringReader(pDateTimeStr);
69 }
70
71 /**
72 * read
73 *
74 * @param pLen
75 * - number of characters to be read from the string
76 * @param pFieldName
77 * - the name of the field which is to be read (e.g. year, month,
78 * day ...)
79 * @param pAllowUnsignificant
80 * @return int
81 * @throws IllegalArgumentException
82 */
83 public int read(int pLen, String pFieldName, boolean pAllowUnsignificant) throws IllegalArgumentException {
84 char[] buf = new char[pLen];
85 int read;
86 try {
87 read = this.iReader.read(buf);
88 this.iPos += pLen;
89 } catch (IOException e) {
90 String msg = "Failed to read " + pFieldName + " field from " + this.iDateTimeStr + '!';
91 throw new IllegalArgumentException(msg);
92 }
93 if (read != pLen) {
94 String msg =
95 "Length of " + pFieldName + " field should be " + pLen + " but only" + read + " characters could be read!";
96 throw new IllegalArgumentException(msg);
97 }
98 // Not significant check
99 if (pAllowUnsignificant) {
100 int cnt = 0;
101 for (int i = 0; i < buf.length; i++) if (buf[i] == '*') ++cnt;
102 if (cnt == buf.length) {
103 this.iUnsignificant = true;
104 return -1;
105 }
106 }
107 this.iUnsignificant = false;
108 String field = new String(buf);
109 int res;
110 try {
111 res = Integer.parseInt(field);
112 } catch (NumberFormatException e) {
113 String msg = "Illegal " + pFieldName + " field \"" + field + "\" in \"" + this.iDateTimeStr + "\"!";
114 throw new IllegalArgumentException(msg);
115 }
116 if (res < 0) throw new IllegalArgumentException(
117 "Negative value is not allowed for " + pFieldName + " in " + this.iDateTimeStr + "!"
118 );
119 // Java 7 parseInt began allowing plus sign
120 if (field.indexOf('+') != -1) throw new IllegalArgumentException(
121 "Plus sign is not allowed for " + pFieldName + " in " + this.iDateTimeStr + "!"
122 );
123 return res;
124 }
125
126 /**
127 * readAndCheck
128 *
129 * @param pLen
130 * - number of digits to read
131 * @param pFieldName
132 * - the name of the field which is to be read (e.g. year, month,
133 * day...)
134 * @param pMin
135 * - the allowed minimum value (-1 is always allowed as not
136 * significant)
137 * @param pMax
138 * - the allowed maximum value
139 * @param pAllowUnsignificant
140 * @return int
141 * @throws IllegalArgumentException
142 */
143 public int readAndCheck(int pLen, String pFieldName, int pMin, int pMax, boolean pAllowUnsignificant)
144 throws IllegalArgumentException {
145 int val = read(pLen, pFieldName, pAllowUnsignificant);
146 if (pAllowUnsignificant && val == -1) return val;
147 if (val < pMin || val > pMax) throw new IllegalArgumentException(
148 pFieldName +
149 " must be between " +
150 pMin +
151 " and " +
152 pMax +
153 ", but " +
154 val +
155 " was read from " +
156 this.iDateTimeStr +
157 " !"
158 );
159 return val;
160 }
161
162 /**
163 * read
164 *
165 * @return a char, 0 if failed
166 */
167 public char read() {
168 try {
169 int i = this.iReader.read();
170 if (i > 0) {
171 ++this.iPos;
172 return (char) i;
173 }
174 return 0;
175 } catch (IOException e) {
176 return 0;
177 }
178 }
179
180 /**
181 * read - Throws an IllegalArgumentException if the read character is not c.
182 *
183 * @param c
184 * - contains the character which should be read from the String.
185 * @throws IllegalArgumentException
186 */
187 public void read(char c) throws IllegalArgumentException {
188 if (read() != c) {
189 String msg = "'" + c + "' expected at position " + getPos() + " in " + this.iDateTimeStr + "!";
190 throw new IllegalArgumentException(msg);
191 }
192 }
193
194 /**
195 * getPos
196 *
197 * @return the position in the reader
198 */
199 public int getPos() {
200 return this.iPos;
201 }
202
203 /**
204 * isUnsignificant
205 *
206 * @return boolean
207 */
208 public boolean isUnsignificant() {
209 return this.iUnsignificant;
210 }
211 }