| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public abstract class NameChecker { |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 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 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 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 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 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 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 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 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 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 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 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 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
public static boolean isNCNameChar(int ch) { |
| 215 |
return XMLCharacterData.isNCName11(ch); |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
public static boolean isNCNameStartChar(int ch) { |
| 227 |
return XMLCharacterData.isNCNameStart11(ch); |
| 228 |
} |
| 229 |
|
| 230 |
} |
| 231 |
|