Search in sources :

Example 46 with XmlCursor

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;
}
Also used : XmlCursor(org.apache.xmlbeans.XmlCursor)

Example 47 with XmlCursor

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();
    }
}
Also used : CTRst(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst) QName(javax.xml.namespace.QName) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell) BaseTestXCell(org.apache.poi.ss.usermodel.BaseTestXCell) Cell(org.apache.poi.ss.usermodel.Cell) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) XmlCursor(org.apache.xmlbeans.XmlCursor) Test(org.junit.Test)

Example 48 with XmlCursor

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;
}
Also used : CTTextLineBreak(org.openxmlformats.schemas.drawingml.x2006.main.CTTextLineBreak) XmlObject(org.apache.xmlbeans.XmlObject) XmlCursor(org.apache.xmlbeans.XmlCursor) CTRegularTextRun(org.openxmlformats.schemas.drawingml.x2006.main.CTRegularTextRun)

Example 49 with XmlCursor

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);
}
Also used : Node(org.w3c.dom.Node) XmlObject(org.apache.xmlbeans.XmlObject) XmlCursor(org.apache.xmlbeans.XmlCursor)

Example 50 with XmlCursor

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);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : XWPFParagraph(org.apache.poi.xwpf.usermodel.XWPFParagraph) CTHMerge(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHMerge) XWPFTableCell(org.apache.poi.xwpf.usermodel.XWPFTableCell) XWPFRun(org.apache.poi.xwpf.usermodel.XWPFRun) XWPFTable(org.apache.poi.xwpf.usermodel.XWPFTable) CTTcPr(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr) XmlCursor(org.apache.xmlbeans.XmlCursor) XWPFTableRow(org.apache.poi.xwpf.usermodel.XWPFTableRow)

Aggregations

XmlCursor (org.apache.xmlbeans.XmlCursor)160 XmlObject (org.apache.xmlbeans.XmlObject)68 QName (javax.xml.namespace.QName)21 XmlException (org.apache.xmlbeans.XmlException)16 TokenType (org.apache.xmlbeans.XmlCursor.TokenType)14 CTTbl (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl)14 CTP (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP)10 ArrayList (java.util.ArrayList)9 POSIXApplicationType (org.ggf.schemas.jsdl.x2005.x11.jsdlPosix.POSIXApplicationType)8 HPCProfileApplicationType (org.ggf.schemas.jsdl.x2006.x07.jsdlHpcpa.HPCProfileApplicationType)8 SPMDApplicationType (org.ogf.schemas.jsdl.x2007.x02.jsdlSpmd.SPMDApplicationType)8 IOException (java.io.IOException)5 POIXMLException (org.apache.poi.POIXMLException)5 InputStream (java.io.InputStream)4 DrawPaint (org.apache.poi.sl.draw.DrawPaint)3 ArrayType (org.dmg.pmml.ArrayType)3 ApplicationType (org.ggf.schemas.jsdl.x2005.x11.jsdl.ApplicationType)3 LineString (org.locationtech.jts.geom.LineString)3 CTRow (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow)3 CTSdtBlock (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtBlock)3