[Practicalxml-commits] SF.net SVN: practicalxml:[139] branches/dev-1.1/src/main/java/net/sf/ practi
Brought to you by:
kdgregory
|
From: Auto-Generated S. C. M. <pra...@li...> - 2009-09-18 15:51:42
|
Revision: 139
http://practicalxml.svn.sourceforge.net/practicalxml/?rev=139&view=rev
Author: kdgregory
Date: 2009-09-18 15:51:32 +0000 (Fri, 18 Sep 2009)
Log Message:
-----------
OutputUtil: add compact() and indented(), allowing arbitrary source and target objects
Modified Paths:
--------------
branches/dev-1.1/src/main/java/net/sf/practicalxml/OutputUtil.java
Modified: branches/dev-1.1/src/main/java/net/sf/practicalxml/OutputUtil.java
===================================================================
--- branches/dev-1.1/src/main/java/net/sf/practicalxml/OutputUtil.java 2009-09-17 12:49:44 UTC (rev 138)
+++ branches/dev-1.1/src/main/java/net/sf/practicalxml/OutputUtil.java 2009-09-18 15:51:32 UTC (rev 139)
@@ -64,6 +64,29 @@
/**
+ * Serializes to a compact format, without prologue or whitespace between
+ * elements, using UTF-8 encoding.
+ */
+ public static void compact(Source src, Result target)
+ {
+ new TransformHelper().transform(src, target);
+ }
+
+
+ /**
+ * Serializes to a human-readable format, with each element starting on a
+ * new line, and child elements indented a specified amount from their
+ * parent.
+ */
+ public static void indented(Source src, Result target, int indent)
+ {
+ new TransformHelper()
+ .setIndent(indent)
+ .transform(src, target);
+ }
+
+
+ /**
* Writes a DOM document to a simple string format, without a prologue or
* whitespace between elements.
* <p>
@@ -75,8 +98,7 @@
public static String compactString(Document dom)
{
StringWriter out = new StringWriter();
- new TransformHelper()
- .transform(new DOMSource(dom), new StreamResult(out));
+ compact(new DOMSource(dom), new StreamResult(out));
return out.toString();
}
@@ -102,8 +124,7 @@
public static String compactString(XMLReader reader)
{
StringWriter out = new StringWriter();
- new TransformHelper()
- .transform(new SAXSource(reader, null), new StreamResult(out));
+ compact(new SAXSource(reader, null), new StreamResult(out));
return out.toString();
}
@@ -115,22 +136,20 @@
* Do not simply write this string to a file unless you use UTF-8 encoding
* or attach a prologue that specifies the encoding.
*
- * @param dom The DOM tree to be output.
- * @param indentSize The number of spaces to indent each level of the
- * tree. Indentation is <em>best effort</em>: the
- * <code>javax.transform</code> API does not provide
- * any way to set indent level, so we use JDK-specific
- * features to achieve this, <em>where available</em>.
- * Note also that indenting will cause problems with
- * elements that contain mixed content, particularly
- * if the text elements cannot be trimmed.
+ * @param dom The DOM tree to be output.
+ * @param indent The number of spaces to indent each level of the
+ * tree. Indentation is <em>best effort</em>: the
+ * <code>javax.transform</code> API does not provide
+ * any way to set indent level, so we use JDK-specific
+ * features to achieve this, <em>where available</em>.
+ * Note also that indenting will cause problems with
+ * elements that contain mixed content, particularly
+ * if the text elements cannot be trimmed.
*/
- public static String indentedString(Document dom, int indentSize)
+ public static String indentedString(Document dom, int indent)
{
StringWriter out = new StringWriter();
- new TransformHelper()
- .setIndent(indentSize)
- .transform(new DOMSource(dom), new StreamResult(out));
+ indented(new DOMSource(dom), new StreamResult(out), indent);
return out.toString();
}
@@ -151,29 +170,27 @@
* Do not simply write this string to a file unless you use UTF-8 encoding
* or attach a prologue that specifies the encoding.
*
- * @param reader Provides a source of SAX events for the transformer.
- * @param indentSize The number of spaces to indent each level of the
- * tree. Indentation is <em>best effort</em>: the
- * <code>javax.transform</code> API does not provide
- * any way to set indent level, so we use JDK-specific
- * features to achieve this, <em>where available</em>.
- * Note also that indenting will cause problems with
- * elements that contain mixed content, particularly
- * if the text elements cannot be trimmed.
+ * @param reader Provides a source of SAX events for the transformer.
+ * @param indent The number of spaces to indent each level of the
+ * tree. Indentation is <em>best effort</em>: the
+ * <code>javax.transform</code> API does not provide
+ * any way to set indent level, so we use JDK-specific
+ * features to achieve this, <em>where available</em>.
+ * Note also that indenting will cause problems with
+ * elements that contain mixed content, particularly
+ * if the text elements cannot be trimmed.
*/
- public static String indentedString(XMLReader reader, int indentSize)
+ public static String indentedString(XMLReader reader, int indent)
{
StringWriter out = new StringWriter();
- new TransformHelper()
- .setIndent(indentSize)
- .transform(new SAXSource(reader, null), new StreamResult(out));
+ indented(new SAXSource(reader, null), new StreamResult(out), indent);
return out.toString();
}
/**
- * Writes a DOM document to a stream using UTF-8 encoding, without a prologue
- * or whitespace between elements.
+ * Writes a DOM document to a stream using UTF-8 encoding, in a compact
+ * format without a prologue or whitespace between elements.
*
* @param dom The DOM tree to be output.
* @param stream The output stream. This stream will be flushed by
@@ -181,16 +198,15 @@
*/
public static void compactStream(Document dom, OutputStream stream)
{
- new TransformHelper()
- .transform(new DOMSource(dom), new StreamResult(stream));
+ compact(new DOMSource(dom), new StreamResult(stream));
flushStream(stream);
}
/**
- * Writes XML to a stream using UTF-8 encoding, without prologue or
- * whitespace between elements, using the passed <code>XMLReader</code>
- * to generate a stream of SAX events.
+ * Writes XML to a stream using UTF-8 encoding, in a compact format
+ * without prologue or whitespace between elements, using the passed
+ * <code>XMLReader</code> to generate a stream of SAX events.
* <p>
* The transformer will call the reader's <code>setContentHandler()</code>
* method, followed by <code>parse()</code>. In the latter method, the
@@ -206,8 +222,7 @@
*/
public static void compactStream(XMLReader reader, OutputStream stream)
{
- new TransformHelper()
- .transform(new SAXSource(reader, null), new StreamResult(stream));
+ compact(new SAXSource(reader, null), new StreamResult(stream));
flushStream(stream);
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|