diff options
author | Tim Allison <tallison@apache.org> | 2015-05-05 01:39:16 +0000 |
---|---|---|
committer | Tim Allison <tallison@apache.org> | 2015-05-05 01:39:16 +0000 |
commit | c233d8db798fdc756d1091f3132e9ccb147bd28c (patch) | |
tree | 9579cff7c4910d4e60aa43440f681eb8d2373e4b /src/ooxml/java/org/apache/poi/xwpf | |
parent | 4d0473912310532228099e83c41cba6e0577e5f4 (diff) | |
download | poi-c233d8db798fdc756d1091f3132e9ccb147bd28c.tar.gz poi-c233d8db798fdc756d1091f3132e9ccb147bd28c.zip |
POI-57889 prevent NPE with on some documents with XWPFParagraph's getNumFmt() and add some other classes to enable calculation of paragraph numbers
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1677723 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/ooxml/java/org/apache/poi/xwpf')
-rw-r--r-- | src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java | 90 |
1 files changed, 88 insertions, 2 deletions
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 c98143a5ae..bc0ddf85a0 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java @@ -20,7 +20,6 @@ import java.math.BigInteger; import java.util.ArrayList; import java.util.Collections; import java.util.List; - import org.apache.poi.POIXMLDocumentPart; import org.apache.poi.util.Internal; import org.apache.poi.wp.usermodel.Paragraph; @@ -28,11 +27,14 @@ import org.apache.xmlbeans.XmlCursor; import org.apache.xmlbeans.XmlObject; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTAbstractNum; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBorder; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDecimalNumber; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFtnEdnRef; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHyperlink; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTInd; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTJc; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTLvl; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTNum; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTNumLvl; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTOnOff; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPBdr; @@ -284,7 +286,8 @@ public class XWPFParagraph implements IBodyElement, IRunBody, ISDTContents, Para break; } } - if(level != null) + if(level != null && level.getNumFmt() != null + && level.getNumFmt().getVal() != null) return level.getNumFmt().getVal().toString(); } } @@ -292,6 +295,89 @@ public class XWPFParagraph implements IBodyElement, IRunBody, ISDTContents, Para } /** + * Returns the text that should be used around the paragraph level numbers. + * + * @return a string (e.g. "%1.") or null if the value is not found. + */ + public String getNumLevelText() { + BigInteger numID = getNumID(); + XWPFNumbering numbering = document.getNumbering(); + if(numID != null && numbering != null) { + XWPFNum num = numbering.getNum(numID); + if(num != null) { + BigInteger ilvl = getNumIlvl(); + CTNum ctNum = num.getCTNum(); + if (ctNum == null) + return null; + + CTDecimalNumber ctDecimalNumber = ctNum.getAbstractNumId(); + if (ctDecimalNumber == null) + return null; + + BigInteger abstractNumId = ctDecimalNumber.getVal(); + if (abstractNumId == null) + return null; + + XWPFAbstractNum xwpfAbstractNum = numbering.getAbstractNum(abstractNumId); + + if (xwpfAbstractNum == null) + return null; + + CTAbstractNum anum = xwpfAbstractNum.getCTAbstractNum(); + + if (anum == null) + return null; + + CTLvl level = null; + for(int i = 0; i < anum.sizeOfLvlArray(); i++) { + CTLvl lvl = anum.getLvlArray(i); + if(lvl != null && lvl.getIlvl() != null && lvl.getIlvl().equals(ilvl)) { + level = lvl; + break; + } + } + if(level != null && level.getLvlText() != null + && level.getLvlText().getVal() != null) + return level.getLvlText().getVal().toString(); + } + } + return null; + } + + + /** + * Gets the numstartOverride for the paragraph numbering for this paragraph. + * @return returns the overridden start number or null if there is no override for this paragraph. + */ + public BigInteger getNumStartOverride() { + BigInteger numID = getNumID(); + XWPFNumbering numbering = document.getNumbering(); + if(numID != null && numbering != null) { + XWPFNum num = numbering.getNum(numID); + + if(num != null) { + CTNum ctNum = num.getCTNum(); + if (ctNum == null) { + return null; + } + BigInteger ilvl = getNumIlvl(); + CTNumLvl level = null; + for(int i = 0; i < ctNum.sizeOfLvlOverrideArray(); i++) { + CTNumLvl ctNumLvl = ctNum.getLvlOverrideArray(i); + if(ctNumLvl != null && ctNumLvl.getIlvl() != null && + ctNumLvl.getIlvl().equals(ilvl)) { + level = ctNumLvl; + break; + } + } + if(level != null && level.getStartOverride() != null) { + return level.getStartOverride().getVal(); + } + } + } + return null; + } + /** * setNumID of Paragraph * @param numPos */ |