From f20bdfe9d5f39b5f0bb54c660304aa2572763444 Mon Sep 17 00:00:00 2001 From: Sergey Vladimirov Date: Sat, 16 Jul 2011 09:25:13 +0000 Subject: [PATCH] make some section properties public to prevent access from converters using reflection git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1147390 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/hwpf/converter/AbstractWordUtils.java | 17 ------ .../poi/hwpf/converter/WordToFoConverter.java | 43 ++++++++------- .../hwpf/converter/WordToHtmlConverter.java | 27 +++++----- .../apache/poi/hwpf/usermodel/Section.java | 52 +++++++++++++++++++ 4 files changed, 85 insertions(+), 54 deletions(-) diff --git a/src/scratchpad/src/org/apache/poi/hwpf/converter/AbstractWordUtils.java b/src/scratchpad/src/org/apache/poi/hwpf/converter/AbstractWordUtils.java index 9a667ff916..c50b119bd6 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/converter/AbstractWordUtils.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/converter/AbstractWordUtils.java @@ -21,7 +21,6 @@ import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Constructor; -import java.lang.reflect.Field; import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.hwpf.HWPFDocumentCore; @@ -32,8 +31,6 @@ import org.apache.poi.hwpf.model.ListTables; import org.apache.poi.hwpf.usermodel.BorderCode; import org.apache.poi.hwpf.usermodel.Paragraph; import org.apache.poi.hwpf.usermodel.Range; -import org.apache.poi.hwpf.usermodel.Section; -import org.apache.poi.hwpf.usermodel.SectionProperties; import org.apache.poi.hwpf.usermodel.TableIterator; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.util.IOUtils; @@ -239,20 +236,6 @@ public class AbstractWordUtils return String.valueOf( number ); } - public static SectionProperties getSectionProperties( Section section ) - { - try - { - Field field = Section.class.getDeclaredField( "_props" ); - field.setAccessible( true ); - return (SectionProperties) field.get( section ); - } - catch ( Exception exc ) - { - throw new Error( exc ); - } - } - static boolean isEmpty( String str ) { return str == null || str.length() == 0; 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 7d118dbca5..939f749dc8 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToFoConverter.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToFoConverter.java @@ -27,17 +27,15 @@ import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; -import org.apache.poi.hwpf.converter.FontReplacer.Triplet; - import org.apache.poi.hpsf.SummaryInformation; import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.hwpf.HWPFDocumentCore; +import org.apache.poi.hwpf.converter.FontReplacer.Triplet; import org.apache.poi.hwpf.usermodel.CharacterRun; import org.apache.poi.hwpf.usermodel.Paragraph; import org.apache.poi.hwpf.usermodel.Picture; import org.apache.poi.hwpf.usermodel.Range; import org.apache.poi.hwpf.usermodel.Section; -import org.apache.poi.hwpf.usermodel.SectionProperties; import org.apache.poi.hwpf.usermodel.Table; import org.apache.poi.hwpf.usermodel.TableCell; import org.apache.poi.hwpf.usermodel.TableRow; @@ -146,18 +144,21 @@ public class WordToFoConverter extends AbstractWordConverter this.foDocumentFacade = new FoDocumentFacade( document ); } - protected String createPageMaster( SectionProperties sep, String type, - int section ) + protected String createPageMaster( Section section, String type, + int sectionIndex ) { - float height = sep.getYaPage() / WordToFoUtils.TWIPS_PER_INCH; - float width = sep.getXaPage() / WordToFoUtils.TWIPS_PER_INCH; - float leftMargin = sep.getDxaLeft() / WordToFoUtils.TWIPS_PER_INCH; - float rightMargin = sep.getDxaRight() / WordToFoUtils.TWIPS_PER_INCH; - float topMargin = sep.getDyaTop() / WordToFoUtils.TWIPS_PER_INCH; - float bottomMargin = sep.getDyaBottom() / WordToFoUtils.TWIPS_PER_INCH; + float height = section.getPageHeight() / WordToFoUtils.TWIPS_PER_INCH; + float width = section.getPageWidth() / WordToFoUtils.TWIPS_PER_INCH; + float leftMargin = section.getMarginLeft() + / WordToFoUtils.TWIPS_PER_INCH; + float rightMargin = section.getMarginRight() + / WordToFoUtils.TWIPS_PER_INCH; + float topMargin = section.getMarginTop() / WordToFoUtils.TWIPS_PER_INCH; + float bottomMargin = section.getMarginBottom() + / WordToFoUtils.TWIPS_PER_INCH; // add these to the header - String pageMasterName = type + "-page" + section; + String pageMasterName = type + "-page" + sectionIndex; Element pageMaster = foDocumentFacade .addSimplePageMaster( pageMasterName ); @@ -178,15 +179,15 @@ public class WordToFoConverter extends AbstractWordConverter // WordToFoUtils.setBorder(regionBody, sep.getBrcLeft(), "left"); // WordToFoUtils.setBorder(regionBody, sep.getBrcRight(), "right"); - if ( sep.getCcolM1() > 0 ) + if ( section.getNumColumns() > 1 ) { - regionBody.setAttribute( "column-count", "" - + ( sep.getCcolM1() + 1 ) ); - if ( sep.getFEvenlySpaced() ) + regionBody.setAttribute( "column-count", + "" + ( section.getNumColumns() ) ); + if ( section.isColumnsEvenlySpaced() ) { - regionBody.setAttribute( "column-gap", - ( sep.getDxaColumns() / WordToFoUtils.TWIPS_PER_INCH ) - + "in" ); + float distance = section.getDistanceBetweenColumns() + / WordToFoUtils.TWIPS_PER_INCH; + regionBody.setAttribute( "column-gap", distance + "in" ); } else { @@ -376,9 +377,7 @@ public class WordToFoConverter extends AbstractWordConverter protected void processSection( HWPFDocumentCore wordDocument, Section section, int sectionCounter ) { - String regularPage = createPageMaster( - WordToFoUtils.getSectionProperties( section ), "page", - sectionCounter ); + String regularPage = createPageMaster( section, "page", sectionCounter ); Element pageSequence = foDocumentFacade.addPageSequence( regularPage ); Element flow = foDocumentFacade.addFlowToPageSequence( pageSequence, diff --git a/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToHtmlConverter.java b/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToHtmlConverter.java index 5daf0375b8..3046c46047 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToHtmlConverter.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToHtmlConverter.java @@ -27,17 +27,15 @@ import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; -import org.apache.poi.hwpf.converter.FontReplacer.Triplet; - import org.apache.poi.hpsf.SummaryInformation; import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.hwpf.HWPFDocumentCore; +import org.apache.poi.hwpf.converter.FontReplacer.Triplet; import org.apache.poi.hwpf.usermodel.CharacterRun; import org.apache.poi.hwpf.usermodel.Paragraph; import org.apache.poi.hwpf.usermodel.Picture; import org.apache.poi.hwpf.usermodel.Range; import org.apache.poi.hwpf.usermodel.Section; -import org.apache.poi.hwpf.usermodel.SectionProperties; import org.apache.poi.hwpf.usermodel.Table; import org.apache.poi.hwpf.usermodel.TableCell; import org.apache.poi.hwpf.usermodel.TableRow; @@ -82,23 +80,22 @@ public class WordToHtmlConverter extends AbstractWordConverter private static String getSectionStyle( Section section ) { - SectionProperties sep = WordToHtmlUtils.getSectionProperties( section ); - - float leftMargin = sep.getDxaLeft() / TWIPS_PER_INCH; - float rightMargin = sep.getDxaRight() / TWIPS_PER_INCH; - float topMargin = sep.getDyaTop() / TWIPS_PER_INCH; - float bottomMargin = sep.getDyaBottom() / TWIPS_PER_INCH; + float leftMargin = section.getMarginLeft() / TWIPS_PER_INCH; + float rightMargin = section.getMarginRight() / TWIPS_PER_INCH; + float topMargin = section.getMarginTop() / TWIPS_PER_INCH; + float bottomMargin = section.getMarginBottom() / TWIPS_PER_INCH; String style = "margin: " + topMargin + "in " + rightMargin + "in " + bottomMargin + "in " + leftMargin + "in; "; - if ( sep.getCcolM1() > 0 ) + if ( section.getNumColumns() > 1 ) { - style += "column-count: " + ( sep.getCcolM1() + 1 ) + "; "; - if ( sep.getFEvenlySpaced() ) + style += "column-count: " + ( section.getNumColumns() ) + "; "; + if ( section.isColumnsEvenlySpaced() ) { - style += "column-gap: " - + ( sep.getDxaColumns() / TWIPS_PER_INCH ) + "in; "; + float distance = section.getDistanceBetweenColumns() + / TWIPS_PER_INCH; + style += "column-gap: " + distance + "in; "; } else { @@ -317,7 +314,7 @@ public class WordToHtmlConverter extends AbstractWordConverter final CharacterRun characterRun = paragraph.getCharacterRun( 0 ); if ( characterRun != null ) { - Triplet triplet = getCharacterRunTriplet(characterRun); + Triplet triplet = getCharacterRunTriplet( characterRun ); pFontSize = characterRun.getFontSize() / 2; pFontName = triplet.fontName; WordToHtmlUtils.addFontFamily( pFontName, style ); diff --git a/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Section.java b/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Section.java index 101f847052..4c902ee5ee 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Section.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Section.java @@ -43,11 +43,63 @@ public final class Section extends Range return s; } + /** + * @return distance to be maintained between columns, in twips. Used when + * {@link #isColumnsEvenlySpaced()} == true + */ + public int getDistanceBetweenColumns() + { + return _props.getDxaColumns(); + } + + public int getMarginBottom() + { + return _props.getDyaBottom(); + } + + public int getMarginLeft() + { + return _props.getDxaLeft(); + } + + public int getMarginRight() + { + return _props.getDxaRight(); + } + + public int getMarginTop() + { + return _props.getDyaTop(); + } + public int getNumColumns() { return _props.getCcolM1() + 1; } + /** + * @return page height (in twips) in current section. Default value is 15840 + * twips + */ + public int getPageHeight() + { + return _props.getYaPage(); + } + + /** + * @return page width (in twips) in current section. Default value is 12240 + * twips + */ + public int getPageWidth() + { + return _props.getXaPage(); + } + + public boolean isColumnsEvenlySpaced() + { + return _props.getFEvenlySpaced(); + } + @Override public String toString() { -- 2.39.5