Debian Codegraph @

net/sf/saxon/om/NameChecker.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.om;
9
10 import net.sf.saxon.serialize.charcode.UTF16CharacterSet;
11 import net.sf.saxon.serialize.charcode.XMLCharacterData;
12 import net.sf.saxon.trans.Err;
13 import net.sf.saxon.trans.XPathException;
14
15 /**
16 * The NameChecker performs validation and analysis of XML names.
17 *
18 * <p>In releases prior to 9.6, there were two name checkers, one for XML 1.0 and
19 * one for XML 1.1. However, XML 1.0 fifth edition uses the same rules for XML names
20 * as XML 1.1, so they were actually checking the same rules for names (although they
21 * were different when checking for valid characters). From 9.6, the name checker
22 * no longer performs checks for valid XML characters, so only one name checker is
23 * needed, and the methods have become static.</p>
24 */
25
26 public abstract class NameChecker {
27
28 /**
29 * Validate whether a given string constitutes a valid QName, as defined in XML Namespaces.
30 * Note that this does not test whether the prefix is actually declared.
31 *
32 * @param name the name to be tested
33 * @return true if the name is a lexically-valid QName
34 */
35
36 public static boolean isQName(String name) {
37 int colon = name.indexOf(':');
38 if (colon < 0) {
39 return isValidNCName(name);
40 }
41 return colon != 0 &&
42 colon != name.length() - 1 &&
43 isValidNCName(name.substring(0, colon)) &&
44 isValidNCName(name.substring(colon + 1));
45 }
46
47 /**
48 * Extract the prefix from a QName. Note, the QName is assumed to be valid.
49 *
50 * @param qname The lexical QName whose prefix is required
51 * @return the prefix, that is the part before the colon. Returns an empty
52 * string if there is no prefix
53 */
54
55 public static String getPrefix(String qname) {
56 int colon = qname.indexOf(':');
57 if (colon < 0) {
58 return "";
59 }
60 return qname.substring(0, colon);
61 }
62
63 /**
64 * Validate a QName, and return the prefix and local name. The local name is checked
65 * to ensure it is a valid NCName. The prefix is not checked, on the theory that the caller
66 * will look up the prefix to find a URI, and if the prefix is invalid, then no URI will
67 * be found.
68 *
69 * @param qname the lexical QName whose parts are required. Note that leading and trailing
70 * whitespace is not permitted
71 * @return an array of two strings, the prefix and the local name. The first
72 * item is a zero-length string if there is no prefix.
73 * @throws QNameException if not a valid QName.
74 */
75
76 public static String[] getQNameParts(CharSequence qname) throws QNameException {
77 String[] parts = new String[2];
78 int colon = -1;
79 int len = qname.length();
80 for (int i = 0; i < len; i++) {
81 if (qname.charAt(i) == ':') {
82 colon = i;
83 break;
84 }
85 }
86 if (colon < 0) {
87 parts[0] = "";
88 parts[1] = qname.toString();
89 if (!isValidNCName(parts[1])) {
90 throw new QNameException("Invalid QName " + Err.wrap(qname));
91 }
92 } else {
93 if (colon == 0) {
94 throw new QNameException("QName cannot start with colon: " + Err.wrap(qname));
95 }
96 if (colon == len - 1) {
97 throw new QNameException("QName cannot end with colon: " + Err.wrap(qname));
98 }
99 parts[0] = qname.subSequence(0, colon).toString();
100 parts[1] = qname.subSequence(colon + 1, len).toString();
101
102 if (!isValidNCName(parts[1])) {
103 if (!isValidNCName(parts[0])) {
104 throw new QNameException("Both the prefix " + Err.wrap(parts[0]) +
105 " and the local part " + Err.wrap(parts[1]) + " are invalid");
106 }
107 throw new QNameException("Invalid QName local part " + Err.wrap(parts[1]));
108 }
109 }
110 return parts;
111 }
112
113 /**
114 * Validate a QName, and return the prefix and local name. Both parts are checked
115 * to ensure they are valid NCNames.
116 * <p><i>Used from compiled code</i></p>
117 *
118 * @param qname the lexical QName whose parts are required. Note that leading and trailing
119 * whitespace is not permitted
120 * @return an array of two strings, the prefix and the local name. The first
121 * item is a zero-length string if there is no prefix.
122 * @throws XPathException if not a valid QName.
123 */
124
125 /*@NotNull*/
126 public static String[] checkQNameParts(CharSequence qname) throws XPathException {
127 try {
128 String[] parts = getQNameParts(qname);
129 if (parts[0].length() > 0 && !isValidNCName(parts[0])) {
130 throw new XPathException("Invalid QName prefix " + Err.wrap(parts[0]));
131 }
132 return parts;
133 } catch (QNameException e) {
134 XPathException err = new XPathException(e.getMessage());
135 err.setErrorCode("FORG0001");
136 throw err;
137 }
138 }
139
140 /**
141 * Validate whether a given string constitutes a valid NCName, as defined in XML Namespaces.
142 *
143 * @param ncName the name to be tested. Any whitespace trimming must have already been applied.
144 * @return true if the name is a lexically-valid QName
145 */
146
147 public static boolean isValidNCName(CharSequence ncName) {
148 if (ncName.length() == 0) {
149 return false;
150 }
151 int s = 1;
152 char ch = ncName.charAt(0);
153 if (UTF16CharacterSet.isHighSurrogate(ch)) {
154 if (!isNCNameStartChar(UTF16CharacterSet.combinePair(ch, ncName.charAt(1)))) {
155 return false;
156 }
157 s = 2;
158 } else {
159 if (!isNCNameStartChar(ch)) {
160 return false;
161 }
162 }
163 for (int i = s; i < ncName.length(); i++) {
164 ch = ncName.charAt(i);
165 if (UTF16CharacterSet.isHighSurrogate(ch)) {
166 if (!isNCNameChar(UTF16CharacterSet.combinePair(ch, ncName.charAt(++i)))) {
167 return false;
168 }
169 } else {
170 if (!isNCNameChar(ch)) {
171 return false;
172 }
173 }
174 }
175 return true;
176 }
177
178 /**
179 * Check to see if a string is a valid Nmtoken according to [7]
180 * in the XML 1.0 Recommendation
181 *
182 * @param nmtoken the string to be tested. Any whitespace trimming must have already been applied.
183 * @return true if nmtoken is a valid Nmtoken
184 */
185
186 public static boolean isValidNmtoken(CharSequence nmtoken) {
187 if (nmtoken.length() == 0) {
188 return false;
189 }
190 for (int i = 0; i < nmtoken.length(); i++) {
191 char ch = nmtoken.charAt(i);
192 if (UTF16CharacterSet.isHighSurrogate(ch)) {
193 if (!isNCNameChar(UTF16CharacterSet.combinePair(ch, nmtoken.charAt(++i)))) {
194 return false;
195 }
196 } else {
197 if (ch != ':' && !isNCNameChar(ch)) {
198 return false;
199 }
200 }
201 }
202 return true;
203 }
204
205
206 /**
207 * Test whether a character can appear in an NCName
208 *
209 * @param ch the character to be tested
210 * @return true if this is a valid character in an NCName. The rules for XML 1.0 fifth
211 * edition are the same as the XML 1.1 rules, and these are the rules that we use.
212 */
213
214 public static boolean isNCNameChar(int ch) {
215 return XMLCharacterData.isNCName11(ch);
216 }
217
218 /**
219 * Test whether a character can appear at the start of an NCName
220 *
221 * @param ch the character to be tested
222 * @return true if this is a valid character at the start of an NCName. The rules for XML 1.0 fifth
223 * edition are the same as the XML 1.1 rules, and these are the rules that we use.
224 */
225
226 public static boolean isNCNameStartChar(int ch) {
227 return XMLCharacterData.isNCNameStart11(ch);
228 }
229
230 }
231