From 33f6c312c4dd59e8c232fcbd14f7179b87d8e523 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 | 80 +++++++++++++++++-- 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, 160 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..19854ef 100644 --- a/src/main/java/org/codehaus/mojo/xml/Resolver.java +++ b/src/main/java/org/codehaus/mojo/xml/Resolver.java @@ -178,7 +178,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 +245,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 +316,7 @@ private URL resolveAsFile( String pResource ) } } - private URL resolveAsURL( String pResource ) + private URL resolveAsURL( String pResource,URI pBaseURI ) { InputStream stream = null; try @@ -318,7 +329,41 @@ private URL resolveAsURL( String pResource ) } catch ( IOException e ) { - return null; + //fall through to relative URI resolution + } + finally + { + if ( stream != null ) + { + try + { + stream.close(); + } + catch ( Throwable t ) + { + // Ignore me + } + } + } + try{ + URI resourceASURI = new URI (pResource); + if (pBaseURI!=null &&!resourceASURI.isAbsolute() && pBaseURI.isAbsolute()){ + resourceASURI=pBaseURI.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 { @@ -334,6 +379,7 @@ private URL resolveAsURL( String pResource ) } } } + return null; } /** @@ -343,6 +389,11 @@ private URL resolveAsURL( String pResource ) */ public URL resolve( String pResource ) { + return resolve(pResource,(URI)null); + } + + + private URL resolve(String pResource,URI pBaseURI){ if ( pResource == null ) { return null; @@ -357,16 +408,19 @@ public URL resolve( String pResource ) URL url = resolveAsResource( pResource ); if ( url == null ) { - url = resolveAsURL( pResource ); + url = resolveAsURL( pResource,null ); //original style resolution if ( url == null ) { url = resolveAsFile( pResource ); } + if (url == null){ //relative URL resolution + url = resolveAsURL( pResource,pBaseURI ); + } } try { - return locator.getResource( pResource ).getURL(); + return locator.getResource( url.toExternalForm()).getURL(); } catch ( ResourceNotFoundException e ) { @@ -398,8 +452,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..7985b93 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 it18 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.