poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From n...@apache.org
Subject svn commit: r996976 - in /poi/trunk: src/documentation/content/xdocs/ src/ooxml/java/org/apache/poi/xwpf/usermodel/ src/ooxml/testcases/org/apache/poi/xwpf/extractor/ src/ooxml/testcases/org/apache/poi/xwpf/usermodel/ test-data/document/
Date Tue, 14 Sep 2010 16:32:02 GMT
Author: nick
Date: Tue Sep 14 16:32:02 2010
New Revision: 996976

URL: http://svn.apache.org/viewvc?rev=996976&view=rev
Log:
Correct XWPFRun detection of bold/italic in a paragraph with multiple runs of different styles

Modified:
    poi/trunk/src/documentation/content/xdocs/status.xml
    poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java
    poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java
    poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/extractor/TestXWPFWordExtractor.java
    poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java
    poi/trunk/test-data/document/TestDocument.docx

Modified: poi/trunk/src/documentation/content/xdocs/status.xml
URL: http://svn.apache.org/viewvc/poi/trunk/src/documentation/content/xdocs/status.xml?rev=996976&r1=996975&r2=996976&view=diff
==============================================================================
--- poi/trunk/src/documentation/content/xdocs/status.xml (original)
+++ poi/trunk/src/documentation/content/xdocs/status.xml Tue Sep 14 16:32:02 2010
@@ -34,6 +34,7 @@
 
     <changes>
         <release version="3.7-beta3" date="2010-??-??">
+           <action dev="poi-developers" type="fix">Correct XWPFRun detection of bold/italic
in a paragraph with multiple runs of different styles</action>
            <action dev="poi-developers" type="add">Link XWPFPicture to XWPFRun, so
that embedded pictures can be access from where they live in the text stream</action>
            <action dev="poi-developers" type="fix">Improve handling of Hyperlinks inside
XWPFParagraph objects through XWPFHyperlinkRun</action>
            <action dev="poi-developers" type="fix">Make XWPFParagraph make more use
