Skip to content

Commit

Permalink
Motivation: When resolving xml-model style validations, relative href
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rosslamont committed Nov 24, 2017
1 parent 40409cb commit 8a5273a
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 10 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,13 @@
<dependency>
<groupId>com.componentcorp.xml.validation</groupId>
<artifactId>jxvc</artifactId>
<version>0.9.3</version>
<version>0.9.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.componentcorp.xml.validation</groupId>
<artifactId>relaxng-compact</artifactId>
<version>0.9.3</version>
<version>0.9.4</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
79 changes: 71 additions & 8 deletions src/main/java/org/codehaus/mojo/xml/Resolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 )
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -318,7 +331,7 @@ private URL resolveAsURL( String pResource )
}
catch ( IOException e )
{
return null;
//fall through to relative URI resolution
}
finally
{
Expand All @@ -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;
}

/**
Expand All @@ -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;
Expand All @@ -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 );
Expand All @@ -366,7 +419,7 @@ public URL resolve( String pResource )

try
{
return locator.getResource( pResource ).getURL();
return locator.getResource( url.toExternalForm()).getURL();
}
catch ( ResourceNotFoundException e )
{
Expand Down Expand Up @@ -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 );
Expand Down
43 changes: 43 additions & 0 deletions src/test/it18/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2006 The Apache Software Foundation.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.codehaus.mojo.xml</groupId>
<artifactId>it15</artifactId>
<version>0.1</version>
<name>Maven XML Plugin IT 15</name>
<description>Integration Test 15 for the Maven XML Plugin</description>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>xml-maven-plugin</artifactId>
<version>0.2</version>
<configuration>
<validationSets>
<validationSet>
<dir>xml</dir>
<schemaLanguage>http://componentcorp.com/xml/ns/xml-model/1.0</schemaLanguage>
</validationSet>
</validationSets>
</configuration>
</plugin>
</plugins>
</build>
</project>
5 changes: 5 additions & 0 deletions src/test/it18/schema.rnc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

element counter {

element item { empty }*
}
21 changes: 21 additions & 0 deletions src/test/it18/xml/doc1.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!--
Copyright 2006 The Apache Software Foundation.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<?xml-model href="../schema.rnc" schematypens="http://www.iana.org/assignments/media-types/application/relax-ng-compact-syntax" ?>
<counter ><item/><item/><item/></counter>

17 changes: 17 additions & 0 deletions src/test/java/org/codehaus/mojo/xml/test/ValidateMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 8a5273a

Please sign in to comment.