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   * <pre>
46   *     namespaceHandle = [&quot;//&quot; authority] &quot;/&quot; [namespaceName]
47   *     namespaceName   = IDENTIFIER *(&quot;/&quot;IDENTIFIER)
48   * </pre>
49   */
50  public class NamespaceHandle {
51  
52  	/**
53  	 * Factory method which tries to build a <code>NamespaceHandle</code> from
54  	 * the passed <code>pUriStr</code>
55  	 *
56  	 * @param pUriStr
57  	 * @return a <code>NamespaceHandle</code> or <code>null</code> in case of
58  	 *         failure
59  	 */
60  	public static NamespaceHandle parse(URIString pUriStr) {
61  		// TODO: tracing TRC.log(uriStr.toString());
62  		URIString uriStr = pUriStr.deepCopy();
63  		Authority auth;
64  		if (uriStr.cutStarting("//")) {
65  			auth = Authority.parse(uriStr);
66  			if (auth == null) return null;
67  		} else {
68  			auth = null;
69  		}
70  		if (!uriStr.cutStarting('/')) {
71  			return null;
72  		}
73  		String nsName = parseNamespaceName(uriStr);
74  		// namespaceName is optimal
75  		pUriStr.set(uriStr);
76  		return new NamespaceHandle(auth, nsName);
77  	}
78  
79  	private Authority iAuth;
80  
81  	private String iNsName;
82  
83  	private NamespaceHandle(Authority pAuth, String pNsName) {
84  		this.iAuth = pAuth;
85  		this.iNsName = pNsName;
86  	}
87  
88  	/**
89  	 * Constructs a NamespaceHandle with namespace name only.
90  	 *
91  	 * @param pNamespaceName
92  	 */
93  	public NamespaceHandle(String pNamespaceName) {
94  		this.iAuth = null;
95  		this.iNsName = pNamespaceName;
96  	}
97  
98  	/**
99  	 * @see java.lang.Object#toString()
100 	 */
101 	@Override
102 	public String toString() {
103 		return (this.iAuth == null ? "" : "//" + this.iAuth.toString()) + "/" + (this.iNsName == null ? "" : this.iNsName);
104 	}
105 
106 	/**
107 	 * getName
108 	 *
109 	 * @return String
110 	 */
111 	public String getName() {
112 		return this.iNsName;
113 	}
114 
115 	/**
116 	 * getUserInfo
117 	 *
118 	 * @return String
119 	 */
120 	public String getUserInfo() {
121 		return this.iAuth == null ? null : this.iAuth.getUserInfo();
122 	}
123 
124 	/**
125 	 * getHost
126 	 *
127 	 * @return String
128 	 */
129 	public String getHost() {
130 		return this.iAuth == null ? null : this.iAuth.getHost();
131 	}
132 
133 	/**
134 	 * getPort
135 	 *
136 	 * @return String
137 	 */
138 	public String getPort() {
139 		return this.iAuth == null ? null : this.iAuth.getPort();
140 	}
141 
142 	private static final String IDENTIFIER = "[A-Za-z][0-9A-Za-z\\._-]*";
143 
144 	private static final Pattern NAMESPACENAME_PAT = Pattern.compile("^(" + IDENTIFIER + "(/" + IDENTIFIER + ")*).*");
145 
146 	/**
147 	 * <code>IDENTIFIER *("/"IDENTIFIER)</code>
148 	 *
149 	 * @param pUriStr
150 	 * @return <code>String</code> containing the namespace name or
151 	 *         <code>null</code> if failed.
152 	 */
153 	public static String parseNamespaceName(URIString pUriStr) {
154 		if (!pUriStr.matchAndCut(NAMESPACENAME_PAT, 1)) return null;
155 		return pUriStr.group(1);
156 	}
157 }