View Javadoc
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.sentrysoftware.wbem.sblim.cimclient.internal.cim;
24  
25  /*-
26   * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
27   * WBEM Java Client
28   * ჻჻჻჻჻჻
29   * Copyright (C) 2023 Sentry Software
30   * ჻჻჻჻჻჻
31   * This program is free software: you can redistribute it and/or modify
32   * it under the terms of the GNU Lesser General Public License as
33   * published by the Free Software Foundation, either version 3 of the
34   * License, or (at your option) any later version.
35   *
36   * This program is distributed in the hope that it will be useful,
37   * but WITHOUT ANY WARRANTY; without even the implied warranty of
38   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
39   * GNU General Lesser Public License for more details.
40   *
41   * You should have received a copy of the GNU General Lesser Public
42   * License along with this program.  If not, see
43   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
44   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
45   */
46  
47  import java.io.IOException;
48  import java.io.StringReader;
49  
50  /**
51   * Class DTString helps parsing CIMDateTime Strings.
52   * 
53   */
54  public class DTStringReader {
55  
56  	private String iDateTimeStr;
57  
58  	private StringReader iReader;
59  
60  	private int iPos = 0;
61  
62  	private boolean iUnsignificant;
63  
64  	/**
65  	 * Ctor.
66  	 * 
67  	 * @param pDateTimeStr
68  	 */
69  	public DTStringReader(String pDateTimeStr) {
70  		this.iDateTimeStr = pDateTimeStr;
71  		this.iReader = new StringReader(pDateTimeStr);
72  	}
73  
74  	/**
75  	 * read
76  	 * 
77  	 * @param pLen
78  	 *            - number of characters to be read from the string
79  	 * @param pFieldName
80  	 *            - the name of the field which is to be read (e.g. year, month,
81  	 *            day ...)
82  	 * @param pAllowUnsignificant
83  	 * @return int
84  	 * @throws IllegalArgumentException
85  	 */
86  	public int read(int pLen, String pFieldName, boolean pAllowUnsignificant)
87  			throws IllegalArgumentException {
88  		char[] buf = new char[pLen];
89  		int read;
90  		try {
91  			read = this.iReader.read(buf);
92  			this.iPos += pLen;
93  		} catch (IOException e) {
94  			String msg = "Failed to read " + pFieldName + " field from " + this.iDateTimeStr + '!';
95  			throw new IllegalArgumentException(msg);
96  		}
97  		if (read != pLen) {
98  			String msg = "Length of " + pFieldName + " field should be " + pLen + " but only"
99  					+ read + " characters could be read!";
100 			throw new IllegalArgumentException(msg);
101 		}
102 		// Not significant check
103 		if (pAllowUnsignificant) {
104 			int cnt = 0;
105 			for (int i = 0; i < buf.length; i++)
106 				if (buf[i] == '*') ++cnt;
107 			if (cnt == buf.length) {
108 				this.iUnsignificant = true;
109 				return -1;
110 			}
111 		}
112 		this.iUnsignificant = false;
113 		String field = new String(buf);
114 		int res;
115 		try {
116 			res = Integer.parseInt(field);
117 		} catch (NumberFormatException e) {
118 			String msg = "Illegal " + pFieldName + " field \"" + field + "\" in \""
119 					+ this.iDateTimeStr + "\"!";
120 			throw new IllegalArgumentException(msg);
121 		}
122 		if (res < 0) throw new IllegalArgumentException("Negative value is not allowed for "
123 				+ pFieldName + " in " + this.iDateTimeStr + "!");
124 		// Java 7 parseInt began allowing plus sign
125 		if (field.indexOf('+') != -1) throw new IllegalArgumentException(
126 				"Plus sign is not allowed for " + pFieldName + " in " + this.iDateTimeStr + "!");
127 		return res;
128 	}
129 
130 	/**
131 	 * readAndCheck
132 	 * 
133 	 * @param pLen
134 	 *            - number of digits to read
135 	 * @param pFieldName
136 	 *            - the name of the field which is to be read (e.g. year, month,
137 	 *            day...)
138 	 * @param pMin
139 	 *            - the allowed minimum value (-1 is always allowed as not
140 	 *            significant)
141 	 * @param pMax
142 	 *            - the allowed maximum value
143 	 * @param pAllowUnsignificant
144 	 * @return int
145 	 * @throws IllegalArgumentException
146 	 */
147 	public int readAndCheck(int pLen, String pFieldName, int pMin, int pMax,
148 			boolean pAllowUnsignificant) throws IllegalArgumentException {
149 		int val = read(pLen, pFieldName, pAllowUnsignificant);
150 		if (pAllowUnsignificant && val == -1) return val;
151 		if (val < pMin || val > pMax) throw new IllegalArgumentException(pFieldName
152 				+ " must be between " + pMin + " and " + pMax + ", but " + val + " was read from "
153 				+ this.iDateTimeStr + " !");
154 		return val;
155 	}
156 
157 	/**
158 	 * read
159 	 * 
160 	 * @return a char, 0 if failed
161 	 */
162 	public char read() {
163 		try {
164 			int i = this.iReader.read();
165 			if (i > 0) {
166 				++this.iPos;
167 				return (char) i;
168 			}
169 			return 0;
170 		} catch (IOException e) {
171 			return 0;
172 		}
173 	}
174 
175 	/**
176 	 * read - Throws an IllegalArgumentException if the read character is not c.
177 	 * 
178 	 * @param c
179 	 *            - contains the character which should be read from the String.
180 	 * @throws IllegalArgumentException
181 	 */
182 	public void read(char c) throws IllegalArgumentException {
183 		if (read() != c) {
184 			String msg = "'" + c + "' expected at position " + getPos() + " in "
185 					+ this.iDateTimeStr + "!";
186 			throw new IllegalArgumentException(msg);
187 		}
188 	}
189 
190 	/**
191 	 * getPos
192 	 * 
193 	 * @return the position in the reader
194 	 */
195 	public int getPos() {
196 		return this.iPos;
197 	}
198 
199 	/**
200 	 * isUnsignificant
201 	 * 
202 	 * @return boolean
203 	 */
204 	public boolean isUnsignificant() {
205 		return this.iUnsignificant;
206 	}
207 
208 }