You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ni...@apache.org on 2014/02/01 22:52:19 UTC

svn commit: r1563486 - in /poi/trunk/src/scratchpad: src/org/apache/poi/hwpf/usermodel/CharacterRun.java src/org/apache/poi/hwpf/usermodel/Paragraph.java testcases/org/apache/poi/hwpf/usermodel/TestRangeProperties.java

Author: nick
Date: Sat Feb  1 21:52:19 2014
New Revision: 1563486

URL: http://svn.apache.org/r1563486
Log:
Expose the StyleIndex of CharacterRuns, and expand the range style tests to cover this + related

Modified:
    poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/usermodel/CharacterRun.java
    poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Paragraph.java
    poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestRangeProperties.java

Modified: poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/usermodel/CharacterRun.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/usermodel/CharacterRun.java?rev=1563486&r1=1563485&r2=1563486&view=diff
==============================================================================
--- poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/usermodel/CharacterRun.java (original)
+++ poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/usermodel/CharacterRun.java Sat Feb  1 21:52:19 2014
@@ -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; 

Modified: poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Paragraph.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Paragraph.java?rev=1563486&r1=1563485&r2=1563486&view=diff
==============================================================================
--- poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Paragraph.java (original)
+++ poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Paragraph.java Sat Feb  1 21:52:19 2014
@@ -204,9 +204,15 @@ public class Paragraph extends Range imp
         _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

Modified: poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestRangeProperties.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestRangeProperties.java?rev=1563486&r1=1563485&r2=1563486&view=diff
==============================================================================
--- poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestRangeProperties.java (original)
+++ poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestRangeProperties.java Sat Feb  1 21:52:19 2014
@@ -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 e
 		
 		// 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 e
 
 		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 e
 
 		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 e
 				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());
       }
 	}
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org