1 29 30 package com.caucho.quercus.lib.dom; 31 32 import com.caucho.quercus.UnimplementedException; 33 import com.caucho.quercus.annotation.Optional; 34 import com.caucho.quercus.annotation.ReturnNullAsFalse; 35 import com.caucho.quercus.env.BooleanValue; 36 import com.caucho.quercus.env.Env; 37 import com.caucho.quercus.env.LongValue; 38 import com.caucho.quercus.env.StringValue; 39 import com.caucho.quercus.env.TempBufferStringValue; 40 import com.caucho.quercus.env.Value; 41 import com.caucho.util.L10N; 42 import com.caucho.vfs.Path; 43 import com.caucho.vfs.ReadStream; 44 import com.caucho.vfs.StringStream; 45 import com.caucho.vfs.TempStream; 46 import com.caucho.vfs.WriteStream; 47 import com.caucho.xml.XmlPrinter; 48 49 import org.w3c.dom.Document ; 50 import org.xml.sax.SAXException ; 51 52 import java.io.IOException ; 53 import java.util.logging.Level ; 54 import java.util.logging.Logger ; 55 56 public class DOMDocument 57 extends DOMNode<Document > 58 { 59 private final static L10N L = new L10N(DOMDocument.class); 60 private final static Logger log = Logger.getLogger(DOMDocument.class.getName()); 61 62 private String _encoding; 63 64 public static DOMDocument __construct(Env env, @Optional("'1.0'") String version, @Optional String encoding) 65 { 66 DOMDocument document = getImpl(env).createDocument(); 67 68 if (version != null && version.length() > 0) 69 document.setVersion(version); 70 71 if (encoding != null && encoding.length() > 0) 72 document.setEncoding(encoding); 73 74 return document; 75 } 76 77 public void setVersion(String version) 78 { 79 _delegate.setXmlVersion(version); 80 } 81 82 DOMDocument(DOMImplementation impl, Document document) 83 { 84 super(impl, document); 85 } 86 87 public String getEncoding() 88 { 89 return _encoding; 90 } 91 92 public void setEncoding(String encoding) 93 { 94 _encoding = encoding; 95 } 96 97 public DOMNode adoptNode(DOMNode source) 98 throws DOMException 99 { 100 return wrap(_delegate.adoptNode(source.getDelegate())); 101 } 102 103 public DOMAttr createAttribute(String name) 104 throws DOMException 105 { 106 try { 107 return wrap(_delegate.createAttribute(name)); 108 } 109 catch (org.w3c.dom.DOMException ex) { 110 throw wrap(ex); 111 } 112 } 113 114 public DOMAttr createAttributeNS(String namespaceURI, String qualifiedName) 115 throws DOMException 116 { 117 try { 118 return wrap(_delegate.createAttributeNS(namespaceURI, qualifiedName)); 119 } 120 catch (org.w3c.dom.DOMException ex) { 121 throw wrap(ex); 122 } 123 } 124 125 public DOMCDATASection createCDATASection(String data) 126 { 127 return wrap(_delegate.createCDATASection(data)); 128 } 129 130 public DOMComment createComment(String data) 131 { 132 return wrap(_delegate.createComment(data)); 133 } 134 135 public DOMDocumentFragment createDocumentFragment() 136 { 137 return wrap(_delegate.createDocumentFragment()); 138 } 139 140 public DOMElement createElement(String tagName) 141 throws DOMException 142 { 143 try { 144 return wrap(_delegate.createElement(tagName)); 145 } 146 catch (org.w3c.dom.DOMException ex) { 147 throw wrap(ex); 148 } 149 } 150 151 public DOMElement createElement(String tagName, String textContent) 152 throws DOMException 153 { 154 try { 155 DOMElement element = createElement(tagName); 156 157 element.setTextContent(textContent); 158 159 return element; 160 } 161 catch (org.w3c.dom.DOMException ex) { 162 throw wrap(ex); 163 } 164 } 165 166 public DOMElement createElementNS(String namespaceURI, String tagName) 167 throws DOMException 168 { 169 try { 170 return wrap(_delegate.createElementNS(namespaceURI, tagName)); 171 } 172 catch (org.w3c.dom.DOMException ex) { 173 throw wrap(ex); 174 } 175 } 176 177 public DOMElement createElementNS(String namespaceURI, 178 String tagName, 179 String textContent) 180 throws DOMException 181 { 182 try { 183 DOMElement element = createElementNS(namespaceURI, tagName); 184 185 element.setTextContent(textContent); 186 187 return element; 188 } 189 catch (org.w3c.dom.DOMException ex) { 190 throw wrap(ex); 191 } 192 } 193 194 public DOMEntityReference createEntityReference(String name) 195 throws DOMException 196 { 197 try { 198 return wrap(_delegate.createEntityReference(name)); 199 } 200 catch (org.w3c.dom.DOMException ex) { 201 throw wrap(ex); 202 } 203 } 204 205 public DOMProcessingInstruction createProcessingInstruction(String target) 206 throws DOMException 207 { 208 try { 209 return createProcessingInstruction(target, null); 210 } 211 catch (org.w3c.dom.DOMException ex) { 212 throw wrap(ex); 213 } 214 } 215 216 public DOMProcessingInstruction createProcessingInstruction(String target, 217 String data) 218 throws DOMException 219 { 220 try { 221 return wrap(_delegate.createProcessingInstruction(target, data)); 222 } 223 catch (org.w3c.dom.DOMException ex) { 224 throw wrap(ex); 225 } 226 } 227 228 public DOMText createTextNode(String data) 229 { 230 return wrap(_delegate.createTextNode(data)); 231 } 232 233 public DOMConfiguration getConfig() 234 { 235 throw new UnimplementedException(); 236 } 237 238 public DOMDocumentType getDoctype() 239 { 240 return wrap(_delegate.getDoctype()); 241 } 242 243 public DOMElement getDocumentElement() 244 { 245 return wrap(_delegate.getDocumentElement()); 246 } 247 248 public String getDocumentURI() 249 { 250 return _delegate.getDocumentURI(); 251 } 252 253 public DOMConfiguration getDomConfig() 254 { 255 return wrap(_delegate.getDomConfig()); 256 } 257 258 public DOMElement getElementById(String elementId) 259 { 260 return wrap(_delegate.getElementById(elementId)); 261 } 262 263 public DOMNodeList getElementsByTagName(String name) 264 { 265 return wrap(_delegate.getElementsByTagName(name)); 266 } 267 268 public DOMNodeList getElementsByTagNameNS(String uri, String name) 269 { 270 return wrap(_delegate.getElementsByTagNameNS(uri, name)); 271 } 272 273 public boolean getFormatOutput() 274 { 275 throw new UnimplementedException(); 276 } 277 278 public DOMImplementation getImplementation() 279 { 280 return getImpl(); 281 } 282 283 public String getInputEncoding() 284 { 285 return _delegate.getInputEncoding(); 286 } 287 288 public boolean getPreserveWhiteSpace() 289 { 290 throw new UnimplementedException(); 291 } 292 293 public boolean getRecover() 294 { 295 throw new UnimplementedException(); 296 } 297 298 public boolean getResolveExternals() 299 { 300 throw new UnimplementedException(); 301 } 302 303 public boolean getStrictErrorChecking() 304 { 305 return _delegate.getStrictErrorChecking(); 306 } 307 308 public boolean getSubstituteEntities() 309 { 310 throw new UnimplementedException(); 311 } 312 313 public boolean getValidateOnParse() 314 { 315 throw new UnimplementedException(); 316 } 317 318 public String getVersion() 319 { 320 return _delegate.getXmlVersion(); 321 } 322 323 public String getXmlEncoding() 324 { 325 return _delegate.getXmlEncoding(); 326 } 327 328 public boolean getXmlStandalone() 329 { 330 return _delegate.getXmlStandalone(); 331 } 332 333 public String getXmlVersion() 334 { 335 return _delegate.getXmlVersion(); 336 } 337 338 public DOMNode importNode(DOMNode node) 339 { 340 return importNode(node, false); 341 } 342 343 public DOMNode importNode(DOMNode importedNode, boolean deep) 344 throws DOMException 345 { 346 try { 347 return wrap(_delegate.importNode(importedNode.getDelegate(), deep)); 348 } 349 catch (org.w3c.dom.DOMException ex) { 350 throw wrap(ex); 351 } 352 } 353 354 public boolean load(Env env, Path path, @Optional Value options) 356 throws IOException 357 { 358 if (options != null) 359 env.stub(L.l("`{0}' is ignored", "options")); 360 361 ReadStream is = null; 362 363 try { 364 is = path.openRead(); 365 366 getImpl().parseXMLDocument(_delegate, is, path.getPath()); 367 } 368 catch (SAXException ex) { 369 env.warning(ex); 370 371 return false; 372 } 373 catch (IOException ex) { 374 env.warning(ex); 375 return false; 376 } 377 finally { 378 if (is != null) { 379 is.close(); 380 } 381 } 382 383 return true; 384 } 385 386 389 public boolean loadHTML(Env env, String source) 391 { 392 ReadStream is = StringStream.open(source); 393 394 try { 395 getImpl().parseHTMLDocument(_delegate, is, null); 396 397 _delegate.setXmlStandalone(true); 398 399 405 } 406 catch (SAXException ex) { 407 env.warning(ex); 408 return false; 409 } 410 catch (IOException ex) { 411 env.warning(ex); 412 return false; 413 } 414 finally { 415 if (is != null) { 416 is.close(); 417 } 418 } 419 420 return true; 421 } 422 423 public boolean loadHTMLFile(Env env, Path path) 425 { 426 ReadStream is = null; 427 428 try { 429 is = path.openRead(); 430 431 getImpl().parseHTMLDocument(_delegate, is, path.getPath()); 432 433 _delegate.setXmlStandalone(true); 434 440 } 441 catch (SAXException ex) { 442 env.warning(ex); 443 return false; 444 } 445 catch (IOException ex) { 446 env.warning(ex); 447 return false; 448 } 449 finally { 450 if (is != null) { 451 is.close(); 452 } 453 } 454 455 return true; 456 } 457 458 public boolean loadXML(Env env, String source, @Optional Value options) 460 { 461 if (options != null) 462 env.stub(L.l("`{0}' is ignored", "options")); 463 464 ReadStream is = StringStream.open(source); 465 466 try { 467 getImpl().parseXMLDocument(_delegate, is, null); 468 } 469 catch (SAXException ex) { 470 env.warning(ex); 471 472 return false; 473 } 474 catch (IOException ex) { 475 env.warning(ex); 476 return false; 477 } 478 finally { 479 if (is != null) { 480 is.close(); 481 } 482 } 483 484 return true; 485 } 486 487 public void normalizeDocument() 488 { 489 _delegate.normalizeDocument(); 490 } 491 492 public boolean relaxNGValidate(String rngFilename) 493 { 494 throw new UnimplementedException(); 495 } 496 497 public boolean relaxNGValidateSource(String rngSource) 498 { 499 throw new UnimplementedException(); 500 } 501 502 public DOMNode renameNode(DOMNode node, String namespaceURI, String qualifiedName) 503 throws DOMException 504 { 505 try { 506 return wrap(_delegate.renameNode(node.getDelegate(), namespaceURI, qualifiedName)); 507 } 508 catch (org.w3c.dom.DOMException ex) { 509 throw wrap(ex); 510 } 511 } 512 513 516 public Value save(Env env, Path path, @Optional Value options) 517 { 518 if (options != null) 519 env.stub(L.l("`{0}' is ignored", "options")); 520 521 return saveToFile(env, path, false); 522 } 523 524 private Value saveToFile(Env env, Path path, boolean isHTML) 525 { 526 WriteStream os = null; 527 528 try { 529 os = path.openWrite(); 530 saveToStream(os, isHTML); 531 } 532 catch (IOException ex) { 533 env.warning(ex); 534 return BooleanValue.FALSE; 535 } 536 finally { 537 if (os != null) { 538 try { 539 os.close(); 540 } 541 catch (Exception ex) { 542 log.log(Level.FINE, ex.toString(), ex); 543 } 544 } 545 } 546 547 return new LongValue(path.getLength()); 548 } 549 550 private void saveToStream(WriteStream os, boolean isHTML) 551 throws IOException 552 { 553 XmlPrinter printer = new XmlPrinter(os); 554 555 printer.setMethod(isHTML ? "html" : "xml"); 556 557 printer.setPrintDeclaration(true); 558 559 printer.setVersion(_delegate.getXmlVersion()); 560 printer.setEncoding(_encoding); 561 562 if (_delegate.getXmlStandalone()) 563 printer.setStandalone("yes"); 564 565 printer.printXml(_delegate); 566 567 if (hasChildNodes()) 568 os.println(); 569 } 570 571 @ReturnNullAsFalse 572 public StringValue saveHTML(Env env) 573 { 574 return saveToString(env, true); 575 } 576 577 private StringValue saveToString(Env env, boolean isHTML) 578 { 579 TempStream tempStream = new TempStream(); 580 581 try { 582 tempStream.openWrite(); 583 WriteStream os = new WriteStream(tempStream); 584 585 saveToStream(os, isHTML); 586 587 os.close(); 588 } 589 catch (IOException ex) { 590 tempStream.discard(); 591 env.warning(ex); 592 return null; 593 } 594 595 TempBufferStringValue result = new TempBufferStringValue(tempStream.getHead()); 596 597 tempStream.discard(); 598 599 return result; 600 } 601 602 605 606 public Value saveHTMLFile(Env env, Path path) 607 { 608 return saveToFile(env, path, true); 609 } 610 611 @ReturnNullAsFalse 612 public StringValue saveXML(Env env) 613 { 614 return saveToString(env, false); 615 } 616 617 public boolean schemaValidate(String schemaFilename) 618 { 619 throw new UnimplementedException(); 620 } 621 622 public boolean schemaValidateSource(String schemaSource) 623 { 624 throw new UnimplementedException(); 625 } 626 627 public void setDocumentURI(String documentURI) 628 { 629 _delegate.setDocumentURI(documentURI); 630 } 631 632 public void setFormatOutput(boolean formatOutput) 633 { 634 throw new UnimplementedException(); 635 } 636 637 public void setPreserveWhiteSpace(boolean preserveWhiteSpace) 638 { 639 throw new UnimplementedException(); 640 } 641 642 public void setRecover(boolean recover) 643 { 644 throw new UnimplementedException(); 645 } 646 647 public void setResolveExternals(boolean resolveExternals) 648 { 649 throw new UnimplementedException(); 650 } 651 652 public void setStrictErrorChecking(boolean strictErrorChecking) 653 { 654 _delegate.setStrictErrorChecking(strictErrorChecking); 655 } 656 657 public void setSubstituteEntities(boolean substituteEntities) 658 { 659 throw new UnimplementedException(); 660 } 661 662 public void setValidateOnParse(boolean validateOnParse) 663 { 664 throw new UnimplementedException(); 665 } 666 667 public void setXmlStandalone(boolean xmlStandalone) 668 throws DOMException 669 { 670 _delegate.setXmlStandalone(xmlStandalone); 671 } 672 673 public void setXmlVersion(String xmlVersion) 674 throws DOMException 675 { 676 _delegate.setXmlVersion(xmlVersion); 677 } 678 679 public boolean validate() 680 { 681 throw new UnimplementedException(); 682 } 683 684 public int xinclude(Env env, @Optional Value options) 685 { 686 if (options != null) 687 env.stub(L.l("`{0}' is ignored", "options")); 688 689 throw new UnimplementedException(); 690 } 691 } 692 | Popular Tags |