From a17637ae4dbf7ebae9090954f8b4999b526f51be Mon Sep 17 00:00:00 2001 From: William Victor Mote Date: Fri, 19 Sep 2003 18:17:40 +0000 Subject: [PATCH] add support for "xsl-region-before", "xsl-region-after" and fo:page-number tag, submitted by Peter Herweg, see http://nagoya.apache.org/bugzilla/show_bug.cgi?id=23274 git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@196927 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/fop/fo/FOInputHandler.java | 16 +++ src/java/org/apache/fop/fo/FOTreeHandler.java | 14 ++ .../org/apache/fop/fo/flow/PageNumber.java | 19 +++ .../fop/fo/pagination/PageSequence.java | 4 + src/java/org/apache/fop/mif/MIFHandler.java | 14 ++ .../apache/fop/rtf/renderer/RTFHandler.java | 127 +++++++++++++++++- 6 files changed, 190 insertions(+), 4 deletions(-) diff --git a/src/java/org/apache/fop/fo/FOInputHandler.java b/src/java/org/apache/fop/fo/FOInputHandler.java index 7a2b1a281..9b5254592 100644 --- a/src/java/org/apache/fop/fo/FOInputHandler.java +++ b/src/java/org/apache/fop/fo/FOInputHandler.java @@ -61,6 +61,7 @@ import org.apache.fop.fo.flow.InstreamForeignObject; import org.apache.fop.fo.flow.Leader; import org.apache.fop.fo.flow.ListBlock; import org.apache.fop.fo.flow.ListItem; +import org.apache.fop.fo.flow.PageNumber; import org.apache.fop.fo.flow.Table; import org.apache.fop.fo.flow.TableColumn; import org.apache.fop.fo.flow.TableBody; @@ -131,6 +132,21 @@ public abstract class FOInputHandler extends AbstractLogEnabled { public abstract void endPageSequence(PageSequence pageSeq) throws FOPException; /** + * + * @param pagenum PageNumber that is starting. + */ + public abstract void startPageNumber(PageNumber pagenum); + + /** + * + * @param pagenum PageNumber that is ending. + */ + public abstract void endPageNumber(PageNumber pagenum); + + /** + * This method is called to indicate the start of a new fo:flow or fo:static-content. + * This method also handles fo:static-content tags, because the StaticContent class + * is derived from the Flow class. * * @param fl Flow that is starting. */ diff --git a/src/java/org/apache/fop/fo/FOTreeHandler.java b/src/java/org/apache/fop/fo/FOTreeHandler.java index a205f263d..2362db1b9 100644 --- a/src/java/org/apache/fop/fo/FOTreeHandler.java +++ b/src/java/org/apache/fop/fo/FOTreeHandler.java @@ -65,6 +65,7 @@ import org.apache.fop.fo.flow.InstreamForeignObject; import org.apache.fop.fo.flow.Leader; import org.apache.fop.fo.flow.ListBlock; import org.apache.fop.fo.flow.ListItem; +import org.apache.fop.fo.flow.PageNumber; import org.apache.fop.fo.flow.Table; import org.apache.fop.fo.flow.TableColumn; import org.apache.fop.fo.flow.TableBody; @@ -514,4 +515,17 @@ public class FOTreeHandler extends FOInputHandler { } } + /** + * + * @param pagenum PageNumber that is starting. + */ + public void startPageNumber(PageNumber pagenum) { + } + + /** + * + * @param pagenum PageNumber that is ending. + */ + public void endPageNumber(PageNumber pagenum) { + } } diff --git a/src/java/org/apache/fop/fo/flow/PageNumber.java b/src/java/org/apache/fop/fo/flow/PageNumber.java index e936e35d8..d0d1c1899 100644 --- a/src/java/org/apache/fop/fo/flow/PageNumber.java +++ b/src/java/org/apache/fop/fo/flow/PageNumber.java @@ -50,6 +50,11 @@ */ package org.apache.fop.fo.flow; +// XML +import org.xml.sax.Attributes; + +// FOP +import org.apache.fop.apps.FOPException; import org.apache.fop.datatypes.ColorType; import org.apache.fop.fo.FONode; import org.apache.fop.fo.FOTreeVisitor; @@ -146,4 +151,18 @@ public class PageNumber extends FObj { fotv.servePageNumber(this); } + /** + * @see org.apache.fop.fo.FObj#handleAttrs + */ + public void handleAttrs(Attributes attlist) throws FOPException { + super.handleAttrs(attlist); + + setup(); + + getFOTreeControl().getFOInputHandler().startPageNumber(this); + } + + protected void end() { + getFOTreeControl().getFOInputHandler().endPageNumber(this); + } } diff --git a/src/java/org/apache/fop/fo/pagination/PageSequence.java b/src/java/org/apache/fop/fo/pagination/PageSequence.java index c5615322f..31f9cd157 100644 --- a/src/java/org/apache/fop/fo/pagination/PageSequence.java +++ b/src/java/org/apache/fop/fo/pagination/PageSequence.java @@ -236,6 +236,10 @@ public class PageSequence extends FObj { // this.properties.get("country"); // this.properties.get("language"); setupID(); + + //call startStructuredPageSequence to ensure, that startPageSequence is called + //before startFlow. + startStructuredPageSequence(); } diff --git a/src/java/org/apache/fop/mif/MIFHandler.java b/src/java/org/apache/fop/mif/MIFHandler.java index 9d6be6156..b392c78d3 100644 --- a/src/java/org/apache/fop/mif/MIFHandler.java +++ b/src/java/org/apache/fop/mif/MIFHandler.java @@ -63,6 +63,7 @@ import org.apache.fop.fo.flow.InstreamForeignObject; import org.apache.fop.fo.flow.Leader; import org.apache.fop.fo.flow.ListBlock; import org.apache.fop.fo.flow.ListItem; +import org.apache.fop.fo.flow.PageNumber; import org.apache.fop.fo.flow.Table; import org.apache.fop.fo.flow.TableBody; import org.apache.fop.fo.flow.TableCell; @@ -447,5 +448,18 @@ public class MIFHandler extends FOInputHandler { } } + /** + * + * @param pagenum PageNumber that is starting. + */ + public void startPageNumber(PageNumber pagenum) { + } + + /** + * + * @param pagenum PageNumber that is ending. + */ + public void endPageNumber(PageNumber pagenum) { + } } diff --git a/src/java/org/apache/fop/rtf/renderer/RTFHandler.java b/src/java/org/apache/fop/rtf/renderer/RTFHandler.java index c900aad06..6479f0642 100644 --- a/src/java/org/apache/fop/rtf/renderer/RTFHandler.java +++ b/src/java/org/apache/fop/rtf/renderer/RTFHandler.java @@ -66,6 +66,7 @@ import org.apache.fop.fo.flow.InstreamForeignObject; import org.apache.fop.fo.flow.Leader; import org.apache.fop.fo.flow.ListBlock; import org.apache.fop.fo.flow.ListItem; +import org.apache.fop.fo.flow.PageNumber; import org.apache.fop.fo.flow.Table; import org.apache.fop.fo.flow.TableColumn; import org.apache.fop.fo.flow.TableBody; @@ -76,10 +77,16 @@ import org.apache.fop.fo.pagination.PageSequence; import org.apache.fop.fo.properties.Constants; import org.apache.fop.fo.Property; import org.apache.fop.apps.Document; +import org.apache.fop.rtf.rtflib.rtfdoc.IRtfAfterContainer; +import org.apache.fop.rtf.rtflib.rtfdoc.IRtfBeforeContainer; +import org.apache.fop.rtf.rtflib.rtfdoc.IRtfPageNumberContainer; import org.apache.fop.rtf.rtflib.rtfdoc.IRtfParagraphContainer; +import org.apache.fop.rtf.rtflib.rtfdoc.RtfAfter; import org.apache.fop.rtf.rtflib.rtfdoc.RtfAttributes; +import org.apache.fop.rtf.rtflib.rtfdoc.RtfBefore; import org.apache.fop.rtf.rtflib.rtfdoc.RtfColorTable; import org.apache.fop.rtf.rtflib.rtfdoc.RtfDocumentArea; +import org.apache.fop.rtf.rtflib.rtfdoc.RtfElement; import org.apache.fop.rtf.rtflib.rtfdoc.RtfFile; import org.apache.fop.rtf.rtflib.rtfdoc.RtfParagraph; import org.apache.fop.rtf.rtflib.rtfdoc.RtfSection; @@ -108,6 +115,14 @@ public class RTFHandler extends FOInputHandler { private RtfDocumentArea docArea; private RtfParagraph para; private boolean warned = false; + private boolean bPrevHeaderSpecified=false;//true, if there has been a + //header in any page-sequence + private boolean bPrevFooterSpecified=false;//true, if there has been a + //footer in any page-sequence + private boolean bHeaderSpecified = false; //true, if there is a header + //in current page-sequence + private boolean bFooterSpecified = false; //true, if there is a footer + //in current page-sequence private BuilderContext m_context = new BuilderContext(null); private static final String ALPHA_WARNING = "WARNING: RTF renderer is " @@ -164,10 +179,9 @@ public class RTFHandler extends FOInputHandler { try { sect = docArea.newSection(); m_context.pushContainer(sect); - if (!warned) { - sect.newParagraph().newText(ALPHA_WARNING); - warned = true; - } + + bHeaderSpecified=false; + bFooterSpecified=false; } catch (IOException ioe) { // FIXME could we throw Exception in all FOInputHandler events? log.error("startPageSequence: " + ioe.getMessage()); @@ -186,12 +200,88 @@ public class RTFHandler extends FOInputHandler { * @see org.apache.fop.fo.FOInputHandler#startFlow(Flow) */ public void startFlow(Flow fl) { + try { + if (fl.getFlowName().equals("xsl-region-body")) { + // if there is no header in current page-sequence but there has been + // a header in a previous page-sequence, insert an empty header. + if (bPrevHeaderSpecified && !bHeaderSpecified) { + RtfAttributes attr=new RtfAttributes(); + attr.set(RtfBefore.HEADER); + + final IRtfBeforeContainer contBefore = (IRtfBeforeContainer)m_context.getContainer(IRtfBeforeContainer.class,true,this); + contBefore.newBefore(attr); + } + + // if there is no footer in current page-sequence but there has been + // a footer in a previous page-sequence, insert an empty footer. + if (bPrevFooterSpecified && !bFooterSpecified) { + RtfAttributes attr=new RtfAttributes(); + attr.set(RtfAfter.FOOTER); + + final IRtfAfterContainer contAfter = (IRtfAfterContainer)m_context.getContainer(IRtfAfterContainer.class,true,this); + contAfter.newAfter(attr); + } + + // print ALPHA_WARNING + if (!warned) { + sect.newParagraph().newText(ALPHA_WARNING); + warned = true; + } + } else if(fl.getFlowName().equals("xsl-region-before")) { + bHeaderSpecified=true; + bPrevHeaderSpecified=true; + + final IRtfBeforeContainer c = (IRtfBeforeContainer)m_context.getContainer(IRtfBeforeContainer.class,true,this); + + RtfAttributes beforeAttributes = ((RtfElement)c).getRtfAttributes(); + if ( beforeAttributes == null ) { + beforeAttributes = new RtfAttributes(); + } + beforeAttributes.set(RtfBefore.HEADER); + + RtfBefore before = c.newBefore(beforeAttributes); + m_context.pushContainer(before); + } else if(fl.getFlowName().equals("xsl-region-after")) { + bFooterSpecified=true; + bPrevFooterSpecified=true; + + final IRtfAfterContainer c = (IRtfAfterContainer)m_context.getContainer(IRtfAfterContainer.class,true,this); + + RtfAttributes afterAttributes = ((RtfElement)c).getRtfAttributes(); + if ( afterAttributes == null ) { + afterAttributes = new RtfAttributes(); + } + + afterAttributes.set(RtfAfter.FOOTER); + + RtfAfter after = c.newAfter(afterAttributes); + m_context.pushContainer(after); + } + } catch(IOException ioe) { + log.error("startFlow: " + ioe.getMessage()); + throw new Error(ioe.getMessage()); + } catch(Exception e) { + log.error("startFlow: " + e.getMessage()); + throw new Error(e.getMessage()); + } } /** * @see org.apache.fop.fo.FOInputHandler#endFlow(Flow) */ public void endFlow(Flow fl) { + try { + if (fl.getFlowName().equals("xsl-region-body")) { + //just do nothing + } else if (fl.getFlowName().equals("xsl-region-before")) { + m_context.popContainer(); + } else if (fl.getFlowName().equals("xsl-region-after")) { + m_context.popContainer(); + } + } catch(Exception e){ + log.error("endFlow: " + e.getMessage()); + throw new Error(e.getMessage()); + } } /** @@ -625,4 +715,33 @@ public class RTFHandler extends FOInputHandler { greenComponent, blueComponent).intValue(); } + /** + * + * @param pagenum PageNumber that is starting. + */ + public void startPageNumber(PageNumber pagenum) { + try { + //insert page number + IRtfPageNumberContainer pageNumberContainer = (IRtfPageNumberContainer)m_context.getContainer(IRtfPageNumberContainer.class,true,this); + m_context.pushContainer(pageNumberContainer.newPageNumber()); + + //set Attribute "WhiteSpaceFalse" in order to prevent the rtf library from + //stripping the whitespaces. This applies to whole paragraph. + if(pageNumberContainer instanceof RtfParagraph) { + RtfParagraph para=(RtfParagraph)pageNumberContainer; + para.getRtfAttributes().set("WhiteSpaceFalse"); + } + } catch(Exception e) { + log.error("startPageNumber: " + e.getMessage()); + throw new Error(e.getMessage()); + } + } + + /** + * + * @param pagenum PageNumber that is ending. + */ + public void endPageNumber(PageNumber pagenum) { + m_context.popContainer(); + } } -- 2.39.5