Search in sources :

Example 36 with XmlCursor

use of org.apache.xmlbeans.XmlCursor 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 37 with XmlCursor

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

Example 38 with XmlCursor

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

the class XWPFHeaderFooter method removeParagraph.

/**
     * Removes a specific paragraph from this header / footer
     *
     * @param paragraph - {@link XWPFParagraph} object to remove
     */
public void removeParagraph(XWPFParagraph paragraph) {
    if (paragraphs.contains(paragraph)) {
        CTP ctP = paragraph.getCTP();
        XmlCursor c = ctP.newCursor();
        c.removeXml();
        c.dispose();
        paragraphs.remove(paragraph);
        bodyElements.remove(paragraph);
    }
}
Also used : CTP(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP) XmlCursor(org.apache.xmlbeans.XmlCursor)

Example 39 with XmlCursor

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

the class XWPFHeaderFooter method isCursorInHdrF.

/**
     * verifies that cursor is on the right position
     *
     * @param cursor
     */
private boolean isCursorInHdrF(XmlCursor cursor) {
    XmlCursor verify = cursor.newCursor();
    verify.toParent();
    boolean result = (verify.getObject() == this.headerFooter);
    verify.dispose();
    return result;
}
Also used : XmlCursor(org.apache.xmlbeans.XmlCursor)

Example 40 with XmlCursor

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

the class XWPFHeaderFooter method clearHeaderFooter.

/**
     * Clears all paragraphs and tables from this header / footer
     */
public void clearHeaderFooter() {
    XmlCursor c = headerFooter.newCursor();
    c.removeXmlContents();
    c.dispose();
    paragraphs.clear();
    tables.clear();
    bodyElements.clear();
}
Also used : XmlCursor(org.apache.xmlbeans.XmlCursor)

Aggregations

XmlCursor (org.apache.xmlbeans.XmlCursor)160 XmlObject (org.apache.xmlbeans.XmlObject)68 QName (javax.xml.namespace.QName)21 XmlException (org.apache.xmlbeans.XmlException)16 TokenType (org.apache.xmlbeans.XmlCursor.TokenType)14 CTTbl (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl)14 CTP (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP)10 ArrayList (java.util.ArrayList)9 POSIXApplicationType (org.ggf.schemas.jsdl.x2005.x11.jsdlPosix.POSIXApplicationType)8 HPCProfileApplicationType (org.ggf.schemas.jsdl.x2006.x07.jsdlHpcpa.HPCProfileApplicationType)8 SPMDApplicationType (org.ogf.schemas.jsdl.x2007.x02.jsdlSpmd.SPMDApplicationType)8 IOException (java.io.IOException)5 POIXMLException (org.apache.poi.POIXMLException)5 InputStream (java.io.InputStream)4 DrawPaint (org.apache.poi.sl.draw.DrawPaint)3 ArrayType (org.dmg.pmml.ArrayType)3 ApplicationType (org.ggf.schemas.jsdl.x2005.x11.jsdl.ApplicationType)3 LineString (org.locationtech.jts.geom.LineString)3 CTRow (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow)3 CTSdtBlock (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtBlock)3