diff --git a/archiva-base/archiva-xml-tools/src/main/java/org/apache/maven/archiva/xml/XMLReader.java b/archiva-base/archiva-xml-tools/src/main/java/org/apache/maven/archiva/xml/XMLReader.java index 4abef4526..f597499e8 100644 --- a/archiva-base/archiva-xml-tools/src/main/java/org/apache/maven/archiva/xml/XMLReader.java +++ b/archiva-base/archiva-xml-tools/src/main/java/org/apache/maven/archiva/xml/XMLReader.java @@ -19,6 +19,7 @@ package org.apache.maven.archiva.xml; * under the License. */ +import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; import org.dom4j.Attribute; import org.dom4j.Document; @@ -56,7 +57,7 @@ public class XMLReader private Document document; - private Map namespaceMap = new HashMap(); + private Map namespaceMap = new HashMap(); public XMLReader( String type, File file ) throws XMLException @@ -98,10 +99,12 @@ public class XMLReader this.documentType = type; this.xmlUrl = url; + InputStream in = null; SAXReader reader = new SAXReader(); + try { - InputStream in = url.openStream(); + in = url.openStream(); InputStreamReader inReader = new InputStreamReader( in, "UTF-8" ); LatinEntityResolutionReader latinReader = new LatinEntityResolutionReader( inReader ); this.document = reader.read( latinReader ); @@ -114,6 +117,10 @@ public class XMLReader { throw new XMLException( "Unable to open stream to " + url + ": " + e.getMessage(), e ); } + finally + { + IOUtils.closeQuietly( in ); + } Element root = this.document.getRootElement(); if ( root == null ) @@ -204,10 +211,10 @@ public class XMLReader Node n; - Iterator it = elem.elementIterator(); + Iterator it = elem.elementIterator(); while ( it.hasNext() ) { - n = (Node) it.next(); + n = it.next(); switch ( n.getNodeType() ) { @@ -269,7 +276,7 @@ public class XMLReader } } - public List getElementList( String xpathExpr ) + public List getElementList( String xpathExpr ) throws XMLException { XPath xpath = createXPath( xpathExpr ); @@ -287,12 +294,12 @@ public class XMLReader if ( evaluated instanceof List ) { - return (List) evaluated; + return (List) evaluated; } else if ( evaluated instanceof Node ) { - List ret = new ArrayList(); - ret.add( evaluated ); + List ret = new ArrayList(); + ret.add( (Element) evaluated ); return ret; } else @@ -303,19 +310,19 @@ public class XMLReader } } - public List getElementListText( String xpathExpr ) + public List getElementListText( String xpathExpr ) throws XMLException { - List elemList = getElementList( xpathExpr ); + List elemList = getElementList( xpathExpr ); if ( elemList == null ) { return null; } - List ret = new ArrayList(); - for ( Iterator iter = elemList.iterator(); iter.hasNext(); ) + List ret = new ArrayList(); + for ( Iterator iter = elemList.iterator(); iter.hasNext(); ) { - Element listelem = (Element) iter.next(); + Element listelem = iter.next(); ret.add( listelem.getTextTrim() ); } return ret; diff --git a/archiva-base/archiva-xml-tools/src/main/java/org/apache/maven/archiva/xml/XMLWriter.java b/archiva-base/archiva-xml-tools/src/main/java/org/apache/maven/archiva/xml/XMLWriter.java index cbb6c7fee..f8f4acb7c 100644 --- a/archiva-base/archiva-xml-tools/src/main/java/org/apache/maven/archiva/xml/XMLWriter.java +++ b/archiva-base/archiva-xml-tools/src/main/java/org/apache/maven/archiva/xml/XMLWriter.java @@ -36,10 +36,12 @@ public class XMLWriter public static void write( Document doc, Writer writer ) throws XMLException { + org.dom4j.io.XMLWriter xmlwriter = null; + try { OutputFormat outputFormat = OutputFormat.createPrettyPrint(); - org.dom4j.io.XMLWriter xmlwriter = new org.dom4j.io.XMLWriter( writer, outputFormat ); + xmlwriter = new org.dom4j.io.XMLWriter( writer, outputFormat ); xmlwriter.write( doc ); xmlwriter.flush(); } @@ -47,5 +49,19 @@ public class XMLWriter { throw new XMLException( "Unable to write xml contents to writer: " + e.getMessage(), e ); } + finally + { + if( xmlwriter != null ) + { + try + { + xmlwriter.close(); + } + catch ( IOException e ) + { + /* quietly ignore */ + } + } + } } }