302 debugPrint("handler.result:" + handler.getResult()); 303 assertEquals(expected, handler.getResult(), "Catalog support for XInclude"); 304 } 305 306 /* 307 Verifies the Catalog support on DOM parser. 308 */ 309 public void testDOM(boolean setUseCatalog, boolean useCatalog, String catalog, 310 String xml, MyHandler handler, String expected) throws Exception { 311 DocumentBuilder docBuilder = getDomBuilder(setUseCatalog, useCatalog, catalog); 312 docBuilder.setEntityResolver(handler); 313 Document doc = docBuilder.parse(xml); 314 315 Node node = doc.getElementsByTagName(elementInSystem).item(0); 316 String result = node.getFirstChild().getTextContent(); 317 318 assertEquals(expected, result.trim(), "Catalog support for DOM"); 319 } 320 321 /* 322 Verifies the Catalog support on resolving DTD, xsd import and include in 323 Schema files. 324 */ 325 public void testValidation(boolean setUseCatalog, boolean useCatalog, String catalog, 326 String xsd, LSResourceResolver resolver) 327 throws Exception { 328 329 SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 330 331 // use resolver or catalog if resolver = null 332 if (resolver != null) { 333 factory.setResourceResolver(resolver); 334 } 335 if (setUseCatalog) { 336 factory.setFeature(XMLConstants.USE_CATALOG, useCatalog); 337 } 338 factory.setProperty(CatalogFeatures.Feature.FILES.getPropertyName(), catalog); 339 340 Schema schema = factory.newSchema(new StreamSource(new StringReader(xsd))); 341 success("XMLSchema.dtd and datatypes.dtd are resolved."); 497 * @return a DOMSource 498 * @throws Exception 499 */ 500 DOMSource getDOMSource(String uri, String systemId, boolean setUseCatalog, 501 boolean useCatalog, String catalog) { 502 DOMSource ds = null; 503 try { 504 DocumentBuilder builder = getDomBuilder(setUseCatalog, useCatalog, catalog); 505 Document doc = builder.parse(new File(uri)); 506 ds = new DOMSource(doc, systemId); 507 } catch (Exception e) {} 508 509 return ds; 510 } 511 512 /** 513 * Creates a StAXSource. 514 * 515 * @param xmlFile the XML source file 516 * @param xmlFileId the systemId of the source 517 * @return a StAXSource 518 * @throws XMLStreamException 519 * @throws FileNotFoundException 520 */ 521 StAXSource getStaxSource(String xmlFile, String xmlFileId) { 522 StAXSource ss = null; 523 try { 524 ss = new StAXSource( 525 XMLInputFactory.newFactory().createXMLEventReader( 526 xmlFileId, new FileInputStream(xmlFile))); 527 } catch (Exception e) {} 528 529 return ss; 530 } 531 532 /** 533 * Creates an XMLStreamReader. 534 * @param catalog the path to a catalog 535 * @param xml the xml to be parsed 536 * @param resolver a resolver to be set on the reader 537 * @return an instance of the XMLStreamReader 538 * @throws FileNotFoundException 539 * @throws XMLStreamException 540 */ 541 XMLStreamReader getStreamReader(boolean setUseCatalog, boolean useCatalog, 542 String catalog, String xml, XMLResolver resolver) 543 throws FileNotFoundException, XMLStreamException { 544 XMLInputFactory factory = XMLInputFactory.newInstance(); 545 factory.setProperty(CatalogFeatures.Feature.FILES.getPropertyName(), catalog); 546 factory.setProperty(XMLInputFactory.IS_COALESCING, true); 547 factory.setProperty(XMLInputFactory.RESOLVER, resolver); 548 if (setUseCatalog) { 549 factory.setProperty(XMLConstants.USE_CATALOG, useCatalog); 550 } 551 552 InputStream entityxml = new FileInputStream(xml); 553 XMLStreamReader streamReader = factory.createXMLStreamReader(xml, entityxml); 554 return streamReader; 555 } 556 557 /** 558 * Returns the text of the first element found by the reader. 559 * @param streamReader the XMLStreamReader 560 * @return the text of the first element 561 * @throws XMLStreamException 562 */ 563 String getText(XMLStreamReader streamReader) throws XMLStreamException { 564 while(streamReader.hasNext()){ 565 int eventType = streamReader.next() ; 566 if(eventType == XMLStreamConstants.START_ELEMENT){ 567 eventType = streamReader.next() ; 568 if(eventType == XMLStreamConstants.CHARACTERS){ 569 return streamReader.getText() ; 570 } 571 } 572 } 573 return null; 574 } 575 576 /** 577 * Returns an instance of TransformerFactory with either a custom URIResolver 578 * or Catalog. 579 * 580 * @param setUseCatalog a flag indicates whether USE_CATALOG shall be set 581 * through the factory 582 * @param useCatalog the value of USE_CATALOG 583 * @param catalog a catalog 584 * @param resolver a custom resolver 585 * @return an instance of TransformerFactory 586 * @throws Exception 587 */ 588 TransformerFactory getTransformerFactory(boolean setUseCatalog, boolean useCatalog, 589 String catalog, URIResolver resolver) 590 throws Exception { 591 592 TransformerFactory factory = TransformerFactory.newInstance(); 593 if (setUseCatalog) { | 302 debugPrint("handler.result:" + handler.getResult()); 303 assertEquals(expected, handler.getResult(), "Catalog support for XInclude"); 304 } 305 306 /* 307 Verifies the Catalog support on DOM parser. 308 */ 309 public void testDOM(boolean setUseCatalog, boolean useCatalog, String catalog, 310 String xml, MyHandler handler, String expected) throws Exception { 311 DocumentBuilder docBuilder = getDomBuilder(setUseCatalog, useCatalog, catalog); 312 docBuilder.setEntityResolver(handler); 313 Document doc = docBuilder.parse(xml); 314 315 Node node = doc.getElementsByTagName(elementInSystem).item(0); 316 String result = node.getFirstChild().getTextContent(); 317 318 assertEquals(expected, result.trim(), "Catalog support for DOM"); 319 } 320 321 /* 322 Verifies the Catalog support on StAX parser. 323 */ 324 public void testStAX(boolean setUseCatalog, boolean useCatalog, String catalog, 325 String xml, XMLResolver resolver, String expected) throws Exception { 326 327 XMLStreamReader streamReader = getStreamReader( 328 setUseCatalog, useCatalog, catalog, xml, resolver); 329 String text = getText(streamReader, XMLStreamConstants.CHARACTERS); 330 assertEquals(expected, text.trim(), "Catalog support for StAX"); 331 } 332 333 /* 334 Verifies that the Catalog support for StAX parser is disabled when 335 USE_CATALOG == false. 336 */ 337 public void testStAXNegative(boolean setUseCatalog, boolean useCatalog, String catalog, 338 String xml, XMLResolver resolver, String expected) throws Exception { 339 340 XMLStreamReader streamReader = getStreamReader( 341 setUseCatalog, useCatalog, catalog, xml, resolver); 342 String text = getText(streamReader, XMLStreamConstants.ENTITY_REFERENCE); 343 assertEquals(expected, text.trim(), "Catalog support for StAX"); 344 } 345 346 /* 347 Verifies the Catalog support on resolving DTD, xsd import and include in 348 Schema files. 349 */ 350 public void testValidation(boolean setUseCatalog, boolean useCatalog, String catalog, 351 String xsd, LSResourceResolver resolver) 352 throws Exception { 353 354 SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 355 356 // use resolver or catalog if resolver = null 357 if (resolver != null) { 358 factory.setResourceResolver(resolver); 359 } 360 if (setUseCatalog) { 361 factory.setFeature(XMLConstants.USE_CATALOG, useCatalog); 362 } 363 factory.setProperty(CatalogFeatures.Feature.FILES.getPropertyName(), catalog); 364 365 Schema schema = factory.newSchema(new StreamSource(new StringReader(xsd))); 366 success("XMLSchema.dtd and datatypes.dtd are resolved."); 522 * @return a DOMSource 523 * @throws Exception 524 */ 525 DOMSource getDOMSource(String uri, String systemId, boolean setUseCatalog, 526 boolean useCatalog, String catalog) { 527 DOMSource ds = null; 528 try { 529 DocumentBuilder builder = getDomBuilder(setUseCatalog, useCatalog, catalog); 530 Document doc = builder.parse(new File(uri)); 531 ds = new DOMSource(doc, systemId); 532 } catch (Exception e) {} 533 534 return ds; 535 } 536 537 /** 538 * Creates a StAXSource. 539 * 540 * @param xmlFile the XML source file 541 * @param xmlFileId the systemId of the source 542 * @param setUseCatalog a flag indicates whether USE_CATALOG shall be set 543 * through the factory 544 * @param useCatalog the value of USE_CATALOG 545 * @param catalog a catalog 546 * @return a StAXSource 547 * @throws XMLStreamException 548 * @throws FileNotFoundException 549 */ 550 StAXSource getStaxSource(String xmlFile, String xmlFileId, boolean setUseCatalog, 551 boolean useCatalog, String catalog) { 552 StAXSource ss = null; 553 try { 554 XMLInputFactory xif = XMLInputFactory.newFactory(); 555 if (setUseCatalog) { 556 xif.setProperty(XMLConstants.USE_CATALOG, useCatalog); 557 } 558 xif.setProperty(CatalogFeatures.Feature.FILES.getPropertyName(), catalog); 559 ss = new StAXSource(xif.createXMLEventReader( 560 xmlFileId, new FileInputStream(xmlFile))); 561 } catch (Exception e) {} 562 563 return ss; 564 } 565 566 /** 567 * Creates an XMLStreamReader. 568 * 569 * @param setUseCatalog a flag indicates whether USE_CATALOG shall be set 570 * through the factory 571 * @param useCatalog the value of USE_CATALOG 572 * @param catalog the path to a catalog 573 * @param xml the xml to be parsed 574 * @param resolver a resolver to be set on the reader 575 * @return an instance of the XMLStreamReader 576 * @throws FileNotFoundException 577 * @throws XMLStreamException 578 */ 579 XMLStreamReader getStreamReader(boolean setUseCatalog, boolean useCatalog, 580 String catalog, String xml, XMLResolver resolver) 581 throws FileNotFoundException, XMLStreamException { 582 XMLInputFactory factory = XMLInputFactory.newInstance(); 583 if (catalog != null) { 584 factory.setProperty(CatalogFeatures.Feature.FILES.getPropertyName(), catalog); 585 } 586 587 factory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, true); 588 factory.setProperty(XMLInputFactory.IS_COALESCING, true); 589 590 if (resolver != null) { 591 factory.setProperty(XMLInputFactory.RESOLVER, resolver); 592 } 593 594 if (setUseCatalog) { 595 factory.setProperty(XMLConstants.USE_CATALOG, useCatalog); 596 } 597 598 InputStream entityxml = new FileInputStream(xml); 599 XMLStreamReader streamReader = factory.createXMLStreamReader(xml, entityxml); 600 return streamReader; 601 } 602 603 /** 604 * Returns the text of the first element found by the reader. 605 * @param streamReader the XMLStreamReader 606 * @return the text of the first element 607 * @throws XMLStreamException 608 */ 609 String getText(XMLStreamReader streamReader, int type) throws XMLStreamException { 610 StringBuilder text = new StringBuilder(); 611 StringBuilder entityRef = new StringBuilder(); 612 613 while(streamReader.hasNext()){ 614 int eventType = streamReader.next(); 615 switch (eventType) { 616 case XMLStreamConstants.START_ELEMENT: 617 break; 618 case XMLStreamConstants.CHARACTERS: 619 text.append(streamReader.getText()); 620 break; 621 case XMLStreamConstants.ENTITY_REFERENCE: 622 entityRef.append(streamReader.getText()); 623 break; 624 } 625 } 626 if (type == XMLStreamConstants.CHARACTERS) { 627 return text.toString(); 628 } else { 629 return entityRef.toString(); 630 } 631 } 632 633 /** 634 * Returns an instance of TransformerFactory with either a custom URIResolver 635 * or Catalog. 636 * 637 * @param setUseCatalog a flag indicates whether USE_CATALOG shall be set 638 * through the factory 639 * @param useCatalog the value of USE_CATALOG 640 * @param catalog a catalog 641 * @param resolver a custom resolver 642 * @return an instance of TransformerFactory 643 * @throws Exception 644 */ 645 TransformerFactory getTransformerFactory(boolean setUseCatalog, boolean useCatalog, 646 String catalog, URIResolver resolver) 647 throws Exception { 648 649 TransformerFactory factory = TransformerFactory.newInstance(); 650 if (setUseCatalog) { |