Search in sources :

Example 21 with XmlObject

use of org.apache.xmlbeans.XmlObject in project poi by apache.

the class XSSFVMLDrawing method findCommentShape.

/**
     * Find a shape with ClientData of type "NOTE" and the specified row and column
     *
     * @return the comment shape or <code>null</code>
     */
protected CTShape findCommentShape(int row, int col) {
    for (XmlObject itm : _items) {
        if (itm instanceof CTShape) {
            CTShape sh = (CTShape) itm;
            if (sh.sizeOfClientDataArray() > 0) {
                CTClientData cldata = sh.getClientDataArray(0);
                if (cldata.getObjectType() == STObjectType.NOTE) {
                    int crow = cldata.getRowArray(0).intValue();
                    int ccol = cldata.getColumnArray(0).intValue();
                    if (crow == row && ccol == col) {
                        return sh;
                    }
                }
            }
        }
    }
    return null;
}
Also used : XmlObject(org.apache.xmlbeans.XmlObject) CTShape(com.microsoft.schemas.vml.CTShape) CTClientData(com.microsoft.schemas.office.excel.CTClientData)

Example 22 with XmlObject

use of org.apache.xmlbeans.XmlObject 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)

Example 23 with XmlObject

use of org.apache.xmlbeans.XmlObject in project poi by apache.

the class XWPFDocument method getTableCell.

/**
     * get the TableCell which belongs to the TableCell
     *
     * @param cell
     */
@Override
public XWPFTableCell getTableCell(CTTc cell) {
    XmlCursor cursor = cell.newCursor();
    cursor.toParent();
    XmlObject o = cursor.getObject();
    if (!(o instanceof CTRow)) {
        return null;
    }
    CTRow row = (CTRow) o;
    cursor.toParent();
    o = cursor.getObject();
    cursor.dispose();
    if (!(o instanceof CTTbl)) {
        return null;
    }
    CTTbl tbl = (CTTbl) o;
    XWPFTable table = getTable(tbl);
    if (table == null) {
        return null;
    }
    XWPFTableRow tableRow = table.getRow(row);
    if (tableRow == null) {
        return null;
    }
    return tableRow.getTableCell(cell);
}
Also used : XmlObject(org.apache.xmlbeans.XmlObject) XmlCursor(org.apache.xmlbeans.XmlCursor)

Example 24 with XmlObject

use of org.apache.xmlbeans.XmlObject in project poi by apache.

the class XWPFDocument method onDocumentRead.

@SuppressWarnings("deprecation")
@Override
protected void onDocumentRead() throws IOException {
    try {
        DocumentDocument doc = DocumentDocument.Factory.parse(getPackagePart().getInputStream(), DEFAULT_XML_OPTIONS);
        ctDocument = doc.getDocument();
        initFootnotes();
        // parse the document with cursor and add
        // the XmlObject to its lists
        XmlCursor cursor = ctDocument.getBody().newCursor();
        cursor.selectPath("./*");
        while (cursor.toNextSelection()) {
            XmlObject o = cursor.getObject();
            if (o instanceof CTP) {
                XWPFParagraph p = new XWPFParagraph((CTP) o, this);
                bodyElements.add(p);
                paragraphs.add(p);
            } else if (o instanceof CTTbl) {
                XWPFTable t = new XWPFTable((CTTbl) o, this);
                bodyElements.add(t);
                tables.add(t);
            } else if (o instanceof CTSdtBlock) {
                XWPFSDT c = new XWPFSDT((CTSdtBlock) o, this);
                bodyElements.add(c);
                contentControls.add(c);
            }
        }
        cursor.dispose();
        // Sort out headers and footers
        if (doc.getDocument().getBody().getSectPr() != null)
            headerFooterPolicy = new XWPFHeaderFooterPolicy(this);
        // Create for each XML-part in the Package a PartClass
        for (RelationPart rp : getRelationParts()) {
            POIXMLDocumentPart p = rp.getDocumentPart();
            String relation = rp.getRelationship().getRelationshipType();
            if (relation.equals(XWPFRelation.STYLES.getRelation())) {
                this.styles = (XWPFStyles) p;
                this.styles.onDocumentRead();
            } else if (relation.equals(XWPFRelation.NUMBERING.getRelation())) {
                this.numbering = (XWPFNumbering) p;
                this.numbering.onDocumentRead();
            } else if (relation.equals(XWPFRelation.FOOTER.getRelation())) {
                XWPFFooter footer = (XWPFFooter) p;
                footers.add(footer);
                footer.onDocumentRead();
            } else if (relation.equals(XWPFRelation.HEADER.getRelation())) {
                XWPFHeader header = (XWPFHeader) p;
                headers.add(header);
                header.onDocumentRead();
            } else if (relation.equals(XWPFRelation.COMMENT.getRelation())) {
                // TODO Create according XWPFComment class, extending POIXMLDocumentPart
                CommentsDocument cmntdoc = CommentsDocument.Factory.parse(p.getPackagePart().getInputStream(), DEFAULT_XML_OPTIONS);
                for (CTComment ctcomment : cmntdoc.getComments().getCommentArray()) {
                    comments.add(new XWPFComment(ctcomment, this));
                }
            } else if (relation.equals(XWPFRelation.SETTINGS.getRelation())) {
                settings = (XWPFSettings) p;
                settings.onDocumentRead();
            } else if (relation.equals(XWPFRelation.IMAGES.getRelation())) {
                XWPFPictureData picData = (XWPFPictureData) p;
                picData.onDocumentRead();
                registerPackagePictureData(picData);
                pictures.add(picData);
            } else if (relation.equals(XWPFRelation.GLOSSARY_DOCUMENT.getRelation())) {
                // Until we do, we do need to load the glossary child parts of it
                for (POIXMLDocumentPart gp : p.getRelations()) {
                    // Trigger the onDocumentRead for all the child parts
                    // Otherwise we'll hit issues on Styles, Settings etc on save
                    // TODO: Refactor this to not need to access protected method
                    // from other package! Remove the static helper method once fixed!!!
                    POIXMLDocumentPart._invokeOnDocumentRead(gp);
                }
            }
        }
        initHyperlinks();
    } catch (XmlException e) {
        throw new POIXMLException(e);
    }
}
Also used : POIXMLDocumentPart(org.apache.poi.POIXMLDocumentPart) XWPFHeaderFooterPolicy(org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy) POIXMLException(org.apache.poi.POIXMLException) XmlCursor(org.apache.xmlbeans.XmlCursor) XmlException(org.apache.xmlbeans.XmlException) XmlObject(org.apache.xmlbeans.XmlObject)

