001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the "License"). You may not use this file except
005: * in compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://jwsdp.dev.java.net/CDDLv1.0.html
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * HEADER in each file and include the License file at
014: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
015: * add the following below this CDDL HEADER, with the
016: * fields enclosed by brackets "[]" replaced with your
017: * own identifying information: Portions Copyright [yyyy]
018: * [name of copyright owner]
019: */
020: /*
021: * $Id: NameImpl.java,v 1.2 2007/07/16 16:41:23 ofung Exp $
022: * $Revision: 1.2 $
023: * $Date: 2007/07/16 16:41:23 $
024: */
025:
026: /*
027: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
028: *
029: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
030: *
031: * The contents of this file are subject to the terms of either the GNU
032: * General Public License Version 2 only ("GPL") or the Common Development
033: * and Distribution License("CDDL") (collectively, the "License"). You
034: * may not use this file except in compliance with the License. You can obtain
035: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
036: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
037: * language governing permissions and limitations under the License.
038: *
039: * When distributing the software, include this License Header Notice in each
040: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
041: * Sun designates this particular file as subject to the "Classpath" exception
042: * as provided by Sun in the GPL Version 2 section of the License file that
043: * accompanied this code. If applicable, add the following below the License
044: * Header, with the fields enclosed by brackets [] replaced by your own
045: * identifying information: "Portions Copyrighted [year]
046: * [name of copyright owner]"
047: *
048: * Contributor(s):
049: *
050: * If you wish your version of this file to be governed by only the CDDL or
051: * only the GPL Version 2, indicate your decision by adding "[Contributor]
052: * elects to include this software in this distribution under the [CDDL or GPL
053: * Version 2] license." If you don't indicate a single choice of license, a
054: * recipient has the option to distribute your version of this file under
055: * either the CDDL, the GPL Version 2 or to extend the choice of license to
056: * its licensees as provided above. However, if you add GPL Version 2 code
057: * and therefore, elected the GPL Version 2 license, then the option applies
058: * only if the new code is made subject to such option by the copyright
059: * holder.
060: */
061: package com.sun.xml.messaging.saaj.soap.name;
062:
063: import java.util.logging.Level;
064: import java.util.logging.Logger;
065:
066: import javax.xml.namespace.QName;
067: import javax.xml.soap.Name;
068: import javax.xml.soap.SOAPConstants;
069:
070: import com.sun.org.apache.xerces.internal.xni.NamespaceContext;
071: import org.w3c.dom.Element;
072:
073: import com.sun.xml.messaging.saaj.soap.impl.ElementImpl;
074: import com.sun.xml.messaging.saaj.util.LogDomainConstants;
075:
076: public class NameImpl implements Name {
077: public static final String XML_NAMESPACE_PREFIX = "xml";
078: public static final String XML_SCHEMA_NAMESPACE_PREFIX = "xs";
079: public static final String SOAP_ENVELOPE_PREFIX = "SOAP-ENV";
080:
081: public static final String XML_NAMESPACE = "http://www.w3.org/XML/1998/namespace";
082: public static final String SOAP11_NAMESPACE = SOAPConstants.URI_NS_SOAP_ENVELOPE;
083: public static final String SOAP12_NAMESPACE = SOAPConstants.URI_NS_SOAP_1_2_ENVELOPE;
084: public static final String XML_SCHEMA_NAMESPACE = "http://www.w3.org/2001/XMLSchema";
085:
086: protected String uri = "";
087: protected String localName = "";
088: protected String prefix = "";
089: private String qualifiedName = null;
090:
091: protected static Logger log = Logger.getLogger(
092: LogDomainConstants.NAMING_DOMAIN,
093: "com.sun.xml.messaging.saaj.soap.name.LocalStrings");
094:
095: protected NameImpl(String name) {
096: this .localName = name == null ? "" : name;
097: }
098:
099: protected NameImpl(String name, String prefix, String uri) {
100: this .uri = uri == null ? "" : uri;
101: this .localName = name == null ? "" : name;
102: this .prefix = prefix == null ? "" : prefix;
103:
104: if (this .prefix.equals("xmlns") && this .uri.equals("")) {
105: this .uri = NamespaceContext.XMLNS_URI;
106: }
107: if (this .uri.equals(NamespaceContext.XMLNS_URI)
108: && this .prefix.equals("")) {
109: this .prefix = "xmlns";
110: }
111: }
112:
113: public static Name convertToName(QName qname) {
114: return new NameImpl(qname.getLocalPart(), qname.getPrefix(),
115: qname.getNamespaceURI());
116: }
117:
118: public static QName convertToQName(Name name) {
119: return new QName(name.getURI(), name.getLocalName(), name
120: .getPrefix());
121: }
122:
123: public static NameImpl createFromUnqualifiedName(String name) {
124: return new NameImpl(name);
125: }
126:
127: public static Name createFromTagName(String tagName) {
128: return createFromTagAndUri(tagName, "");
129: }
130:
131: public static Name createFromQualifiedName(String qualifiedName,
132: String uri) {
133: return createFromTagAndUri(qualifiedName, uri);
134: }
135:
136: protected static Name createFromTagAndUri(String tagName, String uri) {
137: if (tagName == null) {
138: log.severe("SAAJ0201.name.not.created.from.null.tag");
139: throw new IllegalArgumentException(
140: "Cannot create a name from a null tag.");
141: }
142: int index = tagName.indexOf(':');
143: if (index < 0) {
144: return new NameImpl(tagName, "", uri);
145: } else {
146: return new NameImpl(tagName.substring(index + 1), tagName
147: .substring(0, index), uri);
148: }
149: }
150:
151: protected static int getPrefixSeparatorIndex(String qualifiedName) {
152: int index = qualifiedName.indexOf(':');
153: if (index < 0) {
154: log.log(Level.SEVERE, "SAAJ0202.name.invalid.arg.format",
155: new String[] { qualifiedName });
156: throw new IllegalArgumentException("Argument \""
157: + qualifiedName
158: + "\" must be of the form: \"prefix:localName\"");
159: }
160: return index;
161: }
162:
163: public static String getPrefixFromQualifiedName(String qualifiedName) {
164: return qualifiedName.substring(0,
165: getPrefixSeparatorIndex(qualifiedName));
166: }
167:
168: public static String getLocalNameFromQualifiedName(
169: String qualifiedName) {
170: return qualifiedName
171: .substring(getPrefixSeparatorIndex(qualifiedName) + 1);
172: }
173:
174: public static String getPrefixFromTagName(String tagName) {
175: if (isQualified(tagName)) {
176: return getPrefixFromQualifiedName(tagName);
177: }
178: return "";
179: }
180:
181: public static String getLocalNameFromTagName(String tagName) {
182: if (isQualified(tagName)) {
183: return getLocalNameFromQualifiedName(tagName);
184: }
185: return tagName;
186: }
187:
188: public static boolean isQualified(String tagName) {
189: return tagName.indexOf(':') >= 0;
190: }
191:
192: public static NameImpl create(String name, String prefix, String uri) {
193: if (prefix == null)
194: prefix = "";
195: if (uri == null)
196: uri = "";
197: if (name == null)
198: name = "";
199:
200: if (!uri.equals("") && !name.equals("")) {
201: if (uri.equals(NameImpl.SOAP11_NAMESPACE)) {
202: if (name.equalsIgnoreCase("Envelope"))
203: return createEnvelope1_1Name(prefix);
204: else if (name.equalsIgnoreCase("Header"))
205: return createHeader1_1Name(prefix);
206: else if (name.equalsIgnoreCase("Body"))
207: return createBody1_1Name(prefix);
208: else if (name.equalsIgnoreCase("Fault"))
209: return createFault1_1Name(prefix);
210: else
211: return new SOAP1_1Name(name, prefix);
212: } else if (uri.equals(SOAP12_NAMESPACE)) {
213: if (name.equalsIgnoreCase("Envelope"))
214: return createEnvelope1_2Name(prefix);
215: else if (name.equalsIgnoreCase("Header"))
216: return createHeader1_2Name(prefix);
217: else if (name.equalsIgnoreCase("Body"))
218: return createBody1_2Name(prefix);
219: else if (name.equals("Fault") || name.equals("Reason")
220: || name.equals("Detail"))
221: return createFault1_2Name(name, prefix);
222: else if (name.equals("Code") || name.equals("Subcode"))
223: return createCodeSubcode1_2Name(prefix, name);
224: else
225: return new SOAP1_2Name(name, prefix);
226: }
227:
228: }
229: return new NameImpl(name, prefix, uri);
230: }
231:
232: public static String createQName(String prefix, String localName) {
233: if (prefix == null || prefix.equals("")) {
234: return localName;
235: }
236: return prefix + ":" + localName;
237: }
238:
239: public boolean equals(Object obj) {
240: if (!(obj instanceof Name)) {
241: return false;
242: }
243:
244: Name otherName = (Name) obj;
245:
246: if (!uri.equals(otherName.getURI())) {
247: return false;
248: }
249:
250: if (!localName.equals(otherName.getLocalName())) {
251: return false;
252: }
253:
254: return true;
255: }
256:
257: /**
258: * Get the local name part of this XML Name.
259: *
260: * @return a string for the local name.
261: */
262: public String getLocalName() {
263: return localName;
264: }
265:
266: /* getQualifiedName is inherited from QName */
267:
268: /**
269: * Returns the prefix associated with the namespace of the name.
270: *
271: * @return the prefix as a string.
272: */
273: public String getPrefix() {
274: return prefix;
275: }
276:
277: /**
278: * Returns the URI associated of the namespace.
279: *
280: * @return the uri as a string.
281: */
282: public String getURI() {
283: return uri;
284: }
285:
286: /**
287: * Returns a String version of the name suitable for use in an XML document.
288: */
289: public String getQualifiedName() {
290: if (qualifiedName == null) {
291: if (prefix != null && prefix.length() > 0) {
292: qualifiedName = prefix + ":" + localName;
293: } else {
294: qualifiedName = localName;
295: }
296: }
297: return qualifiedName;
298: }
299:
300: /**
301: * Create a name object for a SOAP1.1 Envelope.
302: */
303: public static NameImpl createEnvelope1_1Name(String prefix) {
304: return new Envelope1_1Name(prefix);
305: }
306:
307: /**
308: * Create a name object for a SOAP1.2 Envelope.
309: */
310: public static NameImpl createEnvelope1_2Name(String prefix) {
311: return new Envelope1_2Name(prefix);
312: }
313:
314: /**
315: * Create a name object for a SOAP1.1 Header.
316: */
317: public static NameImpl createHeader1_1Name(String prefix) {
318: return new Header1_1Name(prefix);
319: }
320:
321: /**
322: * Create a name object for a SOAP1.2 Header.
323: */
324: public static NameImpl createHeader1_2Name(String prefix) {
325: return new Header1_2Name(prefix);
326: }
327:
328: /**
329: * Create a name object for a SOAP1.1 Body.
330: */
331: public static NameImpl createBody1_1Name(String prefix) {
332: return new Body1_1Name(prefix);
333: }
334:
335: /**
336: * Create a name object for a SOAP1.2 Body.
337: */
338: public static NameImpl createBody1_2Name(String prefix) {
339: return new Body1_2Name(prefix);
340: }
341:
342: /**
343: * Create a name object for a SOAP1.1 Fault.
344: */
345: public static NameImpl createFault1_1Name(String prefix) {
346: return new Fault1_1Name(prefix);
347: }
348:
349: /**
350: * Create a name object for a SOAP1.2 NotUnderstood element.
351: */
352: public static NameImpl createNotUnderstood1_2Name(String prefix) {
353: return new NotUnderstood1_2Name(prefix);
354: }
355:
356: /**
357: * Create a name object for a SOAP1.2 Upgrade element.
358: */
359: public static NameImpl createUpgrade1_2Name(String prefix) {
360: return new Upgrade1_2Name(prefix);
361: }
362:
363: /**
364: * Create a name object for a SOAP1.2 SupportedEnvelope Upgrade element.
365: */
366: public static NameImpl createSupportedEnvelope1_2Name(String prefix) {
367: return new SupportedEnvelope1_2Name(prefix);
368: }
369:
370: /**
371: * Create a name object for a SOAP1.2
372: * Fault, Reason or Detail.
373: *
374: * @param localName Local Name of element
375: */
376: public static NameImpl createFault1_2Name(String localName,
377: String prefix) {
378: return new Fault1_2Name(localName, prefix);
379: }
380:
381: /**
382: * Create a name object for a SOAP1.2 Fault/Code or Subcode.
383: *
384: * @param localName Either "Code" or "Subcode"
385: */
386: public static NameImpl createCodeSubcode1_2Name(String prefix,
387: String localName) {
388: return new CodeSubcode1_2Name(localName, prefix);
389: }
390:
391: /**
392: * Create a name object for a SOAP1.1 Fault Detail.
393: */
394: public static NameImpl createDetail1_1Name() {
395: return new Detail1_1Name();
396: }
397:
398: public static NameImpl createDetail1_1Name(String prefix) {
399: return new Detail1_1Name(prefix);
400: }
401:
402: public static NameImpl createFaultElement1_1Name(String localName) {
403: return new FaultElement1_1Name(localName);
404: }
405:
406: public static NameImpl createFaultElement1_1Name(String localName,
407: String prefix) {
408: return new FaultElement1_1Name(localName, prefix);
409: }
410:
411: public static NameImpl createSOAP11Name(String string) {
412: return new SOAP1_1Name(string, null);
413: }
414:
415: public static NameImpl createSOAP12Name(String string) {
416: return new SOAP1_2Name(string, null);
417: }
418:
419: public static NameImpl createSOAP12Name(String localName,
420: String prefix) {
421: return new SOAP1_2Name(localName, prefix);
422: }
423:
424: public static NameImpl createXmlName(String localName) {
425: return new NameImpl(localName, XML_NAMESPACE_PREFIX,
426: XML_NAMESPACE);
427: }
428:
429: public static Name copyElementName(Element element) {
430: String localName = element.getLocalName();
431: String prefix = element.getPrefix();
432: String uri = element.getNamespaceURI();
433: return create(localName, prefix, uri);
434: }
435:
436: static class SOAP1_1Name extends NameImpl {
437: SOAP1_1Name(String name, String prefix) {
438: super (
439: name,
440: (prefix == null || prefix.equals("")) ? NameImpl.SOAP_ENVELOPE_PREFIX
441: : prefix, NameImpl.SOAP11_NAMESPACE);
442: }
443: }
444:
445: static class Envelope1_1Name extends SOAP1_1Name {
446: Envelope1_1Name(String prefix) {
447: super ("Envelope", prefix);
448: }
449: }
450:
451: static class Header1_1Name extends SOAP1_1Name {
452: Header1_1Name(String prefix) {
453: super ("Header", prefix);
454: }
455: }
456:
457: static class Body1_1Name extends SOAP1_1Name {
458: Body1_1Name(String prefix) {
459: super ("Body", prefix);
460: }
461: }
462:
463: static class Fault1_1Name extends NameImpl {
464: Fault1_1Name(String prefix) {
465: super (
466: "Fault",
467: (prefix == null || prefix.equals("")) ? SOAP_ENVELOPE_PREFIX
468: : prefix, SOAP11_NAMESPACE);
469: }
470: }
471:
472: static class Detail1_1Name extends NameImpl {
473: Detail1_1Name() {
474: super ("detail");
475: }
476:
477: Detail1_1Name(String prefix) {
478: super ("detail", prefix, "");
479: }
480: }
481:
482: static class FaultElement1_1Name extends NameImpl {
483: FaultElement1_1Name(String localName) {
484: super (localName);
485: }
486:
487: FaultElement1_1Name(String localName, String prefix) {
488: super (localName, prefix, "");
489: }
490: }
491:
492: static class SOAP1_2Name extends NameImpl {
493: SOAP1_2Name(String name, String prefix) {
494: super (
495: name,
496: (prefix == null || prefix.equals("")) ? SOAPConstants.SOAP_ENV_PREFIX
497: : prefix, SOAP12_NAMESPACE);
498: }
499: }
500:
501: static class Envelope1_2Name extends SOAP1_2Name {
502: Envelope1_2Name(String prefix) {
503: super ("Envelope", prefix);
504: }
505: }
506:
507: static class Header1_2Name extends SOAP1_2Name {
508: Header1_2Name(String prefix) {
509: super ("Header", prefix);
510: }
511: }
512:
513: static class Body1_2Name extends SOAP1_2Name {
514: Body1_2Name(String prefix) {
515: super ("Body", prefix);
516: }
517: }
518:
519: static class Fault1_2Name extends NameImpl {
520: Fault1_2Name(String name, String prefix) {
521: super (
522: (name == null || name.equals("")) ? "Fault" : name,
523: (prefix == null || prefix.equals("")) ? SOAPConstants.SOAP_ENV_PREFIX
524: : prefix, SOAP12_NAMESPACE);
525: }
526: }
527:
528: static class NotUnderstood1_2Name extends NameImpl {
529: NotUnderstood1_2Name(String prefix) {
530: super (
531: "NotUnderstood",
532: (prefix == null || prefix.equals("")) ? SOAPConstants.SOAP_ENV_PREFIX
533: : prefix, SOAP12_NAMESPACE);
534: }
535: }
536:
537: static class Upgrade1_2Name extends NameImpl {
538: Upgrade1_2Name(String prefix) {
539: super (
540: "Upgrade",
541: (prefix == null || prefix.equals("")) ? SOAPConstants.SOAP_ENV_PREFIX
542: : prefix, SOAP12_NAMESPACE);
543: }
544: }
545:
546: static class SupportedEnvelope1_2Name extends NameImpl {
547: SupportedEnvelope1_2Name(String prefix) {
548: super ("SupportedEnvelope", (prefix == null || prefix
549: .equals("")) ? SOAPConstants.SOAP_ENV_PREFIX
550: : prefix, SOAP12_NAMESPACE);
551: }
552: }
553:
554: static class CodeSubcode1_2Name extends SOAP1_2Name {
555: CodeSubcode1_2Name(String prefix, String localName) {
556: super(prefix, localName);
557: }
558: }
559:
560: }
|