of XWPFRun, and less on internal StringBuffers</action>

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java?rev=996976&r1=996975&r2=996976&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java Tue Sep 14 16:32:02
2010
@@ -1161,7 +1161,7 @@ public class XWPFDocument extends POIXML
 	 * @see org.apache.poi.xwpf.usermodel.IBody#getParagraphArray(int)
 	 */
 	public XWPFParagraph getParagraphArray(int pos) {
-		if(pos > 0 && pos < paragraphs.size()){		
+		if(pos >= 0 && pos < paragraphs.size()){		
 			return paragraphs.get(pos);
 		}
 		return null;

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java?rev=996976&r1=996975&r2=996976&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java Tue Sep 14 16:32:02
2010
@@ -118,6 +118,19 @@ public class XWPFRun {
     public XWPFParagraph getParagraph() {
         return paragraph;
     }
+    
+    /**
+     * For isBold, isItalic etc
+     */
+    private boolean isCTOnOff(CTOnOff onoff) {
+       if(! onoff.isSetVal())
+          return true;
+       if(onoff.getVal() == STOnOff.ON)
+          return true;
+       if(onoff.getVal() == STOnOff.TRUE)
+          return true;
+       return false;
+    }
 
     /**
      * Whether the bold property shall be applied to all non-complex script
@@ -127,7 +140,9 @@ public class XWPFRun {
      */
     public boolean isBold() {
         CTRPr pr = run.getRPr();
-        return pr != null && pr.isSetB();
+        if(pr == null || !pr.isSetB())
+           return false;
+        return isCTOnOff(pr.getB());
     }
 
     /**
@@ -208,7 +223,9 @@ public class XWPFRun {
      */
     public boolean isItalic() {
         CTRPr pr = run.getRPr();
-        return pr != null && pr.isSetI();
+        if(pr == null || !pr.isSetI())
+           return false;
+        return isCTOnOff(pr.getI());
     }
 
     /**
@@ -284,7 +301,9 @@ public class XWPFRun {
      */
     public boolean isStrike() {
         CTRPr pr = run.getRPr();
-        return pr != null && pr.isSetStrike();
+        if(pr == null || !pr.isSetStrike())
+           return false;
+        return isCTOnOff(pr.getStrike());
     }
 
     /**

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/extractor/TestXWPFWordExtractor.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/extractor/TestXWPFWordExtractor.java?rev=996976&r1=996975&r2=996976&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/extractor/TestXWPFWordExtractor.java
(original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/extractor/TestXWPFWordExtractor.java
Tue Sep 14 16:32:02 2010
@@ -98,16 +98,20 @@ public class TestXWPFWordExtractor exten
         // Now check contents
         extractor.setFetchHyperlinks(false);
         assertEquals(
-				"This is a test document\nThis bit is in bold and italic\n" +
-				"Back to normal\nWe have a hyperlink here, and another.\n",
+				"This is a test document.\nThis bit is in bold and italic\n" +
+				"Back to normal\n" +
+				"This contains BOLD, ITALIC and BOTH, as well as RED and YELLOW text.\n" +
+				"We have a hyperlink here, and another.\n",
                 extractor.getText()
         );
 
         // One hyperlink is a real one, one is just to the top of page
         extractor.setFetchHyperlinks(true);
         assertEquals(
-				"This is a test document\nThis bit is in bold and italic\n" +
-				"Back to normal\nWe have a hyperlink <http://poi.apache.org/> here, and another.\n",
+				"This is a test document.\nThis bit is in bold and italic\n" +
+				"Back to normal\n" +
+				"This contains BOLD, ITALIC and BOTH, as well as RED and YELLOW text.\n" +
+				"We have a hyperlink <http://poi.apache.org/> here, and another.\n",
                 extractor.getText()
         );
     }

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java?rev=996976&r1=996975&r2=996976&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java Tue Sep 14
16:32:02 2010
@@ -20,6 +20,7 @@ import java.math.BigInteger;
 
 import junit.framework.TestCase;
 
+import org.apache.poi.xwpf.XWPFTestDataSamples;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBr;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr;
@@ -190,7 +191,139 @@ public class TestXWPFRun extends TestCas
         run.addBreak(BreakType.TEXT_WRAPPING);
         assertEquals(2, run.getCTR().sizeOfBrArray());
     }
-    
 
+    /**
+     * Test that on an existing document, we do the
+     *  right thing with it
+     */
+    public void testExisting() {
+       XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("TestDocument.docx");
+       XWPFParagraph p;
+       XWPFRun run;
+       
+       
+       // First paragraph is simple
+       p = doc.getParagraphArray(0);
+       assertEquals("This is a test document.", p.getText());
+       assertEquals(2, p.getRuns().size());
+       
+       run = p.getRuns().get(0);
+       assertEquals("This is a test document", run.toString());
+       assertEquals(false, run.isBold());
+       assertEquals(false, run.isItalic());
+       assertEquals(false, run.isStrike());
+       assertEquals(null, run.getCTR().getRPr());
+       
+       run = p.getRuns().get(1);
+       assertEquals(".", run.toString());
+       assertEquals(false, run.isBold());
+       assertEquals(false, run.isItalic());
+       assertEquals(false, run.isStrike());
+       assertEquals(null, run.getCTR().getRPr());
+       
+       
+       // Next paragraph is all in one style, but a different one
+       p = doc.getParagraphArray(1);
+       assertEquals("This bit is in bold and italic", p.getText());
+       assertEquals(1, p.getRuns().size());
+       
+       run = p.getRuns().get(0);
+       assertEquals("This bit is in bold and italic", run.toString());
+       assertEquals(true, run.isBold());
+       assertEquals(true, run.isItalic());
+       assertEquals(false, run.isStrike());
+       assertEquals(true, run.getCTR().getRPr().isSetB());
+       assertEquals(false, run.getCTR().getRPr().getB().isSetVal());
+       
+       
+       // Back to normal
+       p = doc.getParagraphArray(2);
+       assertEquals("Back to normal", p.getText());
+       assertEquals(1, p.getRuns().size());
+       
+       run = p.getRuns().get(0);
+       assertEquals("Back to normal", run.toString());
+       assertEquals(false, run.isBold());
+       assertEquals(false, run.isItalic());
+       assertEquals(false, run.isStrike());
+       assertEquals(null, run.getCTR().getRPr());
+       
+       
+       // Different styles in one paragraph
+       p = doc.getParagraphArray(3);
+       assertEquals("This contains BOLD, ITALIC and BOTH, as well as RED and YELLOW text.",
p.getText());
+       assertEquals(11, p.getRuns().size());
+       
+       run = p.getRuns().get(0);
+       assertEquals("This contains ", run.toString());
+       assertEquals(false, run.isBold());
+       assertEquals(false, run.isItalic());
+       assertEquals(false, run.isStrike());
+       assertEquals(null, run.getCTR().getRPr());
+       
+       run = p.getRuns().get(1);
+       assertEquals("BOLD", run.toString());
+       assertEquals(true, run.isBold());
+       assertEquals(false, run.isItalic());
+       assertEquals(false, run.isStrike());
+       
+       run = p.getRuns().get(2);
+       assertEquals(", ", run.toString());
+       assertEquals(false, run.isBold());
+       assertEquals(false, run.isItalic());
+       assertEquals(false, run.isStrike());
+       assertEquals(null, run.getCTR().getRPr());
+       
+       run = p.getRuns().get(3);
+       assertEquals("ITALIC", run.toString());
+       assertEquals(false, run.isBold());
+       assertEquals(true, run.isItalic());
+       assertEquals(false, run.isStrike());
+       
+       run = p.getRuns().get(4);
+       assertEquals(" and ", run.toString());
+       assertEquals(false, run.isBold());
+       assertEquals(false, run.isItalic());
+       assertEquals(false, run.isStrike());
+       assertEquals(null, run.getCTR().getRPr());
+       
+       run = p.getRuns().get(5);
+       assertEquals("BOTH", run.toString());
+       assertEquals(true, run.isBold());
+       assertEquals(true, run.isItalic());
+       assertEquals(false, run.isStrike());
+       
+       run = p.getRuns().get(6);
+       assertEquals(", as well as ", run.toString());
+       assertEquals(false, run.isBold());
+       assertEquals(false, run.isItalic());
+       assertEquals(false, run.isStrike());
+       assertEquals(null, run.getCTR().getRPr());
+       
+       run = p.getRuns().get(7);
+       assertEquals("RED", run.toString());
+       assertEquals(false, run.isBold());
+       assertEquals(false, run.isItalic());
+       assertEquals(false, run.isStrike());
+       
+       run = p.getRuns().get(8);
+       assertEquals(" and ", run.toString());
+       assertEquals(false, run.isBold());
+       assertEquals(false, run.isItalic());
+       assertEquals(false, run.isStrike());
+       assertEquals(null, run.getCTR().getRPr());
+       
+       run = p.getRuns().get(9);
+       assertEquals("YELLOW", run.toString());
+       assertEquals(false, run.isBold());
+       assertEquals(false, run.isItalic());
+       assertEquals(false, run.isStrike());
+       
+       run = p.getRuns().get(10);
+       assertEquals(" text.", run.toString());
+       assertEquals(false, run.isBold());
+       assertEquals(false, run.isItalic());
+       assertEquals(false, run.isStrike());
+       assertEquals(null, run.getCTR().getRPr());
+    }
 }
-

Modified: poi/trunk/test-data/document/TestDocument.docx
URL: http://svn.apache.org/viewvc/poi/trunk/test-data/document/TestDocument.docx?rev=996976&r1=996975&r2=996976&view=diff
==============================================================================
Binary files - no diff available.



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


Mime
View raw message