Search in sources :

Example 1 with CTShape

use of com.microsoft.schemas.vml.CTShape in project poi by apache.

the class XSSFVMLDrawing method read.

@SuppressWarnings("resource")
protected void read(InputStream is) throws IOException, XmlException {
    Document doc;
    try {
        /*
             * This is a seriously sick fix for the fact that some .xlsx files contain raw bits
             * of HTML, without being escaped or properly turned into XML.
             * The result is that they contain things like >br<, which breaks the XML parsing.
             * This very sick InputStream wrapper attempts to spot these go past, and fix them.
             */
        doc = DocumentHelper.readDocument(new ReplacingInputStream(is, "<br>", "<br/>"));
    } catch (SAXException e) {
        throw new XmlException(e.getMessage(), e);
    }
    XmlObject root = XmlObject.Factory.parse(doc, DEFAULT_XML_OPTIONS);
    _qnames = new ArrayList<QName>();
    _items = new ArrayList<XmlObject>();
    for (XmlObject obj : root.selectPath("$this/xml/*")) {
        Node nd = obj.getDomNode();
        QName qname = new QName(nd.getNamespaceURI(), nd.getLocalName());
        if (qname.equals(QNAME_SHAPE_LAYOUT)) {
            _items.add(CTShapeLayout.Factory.parse(obj.xmlText(), DEFAULT_XML_OPTIONS));
        } else if (qname.equals(QNAME_SHAPE_TYPE)) {
            CTShapetype st = CTShapetype.Factory.parse(obj.xmlText(), DEFAULT_XML_OPTIONS);
            _items.add(st);
            _shapeTypeId = st.getId();
        } else if (qname.equals(QNAME_SHAPE)) {
            CTShape shape = CTShape.Factory.parse(obj.xmlText(), DEFAULT_XML_OPTIONS);
            String id = shape.getId();
            if (id != null) {
                Matcher m = ptrn_shapeId.matcher(id);
                if (m.find()) {
                    _shapeId = Math.max(_shapeId, Integer.parseInt(m.group(1)));
                }
            }
            _items.add(shape);
        } else {
            Document doc2;
            try {
                InputSource is2 = new InputSource(new StringReader(obj.xmlText()));
                doc2 = DocumentHelper.readDocument(is2);
            } catch (SAXException e) {
                throw new XmlException(e.getMessage(), e);
            }
            _items.add(XmlObject.Factory.parse(doc2, DEFAULT_XML_OPTIONS));
        }
        _qnames.add(qname);
    }
}
Also used : InputSource(org.xml.sax.InputSource) Matcher(java.util.regex.Matcher) QName(javax.xml.namespace.QName) Node(org.w3c.dom.Node) CTShape(com.microsoft.schemas.vml.CTShape) Document(org.w3c.dom.Document) ReplacingInputStream(org.apache.poi.util.ReplacingInputStream) SAXException(org.xml.sax.SAXException) CTShapetype(com.microsoft.schemas.vml.CTShapetype) XmlException(org.apache.xmlbeans.XmlException) StringReader(java.io.StringReader) XmlObject(org.apache.xmlbeans.XmlObject)

Example 2 with CTShape

use of com.microsoft.schemas.vml.CTShape in project poi by apache.

the class XSSFVMLDrawing method findCommentShape.

/**
     * Find a shape with ClientData of type "NOTE" and the specified row and column
     *
     * @return the comment shape or <code>null</code>
     */
protected CTShape findCommentShape(int row, int col) {
    for (XmlObject itm : _items) {
        if (itm instanceof CTShape) {
            CTShape sh = (CTShape) itm;
            if (sh.sizeOfClientDataArray() > 0) {
                CTClientData cldata = sh.getClientDataArray(0);
                if (cldata.getObjectType() == STObjectType.NOTE) {
                    int crow = cldata.getRowArray(0).intValue();
                    int ccol = cldata.getColumnArray(0).intValue();
                    if (crow == row && ccol == col) {
                        return sh;
                    }
                }
            }
        }
    }
    return null;
}
Also used : XmlObject(org.apache.xmlbeans.XmlObject) CTShape(com.microsoft.schemas.vml.CTShape) CTClientData(com.microsoft.schemas.office.excel.CTClientData)

