1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/dtdparser/XmlNames.java Tue Mar 06 16:09:35 2012 -0800 1.3 @@ -0,0 +1,147 @@ 1.4 +/* 1.5 + * Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.xml.internal.dtdparser; 1.30 + 1.31 + 1.32 +/** 1.33 + * This class contains static methods used to determine whether identifiers 1.34 + * may appear in certain roles in XML documents. Such methods are used 1.35 + * both to parse and to create such documents. 1.36 + * 1.37 + * @author David Brownell 1.38 + * @version 1.1, 00/08/05 1.39 + */ 1.40 +public class XmlNames { 1.41 + private XmlNames() { 1.42 + } 1.43 + 1.44 + 1.45 + /** 1.46 + * Returns true if the value is a legal XML name. 1.47 + * 1.48 + * @param value the string being tested 1.49 + */ 1.50 + public static boolean isName(String value) { 1.51 + if (value == null) 1.52 + return false; 1.53 + 1.54 + char c = value.charAt(0); 1.55 + if (!XmlChars.isLetter(c) && c != '_' && c != ':') 1.56 + return false; 1.57 + for (int i = 1; i < value.length(); i++) 1.58 + if (!XmlChars.isNameChar(value.charAt(i))) 1.59 + return false; 1.60 + return true; 1.61 + } 1.62 + 1.63 + /** 1.64 + * Returns true if the value is a legal "unqualified" XML name, as 1.65 + * defined in the XML Namespaces proposed recommendation. 1.66 + * These are normal XML names, except that they may not contain 1.67 + * a "colon" character. 1.68 + * 1.69 + * @param value the string being tested 1.70 + */ 1.71 + public static boolean isUnqualifiedName(String value) { 1.72 + if (value == null || value.length() == 0) 1.73 + return false; 1.74 + 1.75 + char c = value.charAt(0); 1.76 + if (!XmlChars.isLetter(c) && c != '_') 1.77 + return false; 1.78 + for (int i = 1; i < value.length(); i++) 1.79 + if (!XmlChars.isNCNameChar(value.charAt(i))) 1.80 + return false; 1.81 + return true; 1.82 + } 1.83 + 1.84 + /** 1.85 + * Returns true if the value is a legal "qualified" XML name, as defined 1.86 + * in the XML Namespaces proposed recommendation. Qualified names are 1.87 + * composed of an optional prefix (an unqualified name), followed by a 1.88 + * colon, and a required "local part" (an unqualified name). Prefixes are 1.89 + * declared, and correspond to particular URIs which scope the "local 1.90 + * part" of the name. (This method cannot check whether the prefix of a 1.91 + * name has been declared.) 1.92 + * 1.93 + * @param value the string being tested 1.94 + */ 1.95 + public static boolean isQualifiedName(String value) { 1.96 + if (value == null) 1.97 + return false; 1.98 + 1.99 + // [6] QName ::= (Prefix ':')? LocalPart 1.100 + // [7] Prefix ::= NCName 1.101 + // [8] LocalPart ::= NCName 1.102 + 1.103 + int first = value.indexOf(':'); 1.104 + 1.105 + // no Prefix, only check LocalPart 1.106 + if (first <= 0) 1.107 + return isUnqualifiedName(value); 1.108 + 1.109 + // Prefix exists, check everything 1.110 + 1.111 + int last = value.lastIndexOf(':'); 1.112 + if (last != first) 1.113 + return false; 1.114 + 1.115 + return isUnqualifiedName(value.substring(0, first)) 1.116 + && isUnqualifiedName(value.substring(first + 1)); 1.117 + } 1.118 + 1.119 + /** 1.120 + * This method returns true if the identifier is a "name token" 1.121 + * as defined in the XML specification. Like names, these 1.122 + * may only contain "name characters"; however, they do not need 1.123 + * to have letters as their initial characters. Attribute values 1.124 + * defined to be of type NMTOKEN(S) must satisfy this predicate. 1.125 + * 1.126 + * @param token the string being tested 1.127 + */ 1.128 + public static boolean isNmtoken(String token) { 1.129 + int length = token.length(); 1.130 + 1.131 + for (int i = 0; i < length; i++) 1.132 + if (!XmlChars.isNameChar(token.charAt(i))) 1.133 + return false; 1.134 + return true; 1.135 + } 1.136 + 1.137 + 1.138 + /** 1.139 + * This method returns true if the identifier is a "name token" as 1.140 + * defined by the XML Namespaces proposed recommendation. 1.141 + * These are like XML "name tokens" but they may not contain the 1.142 + * "colon" character. 1.143 + * 1.144 + * @param token the string being tested 1.145 + * @see #isNmtoken 1.146 + */ 1.147 + public static boolean isNCNmtoken(String token) { 1.148 + return isNmtoken(token) && token.indexOf(':') < 0; 1.149 + } 1.150 +}