diff options
author | Andreas Beeker <kiwiwings@apache.org> | 2020-12-12 23:15:20 +0000 |
---|---|---|
committer | Andreas Beeker <kiwiwings@apache.org> | 2020-12-12 23:15:20 +0000 |
commit | c5f6dfa68e04c5a42b64b3f862756b9c990665dc (patch) | |
tree | abeaa3d26edfb046c64ef744451648999b0d3a53 /src/ooxml/java/org/apache/poi/xwpf/usermodel | |
parent | 1f072afe18b3022f90a7f2dae52b9606b459e045 (diff) | |
download | poi-c5f6dfa68e04c5a42b64b3f862756b9c990665dc.tar.gz poi-c5f6dfa68e04c5a42b64b3f862756b9c990665dc.zip |
Bug 56205 - Upgrade OOXML schema to 3rd edition (transitional)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1884368 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/ooxml/java/org/apache/poi/xwpf/usermodel')
11 files changed, 148 insertions, 189 deletions
diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/ParagraphAlignment.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/ParagraphAlignment.java index 9f6496d49f..e3599032f8 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/ParagraphAlignment.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/ParagraphAlignment.java @@ -20,6 +20,8 @@ package org.apache.poi.xwpf.usermodel; import java.util.HashMap; import java.util.Map; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc; + /** * Specifies all types of alignment which are available to be applied to objects in a * WordprocessingML document @@ -29,18 +31,21 @@ import java.util.Map; public enum ParagraphAlignment { //YK: TODO document each alignment option - LEFT(1), - CENTER(2), - RIGHT(3), - BOTH(4), - MEDIUM_KASHIDA(5), - DISTRIBUTE(6), - NUM_TAB(7), - HIGH_KASHIDA(8), - LOW_KASHIDA(9), - THAI_DISTRIBUTE(10); + START(STJc.INT_START), // 1 + CENTER(STJc.INT_CENTER), // 2 + END(STJc.INT_END), // 3 + BOTH(STJc.INT_BOTH), // 4 + MEDIUM_KASHIDA(STJc.INT_MEDIUM_KASHIDA), // 5 + DISTRIBUTE(STJc.INT_DISTRIBUTE), // 6 + NUM_TAB(STJc.INT_NUM_TAB), // 7 + HIGH_KASHIDA(STJc.INT_HIGH_KASHIDA), // 8 + LOW_KASHIDA(STJc.INT_LOW_KASHIDA), // 9 + THAI_DISTRIBUTE(STJc.INT_THAI_DISTRIBUTE), // 10 + LEFT(STJc.INT_LEFT), // 11 + RIGHT(STJc.INT_RIGHT) // 12 + ; - private static Map<Integer, ParagraphAlignment> imap = new HashMap<>(); + private static final Map<Integer, ParagraphAlignment> imap = new HashMap<>(); static { for (ParagraphAlignment p : values()) { diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/TOC.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/TOC.java index 82acbb2817..74913cf815 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/TOC.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/TOC.java @@ -21,6 +21,7 @@ import java.math.BigInteger; import org.apache.poi.util.Internal; import org.apache.poi.util.LocaleUtil; import org.apache.xmlbeans.impl.xb.xmlschema.SpaceAttribute.Space; +import org.openxmlformats.schemas.officeDocument.x2006.sharedTypes.STOnOff1; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDecimalNumber; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFonts; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP; @@ -35,7 +36,6 @@ import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTabStop; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTabs; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STFldCharType; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTabJc; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTabTlc; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTheme; @@ -61,8 +61,8 @@ public class TOC { fonts.setEastAsiaTheme(STTheme.MINOR_H_ANSI); fonts.setHAnsiTheme(STTheme.MINOR_H_ANSI); fonts.setCstheme(STTheme.MINOR_BIDI); - rPr.addNewB().setVal(STOnOff.OFF); - rPr.addNewBCs().setVal(STOnOff.OFF); + rPr.addNewB().setVal(STOnOff1.OFF); + rPr.addNewBCs().setVal(STOnOff1.OFF); rPr.addNewColor().setVal("auto"); rPr.addNewSz().setVal(BigInteger.valueOf(24)); rPr.addNewSzCs().setVal(BigInteger.valueOf(24)); diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDefaultParagraphStyle.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDefaultParagraphStyle.java index 9a1a973e8b..77679d2db7 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDefaultParagraphStyle.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDefaultParagraphStyle.java @@ -17,26 +17,26 @@ package org.apache.poi.xwpf.usermodel; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPr; +import org.apache.poi.ooxml.util.POIXMLUnits; +import org.apache.poi.util.Units; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPrGeneral; /** * Default Paragraph style, from which other styles will override * TODO Share logic with {@link XWPFParagraph} which also uses CTPPr */ public class XWPFDefaultParagraphStyle { - private CTPPr ppr; + private final CTPPrGeneral ppr; - public XWPFDefaultParagraphStyle(CTPPr ppr) { + public XWPFDefaultParagraphStyle(CTPPrGeneral ppr) { this.ppr = ppr; } - protected CTPPr getPPr() { + protected CTPPrGeneral getPPr() { return ppr; } public int getSpacingAfter() { - if (ppr.isSetSpacing()) - return ppr.getSpacing().getAfter().intValue(); - return -1; + return ppr.isSetSpacing() ? (int) Units.toDXA(POIXMLUnits.parseLength(ppr.getSpacing().xgetAfter())) : -1; } } diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDefaultRunStyle.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDefaultRunStyle.java index 140ca8bfc2..8eb8f6c1e0 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDefaultRunStyle.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDefaultRunStyle.java @@ -17,12 +17,14 @@ package org.apache.poi.xwpf.usermodel; -import org.apache.poi.util.Removal; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr; - import java.math.BigDecimal; import java.math.RoundingMode; +import org.apache.poi.ooxml.util.POIXMLUnits; +import org.apache.poi.util.Removal; +import org.apache.poi.util.Units; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr; + /** * Default Character Run style, from which other styles will override * TODO Share logic with {@link XWPFRun} which also uses CTRPr @@ -64,8 +66,8 @@ public class XWPFDefaultRunStyle { } private BigDecimal getFontSizeAsBigDecimal(int scale) { - return (rpr != null && rpr.isSetSz()) ? - new BigDecimal(rpr.getSz().getVal()).divide(BigDecimal.valueOf(2)).setScale(scale, RoundingMode.HALF_UP) : - null; + return (rpr != null && rpr.isSetSz()) + ? BigDecimal.valueOf(Units.toPoints(POIXMLUnits.parseLength(rpr.getSz().xgetVal()))).divide(BigDecimal.valueOf(4), scale, RoundingMode.HALF_UP) + : null; } } diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java index 420bbb3c7f..64b877060b 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java @@ -64,27 +64,8 @@ import org.apache.xmlbeans.XmlCursor; import org.apache.xmlbeans.XmlException; import org.apache.xmlbeans.XmlObject; import org.apache.xmlbeans.XmlOptions; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBody; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTComment; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDocument1; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFtnEdn; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTOnOff; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtBlock; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTStyles; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.CommentsDocument; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.DocumentDocument; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.EndnotesDocument; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.FootnotesDocument; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.NumberingDocument; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.STDocProtect; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.STHdrFtr; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.StylesDocument; +import org.openxmlformats.schemas.officeDocument.x2006.sharedTypes.STOnOff1; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.*; /** * <p>High(ish) level class for working with .docx files.</p> @@ -508,7 +489,7 @@ public class XWPFDocument extends POIXMLDocument implements Document, IBody { CTSectPr ctSectPr = getSection(); if (!ctSectPr.isSetTitlePg()) { CTOnOff titlePg = ctSectPr.addNewTitlePg(); - titlePg.setVal(STOnOff.ON); + titlePg.setVal(STOnOff1.ON); } // } else if (type == HeaderFooterType.EVEN) { // TODO Add support for Even/Odd headings and footers @@ -530,7 +511,7 @@ public class XWPFDocument extends POIXMLDocument implements Document, IBody { CTSectPr ctSectPr = getSection(); if (!ctSectPr.isSetTitlePg()) { CTOnOff titlePg = ctSectPr.addNewTitlePg(); - titlePg.setVal(STOnOff.ON); + titlePg.setVal(STOnOff1.ON); } // } else if (type == HeaderFooterType.EVEN) { // TODO Add support for Even/Odd headings and footers diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java index 58064b00fd..bbf59867d1 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java @@ -23,10 +23,13 @@ import java.util.List; import java.util.function.Function; import org.apache.poi.ooxml.POIXMLDocumentPart; +import org.apache.poi.ooxml.util.POIXMLUnits; import org.apache.poi.util.Internal; +import org.apache.poi.util.Units; import org.apache.poi.wp.usermodel.Paragraph; import org.apache.xmlbeans.XmlCursor; import org.apache.xmlbeans.XmlObject; +import org.openxmlformats.schemas.officeDocument.x2006.sharedTypes.STOnOff1; import org.openxmlformats.schemas.wordprocessingml.x2006.main.*; /** @@ -429,7 +432,7 @@ public class XWPFParagraph implements IBodyElement, IRunBody, ISDTContents, Para */ public boolean isKeepNext() { if (getCTP() != null && getCTP().getPPr() != null && getCTP().getPPr().isSetKeepNext()) { - return getCTP().getPPr().getKeepNext().getVal() == STOnOff.ON; + return POIXMLUnits.parseOnOff(getCTP().getPPr().getKeepNext().xgetVal()); } return false; } @@ -441,7 +444,7 @@ public class XWPFParagraph implements IBodyElement, IRunBody, ISDTContents, Para */ public void setKeepNext(boolean keepNext) { CTOnOff state = CTOnOff.Factory.newInstance(); - state.setVal(keepNext ? STOnOff.ON : STOnOff.OFF); + state.setVal(keepNext ? STOnOff1.ON : STOnOff1.OFF); getCTP().getPPr().setKeepNext(state); } @@ -874,24 +877,7 @@ public class XWPFParagraph implements IBodyElement, IRunBody, ISDTContents, Para if (ctPageBreak == null) { return false; } - return isTruelike(ctPageBreak.getVal()); - } - - private static boolean isTruelike(final STOnOff.Enum value) { - if (value == null) { - return false; - } - switch (value.intValue()) { - case STOnOff.INT_TRUE: - case STOnOff.INT_X_1: - case STOnOff.INT_ON: - return true; - /*STOnOff.INT_FALSE: - STOnOff.INT_X_0: - STOnOff.INT_OFF:*/ - default: - return false; - } + return POIXMLUnits.parseOnOff(ctPageBreak.xgetVal()); } /** @@ -914,11 +900,7 @@ public class XWPFParagraph implements IBodyElement, IRunBody, ISDTContents, Para CTPPr ppr = getCTPPr(); CTOnOff ctPageBreak = ppr.isSetPageBreakBefore() ? ppr .getPageBreakBefore() : ppr.addNewPageBreakBefore(); - if (pageBreak) { - ctPageBreak.setVal(STOnOff.TRUE); - } else { - ctPageBreak.setVal(STOnOff.FALSE); - } + ctPageBreak.setVal(pageBreak ? STOnOff1.ON : STOnOff1.OFF); } /** @@ -929,7 +911,7 @@ public class XWPFParagraph implements IBodyElement, IRunBody, ISDTContents, Para */ public int getSpacingAfter() { CTSpacing spacing = getCTSpacing(false); - return (spacing != null && spacing.isSetAfter()) ? spacing.getAfter().intValue() : -1; + return (spacing != null && spacing.isSetAfter()) ? (int)Units.toDXA(POIXMLUnits.parseLength(spacing.xgetAfter())) : -1; } /** @@ -995,7 +977,7 @@ public class XWPFParagraph implements IBodyElement, IRunBody, ISDTContents, Para */ public int getSpacingBefore() { CTSpacing spacing = getCTSpacing(false); - return (spacing != null && spacing.isSetBefore()) ? spacing.getBefore().intValue() : -1; + return (spacing != null && spacing.isSetBefore()) ? (int)Units.toDXA(POIXMLUnits.parseLength(spacing.xgetBefore())) : -1; } /** @@ -1088,12 +1070,11 @@ public class XWPFParagraph implements IBodyElement, IRunBody, ISDTContents, Para CTSpacing spacing = getCTSpacing(false); if (spacing == null || !spacing.isSetLine()) { return -1; - } else if (spacing.getLineRule() == null || spacing.getLineRule() == STLineSpacingRule.AUTO) { - BigInteger[] val = spacing.getLine().divideAndRemainder(BigInteger.valueOf(240L)); - return val[0].doubleValue() + (val[1].doubleValue() / 240L); } - BigInteger[] val = spacing.getLine().divideAndRemainder(BigInteger.valueOf(20L)); - return val[0].doubleValue() + (val[1].doubleValue() / 20L); + + double twips = Units.toDXA(POIXMLUnits.parseLength(spacing.xgetLine())); + + return twips / ((spacing.getLineRule() == null || spacing.getLineRule() == STLineSpacingRule.AUTO) ? 240 : 20); } /** @@ -1143,8 +1124,9 @@ public class XWPFParagraph implements IBodyElement, IRunBody, ISDTContents, Para */ public int getIndentationLeft() { CTInd indentation = getCTInd(false); - return (indentation != null && indentation.isSetLeft()) ? indentation.getLeft().intValue() - : -1; + return (indentation != null && indentation.isSetLeft()) + ? (int)Units.toDXA(POIXMLUnits.parseLength(indentation.xgetLeft())) + : -1; } /** @@ -1208,8 +1190,9 @@ public class XWPFParagraph implements IBodyElement, IRunBody, ISDTContents, Para public int getIndentationRight() { CTInd indentation = getCTInd(false); - return (indentation != null && indentation.isSetRight()) ? indentation.getRight().intValue() - : -1; + return (indentation != null && indentation.isSetRight()) + ? (int)Units.toDXA(POIXMLUnits.parseLength(indentation.xgetRight())) + : -1; } /** @@ -1272,7 +1255,8 @@ public class XWPFParagraph implements IBodyElement, IRunBody, ISDTContents, Para */ public int getIndentationHanging() { CTInd indentation = getCTInd(false); - return (indentation != null && indentation.isSetHanging()) ? indentation.getHanging().intValue() : -1; + return (indentation != null && indentation.isSetHanging()) + ? (int)Units.toDXA(POIXMLUnits.parseLength(indentation.xgetHanging())) : -1; } /** @@ -1312,8 +1296,9 @@ public class XWPFParagraph implements IBodyElement, IRunBody, ISDTContents, Para */ public int getIndentationFirstLine() { CTInd indentation = getCTInd(false); - return (indentation != null && indentation.isSetFirstLine()) ? indentation.getFirstLine().intValue() - : -1; + return (indentation != null && indentation.isSetFirstLine()) + ? (int)Units.toDXA(POIXMLUnits.parseLength(indentation.xgetFirstLine())) + : -1; } /** @@ -1376,12 +1361,7 @@ public class XWPFParagraph implements IBodyElement, IRunBody, ISDTContents, Para */ @Override public boolean isWordWrapped() { - CTOnOff wordWrap = getCTPPr().isSetWordWrap() ? getCTPPr() - .getWordWrap() : null; - if (wordWrap != null) { - return isTruelike(wordWrap.getVal()); - } - return false; + return getCTPPr().isSetWordWrap() && POIXMLUnits.parseOnOff(getCTPPr().getWordWrap()); } /** @@ -1394,12 +1374,14 @@ public class XWPFParagraph implements IBodyElement, IRunBody, ISDTContents, Para */ @Override public void setWordWrapped(boolean wrap) { - CTOnOff wordWrap = getCTPPr().isSetWordWrap() ? getCTPPr() - .getWordWrap() : getCTPPr().addNewWordWrap(); + CTPPr ppr = getCTPPr(); if (wrap) { - wordWrap.setVal(STOnOff.TRUE); + CTOnOff wordWrap = ppr.isSetWordWrap() ? ppr.getWordWrap() : ppr.addNewWordWrap(); + wordWrap.setVal(STOnOff1.ON); } else { - wordWrap.unsetVal(); + if (ppr.isSetWordWrap()) { + ppr.unsetWordWrap(); + } } } diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java index bbc49aea1f..c7ba363119 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java @@ -32,10 +32,12 @@ import javax.xml.namespace.QName; import org.apache.poi.ooxml.POIXMLException; import org.apache.poi.ooxml.util.DocumentHelper; +import org.apache.poi.ooxml.util.POIXMLUnits; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.util.HexDump; import org.apache.poi.util.Internal; import org.apache.poi.util.Removal; +import org.apache.poi.util.Units; import org.apache.poi.wp.usermodel.CharacterRun; import org.apache.xmlbeans.SimpleValue; import org.apache.xmlbeans.XmlCursor; @@ -61,6 +63,9 @@ import org.openxmlformats.schemas.drawingml.x2006.picture.CTPicture; import org.openxmlformats.schemas.drawingml.x2006.picture.CTPictureNonVisual; import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTAnchor; import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTInline; +import org.openxmlformats.schemas.officeDocument.x2006.sharedTypes.STHexColorRGB; +import org.openxmlformats.schemas.officeDocument.x2006.sharedTypes.STOnOff1; +import org.openxmlformats.schemas.officeDocument.x2006.sharedTypes.STVerticalAlignRun; import org.openxmlformats.schemas.wordprocessingml.x2006.main.*; import org.w3c.dom.NodeList; import org.w3c.dom.Text; @@ -223,15 +228,7 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun { * For isBold, isItalic etc */ private static boolean isCTOnOff(CTOnOff onoff) { - if (!onoff.isSetVal()) { - return true; - } - final STOnOff.Enum val = onoff.getVal(); - return ( - (STOnOff.TRUE == val) || - (STOnOff.X_1 == val) || - (STOnOff.ON == val) - ); + return !onoff.isSetVal() || POIXMLUnits.parseOnOff(onoff); } /** @@ -297,7 +294,7 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun { public void setBold(boolean value) { CTRPr pr = getRunProperties(true); CTOnOff bold = pr.isSetB() ? pr.getB() : pr.addNewB(); - bold.setVal(value ? STOnOff.TRUE : STOnOff.FALSE); + bold.setVal(value ? STOnOff1.ON : STOnOff1.OFF); } /** @@ -408,7 +405,7 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun { public void setItalic(boolean value) { CTRPr pr = getRunProperties(true); CTOnOff italic = pr.isSetI() ? pr.getI() : pr.addNewI(); - italic.setVal(value ? STOnOff.TRUE : STOnOff.FALSE); + italic.setVal(value ? STOnOff1.ON : STOnOff1.OFF); } /** @@ -573,7 +570,7 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun { public void setStrikeThrough(boolean value) { CTRPr pr = getRunProperties(true); CTOnOff strike = pr.isSetStrike() ? pr.getStrike() : pr.addNewStrike(); - strike.setVal(value ? STOnOff.TRUE : STOnOff.FALSE); + strike.setVal(value ? STOnOff1.ON : STOnOff1.OFF); } @Deprecated @@ -608,7 +605,7 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun { public void setDoubleStrikethrough(boolean value) { CTRPr pr = getRunProperties(true); CTOnOff dstrike = pr.isSetDstrike() ? pr.getDstrike() : pr.addNewDstrike(); - dstrike.setVal(value ? STOnOff.TRUE : STOnOff.FALSE); + dstrike.setVal(value ? STOnOff1.ON : STOnOff1.OFF); } @Override @@ -621,7 +618,7 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun { public void setSmallCaps(boolean value) { CTRPr pr = getRunProperties(true); CTOnOff caps = pr.isSetSmallCaps() ? pr.getSmallCaps() : pr.addNewSmallCaps(); - caps.setVal(value ? STOnOff.TRUE : STOnOff.FALSE); + caps.setVal(value ? STOnOff1.ON : STOnOff1.OFF); } @Override @@ -634,7 +631,7 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun { public void setCapitalized(boolean value) { CTRPr pr = getRunProperties(true); CTOnOff caps = pr.isSetCaps() ? pr.getCaps() : pr.addNewCaps(); - caps.setVal(value ? STOnOff.TRUE : STOnOff.FALSE); + caps.setVal(value ? STOnOff1.ON : STOnOff1.OFF); } @Override @@ -647,7 +644,7 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun { public void setShadow(boolean value) { CTRPr pr = getRunProperties(true); CTOnOff shadow = pr.isSetShadow() ? pr.getShadow() : pr.addNewShadow(); - shadow.setVal(value ? STOnOff.TRUE : STOnOff.FALSE); + shadow.setVal(value ? STOnOff1.ON : STOnOff1.OFF); } @Override @@ -660,7 +657,7 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun { public void setImprinted(boolean value) { CTRPr pr = getRunProperties(true); CTOnOff imprinted = pr.isSetImprint() ? pr.getImprint() : pr.addNewImprint(); - imprinted.setVal(value ? STOnOff.TRUE : STOnOff.FALSE); + imprinted.setVal(value ? STOnOff1.ON : STOnOff1.OFF); } @Override @@ -673,7 +670,7 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun { public void setEmbossed(boolean value) { CTRPr pr = getRunProperties(true); CTOnOff emboss = pr.isSetEmboss() ? pr.getEmboss() : pr.addNewEmboss(); - emboss.setVal(value ? STOnOff.TRUE : STOnOff.FALSE); + emboss.setVal(value ? STOnOff1.ON : STOnOff1.OFF); } /** @@ -704,7 +701,7 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun { if (pr == null || !pr.isSetKern()) { return 0; } - return pr.getKern().getVal().intValue(); + return (int)POIXMLUnits.parseLength(pr.getKern().xgetVal()); } @Override @@ -735,7 +732,7 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun { if (pr == null || !pr.isSetSpacing()) { return 0; } - return pr.getSpacing().getVal().intValue(); + return (int)Units.toDXA(POIXMLUnits.parseLength(pr.getSpacing().xgetVal())); } @Override @@ -878,9 +875,9 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun { private BigDecimal getFontSizeAsBigDecimal(int scale) { CTRPr pr = getRunProperties(false); - return (pr != null && pr.isSetSz()) ? - new BigDecimal(pr.getSz().getVal()).divide(BigDecimal.valueOf(2)).setScale(scale, RoundingMode.HALF_UP) : - null; + return (pr != null && pr.isSetSz()) + ? BigDecimal.valueOf(Units.toPoints(POIXMLUnits.parseLength(pr.getSz().xgetVal()))).divide(BigDecimal.valueOf(4), scale, RoundingMode.HALF_UP) + : null; } /** @@ -936,7 +933,7 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun { */ public int getTextPosition() { CTRPr pr = getRunProperties(false); - return (pr != null && pr.isSetPosition()) ? pr.getPosition().getVal().intValue() + return (pr != null && pr.isSetPosition()) ? (int)(Units.toPoints(POIXMLUnits.parseLength(pr.getPosition().xgetVal())) / 2.) : -1; } @@ -1385,11 +1382,7 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun { if (ctfldChar.getFldCharType() == STFldCharType.BEGIN) { if (ctfldChar.getFfData() != null) { for (CTFFCheckBox checkBox : ctfldChar.getFfData().getCheckBoxList()) { - if (checkBox.getDefault() != null && checkBox.getDefault().getVal() == STOnOff.X_1) { - text.append("|X|"); - } else { - text.append("|_|"); - } + text.append((checkBox.getDefault() != null && POIXMLUnits.parseOnOff(checkBox.getDefault().xgetVal())) ? "|X|" : "|_|"); } } } @@ -1455,13 +1448,14 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun { * @since 4.0.0 */ public int getTextScale() { - CTRPr pr = getRunProperties(true); - CTTextScale scale = pr.isSetW() ? pr.getW() : pr.addNewW(); - int value = scale.getVal(); - if (value == 0) { - value = 100; // 100% scaling, that is, no change. See 17.3.2.43 w (Expanded/Compressed Text) + CTRPr pr = getRunProperties(false); + if (pr == null || !pr.isSetW()) { + return 100; } - return value; + + int value = POIXMLUnits.parsePercent(pr.getW().xgetVal()); + // 100% scaling, that is, no change. See 17.3.2.43 w (Expanded/Compressed Text) + return value == 0 ? 100 : value / 1000; } /** @@ -1522,7 +1516,7 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun { public void setVanish(boolean value) { CTRPr pr = getRunProperties(true); CTOnOff vanish = pr.isSetVanish() ? pr.getVanish() : pr.addNewVanish(); - vanish.setVal(value ? STOnOff.TRUE : STOnOff.FALSE); + vanish.setVal(value ? STOnOff1.ON : STOnOff1.OFF); } /** diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFSettings.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFSettings.java index 39a8444b8b..8df9e3f806 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFSettings.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFSettings.java @@ -29,19 +29,20 @@ import javax.xml.namespace.QName; import org.apache.poi.EncryptedDocumentException; import org.apache.poi.ooxml.POIXMLDocumentPart; +import org.apache.poi.ooxml.util.POIXMLUnits; import org.apache.poi.openxml4j.opc.PackagePart; import org.apache.poi.poifs.crypt.CryptoFunctions; import org.apache.poi.poifs.crypt.HashAlgorithm; import org.apache.xmlbeans.XmlOptions; +import org.openxmlformats.schemas.officeDocument.x2006.sharedTypes.STAlgClass; +import org.openxmlformats.schemas.officeDocument.x2006.sharedTypes.STAlgType; +import org.openxmlformats.schemas.officeDocument.x2006.sharedTypes.STCryptProv; +import org.openxmlformats.schemas.officeDocument.x2006.sharedTypes.STOnOff1; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDocProtect; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTOnOff; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSettings; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTZoom; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.STAlgClass; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.STAlgType; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.STCryptProv; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STDocProtect; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff; import org.openxmlformats.schemas.wordprocessingml.x2006.main.SettingsDocument; public class XWPFSettings extends POIXMLDocumentPart { @@ -54,7 +55,7 @@ public class XWPFSettings extends POIXMLDocumentPart { public XWPFSettings(PackagePart part) throws IOException { super(part); } - + public XWPFSettings() { super(); ctSettings = CTSettings.Factory.newInstance(); @@ -86,12 +87,8 @@ public class XWPFSettings extends POIXMLDocumentPart { zoom = ctSettings.getZoom(); } - - BigInteger percent = zoom.getPercent(); - if(percent == null) { - return 100; - } - return percent.longValue(); + + return (zoom.getPercent() == null) ? 100 : POIXMLUnits.parsePercent(zoom.xgetPercent()) / 1000; } /** @@ -111,7 +108,7 @@ public class XWPFSettings extends POIXMLDocumentPart { CTZoom zoom = ctSettings.getZoom(); zoom.setPercent(BigInteger.valueOf(zoomPercent)); } - + /** * Verifies the documentProtection tag inside settings.xml file <br> * if the protection is enforced (w:enforcement="1") <br> @@ -127,12 +124,8 @@ public class XWPFSettings extends POIXMLDocumentPart { */ public boolean isEnforcedWith() { CTDocProtect ctDocProtect = ctSettings.getDocumentProtection(); + return ctDocProtect != null && POIXMLUnits.parseOnOff(ctDocProtect.xgetEnforcement()); - if (ctDocProtect == null) { - return false; - } - - return ctDocProtect.getEnforcement().equals(STOnOff.X_1); } /** @@ -151,12 +144,8 @@ public class XWPFSettings extends POIXMLDocumentPart { */ public boolean isEnforcedWith(STDocProtect.Enum editValue) { CTDocProtect ctDocProtect = ctSettings.getDocumentProtection(); + return ctDocProtect != null && POIXMLUnits.parseOnOff(ctDocProtect.xgetEnforcement()) && ctDocProtect.getEdit().equals(editValue); - if (ctDocProtect == null) { - return false; - } - - return ctDocProtect.getEnforcement().equals(STOnOff.X_1) && ctDocProtect.getEdit().equals(editValue); } /** @@ -173,7 +162,7 @@ public class XWPFSettings extends POIXMLDocumentPart { * </pre> */ public void setEnforcementEditValue(org.openxmlformats.schemas.wordprocessingml.x2006.main.STDocProtect.Enum editValue) { - safeGetDocumentProtection().setEnforcement(STOnOff.X_1); + safeGetDocumentProtection().setEnforcement(STOnOff1.ON); safeGetDocumentProtection().setEdit(editValue); } @@ -196,7 +185,7 @@ public class XWPFSettings extends POIXMLDocumentPart { */ public void setEnforcementEditValue(org.openxmlformats.schemas.wordprocessingml.x2006.main.STDocProtect.Enum editValue, String password, HashAlgorithm hashAlgo) { - safeGetDocumentProtection().setEnforcement(STOnOff.X_1); + safeGetDocumentProtection().setEnforcement(STOnOff1.ON); safeGetDocumentProtection().setEdit(editValue); if (password == null) { @@ -233,7 +222,7 @@ public class XWPFSettings extends POIXMLDocumentPart { if (hashAlgo == null) { hashAlgo = HashAlgorithm.sha1; } - + switch (hashAlgo) { case md2: providerType = STCryptProv.RSA_FULL; @@ -295,7 +284,7 @@ public class XWPFSettings extends POIXMLDocumentPart { /** * Validates the existing password * - * @param password + * @param password the password * @return true, only if password was set and equals, false otherwise */ public boolean validateProtectionPassword(String password) { @@ -348,7 +337,7 @@ public class XWPFSettings extends POIXMLDocumentPart { * it sets the value of enforcement to "0" (w:enforcement="0") <br> */ public void removeEnforcement() { - safeGetDocumentProtection().setEnforcement(STOnOff.X_0); + safeGetDocumentProtection().setEnforcement(STOnOff1.OFF); } /** @@ -365,12 +354,12 @@ public class XWPFSettings extends POIXMLDocumentPart { */ public void setUpdateFields() { CTOnOff onOff = CTOnOff.Factory.newInstance(); - onOff.setVal(STOnOff.TRUE); + onOff.setVal(STOnOff1.ON); ctSettings.setUpdateFields(onOff); } boolean isUpdateFields() { - return ctSettings.isSetUpdateFields() && ctSettings.getUpdateFields().getVal() == STOnOff.TRUE; + return ctSettings.isSetUpdateFields() && POIXMLUnits.parseOnOff(ctSettings.getUpdateFields().xgetVal()); } /** @@ -443,12 +432,12 @@ public class XWPFSettings extends POIXMLDocumentPart { /** * Turn separate even-and-odd headings on or off * - * @param enable <code>true</code> to turn on separate even and odd headings, + * @param enable <code>true</code> to turn on separate even and odd headings, * <code>false</code> to turn off even and odd headings. */ public void setEvenAndOddHeadings(boolean enable) { CTOnOff onOff = CTOnOff.Factory.newInstance(); - onOff.setVal(enable ? STOnOff.TRUE : STOnOff.FALSE); + onOff.setVal(enable ? STOnOff1.ON : STOnOff1.OFF); ctSettings.setEvenAndOddHeaders(onOff); } @@ -464,12 +453,12 @@ public class XWPFSettings extends POIXMLDocumentPart { /** * Turn mirrored margins on or off * - * @param enable <code>true</code> to turn on mirrored margins, + * @param enable <code>true</code> to turn on mirrored margins, * <code>false</code> to turn off mirrored marginss. */ public void setMirrorMargins(boolean enable) { CTOnOff onOff = CTOnOff.Factory.newInstance(); - onOff.setVal(enable ? STOnOff.TRUE : STOnOff.FALSE); + onOff.setVal(enable ? STOnOff1.ON : STOnOff1.OFF); ctSettings.setMirrorMargins(onOff); } diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTable.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTable.java index 0705bbe696..b8294c771a 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTable.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTable.java @@ -26,10 +26,12 @@ import java.util.function.Consumer; import java.util.function.Function; import org.apache.poi.ooxml.POIXMLDocumentPart; +import org.apache.poi.ooxml.util.POIXMLUnits; import org.apache.poi.util.Internal; +import org.apache.poi.util.Units; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBorder; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDecimalNumber; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTJc; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTJcTable; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTString; @@ -40,7 +42,7 @@ import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJcTable; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth; /** @@ -279,7 +281,7 @@ public class XWPFTable implements IBodyElement, ISDTContents { */ public int getWidth() { CTTblPr tblPr = getTblPr(); - return tblPr.isSetTblW() ? tblPr.getTblW().getW().intValue() : -1; + return tblPr.isSetTblW() ? (int)Units.toDXA(POIXMLUnits.parseLength(tblPr.getTblW().xgetW())) : -1; } /** @@ -407,8 +409,8 @@ public class XWPFTable implements IBodyElement, ISDTContents { */ public void setTableAlignment(TableRowAlign tra) { CTTblPr tPr = getTblPr(true); - CTJc jc = tPr.isSetJc() ? tPr.getJc() : tPr.addNewJc(); - jc.setVal(STJc.Enum.forInt(tra.getValue())); + CTJcTable jc = tPr.isSetJc() ? tPr.getJc() : tPr.addNewJc(); + jc.setVal(STJcTable.Enum.forInt(tra.getValue())); } /** @@ -985,7 +987,7 @@ public class XWPFTable implements IBodyElement, ISDTContents { if (tcm != null) { CTTblWidth tw = margin.apply(tcm); if (tw != null) { - return tw.getW().intValue(); + return (int) Units.toDXA(POIXMLUnits.parseLength(tw.xgetW())); } } return 0; @@ -1154,11 +1156,11 @@ public class XWPFTable implements IBodyElement, ISDTContents { if (typeValue == STTblWidth.DXA || typeValue == STTblWidth.AUTO || typeValue == STTblWidth.NIL) { - result = 0.0 + ctWidth.getW().intValue(); + result = 0.0 + Units.toDXA(POIXMLUnits.parseLength(ctWidth.xgetW())); } else if (typeValue == STTblWidth.PCT) { // Percentage values are stored as integers that are 50 times // percentage. - result = ctWidth.getW().intValue() / 50.0; + result = Units.toDXA(POIXMLUnits.parseLength(ctWidth.xgetW())) / 50.0; } else { // Should never get here } diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java index 8490154cbf..57f7d20516 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java @@ -23,7 +23,9 @@ import java.util.HashMap; import java.util.List; import org.apache.poi.ooxml.POIXMLDocumentPart; +import org.apache.poi.ooxml.util.POIXMLUnits; import org.apache.poi.util.Internal; +import org.apache.poi.util.Units; import org.apache.xmlbeans.XmlCursor; import org.apache.xmlbeans.XmlObject; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP; @@ -528,8 +530,8 @@ public class XWPFTableCell implements IBody, ICell { * @return Width value as a double-precision decimal. * @since 4.0.0 */ - public double getWidthDecimal() { - return XWPFTable.getWidthDecimal(getTcWidth()); + public double getWidthDecimal() { + return XWPFTable.getWidthDecimal(getTcWidth()); } /** @@ -541,7 +543,7 @@ public class XWPFTableCell implements IBody, ICell { * @since 4.0.0 */ public TableWidthType getWidthType() { - return XWPFTable.getWidthType(getTcWidth()); + return XWPFTable.getWidthType(getTcWidth()); } /** @@ -551,7 +553,7 @@ public class XWPFTableCell implements IBody, ICell { * @since 4.0.0 */ public void setWidth(String widthValue) { - XWPFTable.setWidthValue(widthValue, getTcWidth()); + XWPFTable.setWidthValue(widthValue, getTcWidth()); } private CTTblWidth getTcWidth() { @@ -582,6 +584,6 @@ public class XWPFTableCell implements IBody, ICell { } public int getWidth() { - return getTcWidth().getW().intValue(); + return (int) Units.toDXA(POIXMLUnits.parseLength(getTcWidth().xgetW())); } } diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableRow.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableRow.java index 953ca6db08..c01643ec0d 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableRow.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableRow.java @@ -20,10 +20,12 @@ import java.math.BigInteger; import java.util.ArrayList; import java.util.List; +import org.apache.poi.ooxml.util.POIXMLUnits; import org.apache.poi.util.Internal; -import org.apache.poi.xwpf.model.WMLHelper; +import org.apache.poi.util.Units; import org.apache.xmlbeans.XmlCursor; import org.apache.xmlbeans.XmlObject; +import org.openxmlformats.schemas.officeDocument.x2006.sharedTypes.STOnOff1; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHeight; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTOnOff; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow; @@ -101,7 +103,7 @@ public class XWPFTableRow { */ public int getHeight() { CTTrPr properties = getTrPr(); - return properties.sizeOfTrHeightArray() == 0 ? 0 : properties.getTrHeightArray(0).getVal().intValue(); + return properties.sizeOfTrHeightArray() == 0 ? 0 : (int) Units.toDXA(POIXMLUnits.parseLength(properties.getTrHeightArray(0).xgetVal())); } /** @@ -229,7 +231,7 @@ public class XWPFTableRow { CTTrPr trpr = getTrPr(); if (trpr.sizeOfCantSplitArray() > 0) { CTOnOff onoff = trpr.getCantSplitArray(0); - isCant = (onoff.isSetVal() ? WMLHelper.convertSTOnOffToBoolean(onoff.getVal()) : true); + isCant = !onoff.isSetVal() || POIXMLUnits.parseOnOff(onoff.xgetVal()); } } return isCant; @@ -246,7 +248,7 @@ public class XWPFTableRow { public void setCantSplitRow(boolean split) { CTTrPr trpr = getTrPr(); CTOnOff onoff = (trpr.sizeOfCantSplitArray() > 0 ? trpr.getCantSplitArray(0) : trpr.addNewCantSplit()); - onoff.setVal(WMLHelper.convertBooleanToSTOnOff(split)); + onoff.setVal(split ? STOnOff1.ON : STOnOff1.OFF); } /** @@ -254,7 +256,7 @@ public class XWPFTableRow { * table split across pages. NOTE - Word will not repeat a table row unless * all preceding rows of the table are also repeated. This function returns * false if the row will not be repeated even if the repeat tag is present - * for this row. + * for this row. * * @return true if table's header row should be repeated at the top of each * page of table, false otherwise. @@ -269,14 +271,14 @@ public class XWPFTableRow { } return repeat; } - + private boolean getRepeat() { boolean repeat = false; if (ctRow.isSetTrPr()) { CTTrPr trpr = getTrPr(); if (trpr.sizeOfTblHeaderArray() > 0) { CTOnOff rpt = trpr.getTblHeaderArray(0); - repeat = (rpt.isSetVal() ? WMLHelper.convertSTOnOffToBoolean(rpt.getVal()) : true); + repeat = !rpt.isSetVal() || POIXMLUnits.parseOnOff(rpt.xgetVal()); } } return repeat; @@ -293,6 +295,6 @@ public class XWPFTableRow { public void setRepeatHeader(boolean repeat) { CTTrPr trpr = getTrPr(); CTOnOff onoff = (trpr.sizeOfTblHeaderArray() > 0 ? trpr.getTblHeaderArray(0) : trpr.addNewTblHeader()); - onoff.setVal(WMLHelper.convertBooleanToSTOnOff(repeat)); + onoff.setVal(repeat ? STOnOff1.ON : STOnOff1.OFF); } } |