Example 3 with CTShape

use of com.microsoft.schemas.vml.CTShape in project poi by apache.

the class TestXSSFComment method testBug58175.

@Test
public void testBug58175() throws IOException {
    Workbook wb = new SXSSFWorkbook();
    try {
        Sheet sheet = wb.createSheet();
        Row row = sheet.createRow(1);
        Cell cell = row.createCell(3);
        cell.setCellValue("F4");
        CreationHelper factory = wb.getCreationHelper();
        // When the comment box is visible, have it show in a 1x3 space
        ClientAnchor anchor = factory.createClientAnchor();
        anchor.setCol1(cell.getColumnIndex());
        anchor.setCol2(cell.getColumnIndex() + 1);
        anchor.setRow1(row.getRowNum());
        anchor.setRow2(row.getRowNum() + 3);
        XSSFClientAnchor ca = (XSSFClientAnchor) anchor;
        // create comments and vmlDrawing parts if they don't exist
        CommentsTable comments = ((SXSSFWorkbook) wb).getXSSFWorkbook().getSheetAt(0).getCommentsTable(true);
        XSSFVMLDrawing vml = ((SXSSFWorkbook) wb).getXSSFWorkbook().getSheetAt(0).getVMLDrawing(true);
        CTShape vmlShape1 = vml.newCommentShape();
        if (ca.isSet()) {
            String position = ca.getCol1() + ", 0, " + ca.getRow1() + ", 0, " + ca.getCol2() + ", 0, " + ca.getRow2() + ", 0";
            vmlShape1.getClientDataArray(0).setAnchorArray(0, position);
        }
        // create the comment in two different ways and verify that there is no difference
        XSSFComment shape1 = new XSSFComment(comments, comments.newComment(CellAddress.A1), vmlShape1);
        shape1.setColumn(ca.getCol1());
        shape1.setRow(ca.getRow1());
        CTShape vmlShape2 = vml.newCommentShape();
        if (ca.isSet()) {
            String position = ca.getCol1() + ", 0, " + ca.getRow1() + ", 0, " + ca.getCol2() + ", 0, " + ca.getRow2() + ", 0";
            vmlShape2.getClientDataArray(0).setAnchorArray(0, position);
        }
        CellAddress ref = new CellAddress(ca.getRow1(), ca.getCol1());
        XSSFComment shape2 = new XSSFComment(comments, comments.newComment(ref), vmlShape2);
        assertEquals(shape1.getAuthor(), shape2.getAuthor());
        assertEquals(shape1.getClientAnchor(), shape2.getClientAnchor());
        assertEquals(shape1.getColumn(), shape2.getColumn());
        assertEquals(shape1.getRow(), shape2.getRow());
        assertEquals(shape1.getCTComment().toString(), shape2.getCTComment().toString());
        assertEquals(shape1.getCTComment().getRef(), shape2.getCTComment().getRef());
        /*CommentsTable table1 = shape1.getCommentsTable();
            CommentsTable table2 = shape2.getCommentsTable();
            assertEquals(table1.getCTComments().toString(), table2.getCTComments().toString());
            assertEquals(table1.getNumberOfComments(), table2.getNumberOfComments());
            assertEquals(table1.getRelations(), table2.getRelations());*/
        assertEquals("The vmlShapes should have equal content afterwards", vmlShape1.toString().replaceAll("_x0000_s\\d+", "_x0000_s0000"), vmlShape2.toString().replaceAll("_x0000_s\\d+", "_x0000_s0000"));
    } finally {
        wb.close();
    }
}
Also used : CreationHelper(org.apache.poi.ss.usermodel.CreationHelper) CTShape(com.microsoft.schemas.vml.CTShape) RichTextString(org.apache.poi.ss.usermodel.RichTextString) HSSFRichTextString(org.apache.poi.hssf.usermodel.HSSFRichTextString) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) CommentsTable(org.apache.poi.xssf.model.CommentsTable) CellAddress(org.apache.poi.ss.util.CellAddress) ClientAnchor(org.apache.poi.ss.usermodel.ClientAnchor) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) Row(org.apache.poi.ss.usermodel.Row) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) Test(org.junit.Test)

Example 4 with CTShape

use of com.microsoft.schemas.vml.CTShape in project poi by apache.

the class TestXSSFComment method constructor.

/**
     * test properties of a newly constructed comment
     */
@Test
public void constructor() {
    CommentsTable sheetComments = new CommentsTable();
    assertNotNull(sheetComments.getCTComments().getCommentList());
    assertNotNull(sheetComments.getCTComments().getAuthors());
    assertEquals(1, sheetComments.getCTComments().getAuthors().sizeOfAuthorArray());
    assertEquals(1, sheetComments.getNumberOfAuthors());
    CTComment ctComment = sheetComments.newComment(CellAddress.A1);
    CTShape vmlShape = CTShape.Factory.newInstance();
    XSSFComment comment = new XSSFComment(sheetComments, ctComment, vmlShape);
    assertEquals(null, comment.getString());
    assertEquals(0, comment.getRow());
    assertEquals(0, comment.getColumn());
    assertEquals("", comment.getAuthor());
    assertEquals(false, comment.isVisible());
}
Also used : CTComment(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment) CTShape(com.microsoft.schemas.vml.CTShape) CommentsTable(org.apache.poi.xssf.model.CommentsTable) Test(org.junit.Test)

Example 5 with CTShape

use of com.microsoft.schemas.vml.CTShape in project poi by apache.

the class TestXSSFComment method getSetCol.

@Test
public void getSetCol() {
    CommentsTable sheetComments = new CommentsTable();
    XSSFVMLDrawing vml = new XSSFVMLDrawing();
    CTComment ctComment = sheetComments.newComment(CellAddress.A1);
    CTShape vmlShape = vml.newCommentShape();
    XSSFComment comment = new XSSFComment(sheetComments, ctComment, vmlShape);
    comment.setColumn(1);
    assertEquals(1, comment.getColumn());
    assertEquals(1, new CellReference(ctComment.getRef()).getCol());
    assertEquals(1, vmlShape.getClientDataArray(0).getColumnArray(0).intValue());
    comment.setColumn(5);
    assertEquals(5, comment.getColumn());
    assertEquals(5, new CellReference(ctComment.getRef()).getCol());
    assertEquals(5, vmlShape.getClientDataArray(0).getColumnArray(0).intValue());
}
Also used : CTComment(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment) CTShape(com.microsoft.schemas.vml.CTShape) CellReference(org.apache.poi.ss.util.CellReference) CommentsTable(org.apache.poi.xssf.model.CommentsTable) Test(org.junit.Test)

Aggregations

CTShape (com.microsoft.schemas.vml.CTShape)11 Test (org.junit.Test)7 CommentsTable (org.apache.poi.xssf.model.CommentsTable)4 CTClientData (com.microsoft.schemas.office.excel.CTClientData)3 CTShapetype (com.microsoft.schemas.vml.CTShapetype)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 XmlObject (org.apache.xmlbeans.XmlObject)3 CTComment (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment)3 CTShadow (com.microsoft.schemas.vml.CTShadow)2 InputStream (java.io.InputStream)2 BigInteger (java.math.BigInteger)2 CellReference (org.apache.poi.ss.util.CellReference)2 STTrueFalseBlank (com.microsoft.schemas.office.excel.STTrueFalseBlank)1 CTLock (com.microsoft.schemas.office.office.CTLock)1 CTShapeLayout (com.microsoft.schemas.office.office.CTShapeLayout)1 CTFormulas (com.microsoft.schemas.vml.CTFormulas)1 CTGroup (com.microsoft.schemas.vml.CTGroup)1 CTH (com.microsoft.schemas.vml.CTH)1 CTHandles (com.microsoft.schemas.vml.CTHandles)1 CTPath (com.microsoft.schemas.vml.CTPath)1