Search in sources :

Example 11 with XmlException

use of org.apache.xmlbeans.XmlException 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 12 with XmlException

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

the class XSSFPivotCache method readFrom.

@Beta
protected void readFrom(InputStream is) throws IOException {
    try {
        XmlOptions options = new XmlOptions(DEFAULT_XML_OPTIONS);
        //Removing root element
        options.setLoadReplaceDocumentElement(null);
        ctPivotCache = CTPivotCache.Factory.parse(is, options);
    } catch (XmlException e) {
        throw new IOException(e.getLocalizedMessage());
    }
}
Also used : XmlException(org.apache.xmlbeans.XmlException) XmlOptions(org.apache.xmlbeans.XmlOptions) IOException(java.io.IOException) Beta(org.apache.poi.util.Beta)

Example 13 with XmlException

use of org.apache.xmlbeans.XmlException 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 14 with XmlException

use of org.apache.xmlbeans.XmlException 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 15 with XmlException

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

the class XWPFNumbering method onDocumentRead.

/**
     * read numbering form an existing package
     */
@Override
protected void onDocumentRead() throws IOException {
    NumberingDocument numberingDoc = null;
    InputStream is;
    is = getPackagePart().getInputStream();
    try {
        numberingDoc = NumberingDocument.Factory.parse(is, DEFAULT_XML_OPTIONS);
        ctNumbering = numberingDoc.getNumbering();
        //get any Nums
        for (CTNum ctNum : ctNumbering.getNumArray()) {
            nums.add(new XWPFNum(ctNum, this));
        }
        for (CTAbstractNum ctAbstractNum : ctNumbering.getAbstractNumArray()) {
            abstractNums.add(new XWPFAbstractNum(ctAbstractNum, this));
        }
        isNew = false;
    } catch (XmlException e) {
        throw new POIXMLException();
    } finally {
        is.close();
    }
}
Also used : CTNum(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTNum) InputStream(java.io.InputStream) XmlException(org.apache.xmlbeans.XmlException) POIXMLException(org.apache.poi.POIXMLException) NumberingDocument(org.openxmlformats.schemas.wordprocessingml.x2006.main.NumberingDocument) CTAbstractNum(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTAbstractNum)

Aggregations

XmlException (org.apache.xmlbeans.XmlException)112 XmlObject (org.apache.xmlbeans.XmlObject)45 IOException (java.io.IOException)35 DecodingException (org.n52.svalbard.decode.exception.DecodingException)19 EncodingException (org.n52.svalbard.encode.exception.EncodingException)17 POIXMLException (org.apache.poi.POIXMLException)15 InputStream (java.io.InputStream)11 ArrayList (java.util.ArrayList)10 XmlCursor (org.apache.xmlbeans.XmlCursor)10 XmlOptions (org.apache.xmlbeans.XmlOptions)10 OpenXML4JException (org.apache.poi.openxml4j.exceptions.OpenXML4JException)8 POIXMLDocumentPart (org.apache.poi.POIXMLDocumentPart)7 AbstractFeature (org.n52.shetland.ogc.gml.AbstractFeature)7 Geometry (org.locationtech.jts.geom.Geometry)6 Document (org.w3c.dom.Document)6 Node (org.w3c.dom.Node)6 DataAccessException (com.centurylink.mdw.common.exception.DataAccessException)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 File (java.io.File)5