]> source.dussan.org Git - poi.git/commitdiff
Support XWPF field runs, the same way that we handle hyperlink runs
authorNick Burch <nick@apache.org>
Tue, 11 Aug 2015 20:01:26 +0000 (20:01 +0000)
committerNick Burch <nick@apache.org>
Tue, 11 Aug 2015 20:01:26 +0000 (20:01 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1695361 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFFieldRun.java [new file with mode: 0644]
src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFHyperlinkRun.java
src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java
src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFParagraph.java

diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFFieldRun.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFFieldRun.java
new file mode 100644 (file)
index 0000000..997c7d0
--- /dev/null
@@ -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.\r
+ * Any given Field may be made up of multiple of these.\r
+ */\r
+public class XWPFFieldRun extends XWPFRun {\r
+    private CTSimpleField field;\r
+\r
+    public XWPFFieldRun(CTSimpleField field, CTR run, IRunBody p) {\r
+        super(run, p);\r
+        this.field = field;\r
+    }\r
+
+    @Internal\r
+    public CTSimpleField getCTField() {
+        return field;\r
+    }\r
+\r
+    public String getFieldInstruction() {\r
+        return field.getInstr();\r
+    }\r
+\r
+    public void setFieldInstruction(String instruction) {
+        field.setInstr(instruction);\r
+    }\r
+}\r
index 4c8158a41168310619eb2760ca95c396b3b9b1cd..9a79b20f0f9808a5a5284b3cf61191e4dc5a0d25 100644 (file)
@@ -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 XWPFRun {
         super(run, p);\r
         this.hyperlink = hyperlink;\r
     }\r
-\r
+
+    @Internal\r
     public CTHyperlink getCTHyperlink() {\r
         return hyperlink;\r
     }\r
index 0eae9224797451be00ada1194970cfb3c56255a8..65c726d102df2e02c69d8bedfac6dfcd7efc61a9 100644 (file)
@@ -142,13 +142,21 @@ public class XWPFParagraph implements IBodyElement, IRunBody, ISDTContents, Para
                 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);
@@ -164,13 +172,6 @@ public class XWPFParagraph implements IBodyElement, IRunBody, ISDTContents, Para
                     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) {
                 // Smart Tags can be nested many times.
                 // This implementation does not preserve the tagging information
index 31be821e067d6c8834cb62ef4b2d73762fcba11c..f53d524b01ebaee10bf56a3b1dfb5da075d80a21 100644 (file)
@@ -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 extends TestCase {
 
         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() {