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   * 2204488 	  2008-10-28  raman_arora  Fix code to remove compiler warnings
18   * 2524131    2009-01-21  raman_arora  Upgrade client to JDK 1.5 (Phase 1)
19   */
20  
21  package org.metricshub.wbem.sblim.cimclient.internal.uri;
22  
23  /*-
24   * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
25   * WBEM Java Client
26   * ჻჻჻჻჻჻
27   * Copyright 2023 - 2025 MetricsHub
28   * ჻჻჻჻჻჻
29   * Licensed under the Apache License, Version 2.0 (the "License");
30   * you may not use this file except in compliance with the License.
31   * You may obtain a copy of the License at
32   *
33   *      http://www.apache.org/licenses/LICENSE-2.0
34   *
35   * Unless required by applicable law or agreed to in writing, software
36   * distributed under the License is distributed on an "AS IS" BASIS,
37   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
38   * See the License for the specific language governing permissions and
39   * limitations under the License.
40   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
41   */
42  
43  import java.util.regex.Pattern;
44  
45  /**
46   * <pre>
47   *     namespacePath	=	[namespaceType &quot;:&quot;] namespaceHandle
48   *     namespaceType	=	(&quot;http&quot; [&quot;s&quot;]) / (&quot;cimxml.wbem&quot; [&quot;s&quot;])
49   * </pre>
50   */
51  public class NamespacePath {
52  	private String iNamespaceType;
53  
54  	private NamespaceHandle iNamespaceHandle;
55  
56  	protected NamespacePath(String pNamespaceType, NamespaceHandle pNamespaceHandle) {
57  		this.iNamespaceType = pNamespaceType;
58  		this.iNamespaceHandle = pNamespaceHandle;
59  	}
60  
61  	/**
62  	 * Constructs a NamespacePath with namespace name only.
63  	 *
64  	 * @param pNamespaceName
65  	 */
66  	public NamespacePath(String pNamespaceName) {
67  		this.iNamespaceHandle = new NamespaceHandle(pNamespaceName);
68  	}
69  
70  	/**
71  	 * Tries to parse a namespace path from the passed <code>uriStr</code>.
72  	 *
73  	 * @param pUriStr
74  	 * @return <code>NamespacePath</code> instance or <code>null</code> if
75  	 *         failed.
76  	 */
77  	public static NamespacePath parse(URIString pUriStr) {
78  		// TODO: tracing TRC.log(uriStr.toString());
79  		URIString uriStr = pUriStr.deepCopy();
80  		String namespaceType = parseNamespaceType(uriStr);
81  		// can be null
82  		if (namespaceType != null) {
83  			// : needed
84  			if (!uriStr.cutStarting(':')) return null;
85  		}
86  		NamespaceHandle namespaceHandle = NamespaceHandle.parse(uriStr);
87  		if (namespaceHandle == null) {
88  			return null;
89  		}
90  		pUriStr.set(uriStr);
91  		return new NamespacePath(namespaceType, namespaceHandle);
92  	}
93  
94  	/**
95  	 * @see java.lang.Object#toString()
96  	 */
97  	@Override
98  	public String toString() {
99  		StringBuffer buf = new StringBuffer();
100 		if (this.iNamespaceType != null) buf.append(this.iNamespaceType + ':');
101 		buf.append(this.iNamespaceHandle.toString());
102 		return buf.toString();
103 	}
104 
105 	/**
106 	 * getNamespaceType
107 	 *
108 	 * @return the namespace type String
109 	 */
110 	public String getNamespaceType() {
111 		return this.iNamespaceType;
112 	}
113 
114 	/**
115 	 * getNamespaceName
116 	 *
117 	 * @return the namespace name String
118 	 */
119 	public String getNamespaceName() {
120 		return this.iNamespaceHandle == null ? null : this.iNamespaceHandle.getName();
121 	}
122 
123 	/**
124 	 * getUserInfo
125 	 *
126 	 * @return String
127 	 */
128 	public String getUserInfo() {
129 		return this.iNamespaceHandle == null ? null : this.iNamespaceHandle.getUserInfo();
130 	}
131 
132 	/**
133 	 * getHost
134 	 *
135 	 * @return String
136 	 */
137 	public String getHost() {
138 		return this.iNamespaceHandle == null ? null : this.iNamespaceHandle.getHost();
139 	}
140 
141 	/**
142 	 * getPort
143 	 *
144 	 * @return String
145 	 */
146 	public String getPort() {
147 		return this.iNamespaceHandle == null ? null : this.iNamespaceHandle.getPort();
148 	}
149 
150 	private static final Pattern TYPE_PAT = Pattern.compile("^(http(s?)|cimxml\\.wbem(s?)).*");
151 
152 	/**
153 	 * namespaceType = ("http" ["s"]) / ("cimxml.wbem" ["s"])
154 	 *
155 	 * @param pUriStr
156 	 * @return the String containing the namespace type or <code>null</code> if
157 	 *         failed.
158 	 */
159 	private static String parseNamespaceType(URIString pUriStr) {
160 		if (!pUriStr.matchAndCut(TYPE_PAT, 1)) return null;
161 		return pUriStr.group(1);
162 	}
163 }