Search in sources :

Example 1 with Catalog

use of org.apache.xml.resolver.Catalog 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 Catalog

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

the class XMLCatalogManager method resolve.

@Nullable
public String resolve(String uri) {
    try {
        Catalog catalog = myManager.getCatalog();
        if (catalog == null)
            return null;
        String resolved = catalog.resolveSystem(uri);
        return resolved == null ? catalog.resolvePublic(uri, null) : resolved;
    } catch (IOException e) {
        LOG.warn(e);
        return null;
    }
}
Also used : IOException(java.io.IOException) Catalog(org.apache.xml.resolver.Catalog) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with Catalog

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

the class SchemaDistMojo method resolveSchemaLocation.

private String resolveSchemaLocation(String namespaceOrLocation, Path filePath, File workDir) throws MojoExecutionException, IOException {
    for (ArtifactItem artifactItem : artifactItems) {
        Catalog catalog = artifactItem.getResolveCatalog();
        if (catalog == null) {
            continue;
        }
        String publicId = namespaceOrLocation;
        if (publicId.endsWith("#")) {
            publicId = publicId.substring(0, publicId.length() - 1);
        }
        String resolvedString = catalog.resolveEntity(filePath.toString(), publicId, publicId);
        if (resolvedString != null) {
            getLog().debug("-------------------");
            getLog().debug("Resolved namespace/schemaLocation " + namespaceOrLocation + " to " + resolvedString + " using catalog " + catalog);
            URL resolvedUrl = new URL(resolvedString);
            String resolvedPathString = resolvedUrl.getPath();
            Path resolvedPath = new File(resolvedPathString).toPath();
            Path workDirPath = workDir.toPath();
            Path resolvedRelativeToCatalogWorkdir = artifactItem.getWorkDir().toPath().relativize(resolvedPath);
            Path fileRelativeToWorkdir = workDirPath.relativize(filePath);
            getLog().debug("workDirPath: " + workDirPath);
            getLog().debug("resolvedRelativeToCatalogWorkdir: " + resolvedRelativeToCatalogWorkdir + ",  fileRelativeToWorkdir: " + fileRelativeToWorkdir);
            Path relativePath = fileRelativeToWorkdir.getParent().relativize(resolvedRelativeToCatalogWorkdir);
            getLog().debug("Rel: " + relativePath);
            String unixSeparators = FilenameUtils.separatorsToUnix(relativePath.toString());
            getLog().debug("Normalized to use UNIX separators: " + unixSeparators);
            return unixSeparators;
        }
    }
    throw new MojoExecutionException("Cannot resolve namespace " + namespaceOrLocation + " in file " + filePath + " using any of the catalogs");
}
Also used : Path(java.nio.file.Path) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Catalog(org.apache.xml.resolver.Catalog) URL(java.net.URL)

Example 4 with Catalog

use of org.apache.xml.resolver.Catalog 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)

Aggregations

Catalog (org.apache.xml.resolver.Catalog)4 URL (java.net.URL)2 Path (java.nio.file.Path)2 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)2 CatalogManager (org.apache.xml.resolver.CatalogManager)2 File (java.io.File)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 FileVisitor (java.nio.file.FileVisitor)1 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)1 Enumeration (java.util.Enumeration)1 Artifact (org.apache.maven.artifact.Artifact)1 InvalidVersionSpecificationException (org.apache.maven.artifact.versioning.InvalidVersionSpecificationException)1 MojoFailureException (org.apache.maven.plugin.MojoFailureException)1 CatalogResolver (org.apache.xml.resolver.tools.CatalogResolver)1 Nullable (org.jetbrains.annotations.Nullable)1