Support XWPF field runs, the same way that we handle hyperlink runs

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1695361 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2015-08-11 20:01:26 +00:00
parent 574cd922f4
commit 3aa84d4555
4 changed files with 79 additions and 9 deletions

View File

@ -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);
}
}

View 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);
this.hyperlink = hyperlink;
}
@Internal
public CTHyperlink getCTHyperlink() {
return hyperlink;
}

View 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

View 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() {