Example 25 with XmlObject

use of org.apache.xmlbeans.XmlObject in project poi by apache.

the class XWPFFootnote method getTableCell.

/**
     * get the TableCell which belongs to the TableCell
     *
     * @param cell
     * @see org.apache.poi.xwpf.usermodel.IBody#getTableCell(CTTc cell)
     */
public XWPFTableCell getTableCell(CTTc cell) {
    XmlCursor cursor = cell.newCursor();
    cursor.toParent();
    XmlObject o = cursor.getObject();
    if (!(o instanceof CTRow)) {
        return null;
    }
    CTRow row = (CTRow) o;
    cursor.toParent();
    o = cursor.getObject();
    cursor.dispose();
    if (!(o instanceof CTTbl)) {
        return null;
    }
    CTTbl tbl = (CTTbl) o;
    XWPFTable table = getTable(tbl);
    if (table == null) {
        return null;
    }
    XWPFTableRow tableRow = table.getRow(row);
    if (tableRow == null) {
        return null;
    }
    return tableRow.getTableCell(cell);
}
Also used : XmlObject(org.apache.xmlbeans.XmlObject) CTTbl(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl) CTRow(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow) XmlCursor(org.apache.xmlbeans.XmlCursor)

Aggregations

XmlObject (org.apache.xmlbeans.XmlObject)87 XmlCursor (org.apache.xmlbeans.XmlCursor)42 Test (org.junit.Test)14 XmlException (org.apache.xmlbeans.XmlException)13 CTTbl (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl)13 DefaultExchange (org.apache.camel.impl.DefaultExchange)10 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)9 CTP (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP)9 CTPicture (org.openxmlformats.schemas.presentationml.x2006.main.CTPicture)7 POIXMLException (org.apache.poi.POIXMLException)6 CTAxDataSource (org.openxmlformats.schemas.drawingml.x2006.chart.CTAxDataSource)6 CTNumDataSource (org.openxmlformats.schemas.drawingml.x2006.chart.CTNumDataSource)6 CTShapeProperties (org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties)6 CTGroupShape (org.openxmlformats.schemas.presentationml.x2006.main.CTGroupShape)6 ArrayList (java.util.ArrayList)5 QName (javax.xml.namespace.QName)5 CTGraphicalObjectFrame (org.openxmlformats.schemas.presentationml.x2006.main.CTGraphicalObjectFrame)5 Node (org.w3c.dom.Node)5 CTConnector (org.openxmlformats.schemas.presentationml.x2006.main.CTConnector)4 CTShape (org.openxmlformats.schemas.presentationml.x2006.main.CTShape)4