1/*
2 * Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.xml.internal.dtdparser;
27
28
29/**
30 * This class contains static methods used to determine whether identifiers
31 * may appear in certain roles in XML documents.  Such methods are used
32 * both to parse and to create such documents.
33 *
34 * @author David Brownell
35 * @version 1.1, 00/08/05
36 */
37public class XmlNames {
38    private XmlNames() {
39    }
40
41
42    /**
43     * Returns true if the value is a legal XML name.
44     *
45     * @param value the string being tested
46     */
47    public static boolean isName(String value) {
48        if (value == null)
49            return false;
50
51        char c = value.charAt(0);
52        if (!XmlChars.isLetter(c) && c != '_' && c != ':')
53            return false;
54        for (int i = 1; i < value.length(); i++)
55            if (!XmlChars.isNameChar(value.charAt(i)))
56                return false;
57        return true;
58    }
59
60    /**
61     * Returns true if the value is a legal "unqualified" XML name, as
62     * defined in the XML Namespaces proposed recommendation.
63     * These are normal XML names, except that they may not contain
64     * a "colon" character.
65     *
66     * @param value the string being tested
67     */
68    public static boolean isUnqualifiedName(String value) {
69        if (value == null || value.length() == 0)
70            return false;
71
72        char c = value.charAt(0);
73        if (!XmlChars.isLetter(c) && c != '_')
74            return false;
75        for (int i = 1; i < value.length(); i++)
76            if (!XmlChars.isNCNameChar(value.charAt(i)))
77                return false;
78        return true;
79    }
80
81    /**
82     * Returns true if the value is a legal "qualified" XML name, as defined
83     * in the XML Namespaces proposed recommendation.  Qualified names are
84     * composed of an optional prefix (an unqualified name), followed by a
85     * colon, and a required "local part" (an unqualified name).  Prefixes are
86     * declared, and correspond to particular URIs which scope the "local
87     * part" of the name.  (This method cannot check whether the prefix of a
88     * name has been declared.)
89     *
90     * @param value the string being tested
91     */
92    public static boolean isQualifiedName(String value) {
93        if (value == null)
94            return false;
95
96        // [6] QName ::= (Prefix ':')? LocalPart
97        // [7] Prefix ::= NCName
98        // [8] LocalPart ::= NCName
99
100        int first = value.indexOf(':');
101
102        // no Prefix, only check LocalPart
103        if (first <= 0)
104            return isUnqualifiedName(value);
105
106        // Prefix exists, check everything
107
108        int last = value.lastIndexOf(':');
109        if (last != first)
110            return false;
111
112        return isUnqualifiedName(value.substring(0, first))
113                && isUnqualifiedName(value.substring(first + 1));
114    }
115
116    /**
117     * This method returns true if the identifier is a "name token"
118     * as defined in the XML specification.  Like names, these
119     * may only contain "name characters"; however, they do not need
120     * to have letters as their initial characters.  Attribute values
121     * defined to be of type NMTOKEN(S) must satisfy this predicate.
122     *
123     * @param token the string being tested
124     */
125    public static boolean isNmtoken(String token) {
126        int length = token.length();
127
128        for (int i = 0; i < length; i++)
129            if (!XmlChars.isNameChar(token.charAt(i)))
130                return false;
131        return true;
132    }
133
134
135    /**
136     * This method returns true if the identifier is a "name token" as
137     * defined by the XML Namespaces proposed recommendation.
138     * These are like XML "name tokens" but they may not contain the
139     * "colon" character.
140     *
141     * @param token the string being tested
142     * @see #isNmtoken
143     */
144    public static boolean isNCNmtoken(String token) {
145        return isNmtoken(token) && token.indexOf(':') < 0;
146    }
147}
148