javax.xml.catalog.CatalogResolver Java Examples

The following examples show how to use javax.xml.catalog.CatalogResolver. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CatalogTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "hierarchyOfCatFilesData")
public void hierarchyOfCatFiles2(String systemId, String expectedUri) {
    String file1 = getClass().getResource("first_cat.xml").toExternalForm();
    String file2 = getClass().getResource("second_cat.xml").toExternalForm();
    String files = file1 + ";" + file2;

    try {
        setSystemProperty(KEY_FILES, files);
        CatalogResolver catalogResolver = CatalogManager.catalogResolver(CatalogFeatures.defaults());
        String sysId = catalogResolver.resolveEntity(null, systemId).getSystemId();
        Assert.assertEquals(sysId, Paths.get(filepath + expectedUri).toUri().toString().replace("///", "/"),
                "System ID match not right");
    } finally {
        clearSystemProperty(KEY_FILES);
    }

}
 
Example #2
Source File: CatalogTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testIgnoreInvalidCatalog() {
    String catalog = getClass().getResource("catalog_invalid.xml").toExternalForm();
    CatalogFeatures f = CatalogFeatures.builder()
            .with(Feature.FILES, catalog)
            .with(Feature.PREFER, "public")
            .with(Feature.DEFER, "true")
            .with(Feature.RESOLVE, "ignore")
            .build();

    String test = "testInvalidCatalog";
    try {
        CatalogResolver resolver = CatalogManager.catalogResolver(f);
        String actualSystemId = resolver.resolveEntity(
                null,
                "http://remote/xml/dtd/sys/alice/docAlice.dtd")
                .getSystemId();
        System.out.println("testIgnoreInvalidCatalog: expected [null]");
        System.out.println("testIgnoreInvalidCatalog: expected [null]");
        System.out.println("actual [" + actualSystemId + "]");
        Assert.assertEquals(actualSystemId, null);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
 
Example #3
Source File: CatalogTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testInvalidCatalog() throws Exception {
    String expectedMsgId = "JAXP09040001";
    URI catalog = getClass().getResource("catalog_invalid.xml").toURI();

    try {
        CatalogResolver resolver = CatalogManager.catalogResolver(
                CatalogFeatures.defaults(), catalog);
        String actualSystemId = resolver.resolveEntity(
                null,
                "http://remote/xml/dtd/sys/alice/docAlice.dtd")
                .getSystemId();
    } catch (Exception e) {
        String msg = e.getMessage();
        if (msg != null) {
            Assert.assertTrue(msg.contains(expectedMsgId),
                    "Message shall contain the corrent message ID " + expectedMsgId);
        }
    }
}
 
Example #4
Source File: CatalogTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @bug 8146237
 * PREFER from Features API taking precedence over catalog file
 */
@Test
public void testJDK8146237() throws Exception {
    URI catalogFile = getClass().getResource("JDK8146237_catalog.xml").toURI();

    try {
        CatalogFeatures features = CatalogFeatures.builder()
                .with(CatalogFeatures.Feature.PREFER, "system")
                .build();
        Catalog catalog = CatalogManager.catalog(features, catalogFile);
        CatalogResolver catalogResolver = CatalogManager.catalogResolver(catalog);
        String actualSystemId = catalogResolver.resolveEntity(
                "-//FOO//DTD XML Dummy V0.0//EN",
                "http://www.oracle.com/alt1sys.dtd")
                .getSystemId();
        Assert.assertTrue(actualSystemId.contains("dummy.dtd"),
                "Resulting id should contain dummy.dtd, indicating a match by publicId");

    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
 
Example #5
Source File: CatalogTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "catalog")
public void testCatalogResolver(String test, String expected, String catalogFile,
        String xml, SAXParser saxParser) throws Exception {
    URI catalog = null;
    if (catalogFile != null) {
        catalog = getClass().getResource(catalogFile).toURI();
    }
    String url = getClass().getResource(xml).getFile();
    try {
        CatalogResolver cr = CatalogManager.catalogResolver(CatalogFeatures.defaults(), catalog);
        XMLReader reader = saxParser.getXMLReader();
        reader.setEntityResolver(cr);
        MyHandler handler = new MyHandler(saxParser);
        reader.setContentHandler(handler);
        reader.parse(url);
        System.out.println(test + ": expected [" + expected + "] <> actual [" + handler.getResult() + "]");
        Assert.assertEquals(handler.getResult(), expected);
    } catch (SAXException | IOException e) {
        Assert.fail(e.getMessage());
    }
}
 
Example #6
Source File: UriTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testSpecifyBaseByAPI() {
    checkUriResolution(createResolver(),
            "http://remote/dtd/carl/docCarl.dtd",
            "http://local/carlBase/dtd/docCarlURI.dtd");

    CatalogResolver continueResolver = catalogUriResolver(
            CatalogFeatures.builder().with(CatalogFeatures.Feature.RESOLVE,
                    RESOLVE_CONTINUE).build(), CATALOG_URI);
    checkUriResolution(continueResolver, "docCarl.dtd",
            "http://local/alternativeBase/dtd/",
            "http://local/alternativeBase/dtd/docCarl.dtd");
}
 
Example #7
Source File: CatalogTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testRewriteSystem() throws Exception {
    URI catalog = getClass().getResource("rewriteCatalog.xml").toURI();

    try {
        CatalogResolver resolver = CatalogManager.catalogResolver(CatalogFeatures.defaults(), catalog);
        String actualSystemId = resolver.resolveEntity(null, "http://remote.com/dtd/book.dtd").getSystemId();
        Assert.assertTrue(!actualSystemId.contains("//"), "result contains duplicate slashes");
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }

}
 
Example #8
Source File: CatalogTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "resolveUri")
public void testMatch1(String cFile, String href, String expectedFile,
        String expectedUri, String msg) throws Exception {
    URI catalogFile = getClass().getResource(cFile).toURI();
    CatalogResolver cur = CatalogManager.catalogResolver(CatalogFeatures.defaults(), catalogFile);
    Source source = cur.resolve(href, null);
    Assert.assertNotNull(source, "Source returned is null");
    Assert.assertEquals(expectedUri, source.getSystemId(), msg);
}
 
Example #9
Source File: CatalogTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "resolveWithPrefer")
public void resolveWithPrefer(String prefer, String cfile, String publicId,
        String systemId, String expected) throws Exception {
    URI catalogFile = getClass().getResource(cfile).toURI();
    CatalogFeatures f = CatalogFeatures.builder()
            .with(CatalogFeatures.Feature.PREFER, prefer)
            .with(CatalogFeatures.Feature.RESOLVE, "ignore")
            .build();
    CatalogResolver catalogResolver = CatalogManager.catalogResolver(f, catalogFile);
    String result = catalogResolver.resolveEntity(publicId, systemId).getSystemId();
    Assert.assertEquals(expected, result);
}
 
Example #10
Source File: CatalogTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "resolveEntity")
public void testMatch1(String cfile, String prefer, String sysId, String pubId,
        String expectedUri, String expectedFile, String msg) throws Exception {
    URI catalogFile = getClass().getResource(cfile).toURI();
    CatalogFeatures features = CatalogFeatures.builder().with(CatalogFeatures.Feature.PREFER, prefer).build();
    CatalogResolver catalogResolver = CatalogManager.catalogResolver(features, catalogFile);
    InputSource is = catalogResolver.resolveEntity(pubId, sysId);
    Assert.assertNotNull(is, msg);
    String expected = (expectedUri == null) ? expectedFile : expectedUri;
    Assert.assertEquals(expected, is.getSystemId(), msg);
}
 
