View Javadoc
1   /*
2     (C) Copyright IBM Corp. 2006, 2009
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   */
19  
20  package org.metricshub.wbem.sblim.cimclient.internal.uri;
21  
22  /*-
23   * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
24   * WBEM Java Client
25   * ჻჻჻჻჻჻
26   * Copyright 2023 - 2025 MetricsHub
27   * ჻჻჻჻჻჻
28   * Licensed under the Apache License, Version 2.0 (the "License");
29   * you may not use this file except in compliance with the License.
30   * You may obtain a copy of the License at
31   *
32   *      http://www.apache.org/licenses/LICENSE-2.0
33   *
34   * Unless required by applicable law or agreed to in writing, software
35   * distributed under the License is distributed on an "AS IS" BASIS,
36   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
37   * See the License for the specific language governing permissions and
38   * limitations under the License.
39   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
40   */
41  
42  import org.metricshub.wbem.sblim.cimclient.internal.util.MOF;
43  import org.metricshub.wbem.sblim.cimclient.internal.util.Util;
44  
45  /**
46   * Class UntypedStringValue parses an untyped string value.
47   */
48  public class StringValue extends Value implements QuotedValue {
49  	private static final int NORMAL = 0;
50  
51  	private static final int ESCAPED = 1;
52  
53  	private static final int CLOSED = 2;
54  
55  	/**
56  	 * Factory method for parsing quoted strings.
57  	 *
58  	 * @param pUriStr
59  	 * @return <code>Value</code> instance
60  	 * @throws IllegalArgumentException
61  	 *             if parsing failed
62  	 */
63  	public static Value parse(URIString pUriStr) throws IllegalArgumentException {
64  		URIString uriStr = pUriStr.deepCopy();
65  		if (uriStr.charAt(0) != '\"') {
66  			String msg = "Starting '\"' is missing!\n" + uriStr.markPosition();
67  			throw new IllegalArgumentException(msg);
68  		}
69  		int rIdx = 1;
70  		StringBuffer dstBuf = new StringBuffer();
71  		int state = NORMAL;
72  		while (rIdx < uriStr.length()) {
73  			char ch = uriStr.charAt(rIdx++);
74  			if (state == NORMAL) {
75  				if (ch == '\\') {
76  					state = ESCAPED;
77  					continue;
78  				}
79  				if (ch == '"') {
80  					state = CLOSED;
81  					break;
82  				}
83  			} else { // skip if Escaped
84  				state = NORMAL;
85  			}
86  			dstBuf.append(ch);
87  		}
88  		if (state != CLOSED) {
89  			String msg = "Closing '\"' is missing!\n" + uriStr.markPosition(rIdx);
90  			throw new IllegalArgumentException(msg);
91  		}
92  		uriStr.cutStarting(rIdx);
93  		// next character should be ',' or nothing
94  		if (uriStr.length() != 0 && uriStr.charAt(0) != ',') {
95  			String msg = "Next character should be ',' or end of string!\n" + uriStr.markPosition();
96  			throw new IllegalArgumentException(msg);
97  		}
98  		pUriStr.set(uriStr);
99  		return new StringValue(dstBuf.toString());
100 	}
101 
102 	private String iStr;
103 
104 	private StringValue(String pStr) {
105 		this.iStr = pStr;
106 	}
107 
108 	/**
109 	 * @see java.lang.Object#toString()
110 	 */
111 	@Override
112 	public String toString() {
113 		return this.iStr;
114 	}
115 
116 	/**
117 	 * @see QuotedValue#toQuotedString()
118 	 */
119 	public String toQuotedString() {
120 		return Util.quote(this.iStr);
121 	}
122 
123 	/**
124 	 * @see Value#getTypeInfo()
125 	 */
126 	@Override
127 	public String getTypeInfo() {
128 		return MOF.DT_STR;
129 	}
130 }