Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
StringNamespace |
|
| 1.619047619047619;1.619 |
1 | /* $Id: StringNamespace.java 17892 2010-01-12 21:21:35Z linus $ | |
2 | ***************************************************************************** | |
3 | * Copyright (c) 2009 Contributors - see below | |
4 | * All rights reserved. This program and the accompanying materials | |
5 | * are made available under the terms of the Eclipse Public License v1.0 | |
6 | * which accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * tfmorris | |
11 | ***************************************************************************** | |
12 | * | |
13 | * Some portions of this file was previously release using the BSD License: | |
14 | */ | |
15 | ||
16 | // Copyright (c) 2003-2006 The Regents of the University of California. All | |
17 | // Rights Reserved. Permission to use, copy, modify, and distribute this | |
18 | // software and its documentation without fee, and without a written | |
19 | // agreement is hereby granted, provided that the above copyright notice | |
20 | // and this paragraph appear in all copies. This software program and | |
21 | // documentation are copyrighted by The Regents of the University of | |
22 | // California. The software program and documentation are supplied "AS | |
23 | // IS", without any accompanying services from The Regents. The Regents | |
24 | // does not warrant that the operation of the program will be | |
25 | // uninterrupted or error-free. The end-user understands that the program | |
26 | // was developed for research purposes and is advised not to rely | |
27 | // exclusively on the program for any reason. IN NO EVENT SHALL THE | |
28 | // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, | |
29 | // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, | |
30 | // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF | |
31 | // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF | |
32 | // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY | |
33 | // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
34 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE | |
35 | // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF | |
36 | // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, | |
37 | // UPDATES, ENHANCEMENTS, OR MODIFICATIONS. | |
38 | ||
39 | package org.argouml.uml.util.namespace; | |
40 | ||
41 | import java.util.Iterator; | |
42 | import java.util.Stack; | |
43 | ||
44 | import org.apache.log4j.Logger; | |
45 | ||
46 | /** | |
47 | * A StringNamespace is a string based namespace (StringNamespaceElement) | |
48 | * object. It faciliates creation of these objects via a number of helper | |
49 | * methods. | |
50 | * | |
51 | * @author mkl | |
52 | */ | |
53 | public class StringNamespace implements Namespace, Cloneable { | |
54 | ||
55 | 0 | private static final Logger LOG = Logger.getLogger(StringNamespace.class); |
56 | ||
57 | 0 | private Stack ns = new Stack(); |
58 | ||
59 | 0 | private String token = JAVA_NS_TOKEN; |
60 | ||
61 | /** | |
62 | * Constructor. | |
63 | * | |
64 | * The empty namespace with java token by default. | |
65 | */ | |
66 | 0 | public StringNamespace() { |
67 | 0 | } |
68 | ||
69 | /** | |
70 | * empty namespace with given default token. | |
71 | * | |
72 | * @param theToken | |
73 | * the scope seperator to use | |
74 | */ | |
75 | public StringNamespace(String theToken) { | |
76 | 0 | this(); |
77 | 0 | this.token = theToken; |
78 | 0 | } |
79 | ||
80 | /** | |
81 | * construct a namespace from an array of strings with default scope token. | |
82 | * | |
83 | * @param elements | |
84 | * an array of strings which represent the namespace. | |
85 | */ | |
86 | public StringNamespace(String[] elements) { | |
87 | 0 | this(elements, JAVA_NS_TOKEN); |
88 | 0 | } |
89 | ||
90 | /** | |
91 | * construct a namespace from strings with given scope token. | |
92 | * | |
93 | * @param elements | |
94 | * an array of strings which represent the namespace. | |
95 | * @param theToken | |
96 | * scope token to use | |
97 | */ | |
98 | public StringNamespace(String[] elements, String theToken) { | |
99 | 0 | this(theToken); |
100 | 0 | for (int i = 0; i < elements.length; i++) { |
101 | 0 | pushNamespaceElement(new StringNamespaceElement(elements[i])); |
102 | } | |
103 | 0 | } |
104 | ||
105 | /** | |
106 | * Construct a namespace from NamespaceElements with given scope token. | |
107 | * | |
108 | * @param elements | |
109 | * array of NamespaceElements | |
110 | * @param theToken | |
111 | * scope token | |
112 | */ | |
113 | public StringNamespace(NamespaceElement[] elements, String theToken) { | |
114 | 0 | this(theToken); |
115 | ||
116 | 0 | for (int i = 0; i < elements.length; i++) { |
117 | 0 | pushNamespaceElement(new StringNamespaceElement(elements[i] |
118 | .toString())); | |
119 | } | |
120 | 0 | } |
121 | ||
122 | /** | |
123 | * Construct a namespace from NamespaceElements with default scope token. | |
124 | * | |
125 | * @param elements | |
126 | * array of NamespaceElements | |
127 | */ | |
128 | public StringNamespace(NamespaceElement[] elements) { | |
129 | 0 | this(elements, JAVA_NS_TOKEN); |
130 | 0 | } |
131 | ||
132 | /* | |
133 | * @see Namespace#pushNamespaceElement(NamespaceElement) | |
134 | */ | |
135 | public void pushNamespaceElement(NamespaceElement element) { | |
136 | 0 | ns.push(element); |
137 | 0 | } |
138 | ||
139 | /* | |
140 | * @see org.argouml.uml.util.namespace.Namespace#peekNamespaceElement() | |
141 | */ | |
142 | public NamespaceElement peekNamespaceElement() { | |
143 | 0 | return (NamespaceElement) ns.peek(); |
144 | } | |
145 | ||
146 | /** | |
147 | * Store the next namespace element. | |
148 | * | |
149 | * @see org.argouml.uml.util.namespace.Namespace#popNamespaceElement() | |
150 | * @param element The element to store. | |
151 | */ | |
152 | public void pushNamespaceElement(String element) { | |
153 | 0 | ns.push(new StringNamespaceElement(element)); |
154 | 0 | } |
155 | ||
156 | /* | |
157 | * @see org.argouml.uml.util.namespace.Namespace#popNamespaceElement() | |
158 | */ | |
159 | public NamespaceElement popNamespaceElement() { | |
160 | 0 | return (NamespaceElement) ns.pop(); |
161 | } | |
162 | ||
163 | /* | |
164 | * @see org.argouml.uml.util.namespace.Namespace#getBaseNamespace() | |
165 | */ | |
166 | public Namespace getBaseNamespace() { | |
167 | 0 | StringNamespace result = null; |
168 | try { | |
169 | 0 | result = (StringNamespace) this.clone(); |
170 | 0 | } catch (CloneNotSupportedException e) { |
171 | 0 | LOG.debug(e); |
172 | 0 | return null; |
173 | 0 | } |
174 | 0 | result.popNamespaceElement(); |
175 | 0 | return result; |
176 | } | |
177 | ||
178 | /* | |
179 | * @see org.argouml.uml.util.namespace.Namespace#getCommonNamespace( | |
180 | * org.argouml.uml.util.namespace.Namespace) | |
181 | */ | |
182 | public Namespace getCommonNamespace(Namespace namespace) { | |
183 | 0 | Iterator i = iterator(); |
184 | 0 | Iterator j = namespace.iterator(); |
185 | 0 | StringNamespace result = new StringNamespace(token); |
186 | ||
187 | 0 | for (; i.hasNext() && j.hasNext();) { |
188 | 0 | NamespaceElement elem1 = (NamespaceElement) i.next(); |
189 | 0 | NamespaceElement elem2 = (NamespaceElement) j.next(); |
190 | 0 | if (elem1.toString().equals(elem2.toString())) { |
191 | 0 | result.pushNamespaceElement(elem1); |
192 | } | |
193 | 0 | } |
194 | ||
195 | 0 | return result; |
196 | } | |
197 | ||
198 | /* | |
199 | * @see org.argouml.uml.util.namespace.Namespace#iterator() | |
200 | */ | |
201 | public Iterator iterator() { | |
202 | 0 | return ns.iterator(); |
203 | } | |
204 | ||
205 | /* | |
206 | * @see org.argouml.uml.util.namespace.Namespace#isEmpty() | |
207 | */ | |
208 | public boolean isEmpty() { | |
209 | 0 | return ns.isEmpty(); |
210 | } | |
211 | ||
212 | /* | |
213 | * @see org.argouml.uml.util.namespace.Namespace#setDefaultScopeToken(java.lang.String) | |
214 | */ | |
215 | public void setDefaultScopeToken(String theToken) { | |
216 | 0 | this.token = theToken; |
217 | 0 | } |
218 | ||
219 | /** | |
220 | * parse a fully qualified namespace to create a Namespace object. | |
221 | * | |
222 | * @param fqn | |
223 | * string representation of namespace | |
224 | * @param token | |
225 | * the token of the namespace | |
226 | * @return a namespace object | |
227 | */ | |
228 | public static Namespace parse(String fqn, String token) { | |
229 | ||
230 | 0 | String myFqn = fqn; |
231 | 0 | StringNamespace sns = new StringNamespace(token); |
232 | 0 | int i = myFqn.indexOf(token); |
233 | 0 | while (i > -1) { |
234 | 0 | sns.pushNamespaceElement(myFqn.substring(0, i)); |
235 | 0 | myFqn = myFqn.substring(i + token.length()); |
236 | 0 | i = myFqn.indexOf(token); |
237 | } | |
238 | 0 | if (myFqn.length() > 0) { |
239 | 0 | sns.pushNamespaceElement(myFqn); |
240 | } | |
241 | 0 | return sns; |
242 | } | |
243 | ||
244 | /** | |
245 | * parse the name of a (java) class. | |
246 | * | |
247 | * @param c | |
248 | * the class | |
249 | * @return the namespace object | |
250 | */ | |
251 | public static Namespace parse(Class c) { | |
252 | 0 | return parse(c.getName(), JAVA_NS_TOKEN); |
253 | } | |
254 | ||
255 | /* | |
256 | * @see java.lang.Object#hashCode() | |
257 | */ | |
258 | public int hashCode() { | |
259 | 0 | return toString(JAVA_NS_TOKEN).hashCode(); |
260 | } | |
261 | ||
262 | /** | |
263 | * Two namespaces are equal when they are namespaces and have the same | |
264 | * string representation. | |
265 | * | |
266 | * @param namespace | |
267 | * the namespace to compare with | |
268 | * @return true if equal | |
269 | * | |
270 | * @see java.lang.Object#equals(java.lang.Object) | |
271 | */ | |
272 | public boolean equals(Object namespace) { | |
273 | 0 | if (namespace instanceof Namespace) { |
274 | 0 | String ns1 = this.toString(JAVA_NS_TOKEN); |
275 | 0 | String ns2 = ((Namespace) namespace).toString(JAVA_NS_TOKEN); |
276 | 0 | return ns1.equals(ns2); |
277 | } | |
278 | 0 | return false; |
279 | } | |
280 | ||
281 | /* | |
282 | * @see org.argouml.uml.util.namespace.Namespace#toString(java.lang.String) | |
283 | */ | |
284 | public String toString(String theToken) { | |
285 | 0 | StringBuffer result = new StringBuffer(); |
286 | 0 | Iterator i = ns.iterator(); |
287 | 0 | while (i.hasNext()) { |
288 | 0 | result.append(i.next()); |
289 | 0 | if (i.hasNext()) { |
290 | 0 | result.append(theToken); |
291 | } | |
292 | } | |
293 | 0 | return result.toString(); |
294 | } | |
295 | ||
296 | /* | |
297 | * Create a string representation using the default scope token. | |
298 | * | |
299 | * @see java.lang.Object#toString() | |
300 | */ | |
301 | public String toString() { | |
302 | 0 | return toString(token); |
303 | } | |
304 | ||
305 | } |