View Javadoc
1   /*
2     (C) Copyright IBM Corp. 2007, 2013
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, IBM, ebak@de.ibm.com
12   * 
13   * Change History
14   * Flag       Date        Prog         Description
15   *------------------------------------------------------------------------------- 
16   * 1804402    2007-09-28  ebak         IPv6 ready SLP
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   * 2531371    2009-02-10  raman_arora  Upgrade client to JDK 1.5 (Phase 2)
20   *    2650    2013-07-18  blaschke-oss SLP opaque value handling incorrect
21   */
22  
23  package org.metricshub.wbem.sblim.slp.internal;
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.util.Iterator;
46  import java.util.Vector;
47  import org.metricshub.wbem.sblim.slp.ServiceLocationAttribute;
48  
49  /**
50   * AttributeHandler
51   *
52   */
53  public class AttributeHandler {
54  
55  	/*
56  	 * attr-list = attribute / attribute `,' attr-list attribute = `(' attr-tag
57  	 * `=' attr-val-list `)' / attr-tag attr-val-list = attr-val / attr-val `,'
58  	 * attr-val-list attr-tag = 1*safe-tag attr-val = intval / strval / boolval
59  	 * / opaque intval = [-]1*DIGIT strval = 1*safe-val boolval = "true" /
60  	 * "false" opaque = "\FF" 1*escape-val safe-val = ; Any character except
61  	 * reserved. safe-tag = ; Any character except reserved, star and bad-tag.
62  	 * reserved = `(' / `)' / `,' / `\' / `!' / `<' / `=' / `>' / `~' / CTL
63  	 * escape-val = `\' HEXDIG HEXDIG bad-tag = CR / LF / HTAB / `_' star = `*'
64  	 */
65  	/**
66  	 * @param pAttr
67  	 * @return String
68  	 */
69  	public static String buildString(ServiceLocationAttribute pAttr) {
70  		StringBuffer buf = new StringBuffer();
71  		Vector<?> valVec = pAttr.getValues();
72  		if (valVec != null && valVec.size() > 0) buf.append('(');
73  		buf.append(Convert.escape(pAttr.getId(), Convert.ATTR_RESERVED));
74  		if (valVec != null && valVec.size() > 0) {
75  			buf.append('=');
76  			Iterator<?> itr = valVec.iterator();
77  			boolean first = true;
78  			while (itr.hasNext()) {
79  				if (first) first = false; else buf.append(',');
80  				buf.append(AttributeHandler.escapeValue(itr.next()));
81  			}
82  			buf.append(')');
83  		}
84  		return buf.toString();
85  	}
86  
87  	/**
88  	 * escapeValue
89  	 *
90  	 * @param pValue
91  	 * @return String
92  	 */
93  	public static String escapeValue(Object pValue) {
94  		if (pValue instanceof String) {
95  			return Convert.escape((String) pValue, Convert.ATTR_RESERVED);
96  		} else if (pValue instanceof Integer) {
97  			return ((Integer) pValue).toString();
98  		} else if (pValue instanceof Boolean) {
99  			return ((Boolean) pValue).toString();
100 		} else if (pValue instanceof byte[]) {
101 			return AttributeHandler.mkOpaqueStr((byte[]) pValue);
102 		} else if (pValue == null) {
103 			return "";
104 		}
105 		throw new IllegalArgumentException("Type: " + pValue.getClass().getName() + " cannot be an attribute value!");
106 	}
107 
108 	/**
109 	 * mkOpaqueStr
110 	 *
111 	 * @param pBytes
112 	 * @return String
113 	 */
114 	public static String mkOpaqueStr(byte[] pBytes) {
115 		StringBuilder buf = new StringBuilder("\\FF");
116 		for (int i = 0; i < pBytes.length; i++) {
117 			int value = pBytes[i] & 0xff;
118 			String hexStr = Integer.toString(value, 16).toUpperCase();
119 			buf.append('\\');
120 			if (hexStr.length() == 1) buf.append('0');
121 			buf.append(hexStr);
122 		}
123 		return buf.toString();
124 	}
125 }