--- /dev/null
+/* ====================================================================
+ 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
==================================================================== */
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;
super(run, p);\r
this.hyperlink = hyperlink;\r
}\r
-\r
+
+ @Internal\r
public CTHyperlink getCTHyperlink() {\r
return hyperlink;\r
}\r
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);
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
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;
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() {