You are viewing a plain text version of this content. The canonical link for it is here.
Posted to fop-commits@xmlgraphics.apache.org by ac...@apache.org on 2008/07/07 15:28:26 UTC

svn commit: r674484 - /xmlgraphics/fop/trunk/src/java/org/apache/fop/render/xml/AbstractXMLRenderer.java

Author: acumiskey
Date: Mon Jul  7 06:28:26 2008
New Revision: 674484

URL: http://svn.apache.org/viewvc?rev=674484&view=rev
Log:
Added new AbstractXMLRenderer base class.

Added:
    xmlgraphics/fop/trunk/src/java/org/apache/fop/render/xml/AbstractXMLRenderer.java   (with props)

Added: xmlgraphics/fop/trunk/src/java/org/apache/fop/render/xml/AbstractXMLRenderer.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/src/java/org/apache/fop/render/xml/AbstractXMLRenderer.java?rev=674484&view=auto
==============================================================================
--- xmlgraphics/fop/trunk/src/java/org/apache/fop/render/xml/AbstractXMLRenderer.java (added)
+++ xmlgraphics/fop/trunk/src/java/org/apache/fop/render/xml/AbstractXMLRenderer.java Mon Jul  7 06:28:26 2008
@@ -0,0 +1,263 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.xml;
+
+import java.awt.geom.Rectangle2D;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.List;
+
+import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.sax.SAXTransformerFactory;
+import javax.xml.transform.sax.TransformerHandler;
+import javax.xml.transform.stream.StreamResult;
+
+import org.apache.fop.area.BookmarkData;
+import org.apache.fop.area.OffDocumentExtensionAttachment;
+import org.apache.fop.area.OffDocumentItem;
+import org.apache.fop.area.PageViewport;
+import org.apache.fop.fo.extensions.ExtensionAttachment;
+import org.apache.fop.render.PrintRenderer;
+import org.apache.fop.render.RendererContext;
+import org.apache.xmlgraphics.util.QName;
+import org.xml.sax.Attributes;
+import org.xml.sax.ContentHandler;
+import org.xml.sax.SAXException;
+import org.xml.sax.ext.LexicalHandler;
+import org.xml.sax.helpers.AttributesImpl;
+
+public abstract class AbstractXMLRenderer 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();
+
+    /** AttributesImpl instance that can be used during XML generation. */
+    protected AttributesImpl atts = new AttributesImpl();
+
+    /** ContentHandler that the generated XML is written to */
+    protected ContentHandler handler;
+
+    /** The OutputStream to write the generated XML to. */
+    protected OutputStream out;
+
+    protected RendererContext context;
+
+    /** A list of ExtensionAttachements received through processOffDocumentItem() */
+    protected List extensionAttachments;
+
+    /**
+     * Handles SAXExceptions.
+     * @param saxe the SAXException to handle
+     */
+    protected void handleSAXException(SAXException saxe) {
+        throw new RuntimeException(saxe.getMessage());
+    }
+
+    /**
+     * Handles page extension attachments
+     * @param page the page viewport
+     */
+    protected void handlePageExtensionAttachments(PageViewport page) {
+        handleExtensionAttachments(page.getExtensionAttachments());
+    }
+
+    /**
+     * Writes a comment to the generated XML.
+     * @param comment the comment
+     */
+    protected void comment(String comment) {
+        if (handler instanceof LexicalHandler) { 
+            try {
+                ((LexicalHandler) 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
+     * @param atts attributes to add
+     */
+    protected void startElement(String tagName, Attributes atts) {
+        try {
+            handler.startElement(NS, tagName, tagName, atts);
+        } catch (SAXException saxe) {
+            handleSAXException(saxe);
+        }
+    }
+
+    /**
+     * Ends an element.
+     * @param tagName tag name of the element
+     */
+    protected void endElement(String tagName) {
+        try {
+            handler.endElement(NS, tagName, tagName);
+        } catch (SAXException saxe) {
+            handleSAXException(saxe);
+        }
+    }
+
+    /**
+     * Sends plain text to the XML
+     * @param text the text
+     */
+    protected void characters(String text) {
+        try {
+            char[] ca = text.toCharArray();
+            handler.characters(ca, 0, ca.length);
+        } catch (SAXException saxe) {
+            handleSAXException(saxe);
+        }
+    }
+
+    /**
+     * Adds a new attribute to the protected member variable "atts".
+     * @param name name of the attribute
+     * @param value value 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
+     * @param value value of the attribute
+     */
+    protected void addAttribute(QName name, String value) {
+        atts.addAttribute(name.getNamespaceURI(), name.getLocalName(), name.getQName(), 
+                CDATA, value);
+    }
+
+    /**
+     * Adds a new attribute to the protected member variable "atts".
+     * @param name name of the attribute
+     * @param value value of the attribute
+     */
+    protected void addAttribute(String name, int value) {
+        addAttribute(name, Integer.toString(value));
+    }
+
+    private String createString(Rectangle2D rect) {
+        return "" + (int) rect.getX() + " " + (int) rect.getY() + " "
+                  + (int) rect.getWidth() + " " + (int) rect.getHeight();
+    }
+    
+    /**
+     * Adds a new attribute to the protected member variable "atts".
+     * @param name name of the attribute
+     * @param rect a Rectangle2D to format and use as attribute value
+     */
+    protected void addAttribute(String name, Rectangle2D rect) {
+        addAttribute(name, createString(rect));
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void startRenderer(OutputStream outputStream)
+                throws IOException {
+        if (this.handler == null) {
+            SAXTransformerFactory factory
+                = (SAXTransformerFactory)SAXTransformerFactory.newInstance();
+            try {
+                TransformerHandler transformerHandler = factory.newTransformerHandler();
+                setContentHandler(transformerHandler);
+                StreamResult res = new StreamResult(outputStream);
+                transformerHandler.setResult(res);
+            } catch (TransformerConfigurationException tce) {
+                throw new RuntimeException(tce.getMessage());
+            }
+            this.out = outputStream;
+        }
+
+        try {
+            handler.startDocument();
+        } catch (SAXException saxe) {
+            handleSAXException(saxe);
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void stopRenderer() throws IOException {
+        try {
+            handler.endDocument();
+        } catch (SAXException saxe) {
+            handleSAXException(saxe);
+        }
+        if (this.out != null) {
+            this.out.flush();
+        }
+    }
+
+    /** {@inheritDoc} */
+    public void processOffDocumentItem(OffDocumentItem oDI) {
+        if (oDI instanceof BookmarkData) {
+            renderBookmarkTree((BookmarkData) oDI);
+        } else 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;
+            log.warn(warn);
+        }
+    }
+
+    protected void handleDocumentExtensionAttachments() {
+        if (extensionAttachments != null && extensionAttachments.size() > 0) {
+            handleExtensionAttachments(extensionAttachments);
+            extensionAttachments.clear();
+        }
+    }
+
+    /**
+     * Sets an outside TransformerHandler to use instead of the default one
+     * create in this class in startRenderer().
+     * @param handler Overriding TransformerHandler
+     */
+    public void setContentHandler(ContentHandler handler) {
+        this.handler = handler;
+    }
+
+    protected abstract void handleExtensionAttachments(List attachments);
+    
+    protected abstract void renderBookmarkTree(BookmarkData odi);
+}

Propchange: xmlgraphics/fop/trunk/src/java/org/apache/fop/render/xml/AbstractXMLRenderer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: xmlgraphics/fop/trunk/src/java/org/apache/fop/render/xml/AbstractXMLRenderer.java
------------------------------------------------------------------------------
    svn:keywords = Revision Id



---------------------------------------------------------------------
To unsubscribe, e-mail: fop-commits-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: fop-commits-help@xmlgraphics.apache.org


Re: svn commit: r674484 - /xmlgraphics/fop/trunk/src/java/org/apache/fop/render/xml/AbstractXMLRenderer.java

Posted by Jeremias Maerki <de...@jeremias-maerki.ch>.
On 11.07.2008 22:31:03 Andreas Delmelle wrote:
> On Jul 11, 2008, at 10:55, Jeremias Maerki wrote:
> 
> > ... So the IFRenderer actually doesn't generate any XML itself.  
> > That will be the job of the
> > IFSerializer. So IFRenderer will not subclass AbstractXMLRenderer.
> 
> Apart from that, I also completely forgot about the SVGRenderer  
> (currently still in the sandbox).
> Not sure, but that one could be considered an AbstractXMLRenderer as  
> well.

Actually, I'm planning to write an SVGPainter as one of the first
formats for the new intermediate format. Adrian proposed to use SVG for
the intermediate format itself. I fear it would be too slow and too
complicated to do. But I think it's worthwhile to set those two beside
each other to see if my assumption is correct and if my highly optimized
(and proprietary) approach is really worth it. So if it isn't it will be
easy to switch over and as a nice side-benefit (if nothing else) we get
SVG output support.

That means I'm actually abandoning the idea of having an SVGRenderer at
all, especially since the SVGPainter will be easier to implement than
an SVGRenderer.

> And yes, I agree that it's not really a bad thing to have lots of  
> abstractions, as long as they're actually useful.
> If you have only one class that extends it, and there's no immediate  
> benefit, it's worth to stop and think for a second...

+1 to that.

> Considering the above: even if not useful for the new IF, there does  
> seem to be potential benefit for rendering to XML formats.
> 
> 
> Cheers
> 
> Andreas


BTW, I've started working on the new IF yesterday. I've changed the
IFRenderer to subclass AbstractPathOrientedRenderer in the hopes that
this will work out. I'll start with the IFSerializer (implements
IFPainter, writes the actual IF) and with the SVGPainter.


Jeremias Maerki


Re: svn commit: r674484 - /xmlgraphics/fop/trunk/src/java/org/apache/fop/render/xml/AbstractXMLRenderer.java

Posted by Andreas Delmelle <an...@telenet.be>.
On Jul 11, 2008, at 10:55, Jeremias Maerki wrote:

> ... So the IFRenderer actually doesn't generate any XML itself.  
> That will be the job of the
> IFSerializer. So IFRenderer will not subclass AbstractXMLRenderer.

Apart from that, I also completely forgot about the SVGRenderer  
(currently still in the sandbox).
Not sure, but that one could be considered an AbstractXMLRenderer as  
well.

And yes, I agree that it's not really a bad thing to have lots of  
abstractions, as long as they're actually useful.
If you have only one class that extends it, and there's no immediate  
benefit, it's worth to stop and think for a second...

Considering the above: even if not useful for the new IF, there does  
seem to be potential benefit for rendering to XML formats.


Cheers

Andreas

Re: svn commit: r674484 - /xmlgraphics/fop/trunk/src/java/org/apache/fop/render/xml/AbstractXMLRenderer.java

Posted by Jeremias Maerki <de...@jeremias-maerki.ch>.
Adrian,

thanks for uploading your changes to the Temp_AreaTreeRedesign branch.
I made a mistake which sent you off in the wrong direction. The text was
describing a different design than was shown in the graphic [1]. I've
now updated the graphic. The clue here is to avoid going through the SAX
ContentHandler when painting without serialization to the intermediate
format. That would have caused a performance penalty. So the IFRenderer
actually doesn't generate any XML itself. That will be the job of the
IFSerializer. So IFRenderer will not subclass AbstractXMLRenderer. Sorry
for the confusion.

[1] http://wiki.apache.org/xmlgraphics-fop/AreaTreeIntermediateXml/NewDesign

On 08.07.2008 11:25:31 Adrian Cumiskey wrote:
> As it is, I have a class in the Temp_AreaTreeRedesign branch that extends AbstractXMLRenderer.  I 
> thought it useful to provide the class in trunk for anyone else who may wish to create their own XML 
> based renderer for whatever purposes.
> 
> However, IMO you can never have enough abstract classes, helps to hide/break up some of the 
> complexity and remove some unnecessary code dependencies and give yourself options to extend the class.
> 
> Adrian.
> 
> Andreas Delmelle wrote:
> > On Jul 7, 2008, at 15:39, Jeremias Maerki wrote:
> > 
> >>> On 07.07.2008 15:28:26 acumiskey wrote:
> >>>> Author: acumiskey
> >>>> Date: Mon Jul  7 06:28:26 2008
> >>>> New Revision: 674484
> >>>>
> >>>> URL: http://svn.apache.org/viewvc?rev=674484&view=rev
> >>>> Log:
> >>>> Added new AbstractXMLRenderer base class.
> >>>>
> >>>> Added:
> >>>>     
> >>>> xmlgraphics/fop/trunk/src/java/org/apache/fop/render/xml/AbstractXMLRenderer.java   
> >>>> (with props)
> >>>>
> > 
> >> What for? Just curious.
> > 
> > Me too... Just abstracting to abstract seems to make little sense. :-/
> > 
> > 
> > Cheers
> > 
> > Andreas
> > 
> > 




Jeremias Maerki


Re: svn commit: r674484 - /xmlgraphics/fop/trunk/src/java/org/apache/fop/render/xml/AbstractXMLRenderer.java

Posted by Adrian Cumiskey <ad...@gmail.com>.
As it is, I have a class in the Temp_AreaTreeRedesign branch that extends AbstractXMLRenderer.  I 
thought it useful to provide the class in trunk for anyone else who may wish to create their own XML 
based renderer for whatever purposes.

However, IMO you can never have enough abstract classes, helps to hide/break up some of the 
complexity and remove some unnecessary code dependencies and give yourself options to extend the class.

Adrian.

Andreas Delmelle wrote:
> On Jul 7, 2008, at 15:39, Jeremias Maerki wrote:
> 
>>> On 07.07.2008 15:28:26 acumiskey wrote:
>>>> Author: acumiskey
>>>> Date: Mon Jul  7 06:28:26 2008
>>>> New Revision: 674484
>>>>
>>>> URL: http://svn.apache.org/viewvc?rev=674484&view=rev
>>>> Log:
>>>> Added new AbstractXMLRenderer base class.
>>>>
>>>> Added:
>>>>     
>>>> xmlgraphics/fop/trunk/src/java/org/apache/fop/render/xml/AbstractXMLRenderer.java   
>>>> (with props)
>>>>
> 
>> What for? Just curious.
> 
> Me too... Just abstracting to abstract seems to make little sense. :-/
> 
> 
> Cheers
> 
> Andreas
> 
> 


Re: svn commit: r674484 - /xmlgraphics/fop/trunk/src/java/org/apache/fop/render/xml/AbstractXMLRenderer.java

Posted by Andreas Delmelle <an...@telenet.be>.
On Jul 7, 2008, at 15:39, Jeremias Maerki wrote:

>> On 07.07.2008 15:28:26 acumiskey wrote:
>>> Author: acumiskey
>>> Date: Mon Jul  7 06:28:26 2008
>>> New Revision: 674484
>>>
>>> URL: http://svn.apache.org/viewvc?rev=674484&view=rev
>>> Log:
>>> Added new AbstractXMLRenderer base class.
>>>
>>> Added:
>>>     xmlgraphics/fop/trunk/src/java/org/apache/fop/render/xml/ 
>>> AbstractXMLRenderer.java   (with props)
>>>

> What for? Just curious.

Me too... Just abstracting to abstract seems to make little sense. :-/


Cheers

Andreas


Re: svn commit: r674484 - /xmlgraphics/fop/trunk/src/java/org/apache/fop/render/xml/AbstractXMLRenderer.java

Posted by Adrian Cumiskey <ad...@gmail.com>.
Hi Jeremias,

You'll see in a moments time when the recent svn commit on XMLRenderer arrives... :).

Just thought I would put these small changes planned in the Temp_AreaTreeNewDesign into trunk as 
they do no harm and may actually be useful for someone else who may wish to write their own custom 
XML based Renderer.

Adrian.

Jeremias Maerki wrote:
> What for? Just curious.
> 
> On 07.07.2008 15:28:26 acumiskey wrote:
>> Author: acumiskey
>> Date: Mon Jul  7 06:28:26 2008
>> New Revision: 674484
>>
>> URL: http://svn.apache.org/viewvc?rev=674484&view=rev
>> Log:
>> Added new AbstractXMLRenderer base class.
>>
>> Added:
>>     xmlgraphics/fop/trunk/src/java/org/apache/fop/render/xml/AbstractXMLRenderer.java   (with props)
>>
>> Added: xmlgraphics/fop/trunk/src/java/org/apache/fop/render/xml/AbstractXMLRenderer.java
>> URL: http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/src/java/org/apache/fop/render/xml/AbstractXMLRenderer.java?rev=674484&view=auto
>> ==============================================================================
>> --- xmlgraphics/fop/trunk/src/java/org/apache/fop/render/xml/AbstractXMLRenderer.java (added)
>> +++ xmlgraphics/fop/trunk/src/java/org/apache/fop/render/xml/AbstractXMLRenderer.java Mon Jul  7 06:28:26 2008
> <snip/>
> 
> 
> 
> Jeremias Maerki
> 
> 


Re: svn commit: r674484 - /xmlgraphics/fop/trunk/src/java/org/apache/fop/render/xml/AbstractXMLRenderer.java

Posted by Jeremias Maerki <de...@jeremias-maerki.ch>.
What for? Just curious.

On 07.07.2008 15:28:26 acumiskey wrote:
> Author: acumiskey
> Date: Mon Jul  7 06:28:26 2008
> New Revision: 674484
> 
> URL: http://svn.apache.org/viewvc?rev=674484&view=rev
> Log:
> Added new AbstractXMLRenderer base class.
> 
> Added:
>     xmlgraphics/fop/trunk/src/java/org/apache/fop/render/xml/AbstractXMLRenderer.java   (with props)
> 
> Added: xmlgraphics/fop/trunk/src/java/org/apache/fop/render/xml/AbstractXMLRenderer.java
> URL: http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/src/java/org/apache/fop/render/xml/AbstractXMLRenderer.java?rev=674484&view=auto
> ==============================================================================
> --- xmlgraphics/fop/trunk/src/java/org/apache/fop/render/xml/AbstractXMLRenderer.java (added)
> +++ xmlgraphics/fop/trunk/src/java/org/apache/fop/render/xml/AbstractXMLRenderer.java Mon Jul  7 06:28:26 2008
<snip/>



Jeremias Maerki