Example #11
Source File: PropertiesTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static void testPropertiesOnEntityResolver() {
    CatalogResolver entityResolver = catalogResolver((String[]) null);
    entityResolver.resolveEntity("-//REMOTE//DTD DOCDUMMY XML//EN",
            "http://remote/sys/dtd/docDummy.dtd");
    "http://local/base/dtd/docSys.dtd".equals(
            entityResolver.resolveEntity("-//REMOTE//DTD DOC XML//EN",
                    "http://remote/dtd/doc.dtd").getSystemId());
}
 
Example #12
Source File: CatalogTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "supportURIResolver")
public void supportURIResolver(URI catalogFile, Source xsl, Source xml, String expected) throws Exception {

    CatalogResolver cr = CatalogManager.catalogResolver(CatalogFeatures.defaults(), catalogFile);

        TransformerFactory factory = TransformerFactory.newInstance();
        factory.setURIResolver(cr);
        Transformer transformer = factory.newTransformer(xsl);
        StringWriter out = new StringWriter();
        transformer.transform(xml, new StreamResult(out));
        if (expected != null) {
            Assert.assertTrue(out.toString().contains(expected), "supportURIResolver");
        }
}
 
Example #13
Source File: CatalogTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "supportLSResourceResolver1")
public void supportLSResourceResolver1(URI catalogFile, Source source) throws Exception {

    CatalogResolver cr = CatalogManager.catalogResolver(CatalogFeatures.defaults(), catalogFile);

    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Validator validator = factory.newSchema().newValidator();
    validator.setResourceResolver(cr);
    validator.validate(source);
}
 
