diff options
author | Martin Stockhammer <martin.stockhammer@ars.de> | 2017-09-08 10:06:01 +0200 |
---|---|---|
committer | Martin Stockhammer <martin.stockhammer@ars.de> | 2017-09-08 10:06:01 +0200 |
commit | 98715c57ea3dd35ab1cb44ea7a2ccd6397dc25c7 (patch) | |
tree | f3558b5714d73421a1df2239d4ff10b06824ef3d /archiva-modules/archiva-base/archiva-xml-tools | |
parent | 5437dfd6de11e16815ca184c296d2eafdff6905b (diff) | |
download | archiva-98715c57ea3dd35ab1cb44ea7a2ccd6397dc25c7.tar.gz archiva-98715c57ea3dd35ab1cb44ea7a2ccd6397dc25c7.zip |
Migrating xmltools and proxy to java.nio
Diffstat (limited to 'archiva-modules/archiva-base/archiva-xml-tools')
4 files changed, 39 insertions, 47 deletions
diff --git a/archiva-modules/archiva-base/archiva-xml-tools/src/main/java/org/apache/archiva/xml/XMLReader.java b/archiva-modules/archiva-base/archiva-xml-tools/src/main/java/org/apache/archiva/xml/XMLReader.java index d889e92d8..5d3f1cd0e 100644 --- a/archiva-modules/archiva-base/archiva-xml-tools/src/main/java/org/apache/archiva/xml/XMLReader.java +++ b/archiva-modules/archiva-base/archiva-xml-tools/src/main/java/org/apache/archiva/xml/XMLReader.java @@ -20,28 +20,18 @@ package org.apache.archiva.xml; */ import org.apache.commons.lang.StringUtils; -import org.dom4j.Attribute; -import org.dom4j.Document; -import org.dom4j.DocumentException; -import org.dom4j.Element; -import org.dom4j.Namespace; -import org.dom4j.Node; -import org.dom4j.QName; -import org.dom4j.XPath; +import org.dom4j.*; import org.dom4j.io.SAXReader; -import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import java.nio.charset.Charset; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.*; /** * XMLReader - a set of common xml utility methods for reading content out of an xml file. @@ -56,27 +46,27 @@ public class XMLReader private Map<String, String> namespaceMap = new HashMap<>(); - public XMLReader( String type, File file ) + public XMLReader( String type, Path file ) throws XMLException { - if ( !file.exists() ) + if ( !Files.exists(file) ) { - throw new XMLException( "file does not exist: " + file.getAbsolutePath() ); + throw new XMLException( "file does not exist: " + file.toAbsolutePath() ); } - if ( !file.isFile() ) + if ( !Files.isRegularFile(file) ) { - throw new XMLException( "path is not a file: " + file.getAbsolutePath() ); + throw new XMLException( "path is not a file: " + file.toAbsolutePath() ); } - if ( !file.canRead() ) + if ( !Files.isReadable(file) ) { - throw new XMLException( "Cannot read xml file due to permissions: " + file.getAbsolutePath() ); + throw new XMLException( "Cannot read xml file due to permissions: " + file.toAbsolutePath() ); } try { - init( type, file.toURL() ); + init( type, file.toUri().toURL() ); } catch ( MalformedURLException e ) { diff --git a/archiva-modules/archiva-base/archiva-xml-tools/src/test/java/org/apache/archiva/xml/AbstractArchivaXmlTestCase.java b/archiva-modules/archiva-base/archiva-xml-tools/src/test/java/org/apache/archiva/xml/AbstractArchivaXmlTestCase.java index 5f340053f..83155b453 100644 --- a/archiva-modules/archiva-base/archiva-xml-tools/src/test/java/org/apache/archiva/xml/AbstractArchivaXmlTestCase.java +++ b/archiva-modules/archiva-base/archiva-xml-tools/src/test/java/org/apache/archiva/xml/AbstractArchivaXmlTestCase.java @@ -19,12 +19,14 @@ package org.apache.archiva.xml; * under the License. */ -import java.io.File; - import junit.framework.TestCase; import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner; import org.junit.runner.RunWith; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + /** * AbstractArchivaXmlTestCase * @@ -42,17 +44,17 @@ public abstract class AbstractArchivaXmlTestCase protected static final String INFINITE_ARCHIVA = "The " + INFIN + " Archiva"; - protected File getExampleXml( String filename ) + protected Path getExampleXml(String filename ) { - File examplesDir = new File( "src/test/examples" ); - if ( !examplesDir.exists() ) + Path examplesDir = Paths.get("src/test/examples"); + if ( !Files.exists(examplesDir) ) { - fail( "Missing the examples directory: " + examplesDir.getAbsolutePath() ); + fail( "Missing the examples directory: " + examplesDir.toAbsolutePath() ); } - File exampleFile = new File( examplesDir, filename ); - if ( !exampleFile.exists() ) + Path exampleFile = examplesDir.resolve( filename ); + if ( !Files.exists(exampleFile) ) { - fail( "Missing the example xml file: " + exampleFile.getAbsolutePath() ); + fail( "Missing the example xml file: " + exampleFile.toAbsolutePath() ); } return exampleFile; } diff --git a/archiva-modules/archiva-base/archiva-xml-tools/src/test/java/org/apache/archiva/xml/LatinEntityResolutionReaderTest.java b/archiva-modules/archiva-base/archiva-xml-tools/src/test/java/org/apache/archiva/xml/LatinEntityResolutionReaderTest.java index b97e828fb..c3c7dd0de 100644 --- a/archiva-modules/archiva-base/archiva-xml-tools/src/test/java/org/apache/archiva/xml/LatinEntityResolutionReaderTest.java +++ b/archiva-modules/archiva-base/archiva-xml-tools/src/test/java/org/apache/archiva/xml/LatinEntityResolutionReaderTest.java @@ -25,7 +25,6 @@ import org.junit.Assert; import org.junit.Test; import java.io.BufferedReader; -import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; @@ -34,6 +33,7 @@ import java.io.Reader; import java.io.StringWriter; import java.net.URL; import java.nio.charset.Charset; +import java.nio.file.Path; /** * LatinEntityResolutionReaderTest @@ -79,8 +79,8 @@ public class LatinEntityResolutionReaderTest private String toStringFromExample( String examplePath ) throws IOException { - File exampleFile = getExampleXml( examplePath ); - FileReader fileReader = new FileReader( exampleFile ); + Path exampleFile = getExampleXml( examplePath ); + FileReader fileReader = new FileReader( exampleFile.toFile() ); BufferedReader lineReader = new BufferedReader( fileReader ); StringBuilder sb = new StringBuilder(); @@ -105,9 +105,9 @@ public class LatinEntityResolutionReaderTest { try { - File inputFile = getExampleXml( sourcePath ); + Path inputFile = getExampleXml( sourcePath ); - FileReader fileReader = new FileReader( inputFile ); + FileReader fileReader = new FileReader( inputFile.toFile() ); LatinEntityResolutionReader testReader = new LatinEntityResolutionReader( fileReader ); String actualOutput = toStringFromReader( testReader, bufsize ); @@ -125,9 +125,9 @@ public class LatinEntityResolutionReaderTest { try { - File inputFile = getExampleXml( sourcePath ); + Path inputFile = getExampleXml( sourcePath ); - FileReader fileReader = new FileReader( inputFile ); + FileReader fileReader = new FileReader( inputFile.toFile() ); LatinEntityResolutionReader testReader = new LatinEntityResolutionReader( fileReader ); String actualOutput = toStringFromReader( testReader, bufSize ); @@ -208,11 +208,11 @@ public class LatinEntityResolutionReaderTest public void testReaderLeftOver() throws IOException { - File inputFile = getExampleXml( "maven-metadata-leftover.xml" ); + Path inputFile = getExampleXml( "maven-metadata-leftover.xml" ); //Bits from RepositoryMetadataReader.read InputStream in = null; SAXReader reader = new SAXReader(); - URL url = inputFile.toURL(); + URL url = inputFile.toUri().toURL(); in = url.openStream(); InputStreamReader inReader = new InputStreamReader( in, Charset.forName( "UTF-8" ) ); LatinEntityResolutionReader latinReader = new LatinEntityResolutionReader( inReader ); diff --git a/archiva-modules/archiva-base/archiva-xml-tools/src/test/java/org/apache/archiva/xml/XMLReaderTest.java b/archiva-modules/archiva-base/archiva-xml-tools/src/test/java/org/apache/archiva/xml/XMLReaderTest.java index 05c9855f2..fa99a4afa 100644 --- a/archiva-modules/archiva-base/archiva-xml-tools/src/test/java/org/apache/archiva/xml/XMLReaderTest.java +++ b/archiva-modules/archiva-base/archiva-xml-tools/src/test/java/org/apache/archiva/xml/XMLReaderTest.java @@ -19,7 +19,7 @@ package org.apache.archiva.xml; * under the License. */ -import java.io.File; +import java.nio.file.Path; import java.util.ArrayList; import java.util.List; @@ -55,7 +55,7 @@ public class XMLReaderTest public void testNoPrologBasicRead() throws XMLException { - File xmlFile = getExampleXml( "no-prolog-basic.xml" ); + Path xmlFile = getExampleXml( "no-prolog-basic.xml" ); XMLReader reader = new XMLReader( "basic", xmlFile ); List<Element> fruits = reader.getElementList( "//basic/fruits/fruit" ); @@ -66,7 +66,7 @@ public class XMLReaderTest public void testNoPrologEntitiesRead() throws XMLException { - File xmlFile = getExampleXml( "no-prolog-with-entities.xml" ); + Path xmlFile = getExampleXml( "no-prolog-with-entities.xml" ); XMLReader reader = new XMLReader( "basic", xmlFile ); List<Element> names = reader.getElementList( "//basic/names/name" ); @@ -77,7 +77,7 @@ public class XMLReaderTest public void testNoPrologUtf8Read() throws XMLException { - File xmlFile = getExampleXml( "no-prolog-with-utf8.xml" ); + Path xmlFile = getExampleXml( "no-prolog-with-utf8.xml" ); XMLReader reader = new XMLReader( "basic", xmlFile ); List<Element> names = reader.getElementList( "//basic/names/name" ); @@ -88,7 +88,7 @@ public class XMLReaderTest public void testPrologUtf8Read() throws XMLException { - File xmlFile = getExampleXml( "prolog-with-utf8.xml" ); + Path xmlFile = getExampleXml( "prolog-with-utf8.xml" ); XMLReader reader = new XMLReader( "basic", xmlFile ); List<Element> names = reader.getElementList( "//basic/names/name" ); @@ -100,8 +100,8 @@ public class XMLReaderTest public void testProxiedMetadataRead() throws XMLException { - File xmlFile = getExampleXml( "maven-metadata-codehaus-snapshots.xml" ); - XMLReader reader = new XMLReader( "metadata", xmlFile ); + Path xmlFile = getExampleXml( "maven-metadata-codehaus-snapshots.xml" ); + XMLReader reader = new XMLReader( "metadata", xmlFile ); reader.removeNamespaces(); Element groupId = reader.getElement( "//metadata/groupId" ); |