poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From n...@apache.org
Subject svn commit: r1695361 - in /poi/trunk/src/ooxml: java/org/apache/poi/xwpf/usermodel/ testcases/org/apache/poi/xwpf/usermodel/
Date Tue, 11 Aug 2015 20:01:27 GMT
Author: nick
Date: Tue Aug 11 20:01:26 2015
New Revision: 1695361

URL: http://svn.apache.org/r1695361
Log:
Support XWPF field runs, the same way that we handle hyperlink runs

Added:
    poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFFieldRun.java
Modified:
    poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFHyperlinkRun.java
    poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java
    poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFParagraph.java

Added: poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFFieldRun.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFFieldRun.java?rev=1695361&view=auto
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFFieldRun.java (added)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFFieldRun.java Tue Aug 11 20:01:26
2015
@@ -0,0 +1,48 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+package org.apache.poi.xwpf.usermodel;
+
+import org.apache.poi.util.Internal;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSimpleField;
+
+/**
+ * A run of text which is part of a field, such as Title
+ *  of Page number or Author.
+ * Any given Field may be made up of multiple of these.
+ */
+public class XWPFFieldRun extends XWPFRun {
+    private CTSimpleField field;
+
+    public XWPFFieldRun(CTSimpleField field, CTR run, IRunBody p) {
+        super(run, p);
+        this.field = field;
+    }
+
+    @Internal
+    public CTSimpleField getCTField() {
+        return field;
+    }
+
+    public String getFieldInstruction() {
+        return field.getInstr();
+    }
+
+    public void setFieldInstruction(String instruction) {
+        field.setInstr(instruction);
+    }
+}

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFHyperlinkRun.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFHyperlinkRun.java?rev=1695361&r1=1695360&r2=1695361&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFHyperlinkRun.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFHyperlinkRun.java Tue Aug 11
20:01:26 2015
@@ -16,6 +16,7 @@
 ==================================================================== */
 package org.apache.poi.xwpf.usermodel;
 
+import org.apache.poi.util.Internal;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHyperlink;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
 
@@ -30,7 +31,8 @@ public class XWPFHyperlinkRun extends XW
         super(run, p);
         this.hyperlink = hyperlink;
     }
-
+
+    @Internal
     public CTHyperlink getCTHyperlink() {
         return hyperlink;
     }

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java?rev=1695361&r1=1695360&r2=1695361&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java Tue Aug 11 20:01:26
2015
@@ -142,13 +142,21 @@ public class XWPFParagraph implements IB
                 iruns.add(r);
             }
             if (o instanceof CTHyperlink) {
-                CTHyperlink link = (CTHyperlink) o;
+                CTHyperlink link = (CTHyperlink)o;
                 for (CTR r : link.getRArray()) {
                     XWPFHyperlinkRun hr = new XWPFHyperlinkRun(link, r, this);
                     runs.add(hr);
                     iruns.add(hr);
                 }
             }
+            if (o instanceof CTSimpleField) {
+                CTSimpleField field = (CTSimpleField)o;
+                for (CTR r : field.getRArray()) {
+                    XWPFFieldRun fr = new XWPFFieldRun(field, r, this);
+                    runs.add(fr);
+                    iruns.add(fr);
+                }
+            }
             if (o instanceof CTSdtBlock) {
                 XWPFSDT cc = new XWPFSDT((CTSdtBlock) o, part);
                 iruns.add(cc);
@@ -162,13 +170,6 @@ public class XWPFParagraph implements IB
                     XWPFRun cr = new XWPFRun(r, this);
                     runs.add(cr);
                     iruns.add(cr);
-                }
-            }
-            if (o instanceof CTSimpleField) {
-                for (CTR r : ((CTSimpleField) o).getRArray()) {
-                    XWPFRun cr = new XWPFRun(r, this);
-                    runs.add(cr);
-                    iruns.add(cr);
                 }
             }
             if (o instanceof CTSmartTagRun) {

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFParagraph.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFParagraph.java?rev=1695361&r1=1695360&r2=1695361&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFParagraph.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFParagraph.java Tue
Aug 11 20:01:26 2015
@@ -22,6 +22,7 @@ import java.math.BigInteger;
 import java.util.List;
 
 import junit.framework.TestCase;
+
 import org.apache.poi.xwpf.XWPFTestDataSamples;
 import org.openxmlformats.schemas.drawingml.x2006.picture.CTPicture;
 import org.openxmlformats.schemas.drawingml.x2006.picture.PicDocument;
@@ -495,6 +496,24 @@ public final class TestXWPFParagraph ext
 
         assertTrue(p.removeRun(0));
     }
+    
+    public void testFieldRuns() throws Exception {
+        XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("FldSimple.docx");
+        List<XWPFParagraph> ps = doc.getParagraphs();
+        assertEquals(1, ps.size());
+        
+        XWPFParagraph p = ps.get(0);
+        assertEquals(1, p.getRuns().size());
+        assertEquals(1, p.getIRuns().size());
+        
+        XWPFRun r = p.getRuns().get(0);
+        assertEquals(XWPFFieldRun.class, r.getClass());
+        
+        XWPFFieldRun fr = (XWPFFieldRun)r;
+        assertEquals(" FILENAME   \\* MERGEFORMAT ", fr.getFieldInstruction());
+        assertEquals("FldSimple.docx", fr.text());
+        assertEquals("FldSimple.docx", p.getText());
+    }
 
     @SuppressWarnings("deprecation")
     public void testRuns() {



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


Mime
View raw message