From 9e88957356f84a621e214dfadd6e2ac19842774a Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Mon, 1 Aug 2022 11:35:22 +0000 Subject: [PATCH] [bug-66187] issue with XWPFRun creating run properties unnecessarily git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1903163 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/xwpf/usermodel/XWPFRun.java | 39 +++++++++++-------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFRun.java b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFRun.java index fd0d29cb59..c46229a0cf 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFRun.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFRun.java @@ -40,12 +40,7 @@ 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; -import org.apache.xmlbeans.XmlException; -import org.apache.xmlbeans.XmlObject; -import org.apache.xmlbeans.XmlString; -import org.apache.xmlbeans.XmlToken; +import org.apache.xmlbeans.*; import org.apache.xmlbeans.impl.values.XmlAnyTypeImpl; import org.openxmlformats.schemas.drawingml.x2006.chart.CTChart; import org.openxmlformats.schemas.drawingml.x2006.main.CTBlip; @@ -239,8 +234,7 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun { */ public String getLang() { CTRPr pr = getRunProperties(false); - Object lang = (pr == null || pr.sizeOfLangArray() == 0) ? null : pr.getLangArray(0).getVal(); - return (String) lang; + return (pr == null || pr.sizeOfLangArray() == 0) ? null : pr.getLangArray(0).getVal(); } /** @@ -299,7 +293,7 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun { } /** - * Get text color. The returned value is a string in the hex form "RRGGBB". + * Get text color. The returned value is a string in the hex form "RRGGBB". This can be null. */ public String getColor() { String color = null; @@ -963,10 +957,10 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun { } /** - * + * Not yet implemented. */ public void removeBreak() { - // TODO + // TODO not yet implemented } /** @@ -1506,11 +1500,14 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun { /** * Gets the highlight color for the run * - * @return {@link STHighlightColor} for the run. + * @return {@link STHighlightColor} for the run. The default is NONE; * @since 5.2.3 */ public STHighlightColor.Enum getTextHighlightColor() { - CTRPr pr = getRunProperties(true); + CTRPr pr = getRunProperties(false); + if (pr == null) { + return STHighlightColor.NONE; + } CTHighlight highlight = pr.sizeOfHighlightArray() > 0 ? pr.getHighlightArray(0) : pr.addNewHighlight(); STHighlightColor color = highlight.xgetVal(); if (color == null) { @@ -1527,7 +1524,7 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun { * @since 4.0.0 */ public boolean isVanish() { - CTRPr pr = getRunProperties(true); + CTRPr pr = getRunProperties(false); return pr != null && pr.sizeOfVanishArray() > 0 && isCTOnOff(pr.getVanishArray(0)); } @@ -1546,11 +1543,15 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun { /** * Get the vertical alignment value * - * @return {@link STVerticalAlignRun.Enum} value (see 22.9.2.17 ST_VerticalAlignRun (Vertical Positioning Location)) + * @return {@link STVerticalAlignRun.Enum} value (see 22.9.2.17 ST_VerticalAlignRun (Vertical Positioning Location)). + * The default is BASELINE. * @since 4.0.0 */ public STVerticalAlignRun.Enum getVerticalAlignment() { - CTRPr pr = getRunProperties(true); + CTRPr pr = getRunProperties(false); + if (pr == null) { + return STVerticalAlignRun.BASELINE; + } CTVerticalAlignRun vertAlign = pr.sizeOfVertAlignArray() > 0 ? pr.getVertAlignArray(0) : pr.addNewVertAlign(); STVerticalAlignRun.Enum val = vertAlign.getVal(); if (val == null) { @@ -1585,10 +1586,14 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun { * Get the emphasis mark value for the run. * * @return {@link STEm.Enum} emphasis mark type enumeration. See 17.18.24 ST_Em (Emphasis Mark Type). + * The default is NONE. * @since 4.0.0 */ public STEm.Enum getEmphasisMark() { - CTRPr pr = getRunProperties(true); + CTRPr pr = getRunProperties(false); + if (pr == null) { + return STEm.NONE; + } CTEm emphasis = pr.sizeOfEmArray() > 0 ? pr.getEmArray(0) : pr.addNewEm(); STEm.Enum val = emphasis.getVal(); -- 2.39.5