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;
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;
/** 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.
*/
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) {
* 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();
}
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
protected void startElement(String tagName) {
startElement(tagName, EMPTY_ATTS);
}
-
+
/**
* Starts a new element.
* @param tagName tag name of the element
handleSAXException(saxe);
}
}
-
+
/**
* Ends an element.
* @param tagName tag name of the element
handleSAXException(saxe);
}
}
-
+
/**
* Sends plain text to the XML
* @param text the text
handleSAXException(saxe);
}
}
-
+
/**
* Adds a new attribute to the protected member variable "atts".
* @param name name of the attribute
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
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
protected void addAttribute(String name, Rectangle2D rect) {
addAttribute(name, createString(rect));
}
-
+
/**
* Adds the general Area attributes.
* @param area Area to extract attributes from
+ 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) {
extensionAttachments.clear();
}
}
-
+
/** @see org.apache.fop.render.AbstractRenderer#processOffDocumentItem() */
public void processOffDocumentItem(OffDocumentItem oDI) {
if (oDI instanceof OffDocumentExtensionAttachment) {
extensionAttachments.add(attachment);
} else {
String warn = "Ignoring OffDocumentItem: " + oDI;
- comment("WARNING: " + warn);
log.warn(warn);
}
}
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");
}
addAttribute("formatted-nr", page.getPageNumberString());
startElement("pageViewport", atts);
startElement("page");
-
+
handlePageExtensionAttachments(page);
super.renderPage(page);
-
+
endElement("page");
endElement("pageViewport");
}
}
} 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)
*/
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;
}
super.renderText(text);
endElement("text");
}
-
+
/**
* @see org.apache.fop.render.AbstractRenderer#renderWord(WordArea)
*/
endElement("word");
super.renderWord(word);
}
-
+
/**
* @see org.apache.fop.render.AbstractRenderer#renderSpace(SpaceArea)
*/
/*
* 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.
*/
/* $Id$ */
-
+
package org.apache.fop.render.xml;
import org.apache.commons.logging.Log;
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);
}
* @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);
/**
* 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:
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();
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");
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() + ")");
}
}