aboutsummaryrefslogtreecommitdiffstats
path: root/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFCommonSlideData.java
blob: b32a430f393e6662b49e60e8f5edcf8ad5530a97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package org.apache.poi.xslf.usermodel;

import org.apache.xmlbeans.XmlCursor;
import org.apache.xmlbeans.XmlObject;
import org.openxmlformats.schemas.drawingml.x2006.main.CTGraphicalObjectData;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTable;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextBody;
import org.openxmlformats.schemas.presentationml.x2006.main.CTCommonSlideData;
import org.openxmlformats.schemas.presentationml.x2006.main.CTGraphicalObjectFrame;
import org.openxmlformats.schemas.presentationml.x2006.main.CTGroupShape;
import org.openxmlformats.schemas.presentationml.x2006.main.CTShape;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class XSLFCommonSlideData {
    private final CTCommonSlideData data;

    public XSLFCommonSlideData(CTCommonSlideData data) {
        this.data = data;
    }

    public List<DrawingParagraph> getText() {
        CTGroupShape gs = data.getSpTree();

        List<DrawingParagraph> out = new ArrayList<DrawingParagraph>();

        processShape(gs, out);

        for (CTGroupShape shape : gs.getGrpSpArray()) {
            processShape(shape, out);
        }

        CTGraphicalObjectFrame[] graphicFrames = gs.getGraphicFrameArray();
        for (CTGraphicalObjectFrame frame: graphicFrames) {
            CTGraphicalObjectData data = frame.getGraphic().getGraphicData();
            XmlCursor c = data.newCursor();
            c.selectPath("./*");

            while (c.toNextSelection()) {
                XmlObject o = c.getObject();

                if (o instanceof CTTable) {
                    DrawingTable table = new DrawingTable((CTTable) o);

                    for (DrawingTableRow row : table.getRows()) {
                        for (DrawingTableCell cell : row.getCells()) {
                            DrawingTextBody textBody = cell.getTextBody();

                            out.addAll(Arrays.asList(textBody.getParagraphs()));
                        }
                    }
                }
            }
        }

        return out;
    }

    private void processShape(CTGroupShape gs, List<DrawingParagraph> out) {
        CTShape[] shapes = gs.getSpArray();
        for (int i = 0; i < shapes.length; i++) {
            CTTextBody ctTextBody = shapes[i].getTxBody();
            if (ctTextBody==null) {
                continue;
            }

            DrawingTextBody textBody = new DrawingTextBody(ctTextBody);

            out.addAll(Arrays.asList(textBody.getParagraphs()));
        }
    }

}