From 172593de22b707d7d8610913ec5a8c8f5c1b8664 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Thu, 5 Nov 2015 15:15:36 +0000 Subject: [PATCH] Add a XWPFRun equivalent of isHighlighted, and add to the common WP interface git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1712793 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/wp/usermodel/CharacterRun.java | 26 +++++++++++++------ .../apache/poi/xwpf/usermodel/XWPFRun.java | 14 +++++++++- .../poi/xwpf/usermodel/TestXWPFRun.java | 15 +++++++++++ .../poi/hwpf/usermodel/CharacterRun.java | 1 + 4 files changed, 47 insertions(+), 9 deletions(-) diff --git a/src/java/org/apache/poi/wp/usermodel/CharacterRun.java b/src/java/org/apache/poi/wp/usermodel/CharacterRun.java index da2a3630b..549ef6779 100644 --- a/src/java/org/apache/poi/wp/usermodel/CharacterRun.java +++ b/src/java/org/apache/poi/wp/usermodel/CharacterRun.java @@ -55,7 +55,25 @@ public interface CharacterRun { int getKerning(); void setKerning(int kern); + + boolean isHighlighted(); + + // HWPF has colour indexes, XWPF has a highlight enum with the colours in +// byte getHighlightedColor(); +// void setHighlighted(byte color); + + // HWPF has colour indexes, XWPF colour names +// int getColor(); +// void setColor(int color); + + /** + * Gets the fonts which shall be used to display the text contents of + * this run. Specifies a font which shall be used to format all "normal" + * characters in the run + * + * @return a string representing the font + */ String getFontName(); /** @@ -71,14 +89,6 @@ public interface CharacterRun { // short getSubSuperScriptIndex(); // void setSubSuperScriptIndex(short iss); - // HWPF uses indexes, XWPF special vertical alignments -// int getVerticalOffset(); -// void setVerticalOffset(int hpsPos); - - // HWPF has colour indexes, XWPF colour names -// int getColor(); -// void setColor(int color); - // TODO Review these, and add to XWPFRun if possible /* boolean isFldVanished(); 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 5fb16060c..860aff25e 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java @@ -73,11 +73,12 @@ import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTVerticalAlignRun import org.openxmlformats.schemas.wordprocessingml.x2006.main.STBrClear; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STBrType; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STFldCharType; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.STHighlightColor; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STUnderline; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalAlignRun; import org.w3c.dom.NodeList; -import org.w3c.dom.Text; +import org.w3c.dom.Text; /** * XWPFRun object defines a region of text with a common set of properties @@ -623,6 +624,17 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun { kernmes.setVal(BigInteger.valueOf(kern)); } + public boolean isHighlighted() { + CTRPr pr = run.getRPr(); + if (pr == null || !pr.isSetHighlight()) + return false; + if (pr.getHighlight().getVal() == STHighlightColor.NONE) + return false; + return true; + } + // TODO Provide a wrapper round STHighlightColor, then expose getter/setter + // for the highlight colour. Ideally also then add to CharacterRun interface + public int getCharacterSpacing() { CTRPr pr = run.getRPr(); if (pr == null || !pr.isSetSpacing()) diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java index a447098ef..2b1340fc7 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java @@ -23,12 +23,14 @@ import java.util.Iterator; import java.util.List; import junit.framework.TestCase; + import org.apache.poi.xwpf.XWPFTestDataSamples; import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBr; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STBrClear; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.STHighlightColor; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STUnderline; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalAlignRun; @@ -382,6 +384,19 @@ public class TestXWPFRun extends TestCase { assertEquals(1, count); } + + public void testSetGetHighlight() throws Exception { + XWPFRun run = p.createRun(); + assertEquals(false, run.isHighlighted()); + + // TODO Do this using XWPFRun methods + run.getCTR().addNewRPr().addNewHighlight().setVal(STHighlightColor.NONE); + assertEquals(false, run.isHighlighted()); + run.getCTR().getRPr().getHighlight().setVal(STHighlightColor.CYAN); + assertEquals(true, run.isHighlighted()); + run.getCTR().getRPr().getHighlight().setVal(STHighlightColor.NONE); + assertEquals(false, run.isHighlighted()); + } public void testAddPicture() throws Exception { XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("TestDocument.docx"); diff --git a/src/scratchpad/src/org/apache/poi/hwpf/usermodel/CharacterRun.java b/src/scratchpad/src/org/apache/poi/hwpf/usermodel/CharacterRun.java index 4181c197f..9fc0c7807 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/usermodel/CharacterRun.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/usermodel/CharacterRun.java @@ -106,6 +106,7 @@ public final class CharacterRun extends Range * * @return TYPE_CHARACTER */ + @SuppressWarnings("deprecation") public int type() { return TYPE_CHARACTER;