Search in sources :

Example 1 with CatalogManager

use of org.apache.xml.resolver.CatalogManager in project midpoint by Evolveum.

the class SchemaRegistryImpl method initResolver.

private void initResolver() throws IOException {
    CatalogManager catalogManager = new CatalogManager();
    catalogManager.setUseStaticCatalog(true);
    catalogManager.setIgnoreMissingProperties(true);
    catalogManager.setVerbosity(1);
    catalogManager.setPreferPublic(true);
    CatalogResolver catalogResolver = new CatalogResolver(catalogManager);
    Catalog catalog = catalogResolver.getCatalog();
    if (catalogFiles != null && catalogFiles.length > 0) {
        for (File catalogFile : catalogFiles) {
            LOGGER.trace("Using catalog file {}", catalogFile);
            catalog.parseCatalog(catalogFile.getPath());
        }
    } else if (catalogResourceName != null) {
        LOGGER.trace("Using catalog from resource: {}", catalogResourceName);
        Enumeration<URL> catalogs = Thread.currentThread().getContextClassLoader().getResources(catalogResourceName);
        while (catalogs.hasMoreElements()) {
            URL catalogResourceUrl = catalogs.nextElement();
            LOGGER.trace("Parsing catalog from URL: {}", catalogResourceUrl);
            catalog.parseCatalog(catalogResourceUrl);
        }
    } else {
        throw new IllegalStateException("Catalog is not defined");
    }
    builtinSchemaResolver = catalogResolver;
}
Also used : Enumeration(java.util.Enumeration) CatalogResolver(org.apache.xml.resolver.tools.CatalogResolver) File(java.io.File) CatalogManager(org.apache.xml.resolver.CatalogManager) Catalog(org.apache.xml.resolver.Catalog) URL(java.net.URL)

Example 2 with CatalogManager

use of org.apache.xml.resolver.CatalogManager in project cxf by apache.

the class OASISCatalogManager method getResolver.

private static EntityResolver getResolver() {
    try {
        CatalogManager catalogManager = new CatalogManager();
        if (DEBUG_LEVEL != null) {
            catalogManager.debug.setDebug(Integer.parseInt(DEBUG_LEVEL));
        }
        catalogManager.setUseStaticCatalog(false);
        catalogManager.setIgnoreMissingProperties(true);
        return new CatalogResolver(catalogManager) {

            public String getResolvedEntity(String publicId, String systemId) {
                String s = super.getResolvedEntity(publicId, systemId);
                if (s != null && s.startsWith("classpath:")) {
                    try (URIResolver r = new URIResolver(s)) {
                        if (r.isResolved()) {
                            return r.getURL().toExternalForm();
                        }
                    } catch (IOException e) {
                    // ignore
                    }
                }
                return s;
            }
        };
    } catch (Throwable t) {
    // ignore
    }
    return null;
}
Also used : URIResolver(org.apache.cxf.resource.URIResolver) IOException(java.io.IOException) CatalogResolver(org.apache.xml.resolver.tools.CatalogResolver) CatalogManager(org.apache.xml.resolver.CatalogManager)

Example 3 with CatalogManager

use of org.apache.xml.resolver.CatalogManager in project midpoint by Evolveum.

the class SchemaDistMojo method execute.

