use of org.apache.xmlbeans.XmlCursor in project poi by apache.
the class XmlSort method sort.
/**
* Sorts the children of <code>element</code> according to the order indicated by the
* comparator.
* @param element the element whose content is to be sorted. Only element children are sorted,
* attributes are not touched. When elements are reordered, all the text, comments and PIs
* follow the element that they come immediately after.
* @param comp a comparator that is to be used when comparing the <code>QName</code>s of two
* elements.
* @throws IllegalArgumentException if the input <code>XmlObject</code> does not represent
* an element
*/
public static void sort(XmlObject element, Comparator<XmlCursor> comp) {
XmlCursor headCursor = element.newCursor();
if (!headCursor.isStart()) {
throw new IllegalStateException("The element parameter must point to a STARTDOC");
}
// We use insertion sort to minimize the number of swaps, because each swap means
// moving a part of the document
/* headCursor points to the beginning of the list of the already sorted items and
listCursor points to the beginning of the list of unsorted items
At the beginning, headCursor points to the first element and listCursor points to the
second element. The algorithm ends when listCursor cannot be moved to the "next"
element in the unsorted list, i.e. the unsorted list becomes empty */
boolean moved = headCursor.toFirstChild();
if (!moved) {
// therefore there is nothing to sort
return;
}
XmlCursor listCursor = headCursor.newCursor();
boolean moreElements = listCursor.toNextSibling();
while (moreElements) {
moved = false;
// items (elements) that need to be sorted
while (headCursor.comparePosition(listCursor) < 0) {
if (comp.compare(headCursor, listCursor) > 0) {
// We have found the position in the sorted list, insert the element and the
// text following the element in the current position
// Move the element
listCursor.moveXml(headCursor);
// Move the text following the element
while (!listCursor.isStart() && !listCursor.isEnd()) listCursor.moveXml(headCursor);
moreElements = listCursor.isStart();
moved = true;
break;
}
headCursor.toNextSibling();
}
if (!moved) {
// Because during the move of a fragment of XML, the listCursor is also moved, in
// case we didn't need to move XML (the new element to be inserted happened to
// be the last one in order), we need to move this cursor
moreElements = listCursor.toNextSibling();
}
// Reposition the head of the sorted list
headCursor.toParent();
headCursor.toFirstChild();
}
}
use of org.apache.xmlbeans.XmlCursor in project poi by apache.
the class XSLFTable method prototype.
static CTGraphicalObjectFrame prototype(int shapeId) {
CTGraphicalObjectFrame frame = CTGraphicalObjectFrame.Factory.newInstance();
CTGraphicalObjectFrameNonVisual nvGr = frame.addNewNvGraphicFramePr();
CTNonVisualDrawingProps cnv = nvGr.addNewCNvPr();
cnv.setName("Table " + shapeId);
cnv.setId(shapeId + 1);
nvGr.addNewCNvGraphicFramePr().addNewGraphicFrameLocks().setNoGrp(true);
nvGr.addNewNvPr();
frame.addNewXfrm();
CTGraphicalObjectData gr = frame.addNewGraphic().addNewGraphicData();
XmlCursor grCur = gr.newCursor();
grCur.toNextToken();
grCur.beginElement(new QName(DRAWINGML_URI, "tbl"));
CTTable tbl = CTTable.Factory.newInstance();
tbl.addNewTblPr();
tbl.addNewTblGrid();
XmlCursor tblCur = tbl.newCursor();
tblCur.moveXmlContents(grCur);
tblCur.dispose();
grCur.dispose();
gr.setUri(TABLE_URI);
return frame;
}
use of org.apache.xmlbeans.XmlCursor in project poi by apache.
the class XSLFTextParagraph method getDefaultMasterStyle.
/**
* @return master style text paragraph properties, or <code>null</code> if
* there are no master slides or the master slides do not contain a text paragraph
*/
/* package */
CTTextParagraphProperties getDefaultMasterStyle() {
CTPlaceholder ph = _shape.getCTPlaceholder();
String defaultStyleSelector;
switch(ph == null ? -1 : ph.getType().intValue()) {
case STPlaceholderType.INT_TITLE:
case STPlaceholderType.INT_CTR_TITLE:
defaultStyleSelector = "titleStyle";
break;
// no placeholder means plain text box
case -1:
case STPlaceholderType.INT_FTR:
case STPlaceholderType.INT_SLD_NUM:
case STPlaceholderType.INT_DT:
defaultStyleSelector = "otherStyle";
break;
default:
defaultStyleSelector = "bodyStyle";
break;
}
int level = getIndentLevel();
// wind up and find the root master sheet which must be slide master
final String nsPML = "http://schemas.openxmlformats.org/presentationml/2006/main";
final String nsDML = "http://schemas.openxmlformats.org/drawingml/2006/main";
XSLFSheet masterSheet = _shape.getSheet();
for (XSLFSheet m = masterSheet; m != null; m = (XSLFSheet) m.getMasterSheet()) {
masterSheet = m;
XmlObject xo = masterSheet.getXmlObject();
XmlCursor cur = xo.newCursor();
try {
cur.push();
if ((cur.toChild(nsPML, "txStyles") && cur.toChild(nsPML, defaultStyleSelector)) || (cur.pop() && cur.toChild(nsPML, "notesStyle"))) {
while (level >= 0) {
cur.push();
if (cur.toChild(nsDML, "lvl" + (level + 1) + "pPr")) {
return (CTTextParagraphProperties) cur.getObject();
}
cur.pop();
level--;
}
}
} finally {
cur.dispose();
}
}
return null;
}
use of org.apache.xmlbeans.XmlCursor in project poi by apache.
the class XSLFTextParagraph method copy.
void copy(XSLFTextParagraph other) {
if (other == this)
return;
CTTextParagraph thisP = getXmlObject();
CTTextParagraph otherP = other.getXmlObject();
if (thisP.isSetPPr())
thisP.unsetPPr();
if (thisP.isSetEndParaRPr())
thisP.unsetEndParaRPr();
_runs.clear();
for (int i = thisP.sizeOfBrArray(); i > 0; i--) {
thisP.removeBr(i - 1);
}
for (int i = thisP.sizeOfRArray(); i > 0; i--) {
thisP.removeR(i - 1);
}
for (int i = thisP.sizeOfFldArray(); i > 0; i--) {
thisP.removeFld(i - 1);
}
XmlCursor thisC = thisP.newCursor();
thisC.toEndToken();
XmlCursor otherC = otherP.newCursor();
otherC.copyXmlContents(thisC);
otherC.dispose();
thisC.dispose();
List<XSLFTextRun> otherRs = other.getTextRuns();
int i = 0;
for (CTRegularTextRun rtr : thisP.getRArray()) {
XSLFTextRun run = newTextRun(rtr);
run.copy(otherRs.get(i++));
_runs.add(run);
}
// set properties again, in case we are based on a different
// template
TextAlign srcAlign = other.getTextAlign();
if (srcAlign != getTextAlign()) {
setTextAlign(srcAlign);
}
boolean isBullet = other.isBullet();
if (isBullet != isBullet()) {
setBullet(isBullet);
if (isBullet) {
String buFont = other.getBulletFont();
if (buFont != null && !buFont.equals(getBulletFont())) {
setBulletFont(buFont);
}
String buChar = other.getBulletCharacter();
if (buChar != null && !buChar.equals(getBulletCharacter())) {
setBulletCharacter(buChar);
}
PaintStyle buColor = other.getBulletFontColor();
if (buColor != null && !buColor.equals(getBulletFontColor())) {
setBulletFontColor(buColor);
}
Double buSize = other.getBulletFontSize();
if (!doubleEquals(buSize, getBulletFontSize())) {
setBulletFontSize(buSize);
}
}
}
Double leftMargin = other.getLeftMargin();
if (!doubleEquals(leftMargin, getLeftMargin())) {
setLeftMargin(leftMargin);
}
Double indent = other.getIndent();
if (!doubleEquals(indent, getIndent())) {
setIndent(indent);
}
Double spaceAfter = other.getSpaceAfter();
if (!doubleEquals(spaceAfter, getSpaceAfter())) {
setSpaceAfter(spaceAfter);
}
Double spaceBefore = other.getSpaceBefore();
if (!doubleEquals(spaceBefore, getSpaceBefore())) {
setSpaceBefore(spaceBefore);
}
Double lineSpacing = other.getLineSpacing();
if (!doubleEquals(lineSpacing, getLineSpacing())) {
setLineSpacing(lineSpacing);
}
}
use of org.apache.xmlbeans.XmlCursor 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;
}
Aggregations