From f5d15f67d6554a78fb34e00e18985ab3c51725ee Mon Sep 17 00:00:00 2001 From: Sergey Vladimirov Date: Thu, 21 Jul 2011 02:47:04 +0000 Subject: [PATCH] fix XLS:FO footnotes and endnotes processing fo Word-to-FO converter git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1149011 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/hwpf/converter/FoDocumentFacade.java | 5 + .../poi/hwpf/converter/WordToFoConverter.java | 115 ++++++++++++------ 2 files changed, 81 insertions(+), 39 deletions(-) diff --git a/src/scratchpad/src/org/apache/poi/hwpf/converter/FoDocumentFacade.java b/src/scratchpad/src/org/apache/poi/hwpf/converter/FoDocumentFacade.java index 03c8b2da21..5aad12fd3e 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/converter/FoDocumentFacade.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/converter/FoDocumentFacade.java @@ -137,6 +137,11 @@ public class FoDocumentFacade return result; } + public Element createFootnote() + { + return document.createElementNS( NS_XSLFO, "fo:footnote" ); + } + public Element createFootnoteBody() { return document.createElementNS( NS_XSLFO, "fo:footnote-body" ); diff --git a/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToFoConverter.java b/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToFoConverter.java index 38e95995db..0c4d31e005 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToFoConverter.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToFoConverter.java @@ -18,6 +18,7 @@ package org.apache.poi.hwpf.converter; import java.io.File; import java.io.FileWriter; +import java.util.ArrayList; import java.util.List; import java.util.Stack; @@ -97,45 +98,6 @@ public class WordToFoConverter extends AbstractWordConverter } } - @Override - protected void processEndnoteAutonumbered( HWPFDocument doc, int noteIndex, - Element block, Range endnoteTextRange ) - { - // TODO: add endnote implementation? - processFootnoteAutonumbered( doc, noteIndex, block, endnoteTextRange ); - } - - @Override - protected void processFootnoteAutonumbered( HWPFDocument doc, - int noteIndex, Element block, Range footnoteTextRange ) - { - String textIndex = String.valueOf( noteIndex + 1 ); - - { - Element inline = foDocumentFacade.createInline(); - inline.setTextContent( textIndex ); - inline.setAttribute( "baseline-shift", "super" ); - inline.setAttribute( "font-size", "smaller" ); - block.appendChild( inline ); - } - - Element footnoteBody = foDocumentFacade.createFootnoteBody(); - Element footnoteBlock = foDocumentFacade.createBlock(); - footnoteBody.appendChild( footnoteBlock ); - block.appendChild( footnoteBody ); - - { - Element inline = foDocumentFacade.createInline(); - inline.setTextContent( textIndex ); - inline.setAttribute( "baseline-shift", "super" ); - inline.setAttribute( "font-size", "smaller" ); - footnoteBlock.appendChild( inline ); - } - - processCharacters( doc, Integer.MIN_VALUE, footnoteTextRange, - footnoteBlock ); - } - static Document process( File docFile ) throws Exception { final HWPFDocumentCore hwpfDocument = WordToFoUtils.loadDoc( docFile ); @@ -148,6 +110,8 @@ public class WordToFoConverter extends AbstractWordConverter private final Stack blocksProperies = new Stack(); + private List endnotes = new ArrayList( 0 ); + protected final FoDocumentFacade foDocumentFacade; /** @@ -163,6 +127,15 @@ public class WordToFoConverter extends AbstractWordConverter this.foDocumentFacade = new FoDocumentFacade( document ); } + protected Element createNoteInline( String noteIndexText ) + { + Element inline = foDocumentFacade.createInline(); + inline.setTextContent( noteIndexText ); + inline.setAttribute( "baseline-shift", "super" ); + inline.setAttribute( "font-size", "smaller" ); + return inline; + } + protected String createPageMaster( Section section, String type, int sectionIndex ) { @@ -291,6 +264,63 @@ public class WordToFoConverter extends AbstractWordConverter foDocumentFacade.setDescription( summaryInformation.getComments() ); } + @Override + protected void processEndnoteAutonumbered( HWPFDocument doc, int noteIndex, + Element block, Range endnoteTextRange ) + { + final String textIndex = String.valueOf( noteIndex + 1 ); + final String forwardLinkName = "endnote_" + textIndex; + final String backwardLinkName = "endnote_back_" + textIndex; + + Element forwardLink = foDocumentFacade + .createBasicLinkInternal( forwardLinkName ); + forwardLink.appendChild( createNoteInline( textIndex ) ); + forwardLink.setAttribute( "id", backwardLinkName ); + block.appendChild( forwardLink ); + + Element endnote = foDocumentFacade.createBlock(); + Element backwardLink = foDocumentFacade + .createBasicLinkInternal( backwardLinkName ); + backwardLink.appendChild( createNoteInline( textIndex + " " ) ); + backwardLink.setAttribute( "id", forwardLinkName ); + endnote.appendChild( backwardLink ); + processCharacters( doc, Integer.MIN_VALUE, endnoteTextRange, endnote ); + this.endnotes.add( endnote ); + } + + @Override + protected void processFootnoteAutonumbered( HWPFDocument doc, + int noteIndex, Element block, Range footnoteTextRange ) + { + final String textIndex = String.valueOf( noteIndex + 1 ); + final String forwardLinkName = "footnote_" + textIndex; + final String backwardLinkName = "footnote_back_" + textIndex; + + Element footNote = foDocumentFacade.createFootnote(); + block.appendChild( footNote ); + + Element inline = foDocumentFacade.createInline(); + Element forwardLink = foDocumentFacade + .createBasicLinkInternal( forwardLinkName ); + forwardLink.appendChild( createNoteInline( textIndex ) ); + forwardLink.setAttribute( "id", backwardLinkName ); + inline.appendChild( forwardLink ); + footNote.appendChild( inline ); + + Element footnoteBody = foDocumentFacade.createFootnoteBody(); + Element footnoteBlock = foDocumentFacade.createBlock(); + Element backwardLink = foDocumentFacade + .createBasicLinkInternal( backwardLinkName ); + backwardLink.appendChild( createNoteInline( textIndex + " " ) ); + backwardLink.setAttribute( "id", forwardLinkName ); + footnoteBlock.appendChild( backwardLink ); + footnoteBody.appendChild( footnoteBlock ); + footNote.appendChild( footnoteBody ); + + processCharacters( doc, Integer.MIN_VALUE, footnoteTextRange, + footnoteBlock ); + } + protected void processHyperlink( HWPFDocumentCore wordDocument, Element currentBlock, Range textRange, int currentTableLevel, String hyperlink ) @@ -421,6 +451,13 @@ public class WordToFoConverter extends AbstractWordConverter "xsl-region-body" ); processParagraphes( wordDocument, flow, section, Integer.MIN_VALUE ); + + if ( endnotes != null && !endnotes.isEmpty() ) + { + for ( Element endnote : endnotes ) + flow.appendChild( endnote ); + endnotes.clear(); + } } protected void processTable( HWPFDocumentCore wordDocument, Element flow, -- 2.39.5