Skip to content

Commit

Permalink
Fixing issues 215 / 244 due to xjc-ri changes from 2.3.4
Browse files Browse the repository at this point in the history
  • Loading branch information
Laurent SCHOELENS committed Aug 9, 2023
1 parent 93f623a commit 824c7d6
Show file tree
Hide file tree
Showing 13 changed files with 200 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import org.jvnet.jaxb.maven.net.CompositeURILastModifiedResolver;
import org.jvnet.jaxb.maven.net.FileURILastModifiedResolver;
import org.jvnet.jaxb.maven.net.URILastModifiedResolver;
import org.jvnet.jaxb.maven.resolver.tools.LoggingCatalogResolver;
import org.jvnet.jaxb.maven.resolver.tools.MavenCatalogResolver;
import org.jvnet.jaxb.maven.resolver.tools.ReResolvingEntityResolverWrapper;
import org.jvnet.jaxb.maven.util.ArtifactUtils;
Expand Down Expand Up @@ -878,7 +879,7 @@ private void setupEntityResolver() {
}

protected EntityResolver createEntityResolver(CatalogResolver catalogResolver) {
final EntityResolver entityResolver = new ReResolvingEntityResolverWrapper(catalogResolver);
final EntityResolver entityResolver = new ReResolvingEntityResolverWrapper(catalogResolver, getLog());
return entityResolver;
}

Expand Down Expand Up @@ -918,6 +919,9 @@ private CatalogResolver createCatalogResolverByClassName(final String catalogRes
@SuppressWarnings("unchecked")
final Class<? extends CatalogResolver> catalogResolverClass = (Class<? extends CatalogResolver>) draftCatalogResolverClass;
final CatalogResolver catalogResolverInstance = catalogResolverClass.newInstance();
if (catalogResolverInstance instanceof LoggingCatalogResolver) {
((LoggingCatalogResolver) catalogResolverInstance).setLog(getLog());
}
return catalogResolverInstance;
}
} catch (ClassNotFoundException cnfex) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,50 +1,67 @@
package org.jvnet.jaxb.maven.resolver.tools;

import com.sun.org.apache.xml.internal.resolver.CatalogManager;

import org.apache.maven.plugin.logging.Log;
import org.jvnet.jaxb.maven.plugin.logging.NullLog;

import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.text.MessageFormat;

