From: Chris Bowditch Date: Fri, 27 Jan 2006 17:06:16 +0000 (+0000) Subject: TransformerHandler changed to ContentHandler to allow easier integration of XML Rende... X-Git-Tag: fop-0_92-beta~168 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=eff8f140681f1b3b7b9f0ac881283fba632fa21b;p=xmlgraphics-fop.git TransformerHandler changed to ContentHandler to allow easier integration of XML Renderer to a SAX Events chain git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@372914 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/java/org/apache/fop/render/xml/XMLRenderer.java b/src/java/org/apache/fop/render/xml/XMLRenderer.java index 55881e1d1..c51c33522 100644 --- a/src/java/org/apache/fop/render/xml/XMLRenderer.java +++ b/src/java/org/apache/fop/render/xml/XMLRenderer.java @@ -27,8 +27,8 @@ import java.util.Iterator; import java.awt.geom.Rectangle2D; import javax.xml.transform.TransformerConfigurationException; -import javax.xml.transform.sax.SAXTransformerFactory; import javax.xml.transform.sax.TransformerHandler; +import javax.xml.transform.sax.SAXTransformerFactory; import javax.xml.transform.stream.StreamResult; import org.apache.avalon.framework.configuration.Configuration; import org.apache.avalon.framework.configuration.ConfigurationException; @@ -37,6 +37,7 @@ import org.w3c.dom.Document; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.AttributesImpl; +import org.xml.sax.ContentHandler; // FOP import org.apache.fop.render.PrintRenderer; @@ -97,31 +98,31 @@ public class XMLRenderer extends PrintRenderer { /** Main namespace in use. */ public static final String NS = ""; - + /** CDATA type */ public static final String CDATA = "CDATA"; - + /** An empty Attributes object used when no attributes are needed. */ public static final Attributes EMPTY_ATTS = new AttributesImpl(); - + private boolean startedSequence = false; private RendererContext context; /** If not null, the XMLRenderer will mimic another renderer by using its font setup. */ protected Renderer mimic; - + /** TransformerHandler that the generated XML is written to */ - protected TransformerHandler handler; - + protected ContentHandler handler; + /** AttributesImpl instance that can be used during XML generation. */ protected AttributesImpl atts = new AttributesImpl(); - + /** The OutputStream to write the generated XML to. */ protected OutputStream out; /** A list of ExtensionAttachements received through processOffDocumentItem() */ protected List extensionAttachments; - + /** * Creates a new XML renderer. */ @@ -163,7 +164,7 @@ public class XMLRenderer extends PrintRenderer { public void mimicRenderer(Renderer renderer) { this.mimic = renderer; } - + /** @see org.apache.fop.render.PrintRenderer#setupFontInfo(org.apache.fop.fonts.FontInfo) */ public void setupFontInfo(FontInfo inFontInfo) { if (mimic != null) { @@ -178,12 +179,12 @@ public class XMLRenderer extends PrintRenderer { * create in this class in startRenderer(). * @param handler Overriding TransformerHandler */ - public void setTransformerHandler(TransformerHandler handler) { + public void setContentHandler(ContentHandler handler) { this.handler = handler; } private boolean isCoarseXml() { - return ((Boolean) + return ((Boolean) userAgent.getRendererOptions().get("fineDetail")).booleanValue(); } @@ -194,19 +195,7 @@ public class XMLRenderer extends PrintRenderer { protected void handleSAXException(SAXException saxe) { throw new RuntimeException(saxe.getMessage()); } - - /** - * Writes a comment to the generated XML. - * @param comment the comment - */ - protected void comment(String comment) { - try { - handler.comment(comment.toCharArray(), 0, comment.length()); - } catch (SAXException saxe) { - handleSAXException(saxe); - } - } - + /** * Starts a new element (without attributes). * @param tagName tag name of the element @@ -214,7 +203,7 @@ public class XMLRenderer extends PrintRenderer { protected void startElement(String tagName) { startElement(tagName, EMPTY_ATTS); } - + /** * Starts a new element. * @param tagName tag name of the element @@ -227,7 +216,7 @@ public class XMLRenderer extends PrintRenderer { handleSAXException(saxe); } } - + /** * Ends an element. * @param tagName tag name of the element @@ -239,7 +228,7 @@ public class XMLRenderer extends PrintRenderer { handleSAXException(saxe); } } - + /** * Sends plain text to the XML * @param text the text @@ -252,7 +241,7 @@ public class XMLRenderer extends PrintRenderer { handleSAXException(saxe); } } - + /** * Adds a new attribute to the protected member variable "atts". * @param name name of the attribute @@ -261,7 +250,7 @@ public class XMLRenderer extends PrintRenderer { protected void addAttribute(String name, String value) { atts.addAttribute(NS, name, name, CDATA, value); } - + /** * Adds a new attribute to the protected member variable "atts". * @param name name of the attribute @@ -270,7 +259,7 @@ public class XMLRenderer extends PrintRenderer { protected void addAttribute(String name, int value) { addAttribute(name, Integer.toString(value)); } - + /** * Adds a new attribute to the protected member variable "atts". * @param name name of the attribute @@ -279,7 +268,7 @@ public class XMLRenderer extends PrintRenderer { protected void addAttribute(String name, Rectangle2D rect) { addAttribute(name, createString(rect)); } - + /** * Adds the general Area attributes. * @param area Area to extract attributes from @@ -298,9 +287,9 @@ public class XMLRenderer extends PrintRenderer { + area.getBorderAndPaddingWidthBefore() + " " + area.getBorderAndPaddingWidthAfter()); } - + /** - * Adds attributes from traits of an Area. + * Adds attributes from traits of an Area. * @param area Area to extract traits from */ protected void addTraitAttributes(Area area) { @@ -375,7 +364,7 @@ public class XMLRenderer extends PrintRenderer { extensionAttachments.clear(); } } - + /** @see org.apache.fop.render.AbstractRenderer#processOffDocumentItem() */ public void processOffDocumentItem(OffDocumentItem oDI) { if (oDI instanceof OffDocumentExtensionAttachment) { @@ -386,7 +375,6 @@ public class XMLRenderer extends PrintRenderer { extensionAttachments.add(attachment); } else { String warn = "Ignoring OffDocumentItem: " + oDI; - comment("WARNING: " + warn); log.warn(warn); } } @@ -397,28 +385,27 @@ public class XMLRenderer extends PrintRenderer { public void startRenderer(OutputStream outputStream) throws IOException { log.debug("Rendering areas to Area Tree XML"); - + if (this.handler == null) { - SAXTransformerFactory factory + SAXTransformerFactory factory = (SAXTransformerFactory)SAXTransformerFactory.newInstance(); try { - this.handler = factory.newTransformerHandler(); + TransformerHandler transformerHandler = factory.newTransformerHandler(); + this.handler = transformerHandler; StreamResult res = new StreamResult(outputStream); - handler.setResult(res); + transformerHandler.setResult(res); } catch (TransformerConfigurationException tce) { throw new RuntimeException(tce.getMessage()); } - + this.out = outputStream; } - + try { handler.startDocument(); } catch (SAXException saxe) { handleSAXException(saxe); } - comment("Produced by " - + (userAgent.getProducer() != null ? userAgent.getProducer() : "")); startElement("areaTree"); } @@ -451,10 +438,10 @@ public class XMLRenderer extends PrintRenderer { addAttribute("formatted-nr", page.getPageNumberString()); startElement("pageViewport", atts); startElement("page"); - + handlePageExtensionAttachments(page); super.renderPage(page); - + endElement("page"); endElement("pageViewport"); } @@ -473,18 +460,17 @@ public class XMLRenderer extends PrintRenderer { } } else { String warn = "Ignoring non-XMLizable ExtensionAttachment: " + attachment; - comment("WARNING: " + warn); log.warn(warn); } } endElement("extension-attachments"); } } - + private void handlePageExtensionAttachments(PageViewport page) { handleExtensionAttachments(page.getExtensionAttachments()); } - + /** * @see org.apache.fop.render.Renderer#startPageSequence(LineArea) */ @@ -642,7 +628,7 @@ public class XMLRenderer extends PrintRenderer { if (block instanceof BlockViewport) { BlockViewport bvp = (BlockViewport)block; boolean abspos = false; - if (bvp.getPositioning() == Block.ABSOLUTE + if (bvp.getPositioning() == Block.ABSOLUTE || bvp.getPositioning() == Block.FIXED) { abspos = true; } @@ -794,7 +780,7 @@ public class XMLRenderer extends PrintRenderer { super.renderText(text); endElement("text"); } - + /** * @see org.apache.fop.render.AbstractRenderer#renderWord(WordArea) */ @@ -806,7 +792,7 @@ public class XMLRenderer extends PrintRenderer { endElement("word"); super.renderWord(word); } - + /** * @see org.apache.fop.render.AbstractRenderer#renderSpace(SpaceArea) */ diff --git a/src/java/org/apache/fop/render/xml/XMLXMLHandler.java b/src/java/org/apache/fop/render/xml/XMLXMLHandler.java index 9d1185903..ff87cf057 100644 --- a/src/java/org/apache/fop/render/xml/XMLXMLHandler.java +++ b/src/java/org/apache/fop/render/xml/XMLXMLHandler.java @@ -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,7 +15,7 @@ */ /* $Id$ */ - + package org.apache.fop.render.xml; import org.apache.commons.logging.Log; @@ -29,27 +29,27 @@ import org.w3c.dom.Node; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Attr; import org.xml.sax.SAXException; +import org.xml.sax.ContentHandler; +import org.xml.sax.ext.LexicalHandler; import org.xml.sax.helpers.AttributesImpl; -import javax.xml.transform.sax.TransformerHandler; - /** * XML handler for the XML renderer. */ public class XMLXMLHandler implements XMLHandler { - + /** Key for getting the TransformerHandler from the RendererContext */ public static final String HANDLER = "handler"; /** Logging instance */ private static Log log = LogFactory.getLog(XMLXMLHandler.class); - + private AttributesImpl atts = new AttributesImpl(); - + /** @see org.apache.fop.render.XMLHandler */ - public void handleXML(RendererContext context, + public void handleXML(RendererContext context, org.w3c.dom.Document doc, String ns) throws Exception { - TransformerHandler handler = (TransformerHandler) context.getProperty(HANDLER); + ContentHandler handler = (ContentHandler) context.getProperty(HANDLER); writeDocument(doc, handler); } @@ -61,7 +61,7 @@ public class XMLXMLHandler implements XMLHandler { * @throws SAXException In case of a problem while writing XML */ public void writeDocument(Document doc, - TransformerHandler handler) throws SAXException { + ContentHandler handler) throws SAXException { for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling()) { writeNode(n, handler); @@ -71,10 +71,10 @@ public class XMLXMLHandler implements XMLHandler { /** * Writes a node using the given writer. * @param node node to serialize - * @param handler TransformerHandler to write to + * @param handler ContentHandler to write to * @throws SAXException In case of a problem while writing XML */ - public void writeNode(Node node, TransformerHandler handler) throws SAXException { + public void writeNode(Node node, ContentHandler handler) throws SAXException { char[] ca; switch (node.getNodeType()) { case Node.ELEMENT_NODE: @@ -85,11 +85,11 @@ public class XMLXMLHandler implements XMLHandler { int len = attr.getLength(); for (int i = 0; i < len; i++) { Attr a = (Attr) attr.item(i); - atts.addAttribute("", a.getNodeName(), a.getNodeName(), + atts.addAttribute("", a.getNodeName(), a.getNodeName(), "CDATA", a.getNodeValue()); } } - handler.startElement(node.getNamespaceURI(), + handler.startElement(node.getNamespaceURI(), node.getLocalName(), node.getLocalName(), atts); Node c = node.getFirstChild(); @@ -106,9 +106,12 @@ public class XMLXMLHandler implements XMLHandler { break; case Node.CDATA_SECTION_NODE: ca = node.getNodeValue().toCharArray(); - handler.startCDATA(); - handler.characters(ca, 0, ca.length); - handler.endCDATA(); + if (handler instanceof LexicalHandler) { + LexicalHandler lh = (LexicalHandler)handler; + lh.startCDATA(); + handler.characters(ca, 0, ca.length); + lh.endCDATA(); + } break; case Node.ENTITY_REFERENCE_NODE: log.warn("Ignoring ENTITY_REFERENCE_NODE. NYI"); @@ -123,12 +126,15 @@ public class XMLXMLHandler implements XMLHandler { break; case Node.COMMENT_NODE: ca = node.getNodeValue().toCharArray(); - handler.comment(ca, 0, ca.length); + if (handler instanceof LexicalHandler) { + LexicalHandler lh = (LexicalHandler)handler; + lh.comment(ca, 0, ca.length); + } break; case Node.DOCUMENT_TYPE_NODE: break; default: - throw new IllegalArgumentException("Unexpected node type (" + throw new IllegalArgumentException("Unexpected node type (" + node.getNodeType() + ")"); } }