javax.xml.catalog.CatalogManager Java Examples

The following examples show how to use javax.xml.catalog.CatalogManager. 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: MCREntityResolver.java    From mycore with GNU General Public License v3.0 6 votes vote down vote up
private MCREntityResolver() {
    Enumeration<URL> systemResources;
    try {
        systemResources = MCRClassTools.getClassLoader().getResources("catalog.xml");
    } catch (IOException e) {
        throw new ExceptionInInitializerError(e);
    }
    URI[] catalogURIs = MCRStreamUtils.asStream(systemResources)
        .map(URL::toString)
        .peek(c -> LOGGER.info("Using XML catalog: {}", c))
        .map(URI::create)
        .toArray(URI[]::new);
    catalogResolver = CatalogManager.catalogResolver(CatalogFeatures.defaults(), catalogURIs);
    int cacheSize = MCRConfiguration2.getInt(CONFIG_PREFIX + "StaticFiles.CacheSize").orElse(100);
    bytesCache = new MCRCache<>(cacheSize, "EntityResolver Resources");
}
 
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
@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 #5
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 #6
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 #7
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 #8
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 #9
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 #10
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 #11
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 #12
Source File: CatalogTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @bug 8150969
 * Verifies that the defer attribute set in the catalog file takes precedence
 * over other settings, in which case, whether next and delegate Catalogs will
 * be loaded is determined by the defer attribute.
 */
@Test(dataProvider = "invalidAltCatalogs", expectedExceptions = CatalogException.class)
public void testDeferAltCatalogs(String file) throws Exception {
    URI catalogFile = getClass().getResource(file).toURI();
    CatalogFeatures features = CatalogFeatures.builder().
            with(CatalogFeatures.Feature.DEFER, "true")
            .build();
    /*
      Since the defer attribute is set to false in the specified catalog file,
      the parent catalog will try to load the alt catalog, which will fail
      since it points to an invalid catalog.
    */
    Catalog catalog = CatalogManager.catalog(features, catalogFile);
}
 
Example #13
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 #14
Source File: CatalogTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "matchWithPrefer")
public void matchWithPrefer(String prefer, String cfile, String publicId,
        String systemId, String expected) throws Exception {
    URI catalogFile = getClass().getResource(cfile).toURI();
    Catalog c = CatalogManager.catalog(
            CatalogFeatures.builder().with(CatalogFeatures.Feature.PREFER, prefer).build(),
            catalogFile);
    String result;
    if (publicId != null && publicId.length() > 0) {
        result = c.matchPublic(publicId);
    } else {
        result = c.matchSystem(systemId);
    }
    Assert.assertEquals(expected, result);
}
 
Example #15
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 #16
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 #17
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 #18
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 #19
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 #20
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 #21
Source File: XmlCatalogUtil.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Instantiate catalog resolver using new catalog API (javax.xml.catalog.*)
 * added in JDK9. Usage of new API removes dependency on internal API
 * (com.sun.org.apache.xml.internal) for modular runtime.
 */
private static EntityResolver createCatalogResolver(ArrayList<URL> urls) throws Exception {
    // Prepare array of catalog URIs
    URI[] uris = urls.stream()
                         .map(u -> URI.create(u.toExternalForm()))
                         .toArray(URI[]::new);

    //Create CatalogResolver with new JDK9+ API
    return (EntityResolver) CatalogManager.catalogResolver(CATALOG_FEATURES, uris);
}
 
Example #22
Source File: CatalogUtil.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
static EntityResolver getCatalog(EntityResolver entityResolver, File catalogFile, ArrayList<URI> catalogUrls) throws IOException {
    return CatalogManager.catalogResolver(
            CATALOG_FEATURES, catalogUrls.stream().toArray(URI[]::new));
}
 
Example #23
Source File: CatalogTestUtils.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
static CatalogResolver catalogUriResolver(
        CatalogFeatures features, String... catalogName) {
    return (catalogName == null) ?
            CatalogManager.catalogResolver(features) :
            CatalogManager.catalogResolver(features, getCatalogPaths(catalogName));
}
 
Example #24
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 #25
Source File: DeferFeatureTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private CatalogResolver createResolver(Catalog catalog) {
    return CatalogManager.catalogResolver(catalog);
}
 
Example #26
Source File: DeferFeatureTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private Catalog createCatalog(CatalogFeatures feature) {
    return CatalogManager.catalog(feature, getCatalogPath("deferFeature.xml"));
}
 
Example #27
Source File: CatalogFileInputTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions = NullPointerException.class)
public void testNullUri1() {
    URI uri = null;
    Catalog c = CatalogManager.catalog(FEATURES, uri);
}
 
Example #28
Source File: CatalogFileInputTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions = NullPointerException.class)
public void testNullUri() {
    URI uri = null;
    CatalogResolver cr = CatalogManager.catalogResolver(FEATURES, uri);
}
 
Example #29
Source File: CatalogFileInputTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test(dataProvider = "invalidInput", expectedExceptions = IllegalArgumentException.class)
public void testInvalidUri1(String file) {
    Catalog c = CatalogManager.catalog(FEATURES, file != null? URI.create(file) : null);
    System.err.println("Catalog =" + c);
}
 
Example #30
Source File: CatalogFileInputTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test(dataProvider = "invalidInput", expectedExceptions = IllegalArgumentException.class)
public void testInvalidUri(String file) {
    CatalogResolver cr = CatalogManager.catalogResolver(FEATURES, file != null? URI.create(file) : null);
}