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();
}
}
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());
}
}
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 >br<, 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);
}
}
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);
}
}
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();
}
}
Aggregations