Example #14
Source File: CatalogTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "supportXMLResolver")
public void supportXMLResolver(URI catalogFile, String xml, String expected) throws Exception {
    String xmlSource = getClass().getResource(xml).getFile();

    CatalogResolver cr = CatalogManager.catalogResolver(CatalogFeatures.defaults(), catalogFile);

    XMLInputFactory xifactory = XMLInputFactory.newInstance();
    xifactory.setProperty(XMLInputFactory.IS_COALESCING, true);
    xifactory.setProperty(XMLInputFactory.RESOLVER, cr);
    File file = new File(xmlSource);
    String systemId = file.toURI().toASCIIString();
    InputStream entityxml = new FileInputStream(file);
    XMLStreamReader streamReader = xifactory.createXMLStreamReader(systemId, entityxml);
    String result = null;
    while (streamReader.hasNext()) {
        int eventType = streamReader.next();
        if (eventType == XMLStreamConstants.START_ELEMENT) {
            eventType = streamReader.next();
            if (eventType == XMLStreamConstants.CHARACTERS) {
                result = streamReader.getText();
            }
        }
    }
    System.out.println(": expected [" + expected + "] <> actual [" + result.trim() + "]");

    Assert.assertEquals(result.trim(), expected);
}
 
Example #15
Source File: CatalogTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "supportXMLResolver")
public void supportEntityResolver(URI catalogFile, String xml, String expected) throws Exception {
    String xmlSource = getClass().getResource(xml).getFile();

    CatalogResolver cr = CatalogManager.catalogResolver(CatalogFeatures.defaults(), catalogFile);
    MyCatalogHandler handler = new MyCatalogHandler(cr, elementInSystem);
    SAXParser parser = getSAXParser(false, true, null);
    parser.parse(xmlSource, handler);

    Assert.assertEquals(handler.getResult().trim(), expected);
}
 
Example #16
Source File: CatalogTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testRewriteUri() throws Exception {
    URI catalog = getClass().getResource("rewriteCatalog.xml").toURI();

    try {

        CatalogResolver resolver = CatalogManager.catalogResolver(CatalogFeatures.defaults(), catalog);
        String actualSystemId = resolver.resolve("http://remote.com/import/import.xsl", null).getSystemId();
        Assert.assertTrue(!actualSystemId.contains("//"), "result contains duplicate slashes");
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
 
Example #17
Source File: ResolutionChecker.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
static <T extends Throwable> void expectExceptionOnUri(
        CatalogResolver resolver, String href, String base,
        Class<T> expectedExceptionClass) {
    expectThrows(expectedExceptionClass, () -> {
        resolver.resolve(href, base);
    });
}
 
Example #18
Source File: CatalogFileInputTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "acceptedURI")
public void testMatch(String uri, String sysId, String pubId,
        String expectedId, String msg) throws Exception {
    CatalogResolver cr = CatalogManager.catalogResolver(FEATURES, URI.create(uri));
    InputSource is = cr.resolveEntity(pubId, sysId);
    Assert.assertNotNull(is, msg);
    Assert.assertEquals(expectedId, is.getSystemId(), msg);
}
 
Example #19
Source File: CatalogFileInputTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "invalidCatalog", expectedExceptions = CatalogException.class)
public void testCatalogResolverWEmptyCatalog(String uri, String publicId, String msg) {
    CatalogResolver cr = CatalogManager.catalogResolver(
            CatalogFeatures.builder().with(CatalogFeatures.Feature.RESOLVE, "strict").build(),
            uri != null? URI.create(uri) : null);
    InputSource is = cr.resolveEntity(publicId, "");
}
 
