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