Commit e018e331 authored by Administrator's avatar Administrator

Closes some unclosed streams

Some StringWriter streams were not closes in the examples. This commit
fixes this error.
parent 50fe9bb2
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
<groupId>es.uvigo.esei.dai</groupId> <groupId>es.uvigo.esei.dai</groupId>
<artifactId>xml</artifactId> <artifactId>xml</artifactId>
<version>1.0.0</version> <version>1.0.1</version>
<name>Ejemplos de DAI - APIs XML</name> <name>Ejemplos de DAI - APIs XML</name>
<inceptionYear>2014</inceptionYear> <inceptionYear>2014</inceptionYear>
......
...@@ -102,7 +102,7 @@ public class DOMParsing { ...@@ -102,7 +102,7 @@ public class DOMParsing {
return builder.parse(new File(documentPath)); return builder.parse(new File(documentPath));
} }
public static String toXML(Document document) throws TransformerException { public static String toXML(Document document) throws TransformerException, IOException {
// Creación y configuración del transformador. En este caso, se activa // Creación y configuración del transformador. En este caso, se activa
// la indentación del XML // la indentación del XML
final TransformerFactory factory = TransformerFactory.newInstance(); final TransformerFactory factory = TransformerFactory.newInstance();
...@@ -112,10 +112,10 @@ public class DOMParsing { ...@@ -112,10 +112,10 @@ public class DOMParsing {
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes");
// El resultado se almacenará en una cadena de texto // El resultado se almacenará en una cadena de texto
final StringWriter writer = new StringWriter(); try (final StringWriter writer = new StringWriter()) {
transformer.transform(new DOMSource(document), new StreamResult(writer)); transformer.transform(new DOMSource(document), new StreamResult(writer));
return writer.toString(); return writer.toString();
} }
}
} }
package es.uvigo.esei.dai.xml.xpath; package es.uvigo.esei.dai.xml.xpath;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.io.StringWriter; import java.io.StringWriter;
import javax.xml.namespace.NamespaceContext; import javax.xml.namespace.NamespaceContext;
...@@ -76,14 +77,25 @@ public class XSLUtils { ...@@ -76,14 +77,25 @@ public class XSLUtils {
transformer.transform(xmlSource, result); transformer.transform(xmlSource, result);
} }
public static String transformWithXSLT(File xml, File xslt) throws TransformerException { public static String transformWithXSLT(File xml, File xslt) throws TransformerException, IOException {
final TransformerFactory tFactory = TransformerFactory.newInstance(); final TransformerFactory tFactory = TransformerFactory.newInstance();
final Transformer transformer = tFactory.newTransformer(new StreamSource(xslt)); final Transformer transformer = tFactory.newTransformer(new StreamSource(xslt));
final StringWriter writer = new StringWriter(); try (final StringWriter writer = new StringWriter()) {
transformer.transform(new StreamSource(xml), new StreamResult(writer));
return writer.toString();
}
}
public static String transformToString(File xml) throws TransformerException, IOException {
final TransformerFactory tFactory = TransformerFactory.newInstance();
final Transformer transformer = tFactory.newTransformer();
try (final StringWriter writer = new StringWriter()) {
transformer.transform(new StreamSource(xml), new StreamResult(writer)); transformer.transform(new StreamSource(xml), new StreamResult(writer));
return writer.toString(); return writer.toString();
} }
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment