/** * Verifies that cursor is on the right position. * * @param cursor * @return true if the cursor is within a CTFtnEdn element. */ private boolean isCursorInFtn(XmlCursor cursor) { XmlCursor verify = cursor.newCursor(); verify.toParent(); if (verify.getObject() == this.ctFtnEdn) { return true; } return false; }
/** * verifies that cursor is on the right position */ private boolean isCursorInTableCell(XmlCursor cursor) { XmlCursor verify = cursor.newCursor(); verify.toParent(); boolean result = (verify.getObject() == this.ctTc); verify.dispose(); return result; }
/** * 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; }
public void readHdrFtr() { bodyElements = new ArrayList<>(); paragraphs = new ArrayList<>(); tables = new ArrayList<>(); // parse the document with cursor and add // the XmlObject to its lists XmlCursor cursor = headerFooter.newCursor(); cursor.selectPath("./*"); while (cursor.toNextSelection()) { XmlObject o = cursor.getObject(); if (o instanceof CTP) { XWPFParagraph p = new XWPFParagraph((CTP) o, this); paragraphs.add(p); bodyElements.add(p); } if (o instanceof CTTbl) { XWPFTable t = new XWPFTable((CTTbl) o, this); tables.add(t); bodyElements.add(t); } } cursor.dispose(); }
/** * @return the phonetic (ruby) string associated with this run or an empty String if none exists */ public String getPhonetic() { StringBuilder text = new StringBuilder(64); // 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 CTRuby) { handleRuby(o, text, true); } } // Any picture text? if (pictureText != null && pictureText.length() > 0) { text.append("\n").append(pictureText).append("\n"); } c.dispose(); return text.toString(); }
public XWPFFooter(XWPFDocument doc, CTHdrFtr hdrFtr) throws IOException { super(doc, hdrFtr); XmlCursor cursor = headerFooter.newCursor(); cursor.selectPath("./*"); while (cursor.toNextSelection()) { XmlObject o = cursor.getObject(); if (o instanceof CTP) { XWPFParagraph p = new XWPFParagraph((CTP) o, this); paragraphs.add(p); bodyElements.add(p); } if (o instanceof CTTbl) { XWPFTable t = new XWPFTable((CTTbl) o, this); tables.add(t); bodyElements.add(t); } } cursor.dispose(); }
/** * Return direct child objects of this shape * * @param childClass the class to cast the properties to * @param namespace the namespace - usually it is {@code "http://schemas.openxmlformats.org/presentationml/2006/main"} * @param nodename the node name, without prefix * @return the properties object or null if it can't be found */ @SuppressWarnings({"unchecked", "WeakerAccess", "unused", "SameParameterValue"}) protected <T extends XmlObject> T getChild(Class<T> childClass, String namespace, String nodename) { XmlCursor cur = getXmlObject().newCursor(); T child = null; if (cur.toChild(namespace, nodename)) { child = (T)cur.getObject(); } if (cur.toChild(XSLFRelation.NS_DRAWINGML, nodename)) { child = (T)cur.getObject(); } cur.dispose(); return child; }
/** * verifies that cursor is on the right position * * @param cursor */ private boolean isCursorInBody(XmlCursor cursor) { XmlCursor verify = cursor.newCursor(); verify.toParent(); boolean result = (verify.getObject() == this.ctDocument.getBody()); verify.dispose(); return result; }
protected void init() { XmlCursor cursor = ctFtnEdn.newCursor(); //copied from XWPFDocument...should centralize this code //to avoid duplication 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); } } cursor.dispose(); }
XSLFTextParagraph(CTTextParagraph p, XSLFTextShape shape){ _p = p; _runs = new ArrayList<>(); _shape = shape; XmlCursor c = _p.newCursor(); try { if (c.toFirstChild()) { do { XmlObject r = c.getObject(); if (r instanceof CTTextLineBreak) { _runs.add(new XSLFLineBreak((CTTextLineBreak)r, this)); } else if (r instanceof CTRegularTextRun || r instanceof CTTextField) { _runs.add(new XSLFTextRun(r, this)); } } while (c.toNextSibling()); } } finally { c.dispose(); } }
public XWPFHeader(XWPFDocument doc, CTHdrFtr hdrFtr) { super(doc, hdrFtr); XmlCursor cursor = headerFooter.newCursor(); cursor.selectPath("./*"); while (cursor.toNextSelection()) { XmlObject o = cursor.getObject(); if (o instanceof CTP) { XWPFParagraph p = new XWPFParagraph((CTP) o, this); paragraphs.add(p); } if (o instanceof CTTbl) { XWPFTable t = new XWPFTable((CTTbl) o, this); tables.add(t); } } cursor.dispose(); }
public XWPFSDTContent(CTSdtContentBlock block, IBody part, IRunBody parent) { if (block == null) { return; } XmlCursor cursor = block.newCursor(); cursor.selectPath("./*"); while (cursor.toNextSelection()) { XmlObject o = cursor.getObject(); if (o instanceof CTP) { XWPFParagraph p = new XWPFParagraph((CTP) o, part); bodyElements.add(p); // paragraphs.add(p); } else if (o instanceof CTTbl) { XWPFTable t = new XWPFTable((CTTbl) o, part); bodyElements.add(t); // tables.add(t); } else if (o instanceof CTSdtBlock) { XWPFSDT c = new XWPFSDT(((CTSdtBlock) o), part); bodyElements.add(c); // contentControls.add(c); } else if (o instanceof CTR) { XWPFRun run = new XWPFRun((CTR) o, parent); // runs.add(run); bodyElements.add(run); } } cursor.dispose(); }
/** * Returns the string version of the text, with tabs and * carriage returns in place of their xml equivalents. */ @Override public String text() { StringBuilder text = new StringBuilder(64); // 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 CTRuby) { handleRuby(o, text, false); continue; } _getText(o, text); } c.dispose(); return text.toString(); }
/** * create and return a list of all XWPFTableCell * who belongs to this row * * @return a list of {@link XWPFTableCell} */ public List<ICell> getTableICells() { List<ICell> cells = new ArrayList<>(); //Can't use ctRow.getTcList because that only gets table cells //Can't use ctRow.getSdtList because that only gets sdts that are at cell level XmlCursor cursor = ctRow.newCursor(); cursor.selectPath("./*"); while (cursor.toNextSelection()) { XmlObject o = cursor.getObject(); if (o instanceof CTTc) { cells.add(new XWPFTableCell((CTTc) o, this, table.getBody())); } else if (o instanceof CTSdtCell) { cells.add(new XWPFSDTCell((CTSdtCell) o, this, table.getBody())); } } cursor.dispose(); return cells; }
/*package*/ XSLFObjectShape(CTGraphicalObjectFrame shape, XSLFSheet sheet){ super(shape, sheet); CTGraphicalObjectData god = shape.getGraphic().getGraphicData(); XmlCursor xc = god.newCursor(); // select oleObj potentially under AlternateContent // usually the mc:Choice element will be selected first xc.selectPath("declare namespace p='"+PML_NS+"' .//p:oleObj"); try { if (!xc.toNextSelection()) { throw new IllegalStateException("p:oleObj element was not found in\n " + god); } XmlObject xo = xc.getObject(); // Pesky XmlBeans bug - see Bugzilla #49934 // it never happens when using the full ooxml-schemas jar but may happen with the abridged poi-ooxml-schemas if (xo instanceof XmlAnyTypeImpl){ String errStr = "Schemas (*.xsb) for CTOleObject can't be loaded - usually this happens when OSGI " + "loading is used and the thread context classloader has no reference to " + "the xmlbeans classes - use POIXMLTypeLoader.setClassLoader() to set the loader, " + "e.g. with CTOleObject.class.getClassLoader()" ; throw new IllegalStateException(errStr); } _oleObject = (CTOleObject)xo; } finally { xc.dispose(); } }
/** * get the TableCell which belongs to the TableCell */ 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 tr = table.getRow(row); if (tr == null) { return null; } return tr.getTableCell(cell); }
/*package*/ XSLFTable(CTGraphicalObjectFrame shape, XSLFSheet sheet){ super(shape, sheet); CTGraphicalObjectData god = shape.getGraphic().getGraphicData(); XmlCursor xc = god.newCursor(); try { if (!xc.toChild(XSLFRelation.NS_DRAWINGML, "tbl")) { throw new IllegalStateException("a:tbl element was not found in\n " + god); } XmlObject xo = xc.getObject(); // Pesky XmlBeans bug - see Bugzilla #49934 // it never happens when using the full ooxml-schemas jar but may happen with the abridged poi-ooxml-schemas if (xo instanceof XmlAnyTypeImpl){ String errStr = "Schemas (*.xsb) for CTTable can't be loaded - usually this happens when OSGI " + "loading is used and the thread context classloader has no reference to " + "the xmlbeans classes" ; throw new IllegalStateException(errStr); } _table = (CTTable)xo; } finally { xc.dispose(); } _rows = new ArrayList<>(_table.sizeOfTrArray()); for(CTTableRow row : _table.getTrList()) { _rows.add(new XSLFTableRow(row, this)); } updateRowColIndexes(); }
/** * get the TableCell which belongs to the TableCell * * @param cell */ public XWPFTableCell getTableCell(CTTc cell) { XmlCursor cursor = cell.newCursor(); cursor.toParent(); XmlObject o = cursor.getObject(); if (!(o instanceof CTRow)) { cursor.dispose(); 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); return tableRow.getTableCell(cell); }
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(getSheet(), ct.getFrom(), ct.getExt()); } else if (parentXbean instanceof CTAbsoluteAnchor) { CTAbsoluteAnchor ct = (CTAbsoluteAnchor) parentXbean; anchor = new XSSFClientAnchor(getSheet(), ct.getPos(), ct.getExt()); } } return anchor; }