|
1 /* |
|
2 * $Id: NameImpl.java,v 1.48 2006/01/27 12:49:38 vj135062 Exp $ |
|
3 * $Revision: 1.48 $ |
|
4 * $Date: 2006/01/27 12:49:38 $ |
|
5 */ |
|
6 |
|
7 /* |
|
8 * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved. |
|
9 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
10 * |
|
11 * This code is free software; you can redistribute it and/or modify it |
|
12 * under the terms of the GNU General Public License version 2 only, as |
|
13 * published by the Free Software Foundation. Sun designates this |
|
14 * particular file as subject to the "Classpath" exception as provided |
|
15 * by Sun in the LICENSE file that accompanied this code. |
|
16 * |
|
17 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
20 * version 2 for more details (a copy is included in the LICENSE file that |
|
21 * accompanied this code). |
|
22 * |
|
23 * You should have received a copy of the GNU General Public License version |
|
24 * 2 along with this work; if not, write to the Free Software Foundation, |
|
25 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
26 * |
|
27 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
28 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
29 * have any questions. |
|
30 */ |
|
31 package com.sun.xml.internal.messaging.saaj.soap.name; |
|
32 |
|
33 import java.util.logging.Level; |
|
34 import java.util.logging.Logger; |
|
35 |
|
36 import javax.xml.namespace.QName; |
|
37 import javax.xml.soap.Name; |
|
38 import javax.xml.soap.SOAPConstants; |
|
39 |
|
40 import com.sun.org.apache.xerces.internal.xni.NamespaceContext; |
|
41 import org.w3c.dom.Element; |
|
42 |
|
43 import com.sun.xml.internal.messaging.saaj.soap.impl.ElementImpl; |
|
44 import com.sun.xml.internal.messaging.saaj.util.LogDomainConstants; |
|
45 |
|
46 public class NameImpl implements Name { |
|
47 public static final String XML_NAMESPACE_PREFIX = "xml"; |
|
48 public static final String XML_SCHEMA_NAMESPACE_PREFIX = "xs"; |
|
49 public static final String SOAP_ENVELOPE_PREFIX = "SOAP-ENV"; |
|
50 |
|
51 public static final String XML_NAMESPACE = |
|
52 "http://www.w3.org/XML/1998/namespace"; |
|
53 public static final String SOAP11_NAMESPACE = |
|
54 SOAPConstants.URI_NS_SOAP_ENVELOPE; |
|
55 public static final String SOAP12_NAMESPACE = |
|
56 SOAPConstants.URI_NS_SOAP_1_2_ENVELOPE; |
|
57 public static final String XML_SCHEMA_NAMESPACE = |
|
58 "http://www.w3.org/2001/XMLSchema"; |
|
59 |
|
60 protected String uri = ""; |
|
61 protected String localName = ""; |
|
62 protected String prefix = ""; |
|
63 private String qualifiedName = null; |
|
64 |
|
65 protected static Logger log = |
|
66 Logger.getLogger(LogDomainConstants.NAMING_DOMAIN, |
|
67 "com.sun.xml.internal.messaging.saaj.soap.name.LocalStrings"); |
|
68 |
|
69 protected NameImpl(String name) { |
|
70 this.localName = name == null ? "" : name; |
|
71 } |
|
72 |
|
73 protected NameImpl(String name, String prefix, String uri) { |
|
74 this.uri = uri == null ? "" : uri; |
|
75 this.localName = name == null ? "" : name; |
|
76 this.prefix = prefix == null ? "" : prefix; |
|
77 |
|
78 if (this.prefix.equals("xmlns") && this.uri.equals("")) { |
|
79 this.uri = NamespaceContext.XMLNS_URI; |
|
80 } |
|
81 if (this.uri.equals(NamespaceContext.XMLNS_URI) && this.prefix.equals("")) { |
|
82 this.prefix = "xmlns"; |
|
83 } |
|
84 } |
|
85 |
|
86 public static Name convertToName(QName qname) { |
|
87 return new NameImpl(qname.getLocalPart(), |
|
88 qname.getPrefix(), |
|
89 qname.getNamespaceURI()); |
|
90 } |
|
91 |
|
92 public static QName convertToQName(Name name) { |
|
93 return new QName(name.getURI(), |
|
94 name.getLocalName(), |
|
95 name.getPrefix()); |
|
96 } |
|
97 |
|
98 public static NameImpl createFromUnqualifiedName(String name) { |
|
99 return new NameImpl(name); |
|
100 } |
|
101 |
|
102 public static Name createFromTagName(String tagName) { |
|
103 return createFromTagAndUri(tagName, ""); |
|
104 } |
|
105 |
|
106 public static Name createFromQualifiedName( |
|
107 String qualifiedName, |
|
108 String uri) { |
|
109 return createFromTagAndUri(qualifiedName, uri); |
|
110 } |
|
111 |
|
112 protected static Name createFromTagAndUri(String tagName, String uri) { |
|
113 if (tagName == null) { |
|
114 log.severe("SAAJ0201.name.not.created.from.null.tag"); |
|
115 throw new IllegalArgumentException("Cannot create a name from a null tag."); |
|
116 } |
|
117 int index = tagName.indexOf(':'); |
|
118 if (index < 0) { |
|
119 return new NameImpl(tagName, "", uri); |
|
120 } else { |
|
121 return new NameImpl( |
|
122 tagName.substring(index + 1), |
|
123 tagName.substring(0, index), |
|
124 uri); |
|
125 } |
|
126 } |
|
127 |
|
128 protected static int getPrefixSeparatorIndex(String qualifiedName) { |
|
129 int index = qualifiedName.indexOf(':'); |
|
130 if (index < 0) { |
|
131 log.log( |
|
132 Level.SEVERE, |
|
133 "SAAJ0202.name.invalid.arg.format", |
|
134 new String[] { qualifiedName }); |
|
135 throw new IllegalArgumentException( |
|
136 "Argument \"" |
|
137 + qualifiedName |
|
138 + "\" must be of the form: \"prefix:localName\""); |
|
139 } |
|
140 return index; |
|
141 } |
|
142 |
|
143 public static String getPrefixFromQualifiedName(String qualifiedName) { |
|
144 return qualifiedName.substring( |
|
145 0, |
|
146 getPrefixSeparatorIndex(qualifiedName)); |
|
147 } |
|
148 |
|
149 public static String getLocalNameFromQualifiedName(String qualifiedName) { |
|
150 return qualifiedName.substring( |
|
151 getPrefixSeparatorIndex(qualifiedName) + 1); |
|
152 } |
|
153 |
|
154 public static String getPrefixFromTagName(String tagName) { |
|
155 if (isQualified(tagName)) { |
|
156 return getPrefixFromQualifiedName(tagName); |
|
157 } |
|
158 return ""; |
|
159 } |
|
160 |
|
161 public static String getLocalNameFromTagName(String tagName) { |
|
162 if (isQualified(tagName)) { |
|
163 return getLocalNameFromQualifiedName(tagName); |
|
164 } |
|
165 return tagName; |
|
166 } |
|
167 |
|
168 public static boolean isQualified(String tagName) { |
|
169 return tagName.indexOf(':') >= 0; |
|
170 } |
|
171 |
|
172 public static NameImpl create(String name, String prefix, String uri) { |
|
173 if (prefix == null) |
|
174 prefix = ""; |
|
175 if (uri == null) |
|
176 uri = ""; |
|
177 if (name == null) |
|
178 name = ""; |
|
179 |
|
180 if (!uri.equals("") && !name.equals("")) { |
|
181 if (uri.equals(NameImpl.SOAP11_NAMESPACE)) { |
|
182 if (name.equalsIgnoreCase("Envelope")) |
|
183 return createEnvelope1_1Name(prefix); |
|
184 else if (name.equalsIgnoreCase("Header")) |
|
185 return createHeader1_1Name(prefix); |
|
186 else if (name.equalsIgnoreCase("Body")) |
|
187 return createBody1_1Name(prefix); |
|
188 else if (name.equalsIgnoreCase("Fault")) |
|
189 return createFault1_1Name(prefix); |
|
190 else |
|
191 return new SOAP1_1Name(name, prefix); |
|
192 } else if (uri.equals(SOAP12_NAMESPACE)) { |
|
193 if (name.equalsIgnoreCase("Envelope")) |
|
194 return createEnvelope1_2Name(prefix); |
|
195 else if (name.equalsIgnoreCase("Header")) |
|
196 return createHeader1_2Name(prefix); |
|
197 else if (name.equalsIgnoreCase("Body")) |
|
198 return createBody1_2Name(prefix); |
|
199 else if ( |
|
200 name.equals("Fault") |
|
201 || name.equals("Reason") |
|
202 || name.equals("Detail")) |
|
203 return createFault1_2Name(name, prefix); |
|
204 else if (name.equals("Code") || name.equals("Subcode")) |
|
205 return createCodeSubcode1_2Name(prefix, name); |
|
206 else |
|
207 return new SOAP1_2Name(name, prefix); |
|
208 } |
|
209 |
|
210 } |
|
211 return new NameImpl(name, prefix, uri); |
|
212 } |
|
213 |
|
214 public static String createQName(String prefix, String localName) { |
|
215 if (prefix == null || prefix.equals("")) { |
|
216 return localName; |
|
217 } |
|
218 return prefix + ":" + localName; |
|
219 } |
|
220 |
|
221 public boolean equals(Object obj) { |
|
222 if (!(obj instanceof Name)) { |
|
223 return false; |
|
224 } |
|
225 |
|
226 Name otherName = (Name) obj; |
|
227 |
|
228 if (!uri.equals(otherName.getURI())) { |
|
229 return false; |
|
230 } |
|
231 |
|
232 if (!localName.equals(otherName.getLocalName())) { |
|
233 return false; |
|
234 } |
|
235 |
|
236 return true; |
|
237 } |
|
238 |
|
239 /** |
|
240 * Get the local name part of this XML Name. |
|
241 * |
|
242 * @return a string for the local name. |
|
243 */ |
|
244 public String getLocalName() { |
|
245 return localName; |
|
246 } |
|
247 |
|
248 /* getQualifiedName is inherited from QName */ |
|
249 |
|
250 /** |
|
251 * Returns the prefix associated with the namespace of the name. |
|
252 * |
|
253 * @return the prefix as a string. |
|
254 */ |
|
255 public String getPrefix() { |
|
256 return prefix; |
|
257 } |
|
258 |
|
259 /** |
|
260 * Returns the URI associated of the namespace. |
|
261 * |
|
262 * @return the uri as a string. |
|
263 */ |
|
264 public String getURI() { |
|
265 return uri; |
|
266 } |
|
267 |
|
268 /** |
|
269 * Returns a String version of the name suitable for use in an XML document. |
|
270 */ |
|
271 public String getQualifiedName() { |
|
272 if (qualifiedName == null) { |
|
273 if (prefix != null && prefix.length() > 0) { |
|
274 qualifiedName = prefix + ":" + localName; |
|
275 } else { |
|
276 qualifiedName = localName; |
|
277 } |
|
278 } |
|
279 return qualifiedName; |
|
280 } |
|
281 |
|
282 /** |
|
283 * Create a name object for a SOAP1.1 Envelope. |
|
284 */ |
|
285 public static NameImpl createEnvelope1_1Name(String prefix) { |
|
286 return new Envelope1_1Name(prefix); |
|
287 } |
|
288 |
|
289 /** |
|
290 * Create a name object for a SOAP1.2 Envelope. |
|
291 */ |
|
292 public static NameImpl createEnvelope1_2Name(String prefix) { |
|
293 return new Envelope1_2Name(prefix); |
|
294 } |
|
295 |
|
296 /** |
|
297 * Create a name object for a SOAP1.1 Header. |
|
298 */ |
|
299 public static NameImpl createHeader1_1Name(String prefix) { |
|
300 return new Header1_1Name(prefix); |
|
301 } |
|
302 |
|
303 /** |
|
304 * Create a name object for a SOAP1.2 Header. |
|
305 */ |
|
306 public static NameImpl createHeader1_2Name(String prefix) { |
|
307 return new Header1_2Name(prefix); |
|
308 } |
|
309 |
|
310 /** |
|
311 * Create a name object for a SOAP1.1 Body. |
|
312 */ |
|
313 public static NameImpl createBody1_1Name(String prefix) { |
|
314 return new Body1_1Name(prefix); |
|
315 } |
|
316 |
|
317 /** |
|
318 * Create a name object for a SOAP1.2 Body. |
|
319 */ |
|
320 public static NameImpl createBody1_2Name(String prefix) { |
|
321 return new Body1_2Name(prefix); |
|
322 } |
|
323 |
|
324 /** |
|
325 * Create a name object for a SOAP1.1 Fault. |
|
326 */ |
|
327 public static NameImpl createFault1_1Name(String prefix) { |
|
328 return new Fault1_1Name(prefix); |
|
329 } |
|
330 |
|
331 /** |
|
332 * Create a name object for a SOAP1.2 NotUnderstood element. |
|
333 */ |
|
334 public static NameImpl createNotUnderstood1_2Name(String prefix) { |
|
335 return new NotUnderstood1_2Name(prefix); |
|
336 } |
|
337 |
|
338 /** |
|
339 * Create a name object for a SOAP1.2 Upgrade element. |
|
340 */ |
|
341 public static NameImpl createUpgrade1_2Name(String prefix) { |
|
342 return new Upgrade1_2Name(prefix); |
|
343 } |
|
344 |
|
345 /** |
|
346 * Create a name object for a SOAP1.2 SupportedEnvelope Upgrade element. |
|
347 */ |
|
348 public static NameImpl createSupportedEnvelope1_2Name(String prefix) { |
|
349 return new SupportedEnvelope1_2Name(prefix); |
|
350 } |
|
351 |
|
352 /** |
|
353 * Create a name object for a SOAP1.2 |
|
354 * Fault, Reason or Detail. |
|
355 * |
|
356 * @param localName Local Name of element |
|
357 */ |
|
358 public static NameImpl createFault1_2Name( |
|
359 String localName, |
|
360 String prefix) { |
|
361 return new Fault1_2Name(localName, prefix); |
|
362 } |
|
363 |
|
364 /** |
|
365 * Create a name object for a SOAP1.2 Fault/Code or Subcode. |
|
366 * |
|
367 * @param localName Either "Code" or "Subcode" |
|
368 */ |
|
369 public static NameImpl createCodeSubcode1_2Name( |
|
370 String prefix, |
|
371 String localName) { |
|
372 return new CodeSubcode1_2Name(localName, prefix); |
|
373 } |
|
374 |
|
375 /** |
|
376 * Create a name object for a SOAP1.1 Fault Detail. |
|
377 */ |
|
378 public static NameImpl createDetail1_1Name() { |
|
379 return new Detail1_1Name(); |
|
380 } |
|
381 |
|
382 public static NameImpl createDetail1_1Name(String prefix) { |
|
383 return new Detail1_1Name(prefix); |
|
384 } |
|
385 |
|
386 public static NameImpl createFaultElement1_1Name(String localName) { |
|
387 return new FaultElement1_1Name(localName); |
|
388 } |
|
389 |
|
390 public static NameImpl createFaultElement1_1Name(String localName, |
|
391 String prefix) { |
|
392 return new FaultElement1_1Name(localName, prefix); |
|
393 } |
|
394 |
|
395 public static NameImpl createSOAP11Name(String string) { |
|
396 return new SOAP1_1Name(string, null); |
|
397 } |
|
398 public static NameImpl createSOAP12Name(String string) { |
|
399 return new SOAP1_2Name(string, null); |
|
400 } |
|
401 |
|
402 public static NameImpl createSOAP12Name(String localName, String prefix) { |
|
403 return new SOAP1_2Name(localName, prefix); |
|
404 } |
|
405 |
|
406 public static NameImpl createXmlName(String localName) { |
|
407 return new NameImpl(localName, XML_NAMESPACE_PREFIX, XML_NAMESPACE); |
|
408 } |
|
409 |
|
410 public static Name copyElementName(Element element) { |
|
411 String localName = element.getLocalName(); |
|
412 String prefix = element.getPrefix(); |
|
413 String uri = element.getNamespaceURI(); |
|
414 return create(localName, prefix, uri); |
|
415 } |
|
416 |
|
417 |
|
418 static class SOAP1_1Name extends NameImpl { |
|
419 SOAP1_1Name(String name, String prefix) { |
|
420 super( |
|
421 name, |
|
422 (prefix == null || prefix.equals("")) |
|
423 ? NameImpl.SOAP_ENVELOPE_PREFIX |
|
424 : prefix, |
|
425 NameImpl.SOAP11_NAMESPACE); |
|
426 } |
|
427 } |
|
428 |
|
429 static class Envelope1_1Name extends SOAP1_1Name { |
|
430 Envelope1_1Name(String prefix) { |
|
431 super("Envelope", prefix); |
|
432 } |
|
433 } |
|
434 |
|
435 static class Header1_1Name extends SOAP1_1Name { |
|
436 Header1_1Name(String prefix) { |
|
437 super("Header", prefix); |
|
438 } |
|
439 } |
|
440 |
|
441 static class Body1_1Name extends SOAP1_1Name { |
|
442 Body1_1Name(String prefix) { |
|
443 super("Body", prefix); |
|
444 } |
|
445 } |
|
446 |
|
447 static class Fault1_1Name extends NameImpl { |
|
448 Fault1_1Name(String prefix) { |
|
449 super( |
|
450 "Fault", |
|
451 (prefix == null || prefix.equals("")) |
|
452 ? SOAP_ENVELOPE_PREFIX |
|
453 : prefix, |
|
454 SOAP11_NAMESPACE); |
|
455 } |
|
456 } |
|
457 |
|
458 static class Detail1_1Name extends NameImpl { |
|
459 Detail1_1Name() { |
|
460 super("detail"); |
|
461 } |
|
462 |
|
463 Detail1_1Name(String prefix) { |
|
464 super("detail", prefix, ""); |
|
465 } |
|
466 } |
|
467 |
|
468 static class FaultElement1_1Name extends NameImpl { |
|
469 FaultElement1_1Name(String localName) { |
|
470 super(localName); |
|
471 } |
|
472 |
|
473 FaultElement1_1Name(String localName, String prefix) { |
|
474 super(localName, prefix, ""); |
|
475 } |
|
476 } |
|
477 |
|
478 static class SOAP1_2Name extends NameImpl { |
|
479 SOAP1_2Name(String name, String prefix) { |
|
480 super( |
|
481 name, |
|
482 (prefix == null || prefix.equals("")) |
|
483 ? SOAPConstants.SOAP_ENV_PREFIX |
|
484 : prefix, |
|
485 SOAP12_NAMESPACE); |
|
486 } |
|
487 } |
|
488 |
|
489 static class Envelope1_2Name extends SOAP1_2Name { |
|
490 Envelope1_2Name(String prefix) { |
|
491 super("Envelope", prefix); |
|
492 } |
|
493 } |
|
494 |
|
495 static class Header1_2Name extends SOAP1_2Name { |
|
496 Header1_2Name(String prefix) { |
|
497 super("Header", prefix); |
|
498 } |
|
499 } |
|
500 |
|
501 static class Body1_2Name extends SOAP1_2Name { |
|
502 Body1_2Name(String prefix) { |
|
503 super("Body", prefix); |
|
504 } |
|
505 } |
|
506 |
|
507 static class Fault1_2Name extends NameImpl { |
|
508 Fault1_2Name(String name, String prefix) { |
|
509 super( |
|
510 (name == null || name.equals("")) ? "Fault" : name, |
|
511 (prefix == null || prefix.equals("")) |
|
512 ? SOAPConstants.SOAP_ENV_PREFIX |
|
513 : prefix, |
|
514 SOAP12_NAMESPACE); |
|
515 } |
|
516 } |
|
517 |
|
518 static class NotUnderstood1_2Name extends NameImpl { |
|
519 NotUnderstood1_2Name(String prefix) { |
|
520 super( |
|
521 "NotUnderstood", |
|
522 (prefix == null || prefix.equals("")) |
|
523 ? SOAPConstants.SOAP_ENV_PREFIX |
|
524 : prefix, |
|
525 SOAP12_NAMESPACE); |
|
526 } |
|
527 } |
|
528 |
|
529 static class Upgrade1_2Name extends NameImpl { |
|
530 Upgrade1_2Name(String prefix) { |
|
531 super( |
|
532 "Upgrade", |
|
533 (prefix == null || prefix.equals("")) |
|
534 ? SOAPConstants.SOAP_ENV_PREFIX |
|
535 : prefix, |
|
536 SOAP12_NAMESPACE); |
|
537 } |
|
538 } |
|
539 |
|
540 static class SupportedEnvelope1_2Name extends NameImpl { |
|
541 SupportedEnvelope1_2Name(String prefix) { |
|
542 super( |
|
543 "SupportedEnvelope", |
|
544 (prefix == null || prefix.equals("")) |
|
545 ? SOAPConstants.SOAP_ENV_PREFIX |
|
546 : prefix, |
|
547 SOAP12_NAMESPACE); |
|
548 } |
|
549 } |
|
550 |
|
551 static class CodeSubcode1_2Name extends SOAP1_2Name { |
|
552 CodeSubcode1_2Name(String prefix, String localName) { |
|
553 super(prefix, localName); |
|
554 } |
|
555 } |
|
556 |
|
557 } |