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 4, 2023
1 parent 949454e commit c0f704f
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ public class ClasspathCatalogResolver extends

@Override
public String getResolvedEntity(String publicId, String systemId) {
// System.out.println("Resolving [" + publicId + "], [" + systemId + "].");
// System.out.println("Resolving [" + publicId + "], [" + systemId + "].");
final String result = super.getResolvedEntity(publicId, systemId);
// System.out.println("Resolved to [" + result+ "].");
// System.out.println("Resolved to [" + result+ "].");

if (result == null) {
// System.err.println(MessageFormat.format(
Expand Down
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,7 +1,13 @@
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 org.jvnet.jaxb.maven.util.StringUtils;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
Expand All @@ -12,33 +18,51 @@ public class ReResolvingEntityResolverWrapper implements EntityResolver {

public ReResolvingEntityResolverWrapper(EntityResolver entityResolver) {
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;
}

@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);
// System.out.println(MessageFormat.format("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.");
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);
// System.out.println(MessageFormat.format("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());
return new ReResolvingInputSourceWrapper(this.entityResolver, resolvedInputSource, pId, sId, resolvedInputSource.getPublicId(), resolvedInputSource.getSystemId());
}
}

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

private static boolean checkFileExists(String sId) {
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
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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<xs:schema xmlns="B" targetNamespace="B" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="foo">
<xs:restriction base="xs:string"/>
</xs:simpleType>
</xs:schema>
3 changes: 2 additions & 1 deletion maven-plugin/tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<module>MAVEN_JAXB2_PLUGIN-53</module>
<module>MAVEN_JAXB2_PLUGIN-69</module>
<module>MAVEN_JAXB2_PLUGIN-70</module>
<!-- module>MAVEN_JAXB2_PLUGIN-77</module -->
<module>MAVEN_JAXB2_PLUGIN-77</module>
<module>MAVEN_JAXB2_PLUGIN-79</module>
<module>MAVEN_JAXB2_PLUGIN-82</module>
<module>MAVEN_JAXB2_PLUGIN-86</module>
Expand All @@ -52,6 +52,7 @@
<module>gh-issue-23</module>
<module>gh-issue-58</module>
<module>java-9</module>
<module>jt-244</module>
</modules>
<build>
<pluginManagement>
Expand Down

0 comments on commit c0f704f

Please sign in to comment.