aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorNick Burch <nick@apache.org>2014-02-01 21:52:19 +0000
committerNick Burch <nick@apache.org>2014-02-01 21:52:19 +0000
commit2784b5044ae1f7de14a79456869456030b615401 (patch)
tree877e44bb4697dc7192525117c0840357223def18 /src
parente484fd75d01772e90fd1fdf6e7f6412e144da261 (diff)
downloadpoi-2784b5044ae1f7de14a79456869456030b615401.tar.gz
poi-2784b5044ae1f7de14a79456869456030b615401.zip
Expose the StyleIndex of CharacterRuns, and expand the range style tests to cover this + related
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1563486 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src')
-rw-r--r--src/scratchpad/src/org/apache/poi/hwpf/usermodel/CharacterRun.java20
-rw-r--r--src/scratchpad/src/org/apache/poi/hwpf/usermodel/Paragraph.java10
-rw-r--r--src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestRangeProperties.java39
3 files changed, 59 insertions, 10 deletions
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 97875ee248..41c0b8ad68 100644
--- a/src/scratchpad/src/org/apache/poi/hwpf/usermodel/CharacterRun.java
+++ b/src/scratchpad/src/org/apache/poi/hwpf/usermodel/CharacterRun.java
@@ -85,8 +85,9 @@ public final class CharacterRun
public final static short SPRM_FELID = 0x486E;
public final static short SPRM_IDCTHINT = 0x286F;
- SprmBuffer _chpx;
- CharacterProperties _props;
+ protected short _istd;
+ protected SprmBuffer _chpx;
+ protected CharacterProperties _props;
/**
*
@@ -100,6 +101,7 @@ public final class CharacterRun
super(Math.max(parent._start, chpx.getStart()), Math.min(parent._end, chpx.getEnd()), parent);
_props = chpx.getCharacterProperties(ss, istd);
_chpx = chpx.getSprmBuf();
+ _istd = istd;
}
/**
@@ -630,6 +632,20 @@ public final class CharacterRun
return _props.getLidDefault();
}
+ /**
+ * Returns the index of the base style which applies to
+ * this Run. Details of the style can be looked up
+ * from the {@link StyleSheet}, via
+ * {@link StyleSheet#getStyleDescription(int)}.
+ * Note that runs typically override some of the style
+ * properties from the base, so normally style information
+ * should be fetched directly from the {@link CharacterRun}
+ * itself.
+ */
+ public short getStyleIndex() {
+ return _istd;
+ }
+
public String toString() {
String text = text();
return "CharacterRun of " + text.length() + " characters - " + text;
diff --git a/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Paragraph.java b/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Paragraph.java
index 65f907691b..d292396d15 100644
--- a/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Paragraph.java
+++ b/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Paragraph.java
@@ -204,9 +204,15 @@ public class Paragraph extends Range implements Cloneable
_istd = papx.getIstd();
}
-public short getStyleIndex()
+ /**
+ * Returns the index of the style which applies to this
+ * Paragraph. Details of the style can be looked up
+ * from the {@link StyleSheet}, via
+ * {@link StyleSheet#getStyleDescription(int)}
+ */
+ public short getStyleIndex()
{
- return _istd;
+ return _istd;
}
@Deprecated
diff --git a/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestRangeProperties.java b/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestRangeProperties.java
index facdbee89f..e1dfd0386e 100644
--- a/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestRangeProperties.java
+++ b/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestRangeProperties.java
@@ -20,9 +20,11 @@ package org.apache.poi.hwpf.usermodel;
import java.util.List;
import junit.framework.TestCase;
+
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.HWPFTestDataSamples;
import org.apache.poi.hwpf.model.PAPX;
+import org.apache.poi.hwpf.model.StyleSheet;
/**
* Tests to ensure that our ranges end up with
@@ -145,10 +147,20 @@ public final class TestRangeProperties extends TestCase {
// Ensure none of the paragraphs refer to one that isn't there,
// and none of their character runs either
+ // Also check all use the default style
+ StyleSheet ss = a.getStyleSheet();
for(int i=0; i<a.getRange().numParagraphs(); i++) {
Paragraph p = a.getRange().getParagraph(i);
- assertTrue(p.getStyleIndex() < 15);
+ int styleIndex = p.getStyleIndex();
+ assertTrue(styleIndex < 15);
+ assertEquals("Normal", ss.getStyleDescription(styleIndex).getName());
}
+ for(int i=0; i<a.getRange().numCharacterRuns(); i++) {
+ CharacterRun c = a.getRange().getCharacterRun(i);
+ int styleIndex = c.getStyleIndex();
+ assertTrue(styleIndex < 15);
+ assertEquals("Normal", ss.getStyleDescription(styleIndex).getName());
+ }
}
/**
@@ -289,6 +301,7 @@ public final class TestRangeProperties extends TestCase {
Paragraph p1 = r.getParagraph(0);
Paragraph p7 = r.getParagraph(6);
+ StyleSheet ss = r._doc.getStyleSheet();
// Line ending in its own run each time!
assertEquals(2, p1.numCharacterRuns());
@@ -310,6 +323,12 @@ public final class TestRangeProperties extends TestCase {
assertEquals("Times New Roman", c7b.getFontName());
assertEquals(48, c7b.getFontSize());
+
+ // All use the default base style
+ assertEquals("Normal", ss.getStyleDescription(c1a.getStyleIndex()).getName());
+ assertEquals("Normal", ss.getStyleDescription(c1b.getStyleIndex()).getName());
+ assertEquals("Heading 1", ss.getStyleDescription(c7a.getStyleIndex()).getName());
+ assertEquals("Heading 1", ss.getStyleDescription(c7b.getStyleIndex()).getName());
// Now check where they crop up
assertEquals(
@@ -371,14 +390,22 @@ public final class TestRangeProperties extends TestCase {
c7b.getEndOffset()
);
- // This document has 15 styles
- assertEquals(15, a.getStyleSheet().numStyles());
+ // This document also has 22 styles
+ assertEquals(22, ss.numStyles());
// Ensure none of the paragraphs refer to one that isn't there,
// and none of their character runs either
- for(int i=0; i<a.getRange().numParagraphs(); i++) {
- Paragraph p = a.getRange().getParagraph(i);
- assertTrue(p.getStyleIndex() < 15);
+ for(int i=0; i<r.numParagraphs(); i++) {
+ Paragraph p = r.getParagraph(i);
+ int styleIndex = p.getStyleIndex();
+ assertTrue(styleIndex < 22);
+ assertNotNull(ss.getStyleDescription(styleIndex).getName());
+ }
+ for(int i=0; i<r.numCharacterRuns(); i++) {
+ CharacterRun c = r.getCharacterRun(i);
+ int styleIndex = c.getStyleIndex();
+ assertTrue(styleIndex < 22);
+ assertNotNull(ss.getStyleDescription(styleIndex).getName());
}
}
}