Parent Directory | Revision Log | Patch
--- cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/xml/dom/DOMBuilder.java 2005/04/21 12:18:52 164045 +++ cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/xml/dom/DOMBuilder.java 2005/04/21 12:29:22 164046 @@ -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 @@ import javax.xml.transform.sax.Transform * 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 @@ extends AbstractXMLPipe { * 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 @@ extends AbstractXMLPipe { * @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 @@ extends AbstractXMLPipe { * 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 @@ extends AbstractXMLPipe { * 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 @@ extends AbstractXMLPipe { } /** + * 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 @@ extends AbstractXMLPipe { /** * Receive notification of a successfully completed DOM tree generation. */ - void notify(Document doc) - throws SAXException; + void notify(Document doc) throws SAXException; } }
infrastructure at apache.org | ViewVC Help |
Powered by ViewVC 1.1.26 |