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.sentrysoftware.wbem.sblim.cimclient.internal.uri;
21  
22  /*-
23   * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
24   * WBEM Java Client
25   * ჻჻჻჻჻჻
26   * Copyright (C) 2023 Sentry Software
27   * ჻჻჻჻჻჻
28   * This program is free software: you can redistribute it and/or modify
29   * it under the terms of the GNU Lesser General Public License as
30   * published by the Free Software Foundation, either version 3 of the
31   * License, or (at your option) any later version.
32   *
33   * This program is distributed in the hope that it will be useful,
34   * but WITHOUT ANY WARRANTY; without even the implied warranty of
35   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36   * GNU General Lesser Public License for more details.
37   *
38   * You should have received a copy of the GNU General Lesser Public
39   * License along with this program.  If not, see
40   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
41   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
42   */
43  
44  import java.util.regex.Pattern;
45  
46  /**
47   * <pre>
48   *     namespaceHandle = [&quot;//&quot; authority] &quot;/&quot; [namespaceName]
49   *     namespaceName   = IDENTIFIER *(&quot;/&quot;IDENTIFIER)
50   * </pre>
51   */
52  public class NamespaceHandle {
53  
54  	/**
55  	 * Factory method which tries to build a <code>NamespaceHandle</code> from
56  	 * the passed <code>pUriStr</code>
57  	 * 
58  	 * @param pUriStr
59  	 * @return a <code>NamespaceHandle</code> or <code>null</code> in case of
60  	 *         failure
61  	 */
62  	public static NamespaceHandle parse(URIString pUriStr) {
63  		// TODO: tracing TRC.log(uriStr.toString());
64  		URIString uriStr = pUriStr.deepCopy();
65  		Authority auth;
66  		if (uriStr.cutStarting("//")) {
67  			auth = Authority.parse(uriStr);
68  			if (auth == null) return null;
69  		} else {
70  			auth = null;
71  		}
72  		if (!uriStr.cutStarting('/')) { return null; }
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()) + "/"
104 				+ (this.iNsName == null ? "" : this.iNsName);
105 	}
106 
107 	/**
108 	 * getName
109 	 * 
110 	 * @return String
111 	 */
112 	public String getName() {
113 		return this.iNsName;
114 	}
115 
116 	/**
117 	 * getUserInfo
118 	 * 
119 	 * @return String
120 	 */
121 	public String getUserInfo() {
122 		return this.iAuth == null ? null : this.iAuth.getUserInfo();
123 	}
124 
125 	/**
126 	 * getHost
127 	 * 
128 	 * @return String
129 	 */
130 	public String getHost() {
131 		return this.iAuth == null ? null : this.iAuth.getHost();
132 	}
133 
134 	/**
135 	 * getPort
136 	 * 
137 	 * @return String
138 	 */
139 	public String getPort() {
140 		return this.iAuth == null ? null : this.iAuth.getPort();
141 	}
142 
143 	private static final String IDENTIFIER = "[A-Za-z][0-9A-Za-z\\._-]*";
144 
145 	private static final Pattern NAMESPACENAME_PAT = Pattern.compile("^(" + IDENTIFIER + "(/"
146 			+ IDENTIFIER + ")*).*");
147 
148 	/**
149 	 * <code>IDENTIFIER *("/"IDENTIFIER)</code>
150 	 * 
151 	 * @param pUriStr
152 	 * @return <code>String</code> containing the namespace name or
153 	 *         <code>null</code> if failed.
154 	 */
155 	public static String parseNamespaceName(URIString pUriStr) {
156 		if (!pUriStr.matchAndCut(NAMESPACENAME_PAT, 1)) return null;
157 		return pUriStr.group(1);
158 	}
159 
160 }