public void execute() throws MojoExecutionException, MojoFailureException {
    getLog().info("SchemaDist plugin started");
    try {
        processArtifactItems();
    } catch (InvalidVersionSpecificationException e) {
        handleFailure(e);
    }
    final File outDir = initializeOutDir(outputDirectory);
    CatalogManager catalogManager = new CatalogManager();
    catalogManager.setVerbosity(999);
    for (ArtifactItem artifactItem : artifactItems) {
        Artifact artifact = artifactItem.getArtifact();
        getLog().info("SchemaDist unpacking artifact " + artifact);
        File workDir = new File(workDirectory, artifact.getArtifactId());
        initializeOutDir(workDir);
        artifactItem.setWorkDir(workDir);
        unpack(artifactItem, workDir);
        if (translateSchemaLocation) {
            String catalogPath = artifactItem.getCatalog();
            if (catalogPath != null) {
                File catalogFile = new File(workDir, catalogPath);
                if (!catalogFile.exists()) {
                    throw new MojoExecutionException("No catalog file " + catalogPath + " in artifact " + artifact);
                }
                Catalog catalog = new Catalog(catalogManager);
                catalog.setupReaders();
                try {
                    // UGLY HACK. On Windows, file names like d:\abc\def\catalog.xml eventually get treated very strangely
                    // (resulting in names like "file:<current-working-dir>d:\abc\def\catalog.xml" that are obviously wrong)
                    // Prefixing such names with "file:/" helps.
                    String prefix;
                    if (catalogFile.isAbsolute() && !catalogFile.getPath().startsWith("/")) {
                        prefix = "/";
                    } else {
                        prefix = "";
                    }
                    String fileName = "file:" + prefix + catalogFile.getPath();
                    getLog().debug("Calling parseCatalog with: " + fileName);
                    catalog.parseCatalog(fileName);
                } catch (MalformedURLException e) {
                    throw new MojoExecutionException("Error parsing catalog file " + catalogPath + " in artifact " + artifact, e);
                } catch (IOException e) {
                    throw new MojoExecutionException("Error parsing catalog file " + catalogPath + " in artifact " + artifact, e);
                }
                artifactItem.setResolveCatalog(catalog);
            }
        } else {
            getLog().debug("Catalog search disabled for " + artifact);
        }
    }
    for (ArtifactItem artifactItem : artifactItems) {
        Artifact artifact = artifactItem.getArtifact();
        getLog().info("SchemaDist processing artifact " + artifact);
        final File workDir = artifactItem.getWorkDir();
        FileVisitor<Path> fileVisitor = new FileVisitor<Path>() {

            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                // nothing to do
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFile(Path filePath, BasicFileAttributes attrs) throws IOException {
                String fileName = filePath.getFileName().toString();
                if (fileName.endsWith(".xsd")) {
                    getLog().debug("=======================> Processing file " + filePath);
                    try {
                        processXsd(filePath, workDir, outDir);
                    } catch (MojoExecutionException | MojoFailureException e) {
                        throw new RuntimeException(e.getMessage(), e);
                    }
                } else if (fileName.endsWith(".wsdl")) {
                    getLog().debug("=======================> Processing file " + filePath);
                    try {
                        processWsdl(filePath, workDir, outDir);
                    } catch (MojoExecutionException | MojoFailureException e) {
                        throw new RuntimeException(e.getMessage(), e);
                    }
                } else {
                    getLog().debug("=======================> Skipping file " + filePath);
                }
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
                return FileVisitResult.TERMINATE;
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                // nothing to do
                return FileVisitResult.CONTINUE;
            }
        };
        try {
            Files.walkFileTree(workDir.toPath(), fileVisitor);
        } catch (IOException e) {
            throw new MojoExecutionException("Error processing files of artifact " + artifact, e);
        }
    }
    getLog().info("SchemaDist plugin finished");
}
Also used : Path(java.nio.file.Path) MalformedURLException(java.net.MalformedURLException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) CatalogManager(org.apache.xml.resolver.CatalogManager) Artifact(org.apache.maven.artifact.Artifact) Catalog(org.apache.xml.resolver.Catalog) InvalidVersionSpecificationException(org.apache.maven.artifact.versioning.InvalidVersionSpecificationException) FileVisitor(java.nio.file.FileVisitor) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 4 with CatalogManager

use of org.apache.xml.resolver.CatalogManager in project intellij-community by JetBrains.

the class XMLCatalogManagerTest method testCatalogManager.

public void testCatalogManager() throws Exception {
    XMLCatalogManager manager = getManager();
    CatalogManager catalogManager = manager.getManager();
    Vector files = catalogManager.getCatalogFiles();
    assertEquals(1, files.size());
    String filePath = (String) files.get(0);
    assertTrue(filePath, filePath.endsWith("catalog.xml"));
    assertTrue(filePath, new File(new URI(filePath)).exists());
}
Also used : XMLCatalogManager(com.intellij.javaee.XMLCatalogManager) Vector(java.util.Vector) File(java.io.File) URI(java.net.URI) CatalogManager(org.apache.xml.resolver.CatalogManager) XMLCatalogManager(com.intellij.javaee.XMLCatalogManager)

Aggregations

CatalogManager (org.apache.xml.resolver.CatalogManager)4 File (java.io.File)2 Catalog (org.apache.xml.resolver.Catalog)2 CatalogResolver (org.apache.xml.resolver.tools.CatalogResolver)2 XMLCatalogManager (com.intellij.javaee.XMLCatalogManager)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 URI (java.net.URI)1 URL (java.net.URL)1 FileVisitor (java.nio.file.FileVisitor)1 Path (java.nio.file.Path)1 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)1 Enumeration (java.util.Enumeration)1 Vector (java.util.Vector)1 URIResolver (org.apache.cxf.resource.URIResolver)1 Artifact (org.apache.maven.artifact.Artifact)1 InvalidVersionSpecificationException (org.apache.maven.artifact.versioning.InvalidVersionSpecificationException)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 MojoFailureException (org.apache.maven.plugin.MojoFailureException)1