Example #20
Source File: CatalogFileInputTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "invalidCatalog")
public void testCatalogResolverWEmptyCatalog1(String uri, String publicId, String msg) {
    CatalogResolver cr = CatalogManager.catalogResolver(
            CatalogFeatures.builder().with(CatalogFeatures.Feature.RESOLVE, "continue").build(),
            uri != null? URI.create(uri) : null);
    Assert.assertNull(cr.resolveEntity(publicId, ""), msg);
}
 
Example #21
Source File: ResolutionChecker.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
static <T extends Throwable> void expectExceptionOnExtId(
        CatalogResolver resolver, String publicId, String systemId,
        Class<T> expectedExceptionClass) {
    expectThrows(expectedExceptionClass, () -> {
        resolver.resolveEntity(publicId, getNotSpecified(systemId));
    });
}
 
Example #22
Source File: ResolveFeatureTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testContinueResolutionOnEntityResolver() {
    CatalogResolver resolver = createEntityResolver(RESOLVE_CONTINUE);
    resolver.resolveEntity(null, "http://remote/dtd/bob/docBobDummy.dtd");
    checkSysIdResolution(resolver, "http://remote/dtd/bob/docBob.dtd",
            "http://local/base/dtd/docBobSys.dtd");
}
 
Example #23
Source File: ResolveFeatureTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testContinueResolutionOnUriResolver() {
    CatalogResolver resolver = createUriResolver(RESOLVE_CONTINUE);
    resolver.resolve("http://remote/dtd/bob/docBobDummy.dtd", null);
    checkUriResolution(resolver, "http://remote/dtd/bob/docBob.dtd",
            "http://local/base/dtd/docBobURI.dtd");
}
 
Example #24
Source File: DeferFeatureTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "testDeferFeatureByResolve")
public void testDeferFeatureByResolve(Catalog catalog, int catalogCount)
        throws Exception {
    CatalogResolver cr = createResolver(catalog);
    // trigger loading alternative catalogs
    try {
        cr.resolveEntity("-//REMOTE//DTD ALICE DOCALICE", "http://remote/dtd/alice/");
    } catch (CatalogException ce) {}

    Assert.assertEquals(loadedCatalogCount(catalog), catalogCount);
}
 
Example #25
Source File: CatalogTestUtils.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
static CatalogResolver catalogResolver(CatalogFeatures features,
        String... catalogName) {
    return (catalogName == null) ?
            CatalogManager.catalogResolver(features) :
            CatalogManager.catalogResolver(features, getCatalogPaths(catalogName));
}
 
Example #26
Source File: SpecifyCatalogTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private void checkResolutionOnUriResolver(CatalogResolver resolver,
        String matchedUri) {
    checkUriResolution(resolver, ID_URI, matchedUri);
}
 
Example #27
Source File: CatalogTestUtils.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
static CatalogResolver catalogResolver(String... catalogName) {
    return catalogResolver(CatalogFeatures.defaults(), catalogName);
}
 
Example #28
Source File: ResolveFeatureTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private CatalogResolver createEntityResolver(String resolve) {
    return catalogResolver(createFeature(resolve), CATALOG_SYSTEM);
}
 
Example #29
Source File: CatalogTestUtils.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
static CatalogResolver catalogUriResolver(String... catalogName) {
    return catalogUriResolver(CatalogFeatures.defaults(), catalogName);
}
 
Example #30
Source File: SpecifyCatalogTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private void checkResolutionOnEntityResolver(CatalogResolver resolver,
        String matchedUri) {
    checkSysIdResolution(resolver, ID_SYS, matchedUri);
}