Search in sources :

Example 16 with XmlObject

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

the class XSSFDrawing method getAnchorFromParent.

private XSSFAnchor getAnchorFromParent(XmlObject obj) {
    XSSFAnchor anchor = null;
    XmlObject parentXbean = null;
    XmlCursor cursor = obj.newCursor();
    if (cursor.toParent()) {
        parentXbean = cursor.getObject();
    }
    cursor.dispose();
    if (parentXbean != null) {
        if (parentXbean instanceof CTTwoCellAnchor) {
            CTTwoCellAnchor ct = (CTTwoCellAnchor) parentXbean;
            anchor = new XSSFClientAnchor(ct.getFrom(), ct.getTo());
        } else if (parentXbean instanceof CTOneCellAnchor) {
            CTOneCellAnchor ct = (CTOneCellAnchor) parentXbean;
            anchor = new XSSFClientAnchor(ct.getFrom(), CTMarker.Factory.newInstance());
        }
    }
    return anchor;
}
Also used : XmlObject(org.apache.xmlbeans.XmlObject) CTTwoCellAnchor(org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTTwoCellAnchor) CTOneCellAnchor(org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTOneCellAnchor) XmlCursor(org.apache.xmlbeans.XmlCursor)

Example 17 with XmlObject

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

the class XSSFDrawing method addShapes.

private void addShapes(XmlCursor cur, List<XSSFShape> lst) {
    try {
        do {
            cur.push();
            if (cur.toFirstChild()) {
                do {
                    XmlObject obj = cur.getObject();
                    XSSFShape shape;
                    if (obj instanceof CTMarker) {
                        // ignore anchor elements
                        continue;
                    } else if (obj instanceof CTPicture) {
                        shape = new XSSFPicture(this, (CTPicture) obj);
                    } else if (obj instanceof CTConnector) {
                        shape = new XSSFConnector(this, (CTConnector) obj);
                    } else if (obj instanceof CTShape) {
                        shape = hasOleLink(obj) ? new XSSFObjectData(this, (CTShape) obj) : new XSSFSimpleShape(this, (CTShape) obj);
                    } else if (obj instanceof CTGraphicalObjectFrame) {
                        shape = new XSSFGraphicFrame(this, (CTGraphicalObjectFrame) obj);
                    } else if (obj instanceof CTGroupShape) {
                        shape = new XSSFShapeGroup(this, (CTGroupShape) obj);
                    } else if (obj instanceof XmlAnyTypeImpl) {
                        LOG.log(POILogger.WARN, "trying to parse AlternateContent, " + "this unlinks the returned Shapes from the underlying xml content, " + "so those shapes can't be used to modify the drawing, " + "i.e. modifications will be ignored!");
                        // XmlAnyTypeImpl is returned for AlternateContent parts, which might contain a CTDrawing
                        cur.push();
                        cur.toFirstChild();
                        XmlCursor cur2 = null;
                        try {
                            // need to parse AlternateContent again, otherwise the child elements aren't typed,
                            // but also XmlAnyTypes
                            CTDrawing alterWS = CTDrawing.Factory.parse(cur.newXMLStreamReader());
                            cur2 = alterWS.newCursor();
                            if (cur2.toFirstChild()) {
                                addShapes(cur2, lst);
                            }
                        } catch (XmlException e) {
                            LOG.log(POILogger.WARN, "unable to parse CTDrawing in alternate content.", e);
                        } finally {
                            if (cur2 != null) {
                                cur2.dispose();
                            }
                            cur.pop();
                        }
                        continue;
                    } else {
                        // ignore anything else
                        continue;
                    }
                    assert (shape != null);
                    shape.anchor = getAnchorFromParent(obj);
                    lst.add(shape);
                } while (cur.toNextSibling());
            }
            cur.pop();
        } while (cur.toNextSibling());
    } finally {
        cur.dispose();
    }
}
Also used : CTMarker(org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker) CTConnector(org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTConnector) CTGraphicalObjectFrame(org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTGraphicalObjectFrame) CTDrawing(org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTDrawing) CTShape(org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTShape) CTGroupShape(org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTGroupShape) XmlCursor(org.apache.xmlbeans.XmlCursor) XmlException(org.apache.xmlbeans.XmlException) CTPicture(org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTPicture) XmlAnyTypeImpl(org.apache.xmlbeans.impl.values.XmlAnyTypeImpl) XmlObject(org.apache.xmlbeans.XmlObject)

Example 18 with XmlObject

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

the class XSSFVMLDrawing method read.

@SuppressWarnings("resource")
protected void read(InputStream is) throws IOException, XmlException {
    Document doc;
    try {
        /*
             * This is a seriously sick fix for the fact that some .xlsx files contain raw bits
             * of HTML, without being escaped or properly turned into XML.
             * The result is that they contain things like &gt;br&lt;, which breaks the XML parsing.
             * This very sick InputStream wrapper attempts to spot these go past, and fix them.
             */
        doc = DocumentHelper.readDocument(new ReplacingInputStream(is, "<br>", "<br/>"));
    } catch (SAXException e) {
        throw new XmlException(e.getMessage(), e);
    }
    XmlObject root = XmlObject.Factory.parse(doc, DEFAULT_XML_OPTIONS);
    _qnames = new ArrayList<QName>();
    _items = new ArrayList<XmlObject>();
    for (XmlObject obj : root.selectPath("$this/xml/*")) {
        Node nd = obj.getDomNode();
        QName qname = new QName(nd.getNamespaceURI(), nd.getLocalName());
        if (qname.equals(QNAME_SHAPE_LAYOUT)) {
            _items.add(CTShapeLayout.Factory.parse(obj.xmlText(), DEFAULT_XML_OPTIONS));
        } else if (qname.equals(QNAME_SHAPE_TYPE)) {
            CTShapetype st = CTShapetype.Factory.parse(obj.xmlText(), DEFAULT_XML_OPTIONS);
            _items.add(st);
            _shapeTypeId = st.getId();
        } else if (qname.equals(QNAME_SHAPE)) {
            CTShape shape = CTShape.Factory.parse(obj.xmlText(), DEFAULT_XML_OPTIONS);
            String id = shape.getId();
            if (id != null) {
                Matcher m = ptrn_shapeId.matcher(id);
                if (m.find()) {
                    _shapeId = Math.max(_shapeId, Integer.parseInt(m.group(1)));
                }
            }
            _items.add(shape);
        } else {
            Document doc2;
            try {
                InputSource is2 = new InputSource(new StringReader(obj.xmlText()));
                doc2 = DocumentHelper.readDocument(is2);
            } catch (SAXException e) {
                throw new XmlException(e.getMessage(), e);
            }
            _items.add(XmlObject.Factory.parse(doc2, DEFAULT_XML_OPTIONS));
        }
        _qnames.add(qname);
    }
}
Also used : InputSource(org.xml.sax.InputSource) Matcher(java.util.regex.Matcher) QName(javax.xml.namespace.QName) Node(org.w3c.dom.Node) CTShape(com.microsoft.schemas.vml.CTShape) Document(org.w3c.dom.Document) ReplacingInputStream(org.apache.poi.util.ReplacingInputStream) SAXException(org.xml.sax.SAXException) CTShapetype(com.microsoft.schemas.vml.CTShapetype) XmlException(org.apache.xmlbeans.XmlException) StringReader(java.io.StringReader) XmlObject(org.apache.xmlbeans.XmlObject)

Example 19 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 20 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)

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