1 16 17 package org.apache.xerces.impl; 18 19 import java.io.IOException ; 20 21 import org.apache.xerces.impl.msg.XMLMessageFormatter; 22 import org.apache.xerces.util.XML11Char; 23 import org.apache.xerces.util.XMLChar; 24 import org.apache.xerces.util.XMLStringBuffer; 25 import org.apache.xerces.xni.XMLString; 26 import org.apache.xerces.xni.XNIException; 27 28 56 public class XML11DocumentScannerImpl 57 extends XMLDocumentScannerImpl { 58 59 60 61 private String [] fStrings = new String [3]; 62 63 64 private XMLString fString = new XMLString(); 65 66 67 private XMLStringBuffer fStringBuffer = new XMLStringBuffer(); 68 private XMLStringBuffer fStringBuffer2 = new XMLStringBuffer(); 69 private XMLStringBuffer fStringBuffer3 = new XMLStringBuffer(); 70 71 75 76 public XML11DocumentScannerImpl() {super();} 78 82 84 89 protected int scanContent() throws IOException , XNIException { 90 91 XMLString content = fString; 92 int c = fEntityScanner.scanContent(content); 93 if (c == '\r' || c == 0x85 || c == 0x2028) { 94 fEntityScanner.scanChar(); 98 fStringBuffer.clear(); 99 fStringBuffer.append(fString); 100 fStringBuffer.append((char)c); 101 content = fStringBuffer; 102 c = -1; 103 } 104 if (fDocumentHandler != null && content.length > 0) { 105 fDocumentHandler.characters(content, null); 106 } 107 108 if (c == ']' && fString.length == 0) { 109 fStringBuffer.clear(); 110 fStringBuffer.append((char)fEntityScanner.scanChar()); 111 fInScanContent = true; 115 if (fEntityScanner.skipChar(']')) { 120 fStringBuffer.append(']'); 121 while (fEntityScanner.skipChar(']')) { 122 fStringBuffer.append(']'); 123 } 124 if (fEntityScanner.skipChar('>')) { 125 reportFatalError("CDEndInContent", null); 126 } 127 } 128 if (fDocumentHandler != null && fStringBuffer.length != 0) { 129 fDocumentHandler.characters(fStringBuffer, null); 130 } 131 fInScanContent = false; 132 c = -1; 133 } 134 return c; 135 136 } 138 157 protected boolean scanAttributeValue(XMLString value, 158 XMLString nonNormalizedValue, 159 String atName, 160 boolean checkEntities,String eleName) 161 throws IOException , XNIException 162 { 163 int quote = fEntityScanner.peekChar(); 165 if (quote != '\'' && quote != '"') { 166 reportFatalError("OpenQuoteExpected", new Object []{eleName,atName}); 167 } 168 169 fEntityScanner.scanChar(); 170 int entityDepth = fEntityDepth; 171 172 int c = fEntityScanner.scanLiteral(quote, value); 173 if (DEBUG_ATTR_NORMALIZATION) { 174 System.out.println("** scanLiteral -> \"" 175 + value.toString() + "\""); 176 } 177 178 int fromIndex = 0; 179 if (c == quote && (fromIndex = isUnchangedByNormalization(value)) == -1) { 180 181 nonNormalizedValue.setValues(value); 182 int cquote = fEntityScanner.scanChar(); 183 if (cquote != quote) { 184 reportFatalError("CloseQuoteExpected", new Object []{eleName,atName}); 185 } 186 return true; 187 } 188 fStringBuffer2.clear(); 189 fStringBuffer2.append(value); 190 normalizeWhitespace(value, fromIndex); 191 if (DEBUG_ATTR_NORMALIZATION) { 192 System.out.println("** normalizeWhitespace -> \"" 193 + value.toString() + "\""); 194 } 195 if (c != quote) { 196 fScanningAttribute = true; 197 fStringBuffer.clear(); 198 do { 199 fStringBuffer.append(value); 200 if (DEBUG_ATTR_NORMALIZATION) { 201 System.out.println("** value2: \"" 202 + fStringBuffer.toString() + "\""); 203 } 204 if (c == '&') { 205 fEntityScanner.skipChar('&'); 206 if (entityDepth == fEntityDepth) { 207 fStringBuffer2.append('&'); 208 } 209 if (fEntityScanner.skipChar('#')) { 210 if (entityDepth == fEntityDepth) { 211 fStringBuffer2.append('#'); 212 } 213 int ch = scanCharReferenceValue(fStringBuffer, fStringBuffer2); 214 if (ch != -1) { 215 if (DEBUG_ATTR_NORMALIZATION) { 216 System.out.println("** value3: \"" 217 + fStringBuffer.toString() 218 + "\""); 219 } 220 } 221 } 222 else { 223 String entityName = fEntityScanner.scanName(); 224 if (entityName == null) { 225 reportFatalError("NameRequiredInReference", null); 226 } 227 else if (entityDepth == fEntityDepth) { 228 fStringBuffer2.append(entityName); 229 } 230 if (!fEntityScanner.skipChar(';')) { 231 reportFatalError("SemicolonRequiredInReference", 232 new Object []{entityName}); 233 } 234 else if (entityDepth == fEntityDepth) { 235 fStringBuffer2.append(';'); 236 } 237 if (entityName == fAmpSymbol) { 238 fStringBuffer.append('&'); 239 if (DEBUG_ATTR_NORMALIZATION) { 240 System.out.println("** value5: \"" 241 + fStringBuffer.toString() 242 + "\""); 243 } 244 } 245 else if (entityName == fAposSymbol) { 246 fStringBuffer.append('\''); 247 if (DEBUG_ATTR_NORMALIZATION) { 248 System.out.println("** value7: \"" 249 + fStringBuffer.toString() 250 + "\""); 251 } 252 } 253 else if (entityName == fLtSymbol) { 254 fStringBuffer.append('<'); 255 if (DEBUG_ATTR_NORMALIZATION) { 256 System.out.println("** value9: \"" 257 + fStringBuffer.toString() 258 + "\""); 259 } 260 } 261 else if (entityName == fGtSymbol) { 262 fStringBuffer.append('>'); 263 if (DEBUG_ATTR_NORMALIZATION) { 264 System.out.println("** valueB: \"" 265 + fStringBuffer.toString() 266 + "\""); 267 } 268 } 269 else if (entityName == fQuotSymbol) { 270 fStringBuffer.append('"'); 271 if (DEBUG_ATTR_NORMALIZATION) { 272 System.out.println("** valueD: \"" 273 + fStringBuffer.toString() 274 + "\""); 275 } 276 } 277 else { 278 if (fEntityManager.isExternalEntity(entityName)) { 279 reportFatalError("ReferenceToExternalEntity", 280 new Object [] { entityName }); 281 } 282 else { 283 if (!fEntityManager.isDeclaredEntity(entityName)) { 284 if (checkEntities) { 286 if (fValidation) { 287 fErrorReporter.reportError(XMLMessageFormatter.XML_DOMAIN, 288 "EntityNotDeclared", 289 new Object []{entityName}, 290 XMLErrorReporter.SEVERITY_ERROR); 291 } 292 } 293 else { 294 reportFatalError("EntityNotDeclared", 295 new Object []{entityName}); 296 } 297 } 298 fEntityManager.startEntity(entityName, true); 299 } 300 } 301 } 302 } 303 else if (c == '<') { 304 reportFatalError("LessthanInAttValue", 305 new Object [] { eleName, atName }); 306 fEntityScanner.scanChar(); 307 if (entityDepth == fEntityDepth) { 308 fStringBuffer2.append((char)c); 309 } 310 } 311 else if (c == '%' || c == ']') { 312 fEntityScanner.scanChar(); 313 fStringBuffer.append((char)c); 314 if (entityDepth == fEntityDepth) { 315 fStringBuffer2.append((char)c); 316 } 317 if (DEBUG_ATTR_NORMALIZATION) { 318 System.out.println("** valueF: \"" 319 + fStringBuffer.toString() + "\""); 320 } 321 } 322 else if (c == '\n' || c == '\r' || c == 0x85 || c == 0x2028) { 326 fEntityScanner.scanChar(); 327 fStringBuffer.append(' '); 328 if (entityDepth == fEntityDepth) { 329 fStringBuffer2.append('\n'); 330 } 331 } 332 else if (c != -1 && XMLChar.isHighSurrogate(c)) { 333 fStringBuffer3.clear(); 334 if (scanSurrogates(fStringBuffer3)) { 335 fStringBuffer.append(fStringBuffer3); 336 if (entityDepth == fEntityDepth) { 337 fStringBuffer2.append(fStringBuffer3); 338 } 339 if (DEBUG_ATTR_NORMALIZATION) { 340 System.out.println("** valueI: \"" 341 + fStringBuffer.toString() 342 + "\""); 343 } 344 } 345 } 346 else if (c != -1 && XML11Char.isXML11Invalid(c)) { 347 reportFatalError("InvalidCharInAttValue", 348 new Object [] {eleName, atName, Integer.toString(c, 16)}); 349 fEntityScanner.scanChar(); 350 if (entityDepth == fEntityDepth) { 351 fStringBuffer2.append((char)c); 352 } 353 } 354 c = fEntityScanner.scanLiteral(quote, value); 355 if (entityDepth == fEntityDepth) { 356 fStringBuffer2.append(value); 357 } 358 normalizeWhitespace(value); 359 } while (c != quote || entityDepth != fEntityDepth); 360 fStringBuffer.append(value); 361 if (DEBUG_ATTR_NORMALIZATION) { 362 System.out.println("** valueN: \"" 363 + fStringBuffer.toString() + "\""); 364 } 365 value.setValues(fStringBuffer); 366 fScanningAttribute = false; 367 } 368 nonNormalizedValue.setValues(fStringBuffer2); 369 370 int cquote = fEntityScanner.scanChar(); 372 if (cquote != quote) { 373 reportFatalError("CloseQuoteExpected", new Object []{eleName,atName}); 374 } 375 return nonNormalizedValue.equals(value.ch, value.offset, value.length); 376 } 378 403 protected boolean scanPubidLiteral(XMLString literal) 404 throws IOException , XNIException 405 { 406 int quote = fEntityScanner.scanChar(); 407 if (quote != '\'' && quote != '"') { 408 reportFatalError("QuoteRequiredInPublicID", null); 409 return false; 410 } 411 412 fStringBuffer.clear(); 413 boolean skipSpace = true; 415 boolean dataok = true; 416 while (true) { 417 int c = fEntityScanner.scanChar(); 418 if (c == ' ' || c == '\n' || c == '\r' || c == 0x85 || c == 0x2028) { 420 if (!skipSpace) { 421 fStringBuffer.append(' '); 423 skipSpace = true; 424 } 425 } 426 else if (c == quote) { 427 if (skipSpace) { 428 fStringBuffer.length--; 430 } 431 literal.setValues(fStringBuffer); 432 break; 433 } 434 else if (XMLChar.isPubid(c)) { 435 fStringBuffer.append((char)c); 436 skipSpace = false; 437 } 438 else if (c == -1) { 439 reportFatalError("PublicIDUnterminated", null); 440 return false; 441 } 442 else { 443 dataok = false; 444 reportFatalError("InvalidCharInPublicID", 445 new Object []{Integer.toHexString(c)}); 446 } 447 } 448 return dataok; 449 } 450 451 455 protected void normalizeWhitespace(XMLString value) { 456 int end = value.offset + value.length; 457 for (int i = value.offset; i < end; ++i) { 458 int c = value.ch[i]; 459 if (XMLChar.isSpace(c)) { 460 value.ch[i] = ' '; 461 } 462 } 463 } 464 465 469 protected void normalizeWhitespace(XMLString value, int fromIndex) { 470 int end = value.offset + value.length; 471 for (int i = value.offset + fromIndex; i < end; ++i) { 472 int c = value.ch[i]; 473 if (XMLChar.isSpace(c)) { 474 value.ch[i] = ' '; 475 } 476 } 477 } 478 479 486 protected int isUnchangedByNormalization(XMLString value) { 487 int end = value.offset + value.length; 488 for (int i = value.offset; i < end; ++i) { 489 int c = value.ch[i]; 490 if (XMLChar.isSpace(c)) { 491 return i - value.offset; 492 } 493 } 494 return -1; 495 } 496 497 protected boolean isInvalid(int value) { 501 return (XML11Char.isXML11Invalid(value)); 502 } 504 protected boolean isInvalidLiteral(int value) { 508 return (!XML11Char.isXML11ValidLiteral(value)); 509 } 511 protected boolean isValidNameChar(int value) { 515 return (XML11Char.isXML11Name(value)); 516 } 518 protected boolean isValidNameStartChar(int value) { 522 return (XML11Char.isXML11NameStart(value)); 523 } 525 protected boolean isValidNCName(int value) { 529 return (XML11Char.isXML11NCName(value)); 530 } 532 protected boolean isValidNameStartHighSurrogate(int value) { 537 return XML11Char.isXML11NameHighSurrogate(value); 538 } 540 protected boolean versionSupported(String version) { 541 return (version.equals("1.1") || version.equals("1.0")); 542 } 544 protected String getVersionNotSupportedKey () { 548 return "VersionNotSupported11"; 549 } 551 } | Popular Tags |