View Javadoc
1   /*
2     (C) Copyright IBM Corp. 2006, 2010
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   * 2763216    2009-04-14  blaschke-oss Code cleanup: visible spelling/grammar errors
19   * 3023145    2010-07-02  blaschke-oss CharValue uses # constructor instead of valueOf
20   */
21  
22  package org.metricshub.wbem.sblim.cimclient.internal.uri;
23  
24  /*-
25   * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
26   * WBEM Java Client
27   * ჻჻჻჻჻჻
28   * Copyright 2023 - 2025 MetricsHub
29   * ჻჻჻჻჻჻
30   * Licensed under the Apache License, Version 2.0 (the "License");
31   * you may not use this file except in compliance with the License.
32   * You may obtain a copy of the License at
33   *
34   *      http://www.apache.org/licenses/LICENSE-2.0
35   *
36   * Unless required by applicable law or agreed to in writing, software
37   * distributed under the License is distributed on an "AS IS" BASIS,
38   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
39   * See the License for the specific language governing permissions and
40   * limitations under the License.
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   * Class CharValue parses character value.
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  	 * charValue = // example: 'a' '\x32'
58  	 *
59  	 * @param pUriStr
60  	 * @param pThrow
61  	 * @return <code>Value</code> or <code>null</code> if parsing is failed and
62  	 *         <code>pThrow</code> is <code>false</code>
63  	 * @throws IllegalArgumentException
64  	 *             if parsing is failed and <code>pThrow</code> is
65  	 *             <code>true</code>
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  		// find closing '
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  		// next char must be ',' or nothing
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 		// FIXME: is it hexadecimal value?
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 	 * Parses a char16 value.
121 	 *
122 	 * @param pUriStr
123 	 * @return Value or null if parsing failed.
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 	 * getChar
135 	 *
136 	 * @return char
137 	 */
138 	public char get() {
139 		return this.iChar;
140 	}
141 
142 	/**
143 	 * getCharacter
144 	 *
145 	 * @return Character
146 	 */
147 	public Character getCharacter() {
148 		return Character.valueOf(this.iChar);
149 	}
150 
151 	/**
152 	 * @see java.lang.Object#toString()
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 	 * @see QuotedValue#toQuotedString()
164 	 */
165 	public String toQuotedString() {
166 		return "'" + toString() + '\'';
167 	}
168 
169 	/**
170 	 * @see Value#getTypeInfo()
171 	 */
172 	@Override
173 	public String getTypeInfo() {
174 		return MOF.DT_CHAR16;
175 	}
176 }