325N/A/*
325N/A * Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage com.sun.xml.internal.dtdparser;
325N/A
325N/A
325N/A/**
325N/A * This class contains static methods used to determine whether identifiers
325N/A * may appear in certain roles in XML documents. Such methods are used
325N/A * both to parse and to create such documents.
325N/A *
325N/A * @author David Brownell
325N/A * @version 1.1, 00/08/05
325N/A */
325N/Apublic class XmlNames {
325N/A private XmlNames() {
325N/A }
325N/A
325N/A
325N/A /**
325N/A * Returns true if the value is a legal XML name.
325N/A *
325N/A * @param value the string being tested
325N/A */
325N/A public static boolean isName(String value) {
325N/A if (value == null)
325N/A return false;
325N/A
325N/A char c = value.charAt(0);
325N/A if (!XmlChars.isLetter(c) && c != '_' && c != ':')
325N/A return false;
325N/A for (int i = 1; i < value.length(); i++)
325N/A if (!XmlChars.isNameChar(value.charAt(i)))
325N/A return false;
325N/A return true;
325N/A }
325N/A
325N/A /**
325N/A * Returns true if the value is a legal "unqualified" XML name, as
325N/A * defined in the XML Namespaces proposed recommendation.
325N/A * These are normal XML names, except that they may not contain
325N/A * a "colon" character.
325N/A *
325N/A * @param value the string being tested
325N/A */
325N/A public static boolean isUnqualifiedName(String value) {
325N/A if (value == null || value.length() == 0)
325N/A return false;
325N/A
325N/A char c = value.charAt(0);
325N/A if (!XmlChars.isLetter(c) && c != '_')
325N/A return false;
325N/A for (int i = 1; i < value.length(); i++)
325N/A if (!XmlChars.isNCNameChar(value.charAt(i)))
325N/A return false;
325N/A return true;
325N/A }
325N/A
325N/A /**
325N/A * Returns true if the value is a legal "qualified" XML name, as defined
325N/A * in the XML Namespaces proposed recommendation. Qualified names are
325N/A * composed of an optional prefix (an unqualified name), followed by a
325N/A * colon, and a required "local part" (an unqualified name). Prefixes are
325N/A * declared, and correspond to particular URIs which scope the "local
325N/A * part" of the name. (This method cannot check whether the prefix of a
325N/A * name has been declared.)
325N/A *
325N/A * @param value the string being tested
325N/A */
325N/A public static boolean isQualifiedName(String value) {
325N/A if (value == null)
325N/A return false;
325N/A
325N/A // [6] QName ::= (Prefix ':')? LocalPart
325N/A // [7] Prefix ::= NCName
325N/A // [8] LocalPart ::= NCName
325N/A
325N/A int first = value.indexOf(':');
325N/A
325N/A // no Prefix, only check LocalPart
325N/A if (first <= 0)
325N/A return isUnqualifiedName(value);
325N/A
325N/A // Prefix exists, check everything
325N/A
325N/A int last = value.lastIndexOf(':');
325N/A if (last != first)
325N/A return false;
325N/A
325N/A return isUnqualifiedName(value.substring(0, first))
325N/A && isUnqualifiedName(value.substring(first + 1));
325N/A }
325N/A
325N/A /**
325N/A * This method returns true if the identifier is a "name token"
325N/A * as defined in the XML specification. Like names, these
325N/A * may only contain "name characters"; however, they do not need
325N/A * to have letters as their initial characters. Attribute values
325N/A * defined to be of type NMTOKEN(S) must satisfy this predicate.
325N/A *
325N/A * @param token the string being tested
325N/A */
325N/A public static boolean isNmtoken(String token) {
325N/A int length = token.length();
325N/A
325N/A for (int i = 0; i < length; i++)
325N/A if (!XmlChars.isNameChar(token.charAt(i)))
325N/A return false;
325N/A return true;
325N/A }
325N/A
325N/A
325N/A /**
325N/A * This method returns true if the identifier is a "name token" as
325N/A * defined by the XML Namespaces proposed recommendation.
325N/A * These are like XML "name tokens" but they may not contain the
325N/A * "colon" character.
325N/A *
325N/A * @param token the string being tested
325N/A * @see #isNmtoken
325N/A */
325N/A public static boolean isNCNmtoken(String token) {
325N/A return isNmtoken(token) && token.indexOf(':') < 0;
325N/A }
325N/A}