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;
}
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;
}
}
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");
}
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");
}
Aggregations