public class ClasspathCatalogResolver extends
com.sun.org.apache.xml.internal.resolver.tools.CatalogResolver {
com.sun.org.apache.xml.internal.resolver.tools.CatalogResolver implements LoggingCatalogResolver {

public static final String URI_SCHEME_CLASSPATH = "classpath";
private Log log;

public ClasspathCatalogResolver() {
super();
this.log = NullLog.INSTANCE;
}

@Override
public String getResolvedEntity(String publicId, String systemId) {
// System.out.println("Resolving [" + publicId + "], [" + systemId + "].");

log.debug( "ClasspathCatalogResolver : Resolving [" + publicId + "], [" + systemId + "].");
final String result = super.getResolvedEntity(publicId, systemId);
// System.out.println("Resolved to [" + result+ "].");
log.debug("ClasspathCatalogResolver : Resolved to [" + result+ "].");

if (result == null) {
// System.err.println(MessageFormat.format(
// "Could not resolve publicId [{0}], systemId [{1}]",
// publicId, systemId));
log.info(MessageFormat.format(
"ClasspathCatalogResolver : Could not resolve publicId [{0}], systemId [{1}]",
publicId, systemId));
return null;
}

try {
final URI uri = new URI(result);
if (URI_SCHEME_CLASSPATH.equals(uri.getScheme())) {
final String schemeSpecificPart = uri.getSchemeSpecificPart();
// System.out.println("Resolve [" + schemeSpecificPart + "].");
log.debug("ClasspathCatalogResolver : Resolve [" + schemeSpecificPart + "].");

final URL resource = Thread.currentThread()
.getContextClassLoader()
.getResource(schemeSpecificPart);
if (resource == null) {
// System.out.println("Returning [" + null + "].");
log.debug("ClasspathCatalogResolver : Returning [" + null + "].");
return null;
} else {
// System.out.println("Returning to [" + resource.toString()+ "].");
log.debug("ClasspathCatalogResolver : Returning to [" + resource.toString() + "].");
return resource.toString();
}
} else {
// System.out.println("Returning to [" + result+ "].");
log.debug("ClasspathCatalogResolver : Returning to [" + result+ "].");
return result;
}
} catch (URISyntaxException urisex) {
// System.out.println("Returning to [" + result+ "].");
log.debug("ClasspathCatalogResolver : Returning to [" + result+ "].");
return result;
}
}

public void setLog(Log log) {
this.log = log;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.jvnet.jaxb.maven.resolver.tools;

import org.apache.maven.plugin.logging.Log;

/**
* This interface allow Maven XJC Mojo to pass it's logger to the CatalogResolver
* in order to allow debugging with appropriate maven flags.
*/
public interface LoggingCatalogResolver {
void setLog(Log log);
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,10 @@ protected Log getLog() {
@Override
public String getResolvedEntity(String publicId, String systemId) {
getLog().debug(
MessageFormat.format(
"Resolving publicId [{0}], systemId [{1}].", publicId,
systemId));
final String superResolvedEntity = super.getResolvedEntity(publicId,
systemId);
MessageFormat.format("Resolving publicId [{0}], systemId [{1}].", publicId, systemId));
final String superResolvedEntity = super.getResolvedEntity(publicId, systemId);
getLog().debug(
MessageFormat
.format("Parent resolver has resolved publicId [{0}], systemId [{1}] to [{2}].",
publicId, systemId, superResolvedEntity));
MessageFormat.format("Parent resolver has resolved publicId [{0}], systemId [{1}] to [{2}].", publicId, systemId, superResolvedEntity));
if (superResolvedEntity != null) {
systemId = superResolvedEntity;
}
Expand All @@ -69,13 +64,10 @@ public String getResolvedEntity(String publicId, String systemId) {
final URI uri = new URI(systemId);
if (URI_SCHEME_MAVEN.equals(uri.getScheme())) {
getLog().debug(
MessageFormat
.format("Resolving systemId [{1}] as Maven dependency resource.",
publicId, systemId));
MessageFormat.format("Resolving systemId [{1}] as Maven dependency resource.", publicId, systemId));
final String schemeSpecificPart = uri.getSchemeSpecificPart();
try {
final DependencyResource dependencyResource = DependencyResource
.valueOf(schemeSpecificPart);
final DependencyResource dependencyResource = DependencyResource.valueOf(schemeSpecificPart);
try {
final URL url = dependencyResourceResolver
.resolveDependencyResource(dependencyResource);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,44 +1,74 @@
package org.jvnet.jaxb.maven.resolver.tools;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.MessageFormat;
import java.util.Optional;

import org.apache.maven.plugin.logging.Log;
import org.jvnet.jaxb.maven.plugin.logging.NullLog;
import org.jvnet.jaxb.maven.util.StringUtils;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class ReResolvingEntityResolverWrapper implements EntityResolver {

private final EntityResolver entityResolver;
private final Log log;

public ReResolvingEntityResolverWrapper(EntityResolver entityResolver) {
public ReResolvingEntityResolverWrapper(EntityResolver entityResolver, Log log) {
if (entityResolver == null) {
throw new IllegalArgumentException(
"Provided entity resolver must not be null.");
throw new IllegalArgumentException("Provided entity resolver must not be null.");
}
this.entityResolver = entityResolver;
this.log = Optional.ofNullable(log).orElse(NullLog.INSTANCE);
}

@Override
public InputSource resolveEntity(String publicId, String systemId)
throws SAXException, IOException {
// System.out.println(MessageFormat.format("Resolving publicId [{0}], systemId [{1}].",
// publicId, systemId));
final InputSource resolvedInputSource = this.entityResolver
.resolveEntity(publicId, systemId);
log.debug(MessageFormat.format("ReResolvingEntityResolverWrapper : Resolving publicId [{0}], systemId [{1}].", publicId, systemId));
final InputSource resolvedInputSource = this.entityResolver.resolveEntity(publicId, systemId);
if (resolvedInputSource == null) {
// System.out.println("Resolution result is null.");
log.debug("ReResolvingEntityResolverWrapper : Resolution result is null.");
return null;
} else {
// System.out.println(MessageFormat.format(
// "Resolved to publicId [{0}], systemId [{1}].",
// resolvedInputSource.getPublicId(),
// resolvedInputSource.getSystemId()));
final String pId = publicId != null ? publicId
: resolvedInputSource.getPublicId();
final String sId = systemId != null ? systemId
: resolvedInputSource.getSystemId();
return new ReResolvingInputSourceWrapper(this.entityResolver,
resolvedInputSource, pId, sId);
log.debug(MessageFormat.format("ReResolvingEntityResolverWrapper : Resolved to publicId [{0}], systemId [{1}].", resolvedInputSource.getPublicId(), resolvedInputSource.getSystemId()));
final String pId = !StringUtils.isEmpty(publicId) ? publicId : resolvedInputSource.getPublicId();
final String sId = computeSystemId(systemId, resolvedInputSource.getSystemId(), log);
return new ReResolvingInputSourceWrapper(this.entityResolver, resolvedInputSource, pId, sId, resolvedInputSource.getPublicId(), resolvedInputSource.getSystemId());
}
}

private static String computeSystemId(String systemId, String resolvedSystemId, Log log) {
if (systemId == null) {
return resolvedSystemId;
}
if (resolvedSystemId == null) {
return systemId;
}
boolean fileExistsSystemId = checkFileExists(systemId, log);
boolean fileExistsResolvedSystemId = checkFileExists(resolvedSystemId, log);
return !StringUtils.isEmpty(systemId) && fileExistsSystemId ? systemId : fileExistsResolvedSystemId ? resolvedSystemId : systemId;
}

private static boolean checkFileExists(String sId, Log log) {
try {
URI uriSystemId = new URI(sId);
if ("file".equals(uriSystemId.getScheme())) {
if (!Files.exists(Paths.get(uriSystemId))) {
// resolved file does not exist, warn and let's continue with original systemId
log.warn(MessageFormat.format("ReResolvingEntityResolverWrapper : File {0} does not exists.", sId));
return false;
}
}
} catch (URISyntaxException ex) {
// ignore, let it be handled by parser as is
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,18 @@ public class ReResolvingInputSourceWrapper extends InputSource {
private final EntityResolver entityResolver;
private final InputSource inputSource;

private final String resolvedPublicId;
private final String resolvedSystemId;

public ReResolvingInputSourceWrapper(EntityResolver entityResolver,
InputSource inputSource, String publicId, String systemId) {
InputSource inputSource, String publicId, String systemId,
String resolvedPublicId, String resolvedSystemId) {
this.entityResolver = entityResolver;
this.inputSource = inputSource;
this.setPublicId(publicId);
this.setSystemId(systemId);
this.resolvedPublicId = resolvedPublicId;
this.resolvedSystemId = resolvedSystemId;
}

@Override
Expand All @@ -27,22 +33,34 @@ public Reader getCharacterStream() {
if (originalReader == null) {
return null;
} else {
try {
InputSource resolvedEntity = this.entityResolver.resolveEntity(
getPublicId(), getSystemId());
if (resolvedEntity != null) {
return resolvedEntity.getCharacterStream();
} else {
return originalReader;
}
} catch (IOException ioex) {
return originalReader;
} catch (SAXException saxex) {
Reader resolvedEntityReader = getResolvedEntity();
if (resolvedEntityReader != null) {
return resolvedEntityReader;
} else {
return originalReader;
}
}
}

private Reader getResolvedEntity() {
try {
InputSource resolvedEntity = this.entityResolver.resolveEntity(
getPublicId(), getSystemId());
if (resolvedEntity == null) {
resolvedEntity = this.entityResolver.resolveEntity(resolvedPublicId, resolvedSystemId);
}
if (resolvedEntity == null) {
return null;
} else {
return resolvedEntity.getCharacterStream();
}
} catch (IOException ioex) {
return null;
} catch (SAXException saxex) {
return null;
}
}

@Override
public void setCharacterStream(Reader characterStream) {
}
Expand Down
9 changes: 6 additions & 3 deletions maven-plugin/tests/MAVEN_JAXB2_PLUGIN-77/service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,17 @@
<groupId>org.jvnet.jaxb</groupId>
<artifactId>jaxb-maven-plugin</artifactId>
<configuration>
<episode>true</episode>
<useDependenciesAsEpisodes>true</useDependenciesAsEpisodes>
<schemaDirectory>src/main/resources/META-INF/project/schemas</schemaDirectory>
<generatePackage>com.company.project.service.types</generatePackage>
<catalog>src/main/jaxb/catalog.cat</catalog>
<catalogResolver>org.jvnet.jaxb.maven.resolver.tools.ClasspathCatalogResolver</catalogResolver>
<extension>true</extension>
<strict>false</strict>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb</groupId>
<artifactId>jaxb-maven-plugin-tests-MAVEN_JAXB2_PLUGIN-77-common-types</artifactId>
</plugin>
</plugins>
</configuration>
<executions>
<execution>
Expand Down
47 changes: 47 additions & 0 deletions maven-plugin/tests/jt-244/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>jaxb-maven-plugin-tests-244</artifactId>
<parent>
<groupId>org.jvnet.jaxb</groupId>
<artifactId>jaxb-maven-plugin-tests</artifactId>
<version>2.0.4-SNAPSHOT</version>
</parent>
<packaging>jar</packaging>
<name>JAXB Tools :: Maven Plugin :: Test [JAXB-TOOLS 244]</name>
<dependencies>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
</dependency>
<dependency>
<groupId>org.jvnet.jaxb</groupId>
<artifactId>jaxb-maven-plugin-testing</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb</groupId>
<artifactId>jaxb-maven-plugin</artifactId>
<executions>
<execution>
<id>generate</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<strict>false</strict>
<catalog>src/main/resources/catalog.xml</catalog>
<schemaDirectory>src/main/resources/schemas</schemaDirectory>
<schemaIncludes>
<include>a.xsd</include>
</schemaIncludes>
<bindingDirectory>src/main/resources</bindingDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
5 changes: 5 additions & 0 deletions maven-plugin/tests/jt-244/src/main/resources/bindings.xjb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<bindings xmlns="http://java.sun.com/xml/ns/jaxb" version="2.1"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
extensionBindingPrefixes="xjc">
</bindings>
4 changes: 4 additions & 0 deletions maven-plugin/tests/jt-244/src/main/resources/catalog.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog"
prefer="public">
<systemSuffix systemIdSuffix="b.xsd" uri="schemas/common/b.xsd"/>
</catalog>
4 changes: 4 additions & 0 deletions maven-plugin/tests/jt-244/src/main/resources/schemas/a.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<xs:schema xmlns:b="B" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:import namespace="B" schemaLocation="b.xsd"/>
<xs:element name="a" type="b:foo"/>
</xs:schema>
Loading

0 comments on commit 824c7d6

Please sign in to comment.