use of org.apache.xmlbeans.XmlCursor in project poi by apache.
the class XSSFDrawing method getShapes.
/**
* @return list of shapes in this shape group
*/
public List<XSSFShape> getShapes(XSSFShapeGroup groupshape) {
List<XSSFShape> lst = new ArrayList<XSSFShape>();
XmlCursor cur = groupshape.getCTGroupShape().newCursor();
try {
addShapes(cur, lst);
} finally {
cur.dispose();
}
return lst;
}
use of org.apache.xmlbeans.XmlCursor 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.XmlCursor in project poi by apache.
the class XSSFGraphicFrame method appendChartElement.
/**
* The low level code to insert {@code <c:chart>} tag into
* {@code<a:graphicData>}.
*
* Here is the schema (ECMA-376):
* <pre>
* {@code
* <complexType name="CT_GraphicalObjectData">
* <sequence>
* <any minOccurs="0" maxOccurs="unbounded" processContents="strict"/>
* </sequence>
* <attribute name="uri" type="xsd:token"/>
* </complexType>
* }
* </pre>
*/
private void appendChartElement(CTGraphicalObjectData data, String id) {
String r_namespaceUri = STRelationshipId.type.getName().getNamespaceURI();
String c_namespaceUri = XSSFDrawing.NAMESPACE_C;
XmlCursor cursor = data.newCursor();
cursor.toNextToken();
cursor.beginElement(new QName(c_namespaceUri, "chart", "c"));
cursor.insertAttributeWithValue(new QName(r_namespaceUri, "id", "r"), id);
cursor.dispose();
data.setUri(c_namespaceUri);
}
use of org.apache.xmlbeans.XmlCursor 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();
}
use of org.apache.xmlbeans.XmlCursor in project poi by apache.
the class XWPFDocument method getTableCell.
/**
* get the TableCell which belongs to the TableCell
*
* @param cell
*/
@Override
public XWPFTableCell getTableCell(CTTc cell) {
XmlCursor cursor = cell.newCursor();
cursor.toParent();
XmlObject o = cursor.getObject();
if (!(o instanceof CTRow)) {
return null;
}
CTRow row = (CTRow) o;
cursor.toParent();
o = cursor.getObject();
cursor.dispose();
if (!(o instanceof CTTbl)) {
return null;
}
CTTbl tbl = (CTTbl) o;
XWPFTable table = getTable(tbl);
if (table == null) {
return null;
}
XWPFTableRow tableRow = table.getRow(row);
if (tableRow == null) {
return null;
}
return tableRow.getTableCell(cell);
}
Aggregations