1 28 29 package net.n3.nanoxml; 30 31 import java.io.Reader ; 32 import java.io.StringReader ; 33 import java.util.Enumeration ; 34 import java.util.Hashtable ; 35 import java.util.Properties ; 36 import java.util.Stack ; 37 38 45 public class NonValidator implements IXMLValidator 46 { 47 48 51 private static final char[] END_OF_CONDSECTION = { '>', ']', ']'}; 52 53 56 protected IXMLEntityResolver parameterEntityResolver; 57 58 61 protected int peLevel; 62 63 66 protected Hashtable attributeDefaultValues; 67 68 71 protected Stack currentElements; 72 73 76 public NonValidator() 77 { 78 this.attributeDefaultValues = new Hashtable (); 79 this.currentElements = new Stack (); 80 this.parameterEntityResolver = new XMLEntityResolver(); 81 this.peLevel = 0; 82 } 83 84 87 protected void finalize() throws Throwable 88 { 89 this.parameterEntityResolver = null; 90 this.attributeDefaultValues.clear(); 91 this.attributeDefaultValues = null; 92 this.currentElements.clear(); 93 this.currentElements = null; 94 super.finalize(); 95 } 96 97 102 public void setParameterEntityResolver(IXMLEntityResolver resolver) 103 { 104 this.parameterEntityResolver = resolver; 105 } 106 107 112 public IXMLEntityResolver getParameterEntityResolver() 113 { 114 return this.parameterEntityResolver; 115 } 116 117 127 public void parseDTD(String publicID, IXMLReader reader, IXMLEntityResolver entityResolver, 128 boolean external) throws Exception 129 { 130 XMLUtil.skipWhitespace(reader, '%', null, null); 131 132 for (;;) 133 { 134 char ch = XMLUtil.read(reader, null, '%', this.parameterEntityResolver); 135 136 if (ch == '<') 137 { 138 this.processElement(reader, entityResolver); 139 } 140 else if (ch == ']') 141 { 142 return; } 144 else 145 { 146 XMLUtil.errorInvalidInput(reader.getSystemID(), reader.getLineNr(), "" + ch); 147 } 148 149 do 150 { 151 if (external && (peLevel == 0) && reader.atEOFOfCurrentStream()) { return; } 155 156 ch = reader.read(); 157 } 158 while ((ch == ' ') || (ch == '\t') || (ch == '\n') || (ch == '\r')); 159 160 reader.unread(ch); 161 XMLUtil.skipWhitespace(reader, '%', null, null); 162 } 163 } 164 165 173 protected void processElement(IXMLReader reader, IXMLEntityResolver entityResolver) 174 throws Exception 175 { 176 char ch = XMLUtil.read(reader, null, '%', this.parameterEntityResolver); 177 178 if (ch != '!') 179 { 180 XMLUtil.skipTag(reader, '%', this.parameterEntityResolver); 181 return; 182 } 183 184 ch = XMLUtil.read(reader, null, '%', this.parameterEntityResolver); 185 186 switch (ch) 187 { 188 case '-': 189 XMLUtil.skipComment(reader, this.parameterEntityResolver); 190 break; 191 192 case '[': 193 this.processConditionalSection(reader, entityResolver); 194 break; 195 196 case 'E': 197 this.processEntity(reader, entityResolver); 198 break; 199 200 case 'A': 201 this.processAttList(reader, entityResolver); 202 break; 203 204 default: 205 XMLUtil.skipTag(reader, '%', this.parameterEntityResolver); 206 } 207 } 208 209 217 protected void processConditionalSection(IXMLReader reader, IXMLEntityResolver entityResolver) 218 throws Exception 219 { 220 XMLUtil.skipWhitespace(reader, '%', null, null); 221 222 char ch = XMLUtil.read(reader, null, '%', this.parameterEntityResolver); 223 224 if (ch != 'I') 225 { 226 XMLUtil.skipTag(reader, '%', this.parameterEntityResolver); 227 return; 228 } 229 230 ch = XMLUtil.read(reader, null, '%', this.parameterEntityResolver); 231 232 switch (ch) 233 { 234 case 'G': 235 this.processIgnoreSection(reader, entityResolver); 236 return; 237 238 case 'N': 239 break; 240 241 default: 242 XMLUtil.skipTag(reader, '%', this.parameterEntityResolver); 243 return; 244 } 245 246 if (!XMLUtil.checkLiteral(reader, '%', this.parameterEntityResolver, "CLUDE")) 247 { 248 XMLUtil.skipTag(reader, '%', this.parameterEntityResolver); 249 return; 250 } 251 252 XMLUtil.skipWhitespace(reader, '%', null, null); 253 254 ch = XMLUtil.read(reader, null, '%', this.parameterEntityResolver); 255 256 if (ch != '[') 257 { 258 XMLUtil.skipTag(reader, '%', this.parameterEntityResolver); 259 return; 260 } 261 262 Reader subreader = new ContentReader(reader, this.parameterEntityResolver, '\0', 263 NonValidator.END_OF_CONDSECTION, true, ""); 264 StringBuffer buf = new StringBuffer (1024); 265 266 for (;;) 267 { 268 int ch2 = subreader.read(); 269 270 if (ch2 < 0) 271 { 272 break; 273 } 274 275 buf.append((char) ch2); 276 } 277 278 subreader.close(); 279 reader.startNewStream(new StringReader (buf.toString())); 280 } 281 282 290 protected void processIgnoreSection(IXMLReader reader, IXMLEntityResolver entityResolver) 291 throws Exception 292 { 293 if (!XMLUtil.checkLiteral(reader, '%', this.parameterEntityResolver, "NORE")) 294 { 295 XMLUtil.skipTag(reader, '%', this.parameterEntityResolver); 296 return; 297 } 298 299 XMLUtil.skipWhitespace(reader, '%', null, null); 300 301 char ch = XMLUtil.read(reader, null, '%', this.parameterEntityResolver); 302 303 if (ch != '[') 304 { 305 XMLUtil.skipTag(reader, '%', this.parameterEntityResolver); 306 return; 307 } 308 309 Reader subreader = new ContentReader(reader, this.parameterEntityResolver, '\0', 310 NonValidator.END_OF_CONDSECTION, true, ""); 311 subreader.close(); 312 } 313 314 322 protected void processAttList(IXMLReader reader, IXMLEntityResolver entityResolver) 323 throws Exception 324 { 325 if (!XMLUtil.checkLiteral(reader, '%', this.parameterEntityResolver, "TTLIST")) 326 { 327 XMLUtil.skipTag(reader, '%', this.parameterEntityResolver); 328 return; 329 } 330 331 XMLUtil.skipWhitespace(reader, '%', null, null); 332 String elementName = XMLUtil.scanIdentifier(reader, '%', this.parameterEntityResolver); 333 XMLUtil.skipWhitespace(reader, '%', null, null); 334 char ch = XMLUtil.read(reader, null, '%', this.parameterEntityResolver); 335 Properties props = new Properties (); 336 337 while (ch != '>') 338 { 339 reader.unread(ch); 340 String attName = XMLUtil.scanIdentifier(reader, '%', this.parameterEntityResolver); 341 XMLUtil.skipWhitespace(reader, '%', null, null); 342 ch = XMLUtil.read(reader, null, '%', this.parameterEntityResolver); 343 344 if (ch == '(') 345 { 346 while (ch != ')') 347 { 348 ch = XMLUtil.read(reader, null, '%', this.parameterEntityResolver); 349 } 350 } 351 else 352 { 353 reader.unread(ch); 354 XMLUtil.scanIdentifier(reader, '%', this.parameterEntityResolver); 355 } 356 357 XMLUtil.skipWhitespace(reader, '%', null, null); 358 ch = XMLUtil.read(reader, null, '%', this.parameterEntityResolver); 359 360 if (ch == '#') 361 { 362 String str = XMLUtil.scanIdentifier(reader, '%', this.parameterEntityResolver); 363 XMLUtil.skipWhitespace(reader, '%', null, null); 364 365 if (!"FIXED".equals(str)) 366 { 367 XMLUtil.skipWhitespace(reader, '%', null, null); 368 ch = XMLUtil.read(reader, null, '%', this.parameterEntityResolver); 369 continue; 370 } 371 } 372 else 373 { 374 reader.unread(ch); 375 } 376 377 String value = XMLUtil.scanString(reader, '%', false, this.parameterEntityResolver); 378 props.put(attName, value); 379 XMLUtil.skipWhitespace(reader, '%', null, null); 380 ch = XMLUtil.read(reader, null, '%', this.parameterEntityResolver); 381 } 382 383 if (!props.isEmpty()) 384 { 385 this.attributeDefaultValues.put(elementName, props); 386 } 387 } 388 389 397 protected void processEntity(IXMLReader reader, IXMLEntityResolver entityResolver) 398 throws Exception 399 { 400 if (!XMLUtil.checkLiteral(reader, '%', this.parameterEntityResolver, "NTITY")) 401 { 402 XMLUtil.skipTag(reader, '%', this.parameterEntityResolver); 403 return; 404 } 405 406 XMLUtil.skipWhitespace(reader, '\0', null, null); 407 char ch = XMLUtil.read(reader, null, '\0', this.parameterEntityResolver); 408 409 if (ch == '%') 410 { 411 XMLUtil.skipWhitespace(reader, '%', null, null); 412 entityResolver = this.parameterEntityResolver; 413 } 414 else 415 { 416 reader.unread(ch); 417 } 418 419 String key = XMLUtil.scanIdentifier(reader, '%', this.parameterEntityResolver); 420 XMLUtil.skipWhitespace(reader, '%', null, null); 421 ch = XMLUtil.read(reader, null, '%', this.parameterEntityResolver); 422 String systemID = null; 423 String publicID = null; 424 425 switch (ch) 426 { 427 case 'P': 428 if (!XMLUtil.checkLiteral(reader, '%', this.parameterEntityResolver, "UBLIC")) 429 { 430 XMLUtil.skipTag(reader, '%', this.parameterEntityResolver); 431 return; 432 } 433 434 XMLUtil.skipWhitespace(reader, '%', null, null); 435 publicID = XMLUtil.scanString(reader, '%', false, this.parameterEntityResolver); 436 XMLUtil.skipWhitespace(reader, '%', null, null); 437 systemID = XMLUtil.scanString(reader, '%', false, this.parameterEntityResolver); 438 XMLUtil.skipWhitespace(reader, '%', null, null); 439 XMLUtil.read(reader, null, '%', this.parameterEntityResolver); 440 break; 441 442 case 'S': 443 if (!XMLUtil.checkLiteral(reader, '%', this.parameterEntityResolver, "YSTEM")) 444 { 445 XMLUtil.skipTag(reader, '%', this.parameterEntityResolver); 446 return; 447 } 448 449 XMLUtil.skipWhitespace(reader, '%', null, null); 450 systemID = XMLUtil.scanString(reader, '%', false, this.parameterEntityResolver); 451 XMLUtil.skipWhitespace(reader, '%', null, null); 452 XMLUtil.read(reader, null, '%', this.parameterEntityResolver); 453 break; 454 455 case '"': 456 case '\'': 457 reader.unread(ch); 458 String value = XMLUtil.scanString(reader, '%', false, this.parameterEntityResolver); 459 entityResolver.addInternalEntity(key, value); 460 XMLUtil.skipWhitespace(reader, '%', null, null); 461 XMLUtil.read(reader, null, '%', this.parameterEntityResolver); 462 break; 463 default: 464 XMLUtil.skipTag(reader, '%', this.parameterEntityResolver); 465 } 466 467 if (systemID != null) 468 { 469 entityResolver.addExternalEntity(key, publicID, systemID); 470 } 471 } 472 473 482 public void elementStarted(String name, String nsPrefix, String nsSystemId, String systemId, 483 int lineNr) 484 { 485 Properties attribs = (Properties ) this.attributeDefaultValues.get(name); 486 487 if (attribs == null) 488 { 489 attribs = new Properties (); 490 } 491 else 492 { 493 attribs = (Properties ) attribs.clone(); 494 } 495 496 this.currentElements.push(attribs); 497 } 498 499 508 public void elementEnded(String name, String nsPrefix, String nsSystemId, String systemId, 509 int lineNr) 510 { 511 } 513 514 526 public void elementAttributesProcessed(String name, String nsPrefix, String nsSystemId, 527 Properties extraAttributes, String systemId, int lineNr) 528 { 529 Properties props = (Properties ) this.currentElements.pop(); 530 Enumeration enumeration = props.keys(); 531 532 while (enumeration.hasMoreElements()) 533 { 534 String key = (String ) enumeration.nextElement(); 535 extraAttributes.put(key, props.get(key)); 536 } 537 } 538 539 549 public void attributeAdded(String key, String nsPrefix, String nsSystemId, String value, 550 String systemId, int lineNr) 551 { 552 Properties props = (Properties ) this.currentElements.peek(); 553 554 if (props.containsKey(key)) 555 { 556 props.remove(key); 557 } 558 } 559 560 566 public void PCDataAdded(String systemId, int lineNr) 567 { 568 } 570 571 } 572 | Popular Tags |