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 java.util.regex.Pattern;
43  
44  /**
45   * key_name "=" key_value
46   */
47  public class KeyValuePair {
48  	private static final Pattern KEYNAMEPAT = Pattern.compile("^([A-Za-z][0-9A-Za-z_]*).*");
49  
50  	/**
51  	 * Tries to get an <code>KeyValuePair</code> from the passed
52  	 * <code>pUriStr</code>.
53  	 *
54  	 * @param pTyped
55  	 * @param pUriStr
56  	 * @return an <code>KeyValuePair</code> or <code>null</code> if failed.
57  	 * @throws IllegalArgumentException
58  	 */
59  	public static KeyValuePair parse(boolean pTyped, URIString pUriStr) throws IllegalArgumentException {
60  		// TODO: tracing TRC.log(uriStr.toString());
61  		URIString uriStr = pUriStr.deepCopy();
62  		if (!uriStr.matchAndCut(KEYNAMEPAT, 1)) {
63  			String msg = "keyName expected!\n" + uriStr.markPosition();
64  			throw new IllegalArgumentException(msg);
65  		}
66  		String key = uriStr.group(1);
67  		if (!uriStr.cutStarting('=')) {
68  			String msg = "'=' expected!\n" + uriStr.markPosition();
69  			throw new IllegalArgumentException(msg);
70  		}
71  		Value value = Value.parse(pTyped, uriStr);
72  		if (value == null) {
73  			String msg = "value expected!\n" + uriStr.markPosition();
74  			throw new IllegalArgumentException(msg);
75  		}
76  		pUriStr.set(uriStr);
77  		return new KeyValuePair(key, value, pTyped);
78  	}
79  
80  	private String iKey;
81  
82  	private Value iValue;
83  
84  	private boolean iTyped;
85  
86  	private KeyValuePair(String pKey, Value pValue, boolean pTyped) {
87  		this.iKey = pKey;
88  		this.iValue = pValue;
89  		this.iTyped = pTyped;
90  	}
91  
92  	/**
93  	 * @see java.lang.Object#toString()
94  	 */
95  	@Override
96  	public String toString() {
97  		StringBuffer buf = new StringBuffer(this.iKey + '=');
98  		if (this.iTyped) buf.append('(' + this.iValue.getTypeInfo() + ')');
99  		buf.append(
100 			(this.iValue instanceof QuotedValue) ? ((QuotedValue) this.iValue).toQuotedString() : this.iValue.toString()
101 		);
102 		return buf.toString();
103 	}
104 
105 	/**
106 	 * getKey
107 	 *
108 	 * @return the key String
109 	 */
110 	public String getKey() {
111 		return this.iKey;
112 	}
113 
114 	/**
115 	 * getValue
116 	 *
117 	 * @return the value String
118 	 */
119 	public Value getValue() {
120 		return this.iValue;
121 	}
122 }