1 /*
   2  * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.xml.internal.messaging.saaj.soap.name;
  27 
  28 import java.util.logging.Level;
  29 import java.util.logging.Logger;
  30 
  31 import javax.xml.namespace.QName;
  32 import javax.xml.soap.Name;
  33 import javax.xml.soap.SOAPConstants;
  34 
  35 //import com.sun.org.apache.xerces.internal.xni.NamespaceContext;
  36 import org.w3c.dom.Element;
  37 import com.sun.xml.internal.messaging.saaj.util.LogDomainConstants;
  38 
  39 public class NameImpl implements Name {
  40     public static final String XML_NAMESPACE_PREFIX = "xml";
  41     public static final String XML_SCHEMA_NAMESPACE_PREFIX = "xs";
  42     public static final String SOAP_ENVELOPE_PREFIX = "SOAP-ENV";
  43 
  44     public static final String XML_NAMESPACE =
  45         "http://www.w3.org/XML/1998/namespace";
  46     public static final String SOAP11_NAMESPACE =
  47         SOAPConstants.URI_NS_SOAP_ENVELOPE;
  48     public static final String SOAP12_NAMESPACE =
  49         SOAPConstants.URI_NS_SOAP_1_2_ENVELOPE;
  50     public static final String XML_SCHEMA_NAMESPACE =
  51         "http://www.w3.org/2001/XMLSchema";
  52 
  53     protected String uri = "";
  54     protected String localName = "";
  55     protected String prefix = "";
  56     private String qualifiedName = null;
  57 
  58     protected static final Logger log =
  59         Logger.getLogger(LogDomainConstants.NAMING_DOMAIN,
  60                          "com.sun.xml.internal.messaging.saaj.soap.name.LocalStrings");
  61 
  62     /**
  63      * XML Information Set REC
  64      * all namespace attributes (including those named xmlns,
  65      * whose [prefix] property has no value) have a namespace URI of http://www.w3.org/2000/xmlns/
  66      */
  67     public final static String XMLNS_URI = "http://www.w3.org/2000/xmlns/".intern();
  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 = XMLNS_URI;
  80         }
  81         if (this.uri.equals(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     @Override
 222     public boolean equals(Object obj) {
 223         if (!(obj instanceof Name)) {
 224             return false;
 225         }
 226 
 227         Name otherName = (Name) obj;
 228 
 229         if (!uri.equals(otherName.getURI())) {
 230             return false;
 231         }
 232 
 233         if (!localName.equals(otherName.getLocalName())) {
 234             return false;
 235         }
 236 
 237         return true;
 238     }
 239 
 240     @Override
 241     public int hashCode() {
 242         return localName.hashCode();
 243     }
 244 
 245     /**
 246      * Get the local name part of this XML Name.
 247      *
 248      * @return a string for the local name.
 249      */
 250     @Override
 251     public String getLocalName() {
 252         return localName;
 253     }
 254 
 255     /* getQualifiedName is inherited from QName */
 256 
 257     /**
 258      * Returns the prefix associated with the namespace of the name.
 259      *
 260      * @return the prefix as a string.
 261      */
 262     @Override
 263     public String getPrefix() {
 264         return prefix;
 265     }
 266 
 267     /**
 268      * Returns the URI associated of the namespace.
 269      *
 270      * @return the uri as a string.
 271      */
 272     @Override
 273     public String getURI() {
 274         return uri;
 275     }
 276 
 277     /**
 278      * Returns a String version of the name suitable for use in an XML document.
 279      */
 280     @Override
 281     public String getQualifiedName() {
 282         if (qualifiedName == null) {
 283             if (prefix != null && prefix.length() > 0) {
 284                 qualifiedName = prefix + ":" + localName;
 285             } else {
 286                 qualifiedName = localName;
 287             }
 288         }
 289         return qualifiedName;
 290     }
 291 
 292     /**
 293      * Create a name object for a SOAP1.1 Envelope.
 294      *
 295      * @param prefix prefix
 296      * @return Envelope Name
 297      */
 298     public static NameImpl createEnvelope1_1Name(String prefix) {
 299         return new Envelope1_1Name(prefix);
 300     }
 301 
 302     /**
 303      * Create a name object for a SOAP1.2 Envelope.
 304      *
 305      * @param prefix prefix
 306      * @return Envelope Name
 307      */
 308     public static NameImpl createEnvelope1_2Name(String prefix) {
 309         return new Envelope1_2Name(prefix);
 310     }
 311 
 312     /**
 313      * Create a name object for a SOAP1.1 Header.
 314      * @param prefix prefix
 315      * @return Header Name
 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      * @param prefix prefix
 324      * @return Header Name
 325      */
 326     public static NameImpl createHeader1_2Name(String prefix) {
 327         return new Header1_2Name(prefix);
 328     }
 329 
 330     /**
 331      * Create a name object for a SOAP1.1 Body.
 332      * @param prefix prefix
 333      * @return Body Name
 334      */
 335     public static NameImpl createBody1_1Name(String prefix) {
 336         return new Body1_1Name(prefix);
 337     }
 338 
 339     /**
 340      * Create a name object for a SOAP1.2 Body.
 341      * @param prefix prefix
 342      * @return Body Name
 343      */
 344     public static NameImpl createBody1_2Name(String prefix) {
 345         return new Body1_2Name(prefix);
 346     }
 347 
 348     /**
 349      * Create a name object for a SOAP1.1 Fault.
 350      * @param prefix prefix
 351      * @return Fault Name
 352      */
 353     public static NameImpl createFault1_1Name(String prefix) {
 354         return new Fault1_1Name(prefix);
 355     }
 356 
 357     /**
 358      * Create a name object for a SOAP1.2 NotUnderstood element.
 359      * @param prefix prefix
 360      * @return NotUnderstood Name
 361      */
 362     public static NameImpl createNotUnderstood1_2Name(String prefix) {
 363         return new NotUnderstood1_2Name(prefix);
 364     }
 365 
 366     /**
 367      * Create a name object for a SOAP1.2 Upgrade element.
 368      * @param prefix prefix
 369      * @return Upgrade Name
 370      */
 371     public static NameImpl createUpgrade1_2Name(String prefix) {
 372         return new Upgrade1_2Name(prefix);
 373     }
 374 
 375     /**
 376      * Create a name object for a SOAP1.2 SupportedEnvelope Upgrade element.
 377      * @param prefix prefix
 378      * @return Supported Envelope Name
 379      */
 380     public static NameImpl createSupportedEnvelope1_2Name(String prefix) {
 381         return new SupportedEnvelope1_2Name(prefix);
 382     }
 383 
 384     /**
 385      * Create a name object for a SOAP1.2
 386      * Fault, Reason or Detail.
 387      *
 388      * @param localName Local Name of element
 389      * @param prefix prefix
 390      * @return Fault Name
 391      */
 392     public static NameImpl createFault1_2Name(
 393         String localName,
 394         String prefix) {
 395         return new Fault1_2Name(localName, prefix);
 396     }
 397 
 398     /**
 399      * Create a name object for a SOAP1.2 Fault/Code or Subcode.
 400      *
 401      * @param localName Either "Code" or "Subcode"
 402      * @param prefix prefix
 403      * @return CodeSubcode Name
 404      */
 405     public static NameImpl createCodeSubcode1_2Name(
 406         String prefix,
 407         String localName) {
 408         return new CodeSubcode1_2Name(localName, prefix);
 409     }
 410 
 411     /**
 412      * Create a name object for a SOAP1.1 Fault Detail.
 413      * @return Detail Name
 414      */
 415     public static NameImpl createDetail1_1Name() {
 416         return new Detail1_1Name();
 417     }
 418 
 419     public static NameImpl createDetail1_1Name(String prefix) {
 420         return new Detail1_1Name(prefix);
 421     }
 422 
 423     public static NameImpl createFaultElement1_1Name(String localName) {
 424         return new FaultElement1_1Name(localName);
 425     }
 426 
 427     public static NameImpl createFaultElement1_1Name(String localName,
 428                                                      String prefix) {
 429         return new FaultElement1_1Name(localName, prefix);
 430     }
 431 
 432     public static NameImpl createSOAP11Name(String string) {
 433         return new SOAP1_1Name(string, null);
 434     }
 435     public static NameImpl createSOAP12Name(String string) {
 436         return new SOAP1_2Name(string, null);
 437     }
 438 
 439     public static NameImpl createSOAP12Name(String localName, String prefix) {
 440         return new SOAP1_2Name(localName, prefix);
 441     }
 442 
 443     public static NameImpl createXmlName(String localName) {
 444         return new NameImpl(localName, XML_NAMESPACE_PREFIX, XML_NAMESPACE);
 445     }
 446 
 447     public static Name copyElementName(Element element) {
 448         String localName = element.getLocalName();
 449         String prefix = element.getPrefix();
 450         String uri = element.getNamespaceURI();
 451         return create(localName, prefix, uri);
 452     }
 453 
 454 
 455 static class SOAP1_1Name extends NameImpl {
 456     SOAP1_1Name(String name, String prefix) {
 457         super(
 458             name,
 459             (prefix == null || prefix.equals(""))
 460                 ? NameImpl.SOAP_ENVELOPE_PREFIX
 461                 : prefix,
 462             NameImpl.SOAP11_NAMESPACE);
 463     }
 464 }
 465 
 466 static class Envelope1_1Name extends SOAP1_1Name {
 467     Envelope1_1Name(String prefix) {
 468         super("Envelope", prefix);
 469     }
 470 }
 471 
 472 static class Header1_1Name extends SOAP1_1Name {
 473     Header1_1Name(String prefix) {
 474         super("Header", prefix);
 475     }
 476 }
 477 
 478 static class Body1_1Name extends SOAP1_1Name {
 479     Body1_1Name(String prefix) {
 480         super("Body", prefix);
 481     }
 482 }
 483 
 484 static class Fault1_1Name extends NameImpl {
 485     Fault1_1Name(String prefix) {
 486         super(
 487             "Fault",
 488             (prefix == null || prefix.equals(""))
 489                 ? SOAP_ENVELOPE_PREFIX
 490                 : prefix,
 491             SOAP11_NAMESPACE);
 492     }
 493 }
 494 
 495 static class Detail1_1Name extends NameImpl {
 496     Detail1_1Name() {
 497         super("detail");
 498     }
 499 
 500     Detail1_1Name(String prefix) {
 501         super("detail", prefix, "");
 502     }
 503 }
 504 
 505 static class FaultElement1_1Name extends NameImpl {
 506     FaultElement1_1Name(String localName) {
 507         super(localName);
 508     }
 509 
 510     FaultElement1_1Name(String localName, String prefix) {
 511         super(localName, prefix, "");
 512     }
 513 }
 514 
 515 static class SOAP1_2Name extends NameImpl {
 516     SOAP1_2Name(String name, String prefix) {
 517         super(
 518             name,
 519             (prefix == null || prefix.equals(""))
 520                 ? SOAPConstants.SOAP_ENV_PREFIX
 521                 : prefix,
 522             SOAP12_NAMESPACE);
 523     }
 524 }
 525 
 526 static class Envelope1_2Name extends SOAP1_2Name {
 527     Envelope1_2Name(String prefix) {
 528         super("Envelope", prefix);
 529     }
 530 }
 531 
 532 static class Header1_2Name extends SOAP1_2Name {
 533     Header1_2Name(String prefix) {
 534         super("Header", prefix);
 535     }
 536 }
 537 
 538 static class Body1_2Name extends SOAP1_2Name {
 539     Body1_2Name(String prefix) {
 540         super("Body", prefix);
 541     }
 542 }
 543 
 544 static class Fault1_2Name extends NameImpl {
 545     Fault1_2Name(String name, String prefix) {
 546         super(
 547             (name == null || name.equals("")) ? "Fault" : name,
 548             (prefix == null || prefix.equals(""))
 549                 ? SOAPConstants.SOAP_ENV_PREFIX
 550                 : prefix,
 551             SOAP12_NAMESPACE);
 552     }
 553 }
 554 
 555 static class NotUnderstood1_2Name extends NameImpl {
 556     NotUnderstood1_2Name(String prefix) {
 557         super(
 558             "NotUnderstood",
 559             (prefix == null || prefix.equals(""))
 560                 ? SOAPConstants.SOAP_ENV_PREFIX
 561                 : prefix,
 562             SOAP12_NAMESPACE);
 563     }
 564 }
 565 
 566 static class Upgrade1_2Name extends NameImpl {
 567     Upgrade1_2Name(String prefix) {
 568         super(
 569             "Upgrade",
 570             (prefix == null || prefix.equals(""))
 571                 ? SOAPConstants.SOAP_ENV_PREFIX
 572                 : prefix,
 573             SOAP12_NAMESPACE);
 574     }
 575 }
 576 
 577 static class SupportedEnvelope1_2Name extends NameImpl {
 578     SupportedEnvelope1_2Name(String prefix) {
 579         super(
 580             "SupportedEnvelope",
 581             (prefix == null || prefix.equals(""))
 582                 ? SOAPConstants.SOAP_ENV_PREFIX
 583                 : prefix,
 584             SOAP12_NAMESPACE);
 585     }
 586 }
 587 
 588 static class CodeSubcode1_2Name extends SOAP1_2Name {
 589     CodeSubcode1_2Name(String prefix, String localName) {
 590         super(prefix, localName);
 591     }
 592 }
 593 
 594 }