OdfName.java

  1. /**
  2.  * **********************************************************************
  3.  *
  4.  * <p>DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
  5.  *
  6.  * <p>Copyright 2008, 2010 Oracle and/or its affiliates. All rights reserved.
  7.  *
  8.  * <p>Use is subject to license terms.
  9.  *
  10.  * <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
  11.  * except in compliance with the License. You may obtain a copy of the License at
  12.  * http://www.apache.org/licenses/LICENSE-2.0. You can also obtain a copy of the License at
  13.  * http://odftoolkit.org/docs/license.txt
  14.  *
  15.  * <p>Unless required by applicable law or agreed to in writing, software distributed under the
  16.  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  17.  * express or implied.
  18.  *
  19.  * <p>See the License for the specific language governing permissions and limitations under the
  20.  * License.
  21.  *
  22.  * <p>**********************************************************************
  23.  */
  24. package org.odftoolkit.odfdom.pkg;

  25. import java.util.HashMap;

  26. /**
  27.  * The class provides a simplified interface for XML names. The class defines a name for an XML
  28.  * node. It embraces XML NamespaceURI, XML prefix and XML localname.
  29.  */
  30. public class OdfName implements Comparable<OdfName> {

  31.   private OdfNamespace mNS;
  32.   private String mLocalName;
  33.   private String mExpandedName; // i.e. {nsURI}localName
  34.   private static HashMap<String, OdfName> mOdfNames = new HashMap<String, OdfName>();
  35.   private static StringBuilder mSB;

  36.   private OdfName(OdfNamespace ns, String localname, String expandedName) {
  37.     mNS = ns;
  38.     mLocalName = localname;
  39.     mExpandedName = expandedName;
  40.   }

  41.   /**
  42.    * Returns the OdfName for the given namespace and name. Creates a new one, if the OdfName was not
  43.    * asked before.
  44.    *
  45.    * @param name of the XML node
  46.    * @return the OdfName for the given OdfNamesapce and name.
  47.    */
  48.   public static OdfName newName(String name) {
  49.     return createName(null, name);
  50.   }

  51.   /**
  52.    * Returns the OdfName for the given namespace and name. Creates a new one, if the OdfName was not
  53.    * asked before.
  54.    *
  55.    * @param odfNamespace the namespace of the name to be created
  56.    * @param name of the XML node. Can be both local or qualified name.
  57.    * @return the OdfName for the given OdfNamesapce and name.
  58.    */
  59.   public static OdfName newName(OdfNamespace odfNamespace, String name) {
  60.     return createName(odfNamespace, name);
  61.   }

  62.   /**
  63.    * Returns the OdfName for the given namespace and name. Creates a new one, if the OdfName was not
  64.    * asked before.
  65.    *
  66.    * @param namespaceNamed represents a W3C Namespace Name. The interface <code>NamespaceName</code>
  67.    *     is often implemented by an enum.
  68.    * @param name of the XML node. Can be both local or qualified name.
  69.    * @return the OdfName for the given OdfNamesapce and name.
  70.    */
  71.   public static OdfName newName(NamespaceName namespaceNamed, String name) {
  72.     return createName(OdfNamespace.newNamespace(namespaceNamed), name);
  73.   }

  74.   public static OdfName newName(String uri, String qname) {
  75.     String prefix = OdfNamespace.getPrefixPart(qname);
  76.     String localName = OdfNamespace.getLocalPart(qname);
  77.     OdfNamespace ns = OdfNamespace.newNamespace(prefix, uri);
  78.     return createName(ns, localName);
  79.   }

  80.   private static OdfName createName(OdfNamespace odfNamespace, String name) {
  81.     int i = 0;
  82.     // make sure the name is the local name
  83.     if ((i = name.indexOf(':')) >= 0) {
  84.       name = name.substring(i + 1);
  85.     }
  86.     return getOdfName(odfNamespace, name);
  87.   }

  88.   /** Used for receiving an OdfName, in case of a default nameaspace, when there is no prefix. */
  89.   public static OdfName getOdfName(OdfNamespace odfNamespace, String localName) {
  90.     String expandedName = null;
  91.     if (odfNamespace != null) {
  92.       expandedName = createExpandedName(odfNamespace.toString(), localName);
  93.     } else {
  94.       expandedName = localName;
  95.     }
  96.     // return a similar OdfName if one was already created before..
  97.     OdfName odfName = mOdfNames.get(expandedName);
  98.     if (odfName != null) {
  99.       return odfName;
  100.     } else {
  101.       // otherwise create a new OdfName, store it in the map and return it..
  102.       odfName = new OdfName(odfNamespace, localName, expandedName);
  103.       mOdfNames.put(expandedName, odfName);
  104.       return odfName;
  105.     }
  106.   }

  107.   private static String createExpandedName(String nsUri, String localName) {
  108.     if (mSB == null) {
  109.       mSB = new StringBuilder();
  110.     } else {
  111.       mSB.delete(0, mSB.length());
  112.     }
  113.     mSB.append('{');
  114.     mSB.append(nsUri);
  115.     mSB.append('}');
  116.     mSB.append(localName);
  117.     return mSB.toString();
  118.   }

  119.   /**
  120.    * @return the XML Namespace URI, for <text:p> it would be
  121.    *     urn:oasis:names:tc:opendocument:xmlns:text:1.0
  122.    */
  123.   public String getUri() {
  124.     if (mNS == null) {
  125.       return null;
  126.     } else {
  127.       return mNS.getUri();
  128.     }
  129.   }

  130.   /** @return the XML localname, for <text:p> it would be p. */
  131.   public String getLocalName() {
  132.     return mLocalName;
  133.   }

  134.   /** @return the XML prefix, for <text:p> it would be text. */
  135.   public String getPrefix() {
  136.     String prefix = null;
  137.     if (mNS != null) {
  138.       prefix = mNS.getPrefix();
  139.     }
  140.     return prefix;
  141.   }

  142.   /** @return the XML QName, the qualified name e.g. for <text:p> it is text:p. */
  143.   public String getQName() {
  144.     if (mNS != null) {
  145.       return ((mNS.getPrefix() + ":" + mLocalName).intern());
  146.     } else {
  147.       return mLocalName;
  148.     }
  149.   }

  150.   /**
  151.    * @return the OdfName as String, represented by a concatenation of XML Namespace URI (within
  152.    *     brackets) and local name, as for <text:p> it would be
  153.    *     {urn:oasis:names:tc:opendocument:xmlns:text:1.0}p
  154.    */
  155.   @Override
  156.   public String toString() {
  157.     return mExpandedName;
  158.   }

  159.   @Override
  160.   @SuppressWarnings("EqualsWhichDoesntCheckParameterClass")
  161.   public boolean equals(Object obj) {
  162.     if (obj != null) {
  163.       return toString().equals(obj.toString());
  164.     } else {
  165.       return false;
  166.     }
  167.   }

  168.   /**
  169.    * @param namespaceUri of the XML node to be compared.
  170.    * @param name of the XML node to be compared. Can be qualifed name or localname.
  171.    * @return true if the given OdfName has the same namespaceURI and localname.
  172.    */
  173.   public boolean equals(String namespaceUri, String name) {
  174.     if (!mNS.getUri().equals(namespaceUri)) {
  175.       return false;
  176.     }

  177.     int beginIndex = name.indexOf(':');
  178.     if (beginIndex >= 0) {
  179.       return mLocalName.equals(name.substring(beginIndex + 1));
  180.     } else {
  181.       return mLocalName.equals(name);
  182.     }
  183.   }

  184.   @Override
  185.   /** Returns the hashcode of the OdfName */
  186.   public int hashCode() {
  187.     return toString().hashCode();
  188.   }

  189.   /** Compares the by parameter given OdfName with this OdfName */
  190.   public int compareTo(OdfName o) {
  191.     return toString().compareTo(o.toString());
  192.   }
  193. }