From 8a5273a630dda5b2f14927daf15453dad8ac6b4a Mon Sep 17 00:00:00 2001 From: Ross Lamont Date: Fri, 24 Nov 2017 12:57:10 +1100 Subject: [PATCH] Motivation: When resolving xml-model style validations, relative href do not work. As this is relying on the EntityResolver, there are possibly other relative entity resolutions which do not work either. Solution: Added relative URI handling to the resolveURL method of Resolver class. Code attempts the original code path before attempting to resolve relative URI's to preserver legacy behavior as much as possible. - also updated latest jxvc version. --- pom.xml | 4 +- .../java/org/codehaus/mojo/xml/Resolver.java | 79 +++++++++++++++++-- src/test/it18/pom.xml | 43 ++++++++++ src/test/it18/schema.rnc | 5 ++ src/test/it18/xml/doc1.xml | 21 +++++ .../mojo/xml/test/ValidateMojoTest.java | 17 ++++ 6 files changed, 159 insertions(+), 10 deletions(-) create mode 100644 src/test/it18/pom.xml create mode 100644 src/test/it18/schema.rnc create mode 100644 src/test/it18/xml/doc1.xml diff --git a/pom.xml b/pom.xml index 0afdcb9..0145bc3 100644 --- a/pom.xml +++ b/pom.xml @@ -163,13 +163,13 @@ com.componentcorp.xml.validation jxvc - 0.9.3 + 0.9.4 test com.componentcorp.xml.validation relaxng-compact - 0.9.3 + 0.9.4 test diff --git a/src/main/java/org/codehaus/mojo/xml/Resolver.java b/src/main/java/org/codehaus/mojo/xml/Resolver.java index 9d14869..9d5f582 100644 --- a/src/main/java/org/codehaus/mojo/xml/Resolver.java +++ b/src/main/java/org/codehaus/mojo/xml/Resolver.java @@ -28,6 +28,8 @@ import java.net.URISyntaxException; import java.net.URL; import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParserFactory; @@ -178,7 +180,7 @@ public Source resolve( String pHref, String pBase ) if ( null == url ) { - url = resolve( pHref ); + url = resolve( pHref ); //probably should call new method resolve(String,URI) but left alone for legacy reasons. } if ( url != null ) @@ -245,8 +247,19 @@ public LSInput resolveResource( String pType, String pNamespaceURI, String pPubl { return newLSInput( isource ); } + URI baseURI = null; + if (pBaseURI!=null){ + try + { + baseURI = new URI(pBaseURI); + } + catch ( URISyntaxException ex ) + { + baseURI=null; //or perhaps this should be an UndeclaredThrowableException + } + } - URL url = resolve( pSystemId ); + URL url = resolve( pSystemId ,baseURI); if ( url != null ) { try @@ -305,7 +318,7 @@ private URL resolveAsFile( String pResource ) } } - private URL resolveAsURL( String pResource ) + private URL resolveAsURL( String pResource,URI baseURI ) { InputStream stream = null; try @@ -318,7 +331,7 @@ private URL resolveAsURL( String pResource ) } catch ( IOException e ) { - return null; + //fall through to relative URI resolution } finally { @@ -334,6 +347,41 @@ private URL resolveAsURL( String pResource ) } } } + try{ + URI resourceASURI = new URI (pResource); + if (baseURI!=null &&!resourceASURI.isAbsolute() && baseURI.isAbsolute()){ + resourceASURI=baseURI.resolve( resourceASURI ); + final URL url = resourceASURI.toURL(); + stream = url.openStream(); + stream.close(); + stream = null; + return url; + + } + } + catch ( URISyntaxException ex ) + { + //ignore + } + catch ( IOException e ) + { + //ignore + } + finally + { + if ( stream != null ) + { + try + { + stream.close(); + } + catch ( Throwable t ) + { + // Ignore me + } + } + } + return null; } /** @@ -343,6 +391,11 @@ private URL resolveAsURL( String pResource ) */ public URL resolve( String pResource ) { + return resolve(pResource,(URI)null); + } + + + private URL resolve(String pResource,URI baseURI){ if ( pResource == null ) { return null; @@ -357,7 +410,7 @@ public URL resolve( String pResource ) URL url = resolveAsResource( pResource ); if ( url == null ) { - url = resolveAsURL( pResource ); + url = resolveAsURL( pResource,baseURI ); if ( url == null ) { url = resolveAsFile( pResource ); @@ -366,7 +419,7 @@ public URL resolve( String pResource ) try { - return locator.getResource( pResource ).getURL(); + return locator.getResource( url.toExternalForm()).getURL(); } catch ( ResourceNotFoundException e ) { @@ -398,8 +451,18 @@ public InputSource resolveEntity( String pName, String pPublicId, String pBaseUR { return source; } - - URL url = resolve( pSystemId ); + URI baseURI = null; + if (pBaseURI!=null){ + try + { + baseURI = new URI(pBaseURI); + } + catch ( URISyntaxException ex ) + { + throw new SAXException("Incorrectly formatted base URI", ex); + } + } + URL url = resolve( pSystemId ,baseURI); if ( url != null ) { return asInputSource( url ); diff --git a/src/test/it18/pom.xml b/src/test/it18/pom.xml new file mode 100644 index 0000000..a8a7efd --- /dev/null +++ b/src/test/it18/pom.xml @@ -0,0 +1,43 @@ + + + + 4.0.0 + org.codehaus.mojo.xml + it15 + 0.1 + Maven XML Plugin IT 15 + Integration Test 15 for the Maven XML Plugin + + + + org.codehaus.mojo + xml-maven-plugin + 0.2 + + + + xml + http://componentcorp.com/xml/ns/xml-model/1.0 + + + + + + + diff --git a/src/test/it18/schema.rnc b/src/test/it18/schema.rnc new file mode 100644 index 0000000..6617337 --- /dev/null +++ b/src/test/it18/schema.rnc @@ -0,0 +1,5 @@ + +element counter { + + element item { empty }* +} diff --git a/src/test/it18/xml/doc1.xml b/src/test/it18/xml/doc1.xml new file mode 100644 index 0000000..505d839 --- /dev/null +++ b/src/test/it18/xml/doc1.xml @@ -0,0 +1,21 @@ + + + + + diff --git a/src/test/java/org/codehaus/mojo/xml/test/ValidateMojoTest.java b/src/test/java/org/codehaus/mojo/xml/test/ValidateMojoTest.java index 242a067..f36be53 100644 --- a/src/test/java/org/codehaus/mojo/xml/test/ValidateMojoTest.java +++ b/src/test/java/org/codehaus/mojo/xml/test/ValidateMojoTest.java @@ -142,6 +142,23 @@ public void testIt15() } + /** + * Builds, and runs the it15 project. + * @throws Exception The test failed. + */ + public void testIt18() + throws Exception + { + try{ + runTest( "src/test/it18" ); + } + catch(Exception e){ + e.printStackTrace(); + fail(); + } + } + + /** * Builds and runs the xinclude test project * @throws Exception The test failed.