Desktop tool for browsing account info from EVE-Online
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Namespace.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*--
  2. $Id: Namespace.java,v 1.43 2007/11/10 05:28:59 jhunter Exp $
  3. Copyright (C) 2000-2007 Jason Hunter & Brett McLaughlin.
  4. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions
  7. are met:
  8. 1. Redistributions of source code must retain the above copyright
  9. notice, this list of conditions, and the following disclaimer.
  10. 2. Redistributions in binary form must reproduce the above copyright
  11. notice, this list of conditions, and the disclaimer that follows
  12. these conditions in the documentation and/or other materials
  13. provided with the distribution.
  14. 3. The name "JDOM" must not be used to endorse or promote products
  15. derived from this software without prior written permission. For
  16. written permission, please contact <request_AT_jdom_DOT_org>.
  17. 4. Products derived from this software may not be called "JDOM", nor
  18. may "JDOM" appear in their name, without prior written permission
  19. from the JDOM Project Management <request_AT_jdom_DOT_org>.
  20. In addition, we request (but do not require) that you include in the
  21. end-user documentation provided with the redistribution and/or in the
  22. software itself an acknowledgement equivalent to the following:
  23. "This product includes software developed by the
  24. JDOM Project (http://www.jdom.org/)."
  25. Alternatively, the acknowledgment may be graphical using the logos
  26. available at http://www.jdom.org/images/logos.
  27. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  28. WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  29. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  30. DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
  31. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  33. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  34. USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  35. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  36. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  38. SUCH DAMAGE.
  39. This software consists of voluntary contributions made by many
  40. individuals on behalf of the JDOM Project and was originally
  41. created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
  42. Brett McLaughlin <brett_AT_jdom_DOT_org>. For more information
  43. on the JDOM Project, please see <http://www.jdom.org/>.
  44. */
  45. package org.jdom;
  46. import java.util.*;
  47. /**
  48. * An XML namespace representation, as well as a factory for creating XML
  49. * namespace objects. Namespaces are not Serializable, however objects that use
  50. * namespaces have special logic to handle serialization manually. These classes
  51. * call the getNamespace() method on deserialization to ensure there is one
  52. * unique Namespace object for any unique prefix/uri pair.
  53. *
  54. * @version $Revision: 1.43 $, $Date: 2007/11/10 05:28:59 $
  55. * @author Brett McLaughlin
  56. * @author Elliotte Rusty Harold
  57. * @author Jason Hunter
  58. * @author Wesley Biggs
  59. */
  60. public final class Namespace {
  61. // XXX May want to use weak references to keep the maps from growing
  62. // large with extended use
  63. // XXX We may need to make the namespaces HashMap synchronized with
  64. // reader/writer locks or perhaps make Namespace no longer a flyweight.
  65. // As written, multiple put() calls may happen from different threads
  66. // concurrently and cause a ConcurrentModificationException. See
  67. // http://lists.denveronline.net/lists/jdom-interest/2000-September/003009.html.
  68. // No one has ever reported this over the many years, so don't worry yet.
  69. private static final String CVS_ID =
  70. "@(#) $RCSfile: Namespace.java,v $ $Revision: 1.43 $ $Date: 2007/11/10 05:28:59 $ $Name: jdom_1_1 $";
  71. /**
  72. * Factory list of namespaces.
  73. * Keys are <i>prefix</i>&amp;<i>URI</i>.
  74. * Values are Namespace objects
  75. */
  76. private static HashMap namespaces;
  77. /** Define a <code>Namespace</code> for when <i>not</i> in a namespace */
  78. public static final Namespace NO_NAMESPACE = new Namespace("", "");
  79. /** Define a <code>Namespace</code> for the standard xml prefix. */
  80. public static final Namespace XML_NAMESPACE =
  81. new Namespace("xml", "http://www.w3.org/XML/1998/namespace");
  82. /** The prefix mapped to this namespace */
  83. private String prefix;
  84. /** The URI for this namespace */
  85. private String uri;
  86. /**
  87. * This static initializer acts as a factory contructor.
  88. * It sets up storage and required initial values.
  89. */
  90. static {
  91. namespaces = new HashMap(16);
  92. // Add the "empty" namespace
  93. namespaces.put(new NamespaceKey(NO_NAMESPACE), NO_NAMESPACE);
  94. namespaces.put(new NamespaceKey(XML_NAMESPACE), XML_NAMESPACE);
  95. }
  96. /**
  97. * This will retrieve (if in existence) or create (if not) a
  98. * <code>Namespace</code> for the supplied prefix and URI.
  99. *
  100. * @param prefix <code>String</code> prefix to map to
  101. * <code>Namespace</code>.
  102. * @param uri <code>String</code> URI of new <code>Namespace</code>.
  103. * @return <code>Namespace</code> - ready to use namespace.
  104. * @throws IllegalNameException if the given prefix and uri make up
  105. * an illegal namespace name.
  106. */
  107. public static Namespace getNamespace(String prefix, String uri) {
  108. // Sanity checking
  109. if ((prefix == null) || (prefix.trim().equals(""))) {
  110. // Short-cut out for common case of no namespace
  111. if ((uri == null) || (uri.trim().equals(""))) {
  112. return NO_NAMESPACE;
  113. }
  114. prefix = "";
  115. }
  116. else if ((uri == null) || (uri.trim().equals(""))) {
  117. uri = "";
  118. }
  119. // Return existing namespace if found. The preexisting namespaces
  120. // should all be legal. In other words, an illegal namespace won't
  121. // have been placed in this. Thus we can do this test before
  122. // verifying the URI and prefix.
  123. NamespaceKey lookup = new NamespaceKey(prefix, uri);
  124. Namespace preexisting = (Namespace) namespaces.get(lookup);
  125. if (preexisting != null) {
  126. return preexisting;
  127. }
  128. // Ensure proper naming
  129. String reason;
  130. if ((reason = Verifier.checkNamespacePrefix(prefix)) != null) {
  131. throw new IllegalNameException(prefix, "Namespace prefix", reason);
  132. }
  133. if ((reason = Verifier.checkNamespaceURI(uri)) != null) {
  134. throw new IllegalNameException(uri, "Namespace URI", reason);
  135. }
  136. // Unless the "empty" Namespace (no prefix and no URI), require a URI
  137. if ((!prefix.equals("")) && (uri.equals(""))) {
  138. throw new IllegalNameException("", "namespace",
  139. "Namespace URIs must be non-null and non-empty Strings");
  140. }
  141. // Handle XML namespace mislabels. If the user requested the correct
  142. // namespace and prefix -- xml, http://www.w3.org/XML/1998/namespace
  143. // -- then it was already returned from the preexisting namespaces.
  144. // Thus any use of the xml prefix or the
  145. // http://www.w3.org/XML/1998/namespace URI at this point must be
  146. // incorrect.
  147. if (prefix.equals("xml")) {
  148. throw new IllegalNameException(prefix, "Namespace prefix",
  149. "The xml prefix can only be bound to " +
  150. "http://www.w3.org/XML/1998/namespace");
  151. }
  152. // The erratum to Namespaces in XML 1.0 that suggests this
  153. // next check is controversial. Not everyone accepts it.
  154. if (uri.equals("http://www.w3.org/XML/1998/namespace")) {
  155. throw new IllegalNameException(uri, "Namespace URI",
  156. "The http://www.w3.org/XML/1998/namespace must be bound to " +
  157. "the xml prefix.");
  158. }
  159. // Finally, store and return
  160. Namespace ns = new Namespace(prefix, uri);
  161. namespaces.put(lookup, ns);
  162. return ns;
  163. }
  164. /**
  165. * This will retrieve (if in existence) or create (if not) a
  166. * <code>Namespace</code> for the supplied URI, and make it usable
  167. * as a default namespace, as no prefix is supplied.
  168. *
  169. * @param uri <code>String</code> URI of new <code>Namespace</code>.
  170. * @return <code>Namespace</code> - ready to use namespace.
  171. */
  172. public static Namespace getNamespace(String uri) {
  173. return getNamespace("", uri);
  174. }
  175. /**
  176. * This constructor handles creation of a <code>Namespace</code> object
  177. * with a prefix and URI; it is intentionally left <code>private</code>
  178. * so that it cannot be invoked by external programs/code.
  179. *
  180. * @param prefix <code>String</code> prefix to map to this namespace.
  181. * @param uri <code>String</code> URI for namespace.
  182. */
  183. private Namespace(String prefix, String uri) {
  184. this.prefix = prefix;
  185. this.uri = uri;
  186. }
  187. /**
  188. * This returns the prefix mapped to this <code>Namespace</code>.
  189. *
  190. * @return <code>String</code> - prefix for this <code>Namespace</code>.
  191. */
  192. public String getPrefix() {
  193. return prefix;
  194. }
  195. /**
  196. * This returns the namespace URI for this <code>Namespace</code>.
  197. *
  198. * @return <code>String</code> - URI for this <code>Namespace</code>.
  199. */
  200. public String getURI() {
  201. return uri;
  202. }
  203. /**
  204. * This tests for equality - Two <code>Namespaces</code>
  205. * are equal if and only if their URIs are byte-for-byte equals.
  206. *
  207. * @param ob <code>Object</code> to compare to this <code>Namespace</code>.
  208. * @return <code>boolean</code> - whether the supplied object is equal to
  209. * this <code>Namespace</code>.
  210. */
  211. public boolean equals(Object ob) {
  212. if (this == ob) {
  213. return true;
  214. }
  215. if (ob instanceof Namespace) { // instanceof returns false if null
  216. return uri.equals(((Namespace)ob).uri);
  217. }
  218. return false;
  219. }
  220. /**
  221. * This returns a <code>String</code> representation of this
  222. * <code>Namespace</code>, suitable for use in debugging.
  223. *
  224. * @return <code>String</code> - information about this instance.
  225. */
  226. public String toString() {
  227. return "[Namespace: prefix \"" + prefix + "\" is mapped to URI \"" +
  228. uri + "\"]";
  229. }
  230. /**
  231. * This returns a probably unique hash code for the <code>Namespace</code>.
  232. * If two namespaces have the same URI, they are equal and have the same
  233. * hash code, even if they have different prefixes.
  234. *
  235. * @return <code>int</code> - hash code for this <code>Namespace</code>.
  236. */
  237. public int hashCode() {
  238. return uri.hashCode();
  239. }
  240. }