1. /*
  2. * $Id: XmlNames.java,v 1.3 2001/03/16 04:01:34 edwingo Exp $
  3. *
  4. * The Apache Software License, Version 1.1
  5. *
  6. *
  7. * Copyright (c) 2000 The Apache Software Foundation. All rights
  8. * reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. *
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in
  19. * the documentation and/or other materials provided with the
  20. * distribution.
  21. *
  22. * 3. The end-user documentation included with the redistribution,
  23. * if any, must include the following acknowledgment:
  24. * "This product includes software developed by the
  25. * Apache Software Foundation (http://www.apache.org/)."
  26. * Alternately, this acknowledgment may appear in the software itself,
  27. * if and wherever such third-party acknowledgments normally appear.
  28. *
  29. * 4. The names "Crimson" and "Apache Software Foundation" must
  30. * not be used to endorse or promote products derived from this
  31. * software without prior written permission. For written
  32. * permission, please contact apache@apache.org.
  33. *
  34. * 5. Products derived from this software may not be called "Apache",
  35. * nor may "Apache" appear in their name, without prior written
  36. * permission of the Apache Software Foundation.
  37. *
  38. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  39. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  40. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  41. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  42. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  43. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  44. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  45. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  46. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  47. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  48. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  49. * SUCH DAMAGE.
  50. * ====================================================================
  51. *
  52. * This software consists of voluntary contributions made by many
  53. * individuals on behalf of the Apache Software Foundation and was
  54. * originally based on software copyright (c) 1999, Sun Microsystems, Inc.,
  55. * http://www.sun.com. For more information on the Apache Software
  56. * Foundation, please see <http://www.apache.org/>.
  57. */
  58. package com.sun.imageio.metadata;
  59. /**
  60. * This class contains static methods used to determine whether identifiers
  61. * may appear in certain roles in XML documents. Such methods are used
  62. * both to parse and to create such documents.
  63. *
  64. * @version 1.4
  65. * @author David Brownell
  66. */
  67. public class XmlNames
  68. {
  69. /**
  70. * Useful strings from the DOM Level 2 Spec
  71. */
  72. public static final String
  73. SPEC_XML_URI = "http://www.w3.org/XML/1998/namespace";
  74. public static final String
  75. SPEC_XMLNS_URI = "http://www.w3.org/2000/xmlns/";
  76. private XmlNames () { }
  77. /**
  78. * Returns true if the value is a legal XML name.
  79. *
  80. * @param value the string being tested
  81. */
  82. public static boolean isName (String value)
  83. {
  84. if (value == null || "".equals(value))
  85. return false;
  86. char c = value.charAt (0);
  87. if (!XmlChars.isLetter (c) && c != '_' && c != ':')
  88. return false;
  89. for (int i = 1; i < value.length (); i++)
  90. if (!XmlChars.isNameChar (value.charAt (i)))
  91. return false;
  92. return true;
  93. }
  94. /**
  95. * Returns true if the value is a legal "unqualified" XML name, as
  96. * defined in the XML Namespaces proposed recommendation.
  97. * These are normal XML names, except that they may not contain
  98. * a "colon" character.
  99. *
  100. * @param value the string being tested
  101. */
  102. public static boolean isUnqualifiedName (String value)
  103. {
  104. if (value == null || value.length() == 0)
  105. return false;
  106. char c = value.charAt (0);
  107. if (!XmlChars.isLetter (c) && c != '_')
  108. return false;
  109. for (int i = 1; i < value.length (); i++)
  110. if (!XmlChars.isNCNameChar (value.charAt (i)))
  111. return false;
  112. return true;
  113. }
  114. /**
  115. * Returns true if the value is a legal "qualified" XML name, as defined
  116. * in the XML Namespaces proposed recommendation. Qualified names are
  117. * composed of an optional prefix (an unqualified name), followed by a
  118. * colon, and a required "local part" (an unqualified name). Prefixes are
  119. * declared, and correspond to particular URIs which scope the "local
  120. * part" of the name. (This method cannot check whether the prefix of a
  121. * name has been declared.)
  122. *
  123. * @param value the string being tested
  124. */
  125. public static boolean isQualifiedName (String value)
  126. {
  127. if (value == null)
  128. return false;
  129. // [6] QName ::= (Prefix ':')? LocalPart
  130. // [7] Prefix ::= NCName
  131. // [8] LocalPart ::= NCName
  132. int first = value.indexOf (':');
  133. // no Prefix, only check LocalPart
  134. if (first <= 0)
  135. return isUnqualifiedName (value);
  136. // Prefix exists, check everything
  137. int last = value.lastIndexOf (':');
  138. if (last != first)
  139. return false;
  140. return isUnqualifiedName (value.substring (0, first))
  141. && isUnqualifiedName (value.substring (first + 1));
  142. }
  143. /**
  144. * This method returns true if the identifier is a "name token"
  145. * as defined in the XML specification. Like names, these
  146. * may only contain "name characters"; however, they do not need
  147. * to have letters as their initial characters. Attribute values
  148. * defined to be of type NMTOKEN(S) must satisfy this predicate.
  149. *
  150. * @param token the string being tested
  151. */
  152. public static boolean isNmtoken (String token)
  153. {
  154. int length = token.length ();
  155. for (int i = 0; i < length; i++)
  156. if (!XmlChars.isNameChar (token.charAt (i)))
  157. return false;
  158. return true;
  159. }
  160. /**
  161. * This method returns true if the identifier is a "name token" as
  162. * defined by the XML Namespaces proposed recommendation.
  163. * These are like XML "name tokens" but they may not contain the
  164. * "colon" character.
  165. *
  166. * @see #isNmtoken
  167. *
  168. * @param token the string being tested
  169. */
  170. public static boolean isNCNmtoken (String token)
  171. {
  172. return isNmtoken (token) && token.indexOf (':') < 0;
  173. }
  174. /**
  175. * Return the Prefix of qualifiedName. Does not check that Prefix is a
  176. * valid NCName.
  177. *
  178. * @param qualifiedName name to find the Prefix of
  179. * @return prefix or null if it has none
  180. */
  181. public static String getPrefix(String qualifiedName) {
  182. // [6] QName ::= (Prefix ':')? LocalPart
  183. // [7] Prefix ::= NCName
  184. int index = qualifiedName.indexOf(':');
  185. return index <= 0 ? null : qualifiedName.substring(0, index);
  186. }
  187. /**
  188. * Return the LocalPart of qualifiedName. Does not check that Prefix is a
  189. * valid NCName.
  190. *
  191. * @param qualifiedName name to find the LocalPart of
  192. * @return LocalPart or null if it has none
  193. */
  194. public static String getLocalPart(String qualifiedName) {
  195. // [6] QName ::= (Prefix ':')? LocalPart
  196. // [8] LocalPart ::= NCName
  197. int index = qualifiedName.indexOf(':');
  198. if (index < 0) {
  199. return qualifiedName;
  200. }
  201. // ':' at end of qualifiedName
  202. if (index == qualifiedName.length() - 1) {
  203. return null;
  204. }
  205. return qualifiedName.substring(index + 1);
  206. }
  207. }