Search in sources :

Example 1 with CTFldChar

use of org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFldChar in project poi by apache.

the class XWPFRun method text.

/**
     * Returns the string version of the text, with tabs and
     * carriage returns in place of their xml equivalents.
     */
public String text() {
    StringBuffer text = new StringBuffer();
    // Grab the text and tabs of the text run
    // Do so in a way that preserves the ordering
    XmlCursor c = run.newCursor();
    c.selectPath("./*");
    while (c.toNextSelection()) {
        XmlObject o = c.getObject();
        if (o instanceof CTText) {
            String tagName = o.getDomNode().getNodeName();
            //  in the normal text output
            if (!"w:instrText".equals(tagName)) {
                text.append(((CTText) o).getStringValue());
            }
        }
        // Complex type evaluation (currently only for extraction of check boxes)
        if (o instanceof CTFldChar) {
            CTFldChar ctfldChar = ((CTFldChar) o);
            if (ctfldChar.getFldCharType() == STFldCharType.BEGIN) {
                if (ctfldChar.getFfData() != null) {
                    for (CTFFCheckBox checkBox : ctfldChar.getFfData().getCheckBoxList()) {
                        if (checkBox.getDefault() != null && checkBox.getDefault().getVal() == STOnOff.X_1) {
                            text.append("|X|");
                        } else {
                            text.append("|_|");
                        }
                    }
                }
            }
        }
        if (o instanceof CTPTab) {
            text.append("\t");
        }
        if (o instanceof CTBr) {
            text.append("\n");
        }
        if (o instanceof CTEmpty) {
            // Some inline text elements get returned not as
            //  themselves, but as CTEmpty, owing to some odd
            //  definitions around line 5642 of the XSDs
            // This bit works around it, and replicates the above
            //  rules for that case
            String tagName = o.getDomNode().getNodeName();
            if ("w:tab".equals(tagName) || "tab".equals(tagName)) {
                text.append("\t");
            }
            if ("w:br".equals(tagName) || "br".equals(tagName)) {
                text.append("\n");
            }
            if ("w:cr".equals(tagName) || "cr".equals(tagName)) {
                text.append("\n");
            }
        }
        if (o instanceof CTFtnEdnRef) {
            CTFtnEdnRef ftn = (CTFtnEdnRef) o;
            String footnoteRef = ftn.getDomNode().getLocalName().equals("footnoteReference") ? "[footnoteRef:" + ftn.getId().intValue() + "]" : "[endnoteRef:" + ftn.getId().intValue() + "]";
            text.append(footnoteRef);
        }
    }
    c.dispose();
    // Any picture text?
    if (pictureText != null && pictureText.length() > 0) {
        text.append("\n").append(pictureText);
    }
    return text.toString();
}
Also used : CTFtnEdnRef(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFtnEdnRef) CTText(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText) CTEmpty(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTEmpty) XmlObject(org.apache.xmlbeans.XmlObject) XmlString(org.apache.xmlbeans.XmlString) CTPTab(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPTab) CTFFCheckBox(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFFCheckBox) CTBr(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBr) XmlCursor(org.apache.xmlbeans.XmlCursor) CTFldChar(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFldChar)

Aggregations

XmlCursor (org.apache.xmlbeans.XmlCursor)1 XmlObject (org.apache.xmlbeans.XmlObject)1 XmlString (org.apache.xmlbeans.XmlString)1 CTBr (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBr)1 CTEmpty (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTEmpty)1 CTFFCheckBox (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFFCheckBox)1 CTFldChar (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFldChar)1 CTFtnEdnRef (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFtnEdnRef)1 CTPTab (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPTab)1 CTText (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText)1