From 824c7d69680bf7bf1dc49143c067b847022f2aea Mon Sep 17 00:00:00 2001 From: Laurent SCHOELENS Date: Fri, 4 Aug 2023 09:17:20 +0200 Subject: [PATCH] Fixing issues 215 / 244 due to xjc-ri changes from 2.3.4 --- .../org/jvnet/jaxb/maven/RawXJC2Mojo.java | 6 +- .../tools/ClasspathCatalogResolver.java | 39 +++++++---- .../tools/LoggingCatalogResolver.java | 11 ++++ .../resolver/tools/MavenCatalogResolver.java | 18 ++--- .../ReResolvingEntityResolverWrapper.java | 66 ++++++++++++++----- .../tools/ReResolvingInputSourceWrapper.java | 42 ++++++++---- .../MAVEN_JAXB2_PLUGIN-77/service/pom.xml | 9 ++- maven-plugin/tests/jt-244/pom.xml | 47 +++++++++++++ .../jt-244/src/main/resources/bindings.xjb | 5 ++ .../jt-244/src/main/resources/catalog.xml | 4 ++ .../jt-244/src/main/resources/schemas/a.xsd | 4 ++ .../src/main/resources/schemas/common/b.xsd | 5 ++ maven-plugin/tests/pom.xml | 3 +- 13 files changed, 200 insertions(+), 59 deletions(-) create mode 100644 maven-plugin/plugin-core/src/main/java/org/jvnet/jaxb/maven/resolver/tools/LoggingCatalogResolver.java create mode 100644 maven-plugin/tests/jt-244/pom.xml create mode 100644 maven-plugin/tests/jt-244/src/main/resources/bindings.xjb create mode 100644 maven-plugin/tests/jt-244/src/main/resources/catalog.xml create mode 100644 maven-plugin/tests/jt-244/src/main/resources/schemas/a.xsd create mode 100644 maven-plugin/tests/jt-244/src/main/resources/schemas/common/b.xsd diff --git a/maven-plugin/plugin-core/src/main/java/org/jvnet/jaxb/maven/RawXJC2Mojo.java b/maven-plugin/plugin-core/src/main/java/org/jvnet/jaxb/maven/RawXJC2Mojo.java index c675cab1b..92c5e7f94 100644 --- a/maven-plugin/plugin-core/src/main/java/org/jvnet/jaxb/maven/RawXJC2Mojo.java +++ b/maven-plugin/plugin-core/src/main/java/org/jvnet/jaxb/maven/RawXJC2Mojo.java @@ -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; @@ -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; } @@ -918,6 +919,9 @@ private CatalogResolver createCatalogResolverByClassName(final String catalogRes @SuppressWarnings("unchecked") final Class catalogResolverClass = (Class) draftCatalogResolverClass; final CatalogResolver catalogResolverInstance = catalogResolverClass.newInstance(); + if (catalogResolverInstance instanceof LoggingCatalogResolver) { + ((LoggingCatalogResolver) catalogResolverInstance).setLog(getLog()); + } return catalogResolverInstance; } } catch (ClassNotFoundException cnfex) { diff --git a/maven-plugin/plugin-core/src/main/java/org/jvnet/jaxb/maven/resolver/tools/ClasspathCatalogResolver.java b/maven-plugin/plugin-core/src/main/java/org/jvnet/jaxb/maven/resolver/tools/ClasspathCatalogResolver.java index 78031b0a7..3c87d05d9 100644 --- a/maven-plugin/plugin-core/src/main/java/org/jvnet/jaxb/maven/resolver/tools/ClasspathCatalogResolver.java +++ b/maven-plugin/plugin-core/src/main/java/org/jvnet/jaxb/maven/resolver/tools/ClasspathCatalogResolver.java @@ -1,24 +1,37 @@ 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; } @@ -26,25 +39,29 @@ public String getResolvedEntity(String publicId, String systemId) { 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; + } } diff --git a/maven-plugin/plugin-core/src/main/java/org/jvnet/jaxb/maven/resolver/tools/LoggingCatalogResolver.java b/maven-plugin/plugin-core/src/main/java/org/jvnet/jaxb/maven/resolver/tools/LoggingCatalogResolver.java new file mode 100644 index 000000000..ea0828738 --- /dev/null +++ b/maven-plugin/plugin-core/src/main/java/org/jvnet/jaxb/maven/resolver/tools/LoggingCatalogResolver.java @@ -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); +} diff --git a/maven-plugin/plugin-core/src/main/java/org/jvnet/jaxb/maven/resolver/tools/MavenCatalogResolver.java b/maven-plugin/plugin-core/src/main/java/org/jvnet/jaxb/maven/resolver/tools/MavenCatalogResolver.java index 10e7a650e..d44db300b 100644 --- a/maven-plugin/plugin-core/src/main/java/org/jvnet/jaxb/maven/resolver/tools/MavenCatalogResolver.java +++ b/maven-plugin/plugin-core/src/main/java/org/jvnet/jaxb/maven/resolver/tools/MavenCatalogResolver.java @@ -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; } @@ -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); diff --git a/maven-plugin/plugin-core/src/main/java/org/jvnet/jaxb/maven/resolver/tools/ReResolvingEntityResolverWrapper.java b/maven-plugin/plugin-core/src/main/java/org/jvnet/jaxb/maven/resolver/tools/ReResolvingEntityResolverWrapper.java index 268018241..0f89cd766 100644 --- a/maven-plugin/plugin-core/src/main/java/org/jvnet/jaxb/maven/resolver/tools/ReResolvingEntityResolverWrapper.java +++ b/maven-plugin/plugin-core/src/main/java/org/jvnet/jaxb/maven/resolver/tools/ReResolvingEntityResolverWrapper.java @@ -1,7 +1,16 @@ 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; @@ -9,36 +18,57 @@ 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; + } } diff --git a/maven-plugin/plugin-core/src/main/java/org/jvnet/jaxb/maven/resolver/tools/ReResolvingInputSourceWrapper.java b/maven-plugin/plugin-core/src/main/java/org/jvnet/jaxb/maven/resolver/tools/ReResolvingInputSourceWrapper.java index b6df23b64..8d15225b8 100644 --- a/maven-plugin/plugin-core/src/main/java/org/jvnet/jaxb/maven/resolver/tools/ReResolvingInputSourceWrapper.java +++ b/maven-plugin/plugin-core/src/main/java/org/jvnet/jaxb/maven/resolver/tools/ReResolvingInputSourceWrapper.java @@ -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 @@ -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) { } diff --git a/maven-plugin/tests/MAVEN_JAXB2_PLUGIN-77/service/pom.xml b/maven-plugin/tests/MAVEN_JAXB2_PLUGIN-77/service/pom.xml index 723c603cc..79c2b954a 100644 --- a/maven-plugin/tests/MAVEN_JAXB2_PLUGIN-77/service/pom.xml +++ b/maven-plugin/tests/MAVEN_JAXB2_PLUGIN-77/service/pom.xml @@ -26,14 +26,17 @@ org.jvnet.jaxb jaxb-maven-plugin - true - true src/main/resources/META-INF/project/schemas com.company.project.service.types src/main/jaxb/catalog.cat org.jvnet.jaxb.maven.resolver.tools.ClasspathCatalogResolver true - false + + + org.jvnet.jaxb + jaxb-maven-plugin-tests-MAVEN_JAXB2_PLUGIN-77-common-types + + diff --git a/maven-plugin/tests/jt-244/pom.xml b/maven-plugin/tests/jt-244/pom.xml new file mode 100644 index 000000000..dd981ae09 --- /dev/null +++ b/maven-plugin/tests/jt-244/pom.xml @@ -0,0 +1,47 @@ + + 4.0.0 + jaxb-maven-plugin-tests-244 + + org.jvnet.jaxb + jaxb-maven-plugin-tests + 2.0.4-SNAPSHOT + + jar + JAXB Tools :: Maven Plugin :: Test [JAXB-TOOLS 244] + + + org.glassfish.jaxb + jaxb-runtime + + + org.jvnet.jaxb + jaxb-maven-plugin-testing + test + + + + + + org.jvnet.jaxb + jaxb-maven-plugin + + + generate + + generate + + + false + src/main/resources/catalog.xml + src/main/resources/schemas + + a.xsd + + src/main/resources + + + + + + + diff --git a/maven-plugin/tests/jt-244/src/main/resources/bindings.xjb b/maven-plugin/tests/jt-244/src/main/resources/bindings.xjb new file mode 100644 index 000000000..bac4177b9 --- /dev/null +++ b/maven-plugin/tests/jt-244/src/main/resources/bindings.xjb @@ -0,0 +1,5 @@ + + diff --git a/maven-plugin/tests/jt-244/src/main/resources/catalog.xml b/maven-plugin/tests/jt-244/src/main/resources/catalog.xml new file mode 100644 index 000000000..d5bd041f2 --- /dev/null +++ b/maven-plugin/tests/jt-244/src/main/resources/catalog.xml @@ -0,0 +1,4 @@ + + + diff --git a/maven-plugin/tests/jt-244/src/main/resources/schemas/a.xsd b/maven-plugin/tests/jt-244/src/main/resources/schemas/a.xsd new file mode 100644 index 000000000..ec81cfd36 --- /dev/null +++ b/maven-plugin/tests/jt-244/src/main/resources/schemas/a.xsd @@ -0,0 +1,4 @@ + + + + diff --git a/maven-plugin/tests/jt-244/src/main/resources/schemas/common/b.xsd b/maven-plugin/tests/jt-244/src/main/resources/schemas/common/b.xsd new file mode 100644 index 000000000..bb804e736 --- /dev/null +++ b/maven-plugin/tests/jt-244/src/main/resources/schemas/common/b.xsd @@ -0,0 +1,5 @@ + + + + + diff --git a/maven-plugin/tests/pom.xml b/maven-plugin/tests/pom.xml index 119ba9288..0db56c17f 100644 --- a/maven-plugin/tests/pom.xml +++ b/maven-plugin/tests/pom.xml @@ -42,7 +42,7 @@ MAVEN_JAXB2_PLUGIN-53 MAVEN_JAXB2_PLUGIN-69 MAVEN_JAXB2_PLUGIN-70 - + MAVEN_JAXB2_PLUGIN-77 MAVEN_JAXB2_PLUGIN-79 MAVEN_JAXB2_PLUGIN-82 MAVEN_JAXB2_PLUGIN-86 @@ -53,6 +53,7 @@ gh-issue-58 java-9 jt-250 + jt-244