You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by vg...@apache.org on 2005/04/21 14:29:23 UTC
svn commit: r164046 -
/cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/xml/dom/DOMBuilder.java
/cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/xml/dom/DOMStreamer.java
Author: vgritsenko
Date: Thu Apr 21 05:29:22 2005
New Revision: 164046
URL: http://svn.apache.org/viewcvs?rev=164046&view=rev
Log:
DOMStreamer: add missing Recyclable.
DOMBuilder: add constructor taking factory.
Modified:
cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/xml/dom/DOMBuilder.java
cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/xml/dom/DOMStreamer.java
Modified: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/xml/dom/DOMBuilder.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/xml/dom/DOMBuilder.java?rev=164046&r1=164045&r2=164046&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/xml/dom/DOMBuilder.java (original)
+++ cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/xml/dom/DOMBuilder.java Thu Apr 21 05:29:22 2005
@@ -1,12 +1,12 @@
/*
- * Copyright 1999-2004 The Apache Software Foundation.
- *
+ * Copyright 1999-2005 The Apache Software Foundation.
+ *
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -16,10 +16,12 @@
package org.apache.cocoon.xml.dom;
import org.apache.avalon.framework.CascadingRuntimeException;
+
import org.apache.cocoon.xml.AbstractXMLPipe;
-import org.xml.sax.SAXException;
+
import org.w3c.dom.Document;
import org.w3c.dom.Node;
+import org.xml.sax.SAXException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
@@ -31,13 +33,15 @@
* DOM Document from SAX events.
*
* @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
- * @version CVS $Id$
+ * @version $Id$
*/
-public class DOMBuilder
-extends AbstractXMLPipe {
+public class DOMBuilder extends AbstractXMLPipe {
+
+ /** The default transformer factory shared by all instances */
+ protected static final SAXTransformerFactory FACTORY = (SAXTransformerFactory) TransformerFactory.newInstance();
- /** The transformer factory shared by all instances */
- protected static final SAXTransformerFactory factory = (SAXTransformerFactory)TransformerFactory.newInstance();
+ /** The transformer factory */
+ protected SAXTransformerFactory factory;
/** The listener */
protected Listener listener;
@@ -52,7 +56,14 @@
* Construct a new instance of this DOMBuilder.
*/
public DOMBuilder() {
- this( (Listener)null, (Node)null );
+ this((Listener) null, (Node) null);
+ }
+
+ /**
+ * Construct a new instance of this DOMBuilder.
+ */
+ public DOMBuilder(SAXTransformerFactory factory) {
+ this(factory, null, null);
}
/**
@@ -60,13 +71,13 @@
* @deprecated Use DOMBuilder() instead.
*/
public DOMBuilder(DOMFactory factory) {
- this( (Listener)null, (Node)null );
+ this((Listener) null, (Node) null);
}
/**
* Construct a new instance of this DOMBuilder.
*/
- public DOMBuilder( Listener listener ) {
+ public DOMBuilder(Listener listener) {
this(listener, null);
}
@@ -74,7 +85,7 @@
* Construct a new instance of this DOMBuilder.
* @deprecated Use DOMBuilder(listener) instead.
*/
- public DOMBuilder( DOMFactory factory, Listener listener ) {
+ public DOMBuilder(DOMFactory factory, Listener listener) {
this(listener, null);
}
@@ -82,52 +93,47 @@
* Construct a new instance of this DOMBuilder.
* @deprecated Use DOMBuilder(listener, parentNode) instead.
*/
- public DOMBuilder( DOMFactory domFactory, Listener listener, Node parentNode ) {
+ public DOMBuilder(DOMFactory domFactory, Listener listener, Node parentNode) {
this(listener, parentNode);
}
/**
- * Construct a new instance of this DOMBuilder.
+ * Constructs a new instance that appends nodes to the given parent node.
+ * <br/>
+ * <strong>Note:</strong> You cannot use a <code>Listener<code> when appending to a
+ * <code>Node</code>, because the notification occurs at <code>endDocument()</code>
+ * which does not happen here.
*/
- public DOMBuilder( Listener listener, Node parentNode ) {
- super();
- this.listener = listener;
- try {
- TransformerHandler handler = factory.newTransformerHandler();
- this.setContentHandler(handler);
- this.setLexicalHandler(handler);
- this.parentNode = parentNode;
- if (parentNode != null) {
- this.result = new DOMResult( parentNode );
- } else {
- this.result = new DOMResult();
- }
- handler.setResult(this.result);
- } catch (javax.xml.transform.TransformerException local) {
- throw new CascadingRuntimeException("Fatal-Error: Unable to get transformer handler", local);
- }
+ public DOMBuilder(Node parentNode) {
+ this(null, parentNode);
}
/**
- * Constructs a new instance that appends nodes to the given parent node.<br/>
- * Note : you cannot use a <code>Listener<code> when appending to a
- * <code>Node</code>, because the notification occurs at <code>endDocument()</code>
- * which does not happen here.
+ * Construct a new instance of this DOMBuilder.
*/
- public DOMBuilder( Node parentNode ) {
- this(null, null, parentNode);
+ public DOMBuilder(Listener listener, Node parentNode) {
+ this((SAXTransformerFactory) null, listener, parentNode);
}
/**
- * Recycling
+ * Construct a new instance of this DOMBuilder.
*/
- public void recycle() {
- super.recycle();
+ public DOMBuilder(SAXTransformerFactory factory, Listener listener, Node parentNode) {
+ super();
+ this.factory = factory == null? FACTORY: factory;
+ this.listener = listener;
+ this.parentNode = parentNode;
+ setup();
+ }
+ /**
+ * Setup this instance transformer and result objects.
+ */
+ private void setup() {
try {
- TransformerHandler handler = factory.newTransformerHandler();
- this.setContentHandler(handler);
- this.setLexicalHandler(handler);
+ TransformerHandler handler = this.factory.newTransformerHandler();
+ setContentHandler(handler);
+ setLexicalHandler(handler);
if (this.parentNode != null) {
this.result = new DOMResult(this.parentNode);
} else {
@@ -140,37 +146,44 @@
}
/**
+ * Recycle this builder, prepare for re-use.
+ */
+ public void recycle() {
+ super.recycle();
+ setup();
+ }
+
+ /**
* Return the newly built Document.
*/
public Document getDocument() {
- if ((this.result == null) || (this.result.getNode()==null)) {
+ if (this.result == null || this.result.getNode() == null) {
return null;
} else if (this.result.getNode().getNodeType() == Node.DOCUMENT_NODE) {
- return ( (Document)this.result.getNode() );
+ return (Document) this.result.getNode();
} else {
- return ( this.result.getNode().getOwnerDocument() );
+ return this.result.getNode().getOwnerDocument();
}
}
/**
- * Receive notification of the beginning of a document.
+ * Receive notification of the end of a document.
*
* @exception SAXException If this method was not called appropriately.
*/
- public void endDocument()
- throws SAXException {
+ public void endDocument() throws SAXException {
super.endDocument();
-
// Notify the listener
- this.notifyListener();
+ notifyListener();
}
/**
* Receive notification of a successfully completed DOM tree generation.
*/
- protected void notifyListener()
- throws SAXException {
- if ( this.listener != null ) this.listener.notify( this.getDocument() );
+ protected void notifyListener() throws SAXException {
+ if (this.listener != null) {
+ this.listener.notify(getDocument());
+ }
}
/**
@@ -182,7 +195,6 @@
/**
* Receive notification of a successfully completed DOM tree generation.
*/
- void notify(Document doc)
- throws SAXException;
+ void notify(Document doc) throws SAXException;
}
}
Modified: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/xml/dom/DOMStreamer.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/xml/dom/DOMStreamer.java?rev=164046&r1=164045&r2=164046&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/xml/dom/DOMStreamer.java (original)
+++ cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/xml/dom/DOMStreamer.java Thu Apr 21 05:29:22 2005
@@ -1,12 +1,12 @@
/*
* Copyright 1999-2005 The Apache Software Foundation.
- *
+ *
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -15,23 +15,14 @@
*/
package org.apache.cocoon.xml.dom;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.Map;
-
-import javax.xml.transform.Transformer;
-import javax.xml.transform.TransformerConfigurationException;
-import javax.xml.transform.TransformerException;
-import javax.xml.transform.TransformerFactory;
-import javax.xml.transform.dom.DOMSource;
-import javax.xml.transform.sax.SAXResult;
+import org.apache.avalon.excalibur.pool.Recyclable;
import org.apache.cocoon.xml.AbstractXMLProducer;
import org.apache.cocoon.xml.EmbeddedXMLPipe;
import org.apache.cocoon.xml.XMLConsumer;
import org.apache.cocoon.xml.XMLProducer;
-import org.apache.commons.lang.StringUtils;
+import org.apache.commons.lang.StringUtils;
import org.w3c.dom.Attr;
import org.w3c.dom.Comment;
import org.w3c.dom.Element;
@@ -45,6 +36,16 @@
import org.xml.sax.ext.LexicalHandler;
import org.xml.sax.helpers.AttributesImpl;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.sax.SAXResult;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
/**
* The <code>DOMStreamer</code> is a utility class that will generate SAX
* events from a W3C DOM Document.
@@ -61,12 +62,15 @@
* @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
* @author <a href="mailto:pier@apache.org">Pierpaolo Fumagalli</a>
* (Apache Software Foundation)
- * @version CVS $Id$
+ * @version $Id$
*/
-public class DOMStreamer implements XMLProducer {
+public class DOMStreamer implements XMLProducer, Recyclable {
+
+ /** The transformer factory shared by all instances (only used by DefaultDOMStreamer) */
+ private static final TransformerFactory FACTORY = TransformerFactory.newInstance();
/** Default value for normalizeNamespaces. */
- private final static boolean DEFAULT_NORMALIZE_NAMESPACES = true;
+ private static final boolean DEFAULT_NORMALIZE_NAMESPACES = true;
/** Indicates whether namespace normalization should happen. */
protected boolean normalizeNamespaces = DEFAULT_NORMALIZE_NAMESPACES;
@@ -77,9 +81,6 @@
/** DOMStreamer used when namespace normalization should not explicitely happen. */
protected DefaultDOMStreamer defaultDOMStreamer = new DefaultDOMStreamer();
- /** The transformer factory shared by all instances (only used by DefaultDOMStreamer) */
- protected final static TransformerFactory factory = TransformerFactory.newInstance();
-
/**
* Create a new <code>DOMStreamer</code> instance.
*/
@@ -90,40 +91,32 @@
/**
* Create a new <code>DOMStreamer</code> instance.
*/
- public DOMStreamer(XMLConsumer consumer) {
- this(consumer, consumer);
+ public DOMStreamer(ContentHandler content, LexicalHandler lexical) {
+ this();
+ setContentHandler(content);
+ setLexicalHandler(lexical);
}
/**
* Create a new <code>DOMStreamer</code> instance.
*/
- public DOMStreamer(ContentHandler content) {
- this(content, null);
- if (content instanceof LexicalHandler) {
- defaultDOMStreamer.setLexicalHandler((LexicalHandler) content);
- namespaceNormalizingDOMStreamer.setLexicalHandler((LexicalHandler) content);
- }
+ public DOMStreamer(XMLConsumer consumer) {
+ this(consumer, consumer);
}
/**
* Create a new <code>DOMStreamer</code> instance.
*/
- public DOMStreamer(ContentHandler content, LexicalHandler lexical) {
- this();
- defaultDOMStreamer.setContentHandler(content);
- defaultDOMStreamer.setLexicalHandler(lexical);
- namespaceNormalizingDOMStreamer.setContentHandler(content);
- namespaceNormalizingDOMStreamer.setLexicalHandler(lexical);
+ public DOMStreamer(ContentHandler content) {
+ this(content, content instanceof LexicalHandler ? (LexicalHandler) content : null);
}
/**
* 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);
+ setContentHandler(consumer);
+ setLexicalHandler(consumer);
}
/**
@@ -192,10 +185,10 @@
* modify the DOM-tree itself. The currentElementInfo has a pointer to its parent
* elementInfo.
*/
- protected NamespaceNormalizingDOMStreamer.ElementInfo currentElementInfo = null;
+ protected NamespaceNormalizingDOMStreamer.ElementInfo currentElementInfo;
/** Counter used when generating new namespace prefixes. */
- protected int newPrefixCounter = 0;
+ protected int newPrefixCounter;
public void recycle() {
super.recycle();
@@ -214,7 +207,6 @@
* SAX listener.
*
* @param pos Node in the tree where to start traversal
- *
*/
protected void stream(Node pos) throws SAXException {
@@ -704,7 +696,7 @@
throws SAXException {
if (this.transformer == null) {
try {
- this.transformer = factory.newTransformer();
+ this.transformer = FACTORY.newTransformer();
} catch (TransformerConfigurationException e) {
throw new SAXException(e);
}