* under the License.
*/
+import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.dom4j.Attribute;
import org.dom4j.Document;
private Document document;
- private Map namespaceMap = new HashMap();
+ private Map<String, String> namespaceMap = new HashMap<String, String>();
public XMLReader( String type, File file )
throws XMLException
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 );
{
throw new XMLException( "Unable to open stream to " + url + ": " + e.getMessage(), e );
}
+ finally
+ {
+ IOUtils.closeQuietly( in );
+ }
Element root = this.document.getRootElement();
if ( root == null )
Node n;
- Iterator it = elem.elementIterator();
+ Iterator<Node> it = elem.elementIterator();
while ( it.hasNext() )
{
- n = (Node) it.next();
+ n = it.next();
switch ( n.getNodeType() )
{
}
}
- public List getElementList( String xpathExpr )
+ public List<Element> getElementList( String xpathExpr )
throws XMLException
{
XPath xpath = createXPath( xpathExpr );
if ( evaluated instanceof List )
{
- return (List) evaluated;
+ return (List<Element>) evaluated;
}
else if ( evaluated instanceof Node )
{
- List ret = new ArrayList();
- ret.add( evaluated );
+ List<Element> ret = new ArrayList<Element>();
+ ret.add( (Element) evaluated );
return ret;
}
else
}
}
- public List getElementListText( String xpathExpr )
+ public List<String> getElementListText( String xpathExpr )
throws XMLException
{
- List elemList = getElementList( xpathExpr );
+ List<Element> elemList = getElementList( xpathExpr );
if ( elemList == null )
{
return null;
}
- List ret = new ArrayList();
- for ( Iterator iter = elemList.iterator(); iter.hasNext(); )
+ List<String> ret = new ArrayList<String>();
+ for ( Iterator<Element> iter = elemList.iterator(); iter.hasNext(); )
{
- Element listelem = (Element) iter.next();
+ Element listelem = iter.next();
ret.add( listelem.getTextTrim() );
}
return ret;
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();
}
{
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 */
+ }
+ }
+ }
}
}