diff options
author | Jeremias Maerki <jeremias@apache.org> | 2006-01-19 09:46:44 +0000 |
---|---|---|
committer | Jeremias Maerki <jeremias@apache.org> | 2006-01-19 09:46:44 +0000 |
commit | b9d21be592b59117d843b62b694a6d2ee87fb648 (patch) | |
tree | 3907bc9e4de41ff8adbbaf4a09e21026b7e74f2d /src/java/org/apache/fop/render | |
parent | 9904a3e4e8726a6e21ca4190b83a2f57349f9e8e (diff) | |
download | xmlgraphics-fop-b9d21be592b59117d843b62b694a6d2ee87fb648.tar.gz xmlgraphics-fop-b9d21be592b59117d843b62b694a6d2ee87fb648.zip |
New interface XMLizable (copied from Apache Excalibur) to mark classes that can serialize themselves to XML (SAX stream).
New interface ContentHandlerFactory (plus associated Factory with Service discovery) for sub-document parsing plug-ins. Used by the area tree but could be used independently.
ExtensionAttachments are now part of the area tree and are written by XMLRenderer and parsed by AreaTreeParser.
The PS Extensions are extended to support serialization to and deserialization from XML.
Added a test case that tests the PS Extension WRT the handling in the area tree XML.
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@370452 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org/apache/fop/render')
4 files changed, 212 insertions, 3 deletions
diff --git a/src/java/org/apache/fop/render/ps/extensions/PSExtensionHandler.java b/src/java/org/apache/fop/render/ps/extensions/PSExtensionHandler.java new file mode 100644 index 000000000..e22951236 --- /dev/null +++ b/src/java/org/apache/fop/render/ps/extensions/PSExtensionHandler.java @@ -0,0 +1,88 @@ +/* + * Copyright 2006 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* $Id$ */ + +package org.apache.fop.render.ps.extensions; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.fop.area.AreaTreeParser; +import org.apache.fop.util.ContentHandlerFactory; +import org.xml.sax.Attributes; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.DefaultHandler; + +/** + * ContentHandler (parser) for restoring PSExtension objects from XML. + */ +public class PSExtensionHandler extends DefaultHandler + implements ContentHandlerFactory.ObjectSource { + + /** Logger instance */ + protected static Log log = LogFactory.getLog(PSExtensionHandler.class); + + private StringBuffer content = new StringBuffer(); + private Attributes lastAttributes; + + private PSSetupCode returnedObject; + + /** @see org.xml.sax.helpers.DefaultHandler */ + public void startElement(String uri, String localName, String qName, Attributes attributes) + throws SAXException { + boolean handled = false; + if (PSSetupCode.CATEGORY.equals(uri)) { + lastAttributes = attributes; + handled = true; + if ("ps-setup-code".equals(localName)) { + //handled in endElement + } else { + handled = false; + } + } + if (!handled) { + if (PSSetupCode.CATEGORY.equals(uri)) { + throw new SAXException("Unhandled element " + localName + + " in namespace: " + uri); + } else { + log.warn("Unhandled element " + localName + + " in namespace: " + uri); + } + } + } + + /** @see org.xml.sax.helpers.DefaultHandler */ + public void endElement(String uri, String localName, String qName) throws SAXException { + if (PSSetupCode.CATEGORY.equals(uri)) { + if ("ps-setup-code".equals(localName)) { + String name = lastAttributes.getValue("name"); + this.returnedObject = new PSSetupCode(name, content.toString()); + } + } + content.setLength(0); //Reset text buffer (see characters()) + } + + /** @see org.xml.sax.helpers.DefaultHandler#characters(char[], int, int) */ + public void characters(char[] ch, int start, int length) throws SAXException { + content.append(ch, start, length); + } + + /** @see org.apache.fop.area.AreaTreeParser.ObjectSource#getObject() */ + public Object getObject() { + return returnedObject; + } + +} diff --git a/src/java/org/apache/fop/render/ps/extensions/PSExtensionHandlerFactory.java b/src/java/org/apache/fop/render/ps/extensions/PSExtensionHandlerFactory.java new file mode 100644 index 000000000..e2b85adbf --- /dev/null +++ b/src/java/org/apache/fop/render/ps/extensions/PSExtensionHandlerFactory.java @@ -0,0 +1,41 @@ +/* + * Copyright 2006 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* $Id$ */ + +package org.apache.fop.render.ps.extensions; + +import org.apache.fop.util.ContentHandlerFactory; +import org.xml.sax.ContentHandler; + +/** + * Factory for the ContentHandler that handles serialized PSSetupCode instances. + */ +public class PSExtensionHandlerFactory implements ContentHandlerFactory { + + private static final String[] NAMESPACES = new String[] {PSSetupCode.CATEGORY}; + + /** @see org.apache.fop.util.ContentHandlerFactory#getSupportedNamespaces() */ + public String[] getSupportedNamespaces() { + return NAMESPACES; + } + + /** @see org.apache.fop.util.ContentHandlerFactory#createContentHandler() */ + public ContentHandler createContentHandler() { + return new PSExtensionHandler(); + } + +} diff --git a/src/java/org/apache/fop/render/ps/extensions/PSSetupCode.java b/src/java/org/apache/fop/render/ps/extensions/PSSetupCode.java index 846946f99..49cb22979 100644 --- a/src/java/org/apache/fop/render/ps/extensions/PSSetupCode.java +++ b/src/java/org/apache/fop/render/ps/extensions/PSSetupCode.java @@ -1,5 +1,5 @@ /* - * Copyright 2005 The Apache Software Foundation. + * Copyright 2005-2006 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. @@ -21,11 +21,15 @@ package org.apache.fop.render.ps.extensions; import java.io.Serializable; import org.apache.fop.fo.extensions.ExtensionAttachment; +import org.apache.fop.util.XMLizable; +import org.xml.sax.ContentHandler; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; /** * This is the pass-through value object for the PostScript extension. */ -public class PSSetupCode implements ExtensionAttachment, Serializable { +public class PSSetupCode implements ExtensionAttachment, Serializable, XMLizable { /** The category URI for this extension attachment. */ public static final String CATEGORY = "apache:fop:extensions:postscript"; @@ -81,9 +85,26 @@ public class PSSetupCode implements ExtensionAttachment, Serializable { return CATEGORY; } - /** @see java.lang.Object#toString() */ public String toString() { return "PSSetupCode(name=" + getName() + ")"; } + + private static final String ATT_NAME = "name"; + private static final String ELEMENT = "ps-setup-code"; + + /** @see org.apache.fop.util.XMLizable#toSAX(org.xml.sax.ContentHandler) */ + public void toSAX(ContentHandler handler) throws SAXException { + AttributesImpl atts = new AttributesImpl(); + if (name != null && name.length() > 0) { + atts.addAttribute(null, ATT_NAME, ATT_NAME, "CDATA", name); + } + handler.startElement(CATEGORY, ELEMENT, ELEMENT, atts); + if (content != null && content.length() > 0) { + char[] chars = content.toCharArray(); + handler.characters(chars, 0, chars.length); + } + handler.endElement(CATEGORY, ELEMENT, ELEMENT); + } + } diff --git a/src/java/org/apache/fop/render/xml/XMLRenderer.java b/src/java/org/apache/fop/render/xml/XMLRenderer.java index 1b9afc566..982d138bc 100644 --- a/src/java/org/apache/fop/render/xml/XMLRenderer.java +++ b/src/java/org/apache/fop/render/xml/XMLRenderer.java @@ -43,6 +43,7 @@ import org.apache.fop.render.PrintRenderer; import org.apache.fop.render.Renderer; import org.apache.fop.render.RendererContext; import org.apache.fop.render.XMLHandler; +import org.apache.fop.util.XMLizable; import org.apache.fop.apps.FOUserAgent; import org.apache.fop.apps.FOPException; import org.apache.fop.apps.MimeConstants; @@ -56,6 +57,8 @@ import org.apache.fop.area.NormalFlow; import org.apache.fop.area.Footnote; import org.apache.fop.area.LineArea; import org.apache.fop.area.MainReference; +import org.apache.fop.area.OffDocumentExtensionAttachment; +import org.apache.fop.area.OffDocumentItem; import org.apache.fop.area.PageViewport; import org.apache.fop.area.RegionReference; import org.apache.fop.area.RegionViewport; @@ -75,6 +78,7 @@ import org.apache.fop.area.inline.TextArea; import org.apache.fop.area.inline.SpaceArea; import org.apache.fop.area.inline.WordArea; import org.apache.fop.fo.Constants; +import org.apache.fop.fo.extensions.ExtensionAttachment; import org.apache.fop.fonts.FontInfo; import org.apache.fop.fonts.FontSetup; import org.apache.fop.fonts.FontTriplet; @@ -114,6 +118,9 @@ public class XMLRenderer extends PrintRenderer { /** 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. @@ -357,6 +364,28 @@ public class XMLRenderer extends PrintRenderer { + (int) rect.getWidth() + " " + (int) rect.getHeight(); } + private void handleDocumentExtensionAttachments() { + if (extensionAttachments != null && extensionAttachments.size() > 0) { + handleExtensionAttachments(extensionAttachments); + extensionAttachments.clear(); + } + } + + /** @see org.apache.fop.render.AbstractRenderer#processOffDocumentItem() */ + public void processOffDocumentItem(OffDocumentItem oDI) { + if (oDI instanceof OffDocumentExtensionAttachment) { + ExtensionAttachment attachment = ((OffDocumentExtensionAttachment)oDI).getAttachment(); + if (extensionAttachments == null) { + extensionAttachments = new java.util.ArrayList(); + } + extensionAttachments.add(attachment); + } else { + String warn = "Ignoring OffDocumentItem: " + oDI; + comment("WARNING: " + warn); + log.warn(warn); + } + } + /** * @see org.apache.fop.render.Renderer#startRenderer(OutputStream) */ @@ -416,15 +445,45 @@ public class XMLRenderer extends PrintRenderer { addAttribute("nr", page.getPageNumberString()); startElement("pageViewport", atts); startElement("page"); + + handlePageExtensionAttachments(page); super.renderPage(page); + endElement("page"); endElement("pageViewport"); } + private void handleExtensionAttachments(List attachments) { + if (attachments != null && attachments.size() > 0) { + startElement("extension-attachments"); + Iterator i = attachments.iterator(); + while (i.hasNext()) { + ExtensionAttachment attachment = (ExtensionAttachment)i.next(); + if (attachment instanceof XMLizable) { + try { + ((XMLizable)attachment).toSAX(this.handler); + } catch (SAXException e) { + log.error("Error while serializing Extension Attachment", e); + } + } 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) */ public void startPageSequence(LineArea seqTitle) { + handleDocumentExtensionAttachments(); if (startedSequence) { endElement("pageSequence"); } |