Search in sources :

Example 61 with XmlObject

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

the class XWPFFootnote method insertNewParagraph.

/**
     * add a new paragraph at position of the cursor
     *
     * @param cursor
     * @return the inserted paragraph
     * @see org.apache.poi.xwpf.usermodel.IBody#insertNewParagraph(XmlCursor cursor)
     */
public XWPFParagraph insertNewParagraph(final XmlCursor cursor) {
    if (isCursorInFtn(cursor)) {
        String uri = CTP.type.getName().getNamespaceURI();
        String localPart = "p";
        cursor.beginElement(localPart, uri);
        cursor.toParent();
        CTP p = (CTP) cursor.getObject();
        XWPFParagraph newP = new XWPFParagraph(p, this);
        XmlObject o = null;
        while (!(o instanceof CTP) && (cursor.toPrevSibling())) {
            o = cursor.getObject();
        }
        if ((!(o instanceof CTP)) || (CTP) o == p) {
            paragraphs.add(0, newP);
        } else {
            int pos = paragraphs.indexOf(getParagraph((CTP) o)) + 1;
            paragraphs.add(pos, newP);
        }
        int i = 0;
        XmlCursor p2 = p.newCursor();
        cursor.toCursor(p2);
        p2.dispose();
        while (cursor.toPrevSibling()) {
            o = cursor.getObject();
            if (o instanceof CTP || o instanceof CTTbl)
                i++;
        }
        bodyElements.add(i, newP);
        p2 = p.newCursor();
        cursor.toCursor(p2);
        cursor.toEndToken();
        p2.dispose();
        return newP;
    }
    return null;
}
Also used : XmlObject(org.apache.xmlbeans.XmlObject) CTTbl(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl) CTP(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP) XmlCursor(org.apache.xmlbeans.XmlCursor)

Example 62 with XmlObject

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

the class XWPFHeaderFooter method readHdrFtr.

public void readHdrFtr() {
    bodyElements = new ArrayList<IBodyElement>();
    paragraphs = new ArrayList<XWPFParagraph>();
    tables = new ArrayList<XWPFTable>();
    // parse the document with cursor and add
    // the XmlObject to its lists
    XmlCursor cursor = headerFooter.newCursor();
    cursor.selectPath("./*");
    while (cursor.toNextSelection()) {
        XmlObject o = cursor.getObject();
        if (o instanceof CTP) {
            XWPFParagraph p = new XWPFParagraph((CTP) o, this);
            paragraphs.add(p);
            bodyElements.add(p);
        }
        if (o instanceof CTTbl) {
            XWPFTable t = new XWPFTable((CTTbl) o, this);
            tables.add(t);
            bodyElements.add(t);
        }
    }
    cursor.dispose();
}
Also used : XmlObject(org.apache.xmlbeans.XmlObject) CTTbl(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl) CTP(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP) XmlCursor(org.apache.xmlbeans.XmlCursor)

Example 63 with XmlObject

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

the class XWPFDocument method insertNewParagraph.

/**
     * Add a new paragraph at position of the cursor. The cursor must be on the
     * {@link org.apache.xmlbeans.XmlCursor.TokenType#START} tag of an subelement
     * of the documents body. When this method is done, the cursor passed as
     * parameter points to the {@link org.apache.xmlbeans.XmlCursor.TokenType#END}
     * of the newly inserted paragraph.
     *
     * @param cursor
     * @return the {@link XWPFParagraph} object representing the newly inserted
     * CTP object
     */
@Override
public XWPFParagraph insertNewParagraph(XmlCursor cursor) {
    if (isCursorInBody(cursor)) {
        String uri = CTP.type.getName().getNamespaceURI();
        /*
             * TODO DO not use a coded constant, find the constant in the OOXML
             * classes instead, as the child of type CT_Paragraph is defined in the 
             * OOXML schema as 'p'
             */
        String localPart = "p";
        // creates a new Paragraph, cursor is positioned inside the new
        // element
        cursor.beginElement(localPart, uri);
        // move the cursor to the START token to the paragraph just created
        cursor.toParent();
        CTP p = (CTP) cursor.getObject();
        XWPFParagraph newP = new XWPFParagraph(p, this);
        XmlObject o = null;
        /*
             * move the cursor to the previous element until a) the next
             * paragraph is found or b) all elements have been passed
             */
        while (!(o instanceof CTP) && (cursor.toPrevSibling())) {
            o = cursor.getObject();
        }
        /*
             * if the object that has been found is a) not a paragraph or b) is
             * the paragraph that has just been inserted, as the cursor in the
             * while loop above was not moved as there were no other siblings,
             * then the paragraph that was just inserted is the first paragraph
             * in the body. Otherwise, take the previous paragraph and calculate
             * the new index for the new paragraph.
             */
        if ((!(o instanceof CTP)) || (CTP) o == p) {
            paragraphs.add(0, newP);
        } else {
            int pos = paragraphs.indexOf(getParagraph((CTP) o)) + 1;
            paragraphs.add(pos, newP);
        }
        /*
             * create a new cursor, that points to the START token of the just
             * inserted paragraph
             */
        XmlCursor newParaPos = p.newCursor();
        try {
            /*
                 * Calculate the paragraphs index in the list of all body
                 * elements
                 */
            int i = 0;
            cursor.toCursor(newParaPos);
            while (cursor.toPrevSibling()) {
                o = cursor.getObject();
                if (o instanceof CTP || o instanceof CTTbl)
                    i++;
            }
            bodyElements.add(i, newP);
            cursor.toCursor(newParaPos);
            cursor.toEndToken();
            return newP;
        } finally {
            newParaPos.dispose();
        }
    }
    return null;
}
Also used : XmlObject(org.apache.xmlbeans.XmlObject) XmlCursor(org.apache.xmlbeans.XmlCursor)

Example 64 with XmlObject

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

the class XWPFDocument method insertNewTbl.

@Override
public XWPFTable insertNewTbl(XmlCursor cursor) {
    if (isCursorInBody(cursor)) {
        String uri = CTTbl.type.getName().getNamespaceURI();
        String localPart = "tbl";
        cursor.beginElement(localPart, uri);
        cursor.toParent();
        CTTbl t = (CTTbl) cursor.getObject();
        XWPFTable newT = new XWPFTable(t, this);
        XmlObject o = null;
        while (!(o instanceof CTTbl) && (cursor.toPrevSibling())) {
            o = cursor.getObject();
        }
        if (!(o instanceof CTTbl)) {
            tables.add(0, newT);
        } else {
            int pos = tables.indexOf(getTable((CTTbl) o)) + 1;
            tables.add(pos, newT);
        }
        int i = 0;
        XmlCursor tableCursor = t.newCursor();
        try {
            cursor.toCursor(tableCursor);
            while (cursor.toPrevSibling()) {
                o = cursor.getObject();
                if (o instanceof CTP || o instanceof CTTbl)
                    i++;
            }
            bodyElements.add(i, newT);
            cursor.toCursor(tableCursor);
            cursor.toEndToken();
            return newT;
        } finally {
            tableCursor.dispose();
        }
    }
    return null;
}
Also used : XmlObject(org.apache.xmlbeans.XmlObject) XmlCursor(org.apache.xmlbeans.XmlCursor)

Example 65 with XmlObject

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

the class XWPFFootnote method insertNewTbl.

/**
     * @param cursor
     * @return the inserted table
     * @see org.apache.poi.xwpf.usermodel.IBody#insertNewTbl(XmlCursor cursor)
     */
public XWPFTable insertNewTbl(XmlCursor cursor) {
    if (isCursorInFtn(cursor)) {
        String uri = CTTbl.type.getName().getNamespaceURI();
        String localPart = "tbl";
        cursor.beginElement(localPart, uri);
        cursor.toParent();
        CTTbl t = (CTTbl) cursor.getObject();
        XWPFTable newT = new XWPFTable(t, this);
        cursor.removeXmlContents();
        XmlObject o = null;
        while (!(o instanceof CTTbl) && (cursor.toPrevSibling())) {
            o = cursor.getObject();
        }
        if (!(o instanceof CTTbl)) {
            tables.add(0, newT);
        } else {
            int pos = tables.indexOf(getTable((CTTbl) o)) + 1;
            tables.add(pos, newT);
        }
        int i = 0;
        cursor = t.newCursor();
        while (cursor.toPrevSibling()) {
            o = cursor.getObject();
            if (o instanceof CTP || o instanceof CTTbl)
                i++;
        }
        bodyElements.add(i, newT);
        XmlCursor c2 = t.newCursor();
        cursor.toCursor(c2);
        cursor.toEndToken();
        c2.dispose();
        return newT;
    }
    return null;
}
Also used : CTTbl(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl) XmlObject(org.apache.xmlbeans.XmlObject) CTP(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP) XmlCursor(org.apache.xmlbeans.XmlCursor)

Aggregations

XmlObject (org.apache.xmlbeans.XmlObject)102 XmlCursor (org.apache.xmlbeans.XmlCursor)49 XmlException (org.apache.xmlbeans.XmlException)17 Test (org.junit.Test)14 CTTbl (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl)13 CTAxDataSource (org.openxmlformats.schemas.drawingml.x2006.chart.CTAxDataSource)12 CTNumDataSource (org.openxmlformats.schemas.drawingml.x2006.chart.CTNumDataSource)12 DefaultExchange (org.apache.camel.impl.DefaultExchange)10 ArrayList (java.util.ArrayList)9 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 IOException (java.io.IOException)6 QName (javax.xml.namespace.QName)6 POIXMLException (org.apache.poi.POIXMLException)6 CTShapeProperties (org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties)6 CTGroupShape (org.openxmlformats.schemas.presentationml.x2006.main.CTGroupShape)6 CTGraphicalObjectFrame (org.openxmlformats.schemas.presentationml.x2006.main.CTGraphicalObjectFrame)5 Node (org.w3c.dom.Node)5 CTConnector (org.openxmlformats.schemas.presentationml.x2006.main.CTConnector)4