You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by re...@apache.org on 2009/01/05 18:09:54 UTC
svn commit: r731622 -
/cocoon/cocoon3/trunk/cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/util/XMLUtils.java
Author: reinhard
Date: Mon Jan 5 09:09:54 2009
New Revision: 731622
URL: http://svn.apache.org/viewvc?rev=731622&view=rev
Log:
. add a utility method to parse a string and pass the SAX events to a SAXXMLConsumer
Modified:
cocoon/cocoon3/trunk/cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/util/XMLUtils.java
Modified: cocoon/cocoon3/trunk/cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/util/XMLUtils.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/util/XMLUtils.java?rev=731622&r1=731621&r2=731622&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/util/XMLUtils.java (original)
+++ cocoon/cocoon3/trunk/cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/util/XMLUtils.java Mon Jan 5 09:09:54 2009
@@ -20,6 +20,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+import java.io.StringReader;
import java.util.Properties;
import javax.xml.transform.TransformerFactory;
@@ -34,34 +35,20 @@
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
+/**
+ * Utilities for the usage of an {@link XMLReader} together with a {@link XMLConsumer}.
+ */
public abstract class XMLUtils {
private static final SAXTransformerFactory SAX_TRANSFORMER_FACTORY = (SAXTransformerFactory) TransformerFactory
.newInstance();
/**
- * Use a SAX parser and read the content of an {@link InputStream} into an {@link XMLConsumer}.
+ * Serialize the content of a {@link SaxBuffer} into an {@link OutputStream}.
*
- * @param The {@link InputStream} to be parsed.
- * @param The target {@link XMLConsumer}.
- * @throws An IOException if the {@link InputStream} can't be used.
- * @throws SAXException if the {@link InputStream} can't be parsed.
+ * @param outputStream OutputStream to use.
+ * @param saxBuffer A SaxBuffer containing the SAX events.
*/
- public static void toSax(final InputStream inputStream, final XMLConsumer xmlConsumer) throws IOException,
- SAXException {
- XMLReader xmlReader;
-
- try {
- xmlReader = XMLReaderFactory.createXMLReader();
- xmlReader.setContentHandler(xmlConsumer);
- xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler", xmlConsumer);
- } catch (SAXException e) {
- throw new ProcessingException("Cannot create and prepare an XMLReader.", e);
- }
-
- xmlReader.parse(new InputSource(new BufferedInputStream(inputStream)));
- }
-
public static void toOutputStream(final OutputStream outputStream, final SaxBuffer saxBuffer) {
try {
TransformerHandler transformerHandler = SAX_TRANSFORMER_FACTORY.newTransformerHandler();
@@ -77,4 +64,43 @@
throw new ProcessingException("Can't stream the provided SaxBuffer.", e);
}
}
+
+ /**
+ * Use a SAX parser and read the content of an {@link InputStream} into an {@link XMLConsumer}.
+ *
+ * @param inputStream InputStream containing the content to be parsed.
+ * @param xmlConsumer The XMLConsumer to use.
+ * @throws IOException if the {@link InputStream} can't be used.
+ * @throws SAXException if the {@link InputStream} can't be parsed.
+ */
+ public static void toSax(final InputStream inputStream, final XMLConsumer xmlConsumer) throws IOException,
+ SAXException {
+ createXMLReader(xmlConsumer).parse(new InputSource(new BufferedInputStream(inputStream)));
+ }
+
+ /**
+ * Use a SAX parser and read the content of an {@link InputStream} into an {@link XMLConsumer}.
+ *
+ * @param string A String to be parsed.
+ * @param xmlConsumer The XMLConsumer to use.
+ * @throws IOException if the {@link InputStream} can't be used.
+ * @throws SAXException if the {@link InputStream} can't be parsed.
+ */
+ public static void toSax(final String string, final XMLConsumer xmlConsumer) throws IOException, SAXException {
+ createXMLReader(xmlConsumer).parse(new InputSource(new StringReader(string)));
+ }
+
+ private static XMLReader createXMLReader(final XMLConsumer xmlConsumer) {
+ XMLReader xmlReader;
+
+ try {
+ xmlReader = XMLReaderFactory.createXMLReader();
+ xmlReader.setContentHandler(xmlConsumer);
+ xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler", xmlConsumer);
+ } catch (SAXException e) {
+ throw new ProcessingException("Cannot create and prepare an XMLReader.", e);
+ }
+
+ return xmlReader;
+ }
}
\ No newline at end of file