cziegeler 01/07/05 06:33:57
Modified: src/org/apache/cocoon/xml/dom Tag: cocoon_20_branch
DOMStreamer.java
Log:
Rewritten DOMStreamer. It uses now the trax API instead of an own logic
Revision Changes Path
No revision
No revision
1.1.1.1.2.2 +32 -205 xml-cocoon2/src/org/apache/cocoon/xml/dom/DOMStreamer.java
Index: DOMStreamer.java
===================================================================
RCS file: /home/cvs/xml-cocoon2/src/org/apache/cocoon/xml/dom/DOMStreamer.java,v
retrieving revision 1.1.1.1.2.1
retrieving revision 1.1.1.1.2.2
diff -u -r1.1.1.1.2.1 -r1.1.1.1.2.2
--- DOMStreamer.java 2001/07/05 09:11:25 1.1.1.1.2.1
+++ DOMStreamer.java 2001/07/05 13:33:55 1.1.1.1.2.2
@@ -8,36 +8,37 @@
package org.apache.cocoon.xml.dom;
-import java.util.Vector;
+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.cocoon.xml.AbstractXMLProducer;
import org.apache.cocoon.xml.XMLConsumer;
-import org.w3c.dom.Attr;
-import org.w3c.dom.CDATASection;
-import org.w3c.dom.Comment;
-import org.w3c.dom.Document;
-import org.w3c.dom.DocumentType;
-import org.w3c.dom.Element;
-import org.w3c.dom.EntityReference;
-import org.w3c.dom.NamedNodeMap;
-import org.w3c.dom.Node;
-import org.w3c.dom.NodeList;
-import org.w3c.dom.ProcessingInstruction;
-import org.w3c.dom.Text;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.ext.LexicalHandler;
-import org.xml.sax.helpers.AttributesImpl;
+import org.w3c.dom.Node;
/**
* The <code>DOMStreamer</code> is a utility class that will generate SAX
* events from a W3C DOM Document.
*
+ * @author <a href="mailto:[EMAIL PROTECTED]">Carsten Ziegeler</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Pierpaolo Fumagalli</a>
* (Apache Software Foundation, Exoffice Technologies)
- * @version CVS $Revision: 1.1.1.1.2.1 $ $Date: 2001/07/05 09:11:25 $
+ * @version CVS $Revision: 1.1.1.1.2.2 $ $Date: 2001/07/05 13:33:55 $
*/
public class DOMStreamer extends AbstractXMLProducer {
+ /** The transformer factory shared by all instances */
+ protected static TransformerFactory factory = TransformerFactory.newInstance();
+
+ /** The private transformer for this instance */
+ protected Transformer transformer;
+
/**
* Create a new <code>DOMStreamer</code> instance.
*/
@@ -49,7 +50,7 @@
* Create a new <code>DOMStreamer</code> instance.
*/
public DOMStreamer(XMLConsumer consumer) {
- this(consumer,consumer);
+ this(consumer, consumer);
}
/**
@@ -62,13 +63,6 @@
/**
* Create a new <code>DOMStreamer</code> instance.
*/
- public DOMStreamer(LexicalHandler lexical) {
- this(null,lexical);
- }
-
- /**
- * Create a new <code>DOMStreamer</code> instance.
- */
public DOMStreamer(ContentHandler content, LexicalHandler lexical) {
this();
super.setContentHandler(content);
@@ -78,192 +72,25 @@
/**
* Start the production of SAX events.
*/
- public void stream(Object object)
+ public void stream(Node node)
throws SAXException {
- try {
- processNode((Node)object);
- } catch (ClassCastException e) {
- getLogger().error("DOMStreamer", e);
- throw new SAXException(e);
- }
- }
-
- /** Process a generic node */
- private void processNode(Node n)
- throws SAXException {
- if (n==null) return;
- try {
- switch (n.getNodeType()) {
- case Node.DOCUMENT_NODE:
- this.setDocument((Document)n);
- break;
- case Node.DOCUMENT_TYPE_NODE:
- this.setDocumentType((DocumentType)n);
- break;
- case Node.ELEMENT_NODE:
- this.setElement((Element)n);
- break;
- case Node.TEXT_NODE:
- this.setText((Text)n);
- break;
- case Node.CDATA_SECTION_NODE:
- this.setCDATASection((CDATASection)n);
- break;
- case Node.PROCESSING_INSTRUCTION_NODE:
- this.setProcessingInstruction((ProcessingInstruction)n);
- break;
- case Node.COMMENT_NODE:
- this.setComment((Comment)n);
- break;
- case Node.ENTITY_REFERENCE_NODE:
- this.setEntityReference((EntityReference)n);
- break;
- case Node.ENTITY_NODE:
- case Node.NOTATION_NODE:
- // Do nothing for ENTITY and NOTATION nodes
- break;
- case Node.DOCUMENT_FRAGMENT_NODE:
- // Process all children
- processChildren(n);
- break;
- case Node.ATTRIBUTE_NODE:
- throw new SAXException("Unexpected Attribute node");
- default:
- throw new SAXException("Unknown node type "+n.getNodeType()+
- " class "+n.getClass().getName());
+ if (this.transformer == null) {
+ try {
+ this.transformer = factory.newTransformer();
+ } catch (TransformerConfigurationException e) {
+ getLogger().error("DOMStreamer", e);
+ throw new SAXException(e);
}
- } catch (ClassCastException e) {
- getLogger().error("Error casting node to appropriate type", e);
- throw new SAXException("Error casting node to appropriate type");
}
- }
+ DOMSource source = new DOMSource(node);
+ SAXResult result = new SAXResult(super.contentHandler);
+ result.setLexicalHandler(super.lexicalHandler);
- /** Process all children nodes of a Node */
- private void processChildren(Node n)
- throws SAXException {
- NodeList l=n.getChildNodes();
- for(int x=0;x<l.getLength();x++) processNode(l.item(x));
- }
-
- /** Process a Document node */
- private void setDocument(Document n)
- throws SAXException {
- if (super.contentHandler!=null) super.contentHandler.startDocument();
- this.processChildren(n);
- if (super.contentHandler!=null) super.contentHandler.endDocument();
- }
-
- /** Process a DocumentType node */
- private void setDocumentType(DocumentType n)
- throws SAXException {
- if (super.lexicalHandler==null) return;
- super.lexicalHandler.startDTD(n.getName(),n.getPublicId(),n.getSystemId());
- super.lexicalHandler.endDTD();
- }
-
- /** Process a Element node */
- private void setElement(Element n)
- throws SAXException {
- if (super.contentHandler==null) {
- this.processChildren(n);
- return;
- }
- // Setup attributes
- AttributesImpl atts=new AttributesImpl();
- NamedNodeMap map=n.getAttributes();
- Vector nslist=new Vector();
- for (int x=0; x<map.getLength(); x++) {
- if (map.item(x).getNodeType()!=Node.ATTRIBUTE_NODE) continue;
- Attr a=(Attr)map.item(x);
- // Start getting and normalizing the values from the attribute
- String uri=a.getNamespaceURI(); uri= (uri==null ? "" : uri);
- String pre=a.getPrefix(); pre= (pre==null ? "" : pre);
- String raw=a.getName(); raw= (raw==null ? "" : raw);
- String loc=a.getLocalName(); loc= (loc==null ? raw : loc);
- String val=a.getValue(); val= (val==null ? "" : val);
- // Check if we need to declare the start of a namespace prefix
- // Should we rely on URI instead of prefixes???
- if (raw.equals("xmlns") || raw.startsWith("xmlns:")) {
- String prefix="";
- if (raw.length()>5) prefix=raw.substring(6);
- nslist.addElement(prefix);
- super.contentHandler.startPrefixMapping(prefix,val);
- }
- atts.addAttribute(uri,loc,raw,"CDATA",val);
- }
- // Get and normalize values for the Element
- String uri=n.getNamespaceURI(); uri= (uri==null ? "" : uri);
- String pre=n.getPrefix(); pre= (pre==null ? "" : pre);
- String raw=n.getTagName(); raw= (raw==null ? "" : raw);
- String loc=n.getLocalName(); loc= (loc==null ? raw : loc);
- super.contentHandler.startElement(uri,loc,raw,atts);
- this.processChildren(n);
- super.contentHandler.endElement(uri,loc,raw);
- // Rerun through attributes to check for namespaces we declared.
- // Should we store those before in, maybe, a hashtable?
- for (int x=0; x<nslist.size(); x++) {
- String prefix=(String)nslist.elementAt(x);
- super.contentHandler.endPrefixMapping(prefix);
- }
- }
-
- /** Process a Text node */
- private void setText(Text n)
- throws SAXException {
- char data[]=n.getData().toCharArray();
- if (super.contentHandler!=null)
- super.contentHandler.characters(data,0,data.length);
- }
-
- /** Process a CDATASection node */
- private void setCDATASection(CDATASection n)
- throws SAXException {
- if (super.lexicalHandler!=null) super.lexicalHandler.startCDATA();
- char data[]=n.getData().toCharArray();
- if (super.contentHandler!=null)
- super.contentHandler.characters(data,0,data.length);
- if (super.lexicalHandler!=null) super.lexicalHandler.endCDATA();
- }
-
- /** Process a ProcessingInstruction node */
- private void setProcessingInstruction(ProcessingInstruction n)
- throws SAXException {
- if (super.contentHandler==null) return;
- super.contentHandler.processingInstruction(n.getTarget(),n.getData());
- }
-
- /** Process a Comment node */
- private void setComment(Comment n)
- throws SAXException {
- if (super.lexicalHandler==null) return;
- char data[]=n.getData().toCharArray();
- super.lexicalHandler.comment(data,0,data.length);
- }
-
- /** Process a EntityReference node */
- private void setEntityReference(EntityReference n)
- throws SAXException {
- if (n.hasChildNodes()) {
- if (super.lexicalHandler==null) this.processChildren(n);
- else {
- super.lexicalHandler.startEntity(n.getNodeName());
- this.processChildren(n);
- super.lexicalHandler.endEntity(n.getNodeName());
- }
- return;
- } else {
- if (n.getNodeValue()==null) {
- if (super.contentHandler==null) return;
- else super.contentHandler.skippedEntity(n.getNodeName());
- } else {
- char value[]=n.getNodeValue().toCharArray();
- if (super.lexicalHandler!=null)
- super.lexicalHandler.startEntity(n.getNodeName());
- if (super.contentHandler!=null)
- super.contentHandler.characters(value,0,value.length);
- if (super.lexicalHandler!=null)
- super.lexicalHandler.endEntity(n.getNodeName());
- }
+ try {
+ transformer.transform(source, result);
+ } catch (TransformerException e) {
+ getLogger().error("DOMStreamer", e);
+ throw new SAXException(e);
}
}
}
----------------------------------------------------------------------
In case of troubles, e-mail: [EMAIL PROTECTED]
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]