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
This commit is contained in:
Nick Burch 2015-11-05 15:15:36 +00:00
parent 5636e64d68
commit 172593de22
4 changed files with 47 additions and 9 deletions

View File

@ -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();

View File

@ -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())

View File

@ -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");

View File

@ -106,6 +106,7 @@ public final class CharacterRun extends Range
*
* @return TYPE_CHARACTER
*/
@SuppressWarnings("deprecation")
public int type()
{
return TYPE_CHARACTER;