bruno 2003/03/10 07:42:39
Modified: src/java/org/apache/cocoon/xml/dom DOMStreamer.java
Log:
sync with 2.1 head
Revision Changes Path
1.2 +87 -47 cocoon-2.0/src/java/org/apache/cocoon/xml/dom/DOMStreamer.java
Index: DOMStreamer.java
===================================================================
RCS file: /home/cvs/cocoon-2.0/src/java/org/apache/cocoon/xml/dom/DOMStreamer.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- DOMStreamer.java 9 Mar 2003 00:03:48 -0000 1.1
+++ DOMStreamer.java 10 Mar 2003 15:42:39 -0000 1.2
@@ -52,6 +52,7 @@
import org.apache.cocoon.xml.AbstractXMLProducer;
import org.apache.cocoon.xml.XMLConsumer;
+import org.apache.cocoon.xml.XMLProducer;
import org.apache.cocoon.xml.EmbeddedXMLPipe;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
@@ -74,7 +75,7 @@
* events from a W3C DOM Document.
*
* <p>The DOMStreamer uses a different strategy based on the value of the
- * normalizeNamespacesOn property:
+ * normalizeNamespaces property:
* <ul>
* <li>if true (the default), the DOMStreamer will normalize namespace
* declarations (i.e. add missing xmlns attributes or correct them). See
@@ -87,10 +88,10 @@
* (Apache Software Foundation)
* @version CVS $Id$
*/
-public class DOMStreamer extends AbstractXMLProducer {
+public class DOMStreamer implements XMLProducer {
/** Indicates whether namespace normalization should happen. */
- protected boolean normalizeNamespacesOn = true;
+ protected boolean normalizeNamespaces = true;
/** DOMStreamer used in case of namespace normalization. */
protected NamespaceNormalizingDOMStreamer namespaceNormalizingDOMStreamer = new
NamespaceNormalizingDOMStreamer();
@@ -119,10 +120,11 @@
* Create a new <code>DOMStreamer</code> instance.
*/
public DOMStreamer(ContentHandler content) {
- this(content,null);
+ this(content, null);
if (content instanceof LexicalHandler) {
- setLexicalHandler((LexicalHandler) content);
- }
+ defaultDOMStreamer.setLexicalHandler((LexicalHandler) content);
+ namespaceNormalizingDOMStreamer.setLexicalHandler((LexicalHandler)
content);
+ }
}
/**
@@ -130,32 +132,61 @@
*/
public DOMStreamer(ContentHandler content, LexicalHandler lexical) {
this();
- setContentHandler(content);
- setLexicalHandler(lexical);
+ defaultDOMStreamer.setContentHandler(content);
+ defaultDOMStreamer.setLexicalHandler(lexical);
+ namespaceNormalizingDOMStreamer.setContentHandler(content);
+ namespaceNormalizingDOMStreamer.setLexicalHandler(lexical);
+ }
+
+ /**
+ * Set the <code>XMLConsumer</code> that will receive XML data.
+ */
+ public void setConsumer(XMLConsumer consumer) {
+ defaultDOMStreamer.setContentHandler(consumer);
+ defaultDOMStreamer.setLexicalHandler(consumer);
+ namespaceNormalizingDOMStreamer.setContentHandler(consumer);
+ namespaceNormalizingDOMStreamer.setLexicalHandler(consumer);
+ }
+
+ /**
+ * Set the <code>LexicalHandler</code> that will receive XML data.
+ * <br>
+ * Subclasses may retrieve this <code>LexicalHandler</code> instance
+ * accessing the protected <code>super.lexicalHandler</code> field.
+ *
+ * @exception IllegalStateException If the <code>LexicalHandler</code> or
+ * the <code>XMLConsumer</code> were
+ * already set.
+ */
+ public void setLexicalHandler(LexicalHandler handler) {
+ // FIXME Shouldn't be used IHMO.
+
+ defaultDOMStreamer.setLexicalHandler(handler);
+ namespaceNormalizingDOMStreamer.setLexicalHandler(handler);
}
/**
* Start the production of SAX events.
*/
public void stream(Node node) throws SAXException {
- if (normalizeNamespacesOn)
+ if (normalizeNamespaces)
namespaceNormalizingDOMStreamer.stream(node);
else
defaultDOMStreamer.stream(node);
}
- public boolean isNormalizeNamespacesOn() {
- return normalizeNamespacesOn;
+ public boolean isNormalizeNamespaces() {
+ return normalizeNamespaces;
}
- public void setNormalizeNamespacesOn(boolean normalizeNamespacesOn) {
- this.normalizeNamespacesOn = normalizeNamespacesOn;
+ public void setNormalizeNamespaces(boolean normalizeNamespaces) {
+ this.normalizeNamespaces = normalizeNamespaces;
}
public void recycle() {
- super.recycle();
+ defaultDOMStreamer.recycle();
namespaceNormalizingDOMStreamer.recycle();
- normalizeNamespacesOn = true;
+ normalizeNamespaces = true;
}
/**
@@ -182,7 +213,7 @@
* @author Bruno Dumon (bruno at outerthought dot org)
* @author Xalan team
*/
- public class NamespaceNormalizingDOMStreamer {
+ public class NamespaceNormalizingDOMStreamer extends AbstractXMLProducer {
/**
* Information about the current element. Used to remember the localName,
qName
* and namespaceURI for generating the endElement event, and holds the
namespaces
@@ -196,6 +227,7 @@
protected int newPrefixCounter = 0;
public void recycle() {
+ super.recycle();
currentElementInfo = null;
newPrefixCounter = 0;
}
@@ -215,7 +247,11 @@
*/
protected void stream(Node pos) throws SAXException {
- contentHandler.startDocument();
+ // Start document only if we're streaming a document
+ boolean isDoc = (pos.getNodeType() == Node.DOCUMENT_NODE);
+ if (isDoc) {
+ contentHandler.startDocument();
+ }
Node top = pos;
@@ -248,10 +284,13 @@
pos = nextNode;
}
- contentHandler.endDocument();
+
+ if (isDoc) {
+ contentHandler.endDocument();
+ }
}
- private final void dispatachChars(Node node) throws SAXException {
+ private final void dispatchChars(Node node) throws SAXException {
String data = ((Text) node).getData();
contentHandler.characters(data.toCharArray(), 0, data.length());
}
@@ -365,7 +404,8 @@
else {
if (attr.getPrefix() != null && declaredUri
== null) {
// prefix is not null and is not yet
declared: declare it
- currentElementInfo.put(prefix,
attr.getNamespaceURI());
+ attrPrefix = attr.getPrefix();
+ currentElementInfo.put(attrPrefix,
attr.getNamespaceURI());
} else {
// attribute has no prefix (which is
not allowed for namespaced attributes) or
// the prefix is already bound to
something else: generate a new prefix
@@ -421,7 +461,7 @@
if (lexicalHandler != null)
lexicalHandler.startCDATA();
- dispatachChars(node);
+ dispatchChars(node);
if (lexicalHandler != null)
lexicalHandler.endCDATA();
@@ -429,7 +469,7 @@
break;
case Node.TEXT_NODE:
{
- dispatachChars(node);
+ dispatchChars(node);
}
break;
case Node.ENTITY_REFERENCE_NODE:
@@ -580,7 +620,7 @@
* @author <a href="mailto:[EMAIL PROTECTED]">Pierpaolo Fumagalli</a>
* (Apache Software Foundation)
*/
- public class DefaultDOMStreamer {
+ public class DefaultDOMStreamer extends AbstractXMLProducer {
/** The private transformer for this instance */
protected Transformer transformer;
@@ -588,36 +628,36 @@
/**
* Start the production of SAX events.
*/
- public void stream(Node node)
- throws SAXException {
- if (this.transformer == null) {
- try {
- this.transformer = factory.newTransformer();
- } catch (TransformerConfigurationException e) {
- getLogger().error("DOMStreamer", e);
- throw new SAXException(e);
+ public void stream(Node node)
+ throws SAXException {
+ if (this.transformer == null) {
+ try {
+ this.transformer = factory.newTransformer();
+ } catch (TransformerConfigurationException e) {
+ getLogger().error("DOMStreamer", e);
+ throw new SAXException(e);
+ }
}
- }
- DOMSource source = new DOMSource(node);
+ DOMSource source = new DOMSource(node);
- ContentHandler handler;
- if (node.getNodeType() == Node.DOCUMENT_NODE) {
- // Pass all SAX events
+ ContentHandler handler;
+ if (node.getNodeType() == Node.DOCUMENT_NODE) {
+ // Pass all SAX events
handler = contentHandler;
- } else {
- // Strip start/endDocument
+ } else {
+ // Strip start/endDocument
handler = new EmbeddedXMLPipe(contentHandler);
- }
+ }
- SAXResult result = new SAXResult(handler);
+ SAXResult result = new SAXResult(handler);
result.setLexicalHandler(lexicalHandler);
- try {
- transformer.transform(source, result);
- } catch (TransformerException e) {
- getLogger().error("DOMStreamer", e);
- throw new SAXException(e);
+ try {
+ transformer.transform(source, result);
+ } catch (TransformerException e) {
+ getLogger().error("DOMStreamer", e);
+ throw new SAXException(e);
+ }
}
}
-}
}