use of org.apache.xmlbeans.XmlCursor in project poi by apache.
the class XWPFTableCell method isCursorInTableCell.
/**
* 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;
}
use of org.apache.xmlbeans.XmlCursor in project poi by apache.
the class TestSXSSFCell method testPreserveSpaces.
@Test
public void testPreserveSpaces() throws IOException {
String[] samplesWithSpaces = { " POI", "POI ", " POI ", "\nPOI", "\n\nPOI \n" };
for (String str : samplesWithSpaces) {
Workbook swb = _testDataProvider.createWorkbook();
Cell sCell = swb.createSheet().createRow(0).createCell(0);
sCell.setCellValue(str);
assertEquals(sCell.getStringCellValue(), str);
// read back as XSSF and check that xml:spaces="preserve" is set
XSSFWorkbook xwb = (XSSFWorkbook) _testDataProvider.writeOutAndReadBack(swb);
XSSFCell xCell = xwb.getSheetAt(0).getRow(0).getCell(0);
CTRst is = xCell.getCTCell().getIs();
XmlCursor c = is.newCursor();
c.toNextToken();
String t = c.getAttributeText(new QName("http://www.w3.org/XML/1998/namespace", "space"));
c.dispose();
assertEquals("expected xml:spaces=\"preserve\" \"" + str + "\"", "preserve", t);
xwb.close();
swb.close();
}
}
use of org.apache.xmlbeans.XmlCursor in project poi by apache.
the class DrawingParagraph method getText.
public CharSequence getText() {
StringBuilder text = new StringBuilder();
XmlCursor c = p.newCursor();
c.selectPath("./*");
while (c.toNextSelection()) {
XmlObject o = c.getObject();
if (o instanceof CTRegularTextRun) {
CTRegularTextRun txrun = (CTRegularTextRun) o;
text.append(txrun.getT());
} else if (o instanceof CTTextLineBreak) {
text.append('\n');
}
}
c.dispose();
return text;
}
use of org.apache.xmlbeans.XmlCursor in project poi by apache.
the class XSSFVMLDrawing method write.
protected void write(OutputStream out) throws IOException {
XmlObject rootObject = XmlObject.Factory.newInstance();
XmlCursor rootCursor = rootObject.newCursor();
rootCursor.toNextToken();
rootCursor.beginElement("xml");
for (int i = 0; i < _items.size(); i++) {
XmlCursor xc = _items.get(i).newCursor();
rootCursor.beginElement(_qnames.get(i));
while (xc.toNextToken() == XmlCursor.TokenType.ATTR) {
Node anode = xc.getDomNode();
rootCursor.insertAttributeWithValue(anode.getLocalName(), anode.getNamespaceURI(), anode.getNodeValue());
}
xc.toStartDoc();
xc.copyXmlContents(rootCursor);
rootCursor.toNextToken();
xc.dispose();
}
rootCursor.dispose();
rootObject.save(out, DEFAULT_XML_OPTIONS);
}
use of org.apache.xmlbeans.XmlCursor in project pmph by BCSquad.
the class WordHelper method insertXWPFParagraph.
public static void insertXWPFParagraph(String key, XWPFDocument doc2, XWPFTable xwpfTable) {
List<XWPFParagraph> paragraphList = doc2.getParagraphs();
if (paragraphList != null && paragraphList.size() > 0) {
for (int i = 0; i < paragraphList.size(); i++) {
List<XWPFRun> runs = paragraphList.get(i).getRuns();
for (XWPFRun run : runs) {
String text = run.getText(0);
if (text != null) {
if (text.equals(key)) {
XmlCursor cursor = paragraphList.get(i + 1).getCTP().newCursor();
XWPFTable table = doc2.insertNewTbl(cursor);
XWPFTableRow tableRow = table.getRow(0);
for (int cellNum = 0; cellNum < 27; cellNum++) {
tableRow.addNewTableCell();
}
for (int rowNum = 0; rowNum < 3; rowNum++) {
table.createRow();
}
// 合并列(没有作用)
for (int rowNum = 0; rowNum < table.getRows().size(); rowNum++) {
for (int cellIndex = 0; cellIndex < tableRow.getTableCells().size(); cellIndex++) {
CTHMerge hmerge = CTHMerge.Factory.newInstance();
if (cellIndex == 0) {
// The first merged cell is set with RESTART merge value
hmerge.setVal(STMerge.RESTART);
} else {
// Cells which join (merge) the first one, are set with CONTINUE
hmerge.setVal(STMerge.CONTINUE);
}
XWPFTableCell cell = table.getRow(rowNum).getCell(cellIndex);
// Try getting the TcPr. Not simply setting an new one every time.
CTTcPr tcPr = cell.getCTTc().getTcPr();
if (tcPr != null) {
tcPr.setHMerge(hmerge);
} else {
// only set an new TcPr if there is not one already
tcPr = CTTcPr.Factory.newInstance();
tcPr.setHMerge(hmerge);
cell.getCTTc().setTcPr(tcPr);
}
}
}
// 合并行
for (int col = 0; col < tableRow.getTableCells().size(); col++) {
for (int rowIndex = 0; rowIndex < table.getRows().size(); rowIndex++) {
XWPFTableCell cell = table.getRow(rowIndex).getCell(col);
if (rowIndex == 0) {
// The first merged cell is set with RESTART merge value
cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART);
} else {
// Cells which join (merge) the first one, are set with CONTINUE
cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE);
}
}
}
}
}
}
}
}
}
Aggregations