use of org.apache.xmlbeans.XmlCursor 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.XmlCursor in project poi by apache.
the class XWPFFootnote method getTableCell.
/**
* get the TableCell which belongs to the TableCell
*
* @param cell
* @see org.apache.poi.xwpf.usermodel.IBody#getTableCell(CTTc cell)
*/
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);
}
use of org.apache.xmlbeans.XmlCursor in project poi by apache.
the class XWPFHeaderFooter method removeParagraph.
/**
* Removes a specific paragraph from this header / footer
*
* @param paragraph - {@link XWPFParagraph} object to remove
*/
public void removeParagraph(XWPFParagraph paragraph) {
if (paragraphs.contains(paragraph)) {
CTP ctP = paragraph.getCTP();
XmlCursor c = ctP.newCursor();
c.removeXml();
c.dispose();
paragraphs.remove(paragraph);
bodyElements.remove(paragraph);
}
}
use of org.apache.xmlbeans.XmlCursor in project poi by apache.
the class XWPFHeaderFooter method isCursorInHdrF.
/**
* verifies that cursor is on the right position
*
* @param cursor
*/
private boolean isCursorInHdrF(XmlCursor cursor) {
XmlCursor verify = cursor.newCursor();
verify.toParent();
boolean result = (verify.getObject() == this.headerFooter);
verify.dispose();
return result;
}
use of org.apache.xmlbeans.XmlCursor in project poi by apache.
the class XWPFHeaderFooter method clearHeaderFooter.
/**
* Clears all paragraphs and tables from this header / footer
*/
public void clearHeaderFooter() {
XmlCursor c = headerFooter.newCursor();
c.removeXmlContents();
c.dispose();
paragraphs.clear();
tables.clear();
bodyElements.clear();
}
Aggregations