Skip to content

Commit

Permalink
Merge pull request #104 from airslate-oss/MSP-610-add-graphic-usermodels
Browse files Browse the repository at this point in the history
[MSP-610] added graphic usermodels
  • Loading branch information
alekseytatarynov authored Jun 13, 2023
2 parents 047b9b2 + 2530f2d commit 6ee240b
Show file tree
Hide file tree
Showing 36 changed files with 2,108 additions and 313 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ subprojects {
apply plugin: 'de.thetaphi.forbiddenapis'
apply plugin: 'com.github.spotbugs'

version = '5.2.3-AIRSLATE-26.8'
version = '5.2.3-AIRSLATE-43'
ext {
bouncyCastleVersion = '1.70'
commonsCodecVersion = '1.15'
Expand Down
4 changes: 2 additions & 2 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ under the License.

<description>The Apache POI project Ant build.</description>

<property name="version.id" value="5.2.3-AIRSLATE"/>
<property name="version.id" value="5.2.3-AIRSLATE-43"/>
<property name="release.rc" value=""/>

<property environment="env"/>
Expand Down Expand Up @@ -961,7 +961,7 @@ under the License.
<zipfileset src="${ooxml.xsds.izip.1}"/>
<fileset dir="${ooxml.visio.xsd.dir}"/>
<fileset dir="${ooxml.schema.xsdconfig.dir}" includes="ooxmlSchemas.xsdconfig,markup-compatibility.xsd,vmlDrawing.xsd"/>
<fileset dir="${ooxml.schema.xsdconfig.dir}" includes="dml-drawing.xsd,word12.xsd,XAdES*.xsd,xlThreaded*.xsd,xmldsig*.xsd"/>
<fileset dir="${ooxml.schema.xsdconfig.dir}" includes="dml-drawing.xsd,word12.xsd,XAdES*.xsd,xlThreaded*.xsd,xmldsig*.xsd,office-word-2010-wps.xsd"/>
<zipfileset src="${ooxml.xsds.izip.2}" includes="opc-digSig.xsd,opc-relationships.xsd"/>
<fileset dir="${ooxml.security.xsd.dir}" includes="signatureInfo.xsd"/>
</copy>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ public class TestAllFiles {
"poifs/protected_sha512.xlsx",
"poifs/60320-protected.xlsx",
"poifs/protected_sha512.xlsx",
"spreadsheet/poc-xmlbomb.xlsx",
"spreadsheet/poc-xmlbomb-empty.xlsx",
};

// cheap workaround of skipping the few problematic files
Expand Down
2 changes: 1 addition & 1 deletion poi-ooxml-full/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ task copy_xsds(type: Copy) {
from ('src/main/xmlschema/org/apache/poi/xdgf')
from ('src/main/xmlschema/org/apache/poi/schemas') {
include 'XAdES*.xsd', '*.xsdconfig', 'xmldsig*.xsd', 'ooxmlSchemas.xsdconfig', 'markup-compatibility.xsd',
'vmlDrawing.xsd', 'word12.xsd', 'xlThreaded*.xsd', 'dml-drawing.xsd'
'vmlDrawing.xsd', 'word12.xsd', 'xlThreaded*.xsd', 'dml-drawing.xsd', 'office-word-2010-wps.xsd'
exclude '*.zip'
}
from ('src/main/xmlschema/org/apache/poi/poifs/crypt') {
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<xsd:schema elementFormDefault="qualified"
targetNamespace="http://schemas.microsoft.com/office/word/2010/wordprocessingShape"
xmlns="http://schemas.microsoft.com/office/word/2010/wordprocessingShape"
xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"
xmlns:w12="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:import schemaLocation="dml-main.xsd" namespace="http://schemas.openxmlformats.org/drawingml/2006/main"/>
<xsd:import schemaLocation="shared-relationshipReference.xsd"
namespace="http://schemas.openxmlformats.org/officeDocument/2006/relationships"/>
<xsd:complexType name="CT_TextboxInfo">
<xsd:sequence>
<xsd:element ref="w12:txbxContent" minOccurs="0" maxOccurs="1"/>
<xsd:element name="extLst" type="a:CT_OfficeArtExtensionList" minOccurs="0" maxOccurs="1"/>
</xsd:sequence>
<xsd:attribute name="id" type="xsd:unsignedShort" use="optional" default="0"/>
</xsd:complexType>
<xsd:complexType name="CT_LinkedTextboxInformation">
<xsd:sequence>
<xsd:element name="extLst" type="a:CT_OfficeArtExtensionList" minOccurs="0" maxOccurs="1"/>
</xsd:sequence>
<xsd:attribute name="id" type="xsd:unsignedShort" use="required"/>
<xsd:attribute name="seq" type="xsd:unsignedShort" use="required"/>
</xsd:complexType>
<xsd:complexType name="CT_WordprocessingShape">
<xsd:sequence minOccurs="1" maxOccurs="1">
<xsd:element name="cNvPr" type="a:CT_NonVisualDrawingProps" minOccurs="0" maxOccurs="1"/>
<xsd:choice minOccurs="1" maxOccurs="1">
<xsd:element name="cNvSpPr" type="a:CT_NonVisualDrawingShapeProps" minOccurs="1" maxOccurs="1"/>
<xsd:element name="cNvCnPr" type="a:CT_NonVisualConnectorProperties" minOccurs="1" maxOccurs="1"/>
</xsd:choice>
<xsd:element name="spPr" type="a:CT_ShapeProperties" minOccurs="1" maxOccurs="1"/>
<xsd:element name="style" type="a:CT_ShapeStyle" minOccurs="0" maxOccurs="1"/>
<xsd:element name="extLst" type="a:CT_OfficeArtExtensionList" minOccurs="0" maxOccurs="1"/>
<xsd:choice minOccurs="0" maxOccurs="1">
<xsd:element name="txbx" type="CT_TextboxInfo" minOccurs="1" maxOccurs="1"/>
<xsd:element name="linkedTxbx" type="CT_LinkedTextboxInformation" minOccurs="1" maxOccurs="1"/>
</xsd:choice>
<xsd:element name="bodyPr" type="a:CT_TextBodyProperties" minOccurs="1" maxOccurs="1"/>
</xsd:sequence>
<xsd:attribute name="normalEastAsianFlow" type="xsd:boolean" use="optional" default="false"/>
</xsd:complexType>
<xsd:element name="wsp" type="CT_WordprocessingShape"/>
</xsd:schema>
1 change: 1 addition & 0 deletions poi-ooxml-lite/src/main/java9/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
exports com.microsoft.schemas.office.excel;
exports com.microsoft.schemas.office.office;
exports com.microsoft.schemas.office.visio.x2012.main;
exports com.microsoft.schemas.office.word.x2010.wordprocessingShape;
exports com.microsoft.schemas.office.word;
exports com.microsoft.schemas.office.x2006.digsig;
exports com.microsoft.schemas.vml;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public class POIXMLTypeLoader {
map.put("http://schemas.openxmlformats.org/presentationml/2006/main", "p");
map.put("http://schemas.openxmlformats.org/wordprocessingml/2006/main", "w");
map.put("http://schemas.microsoft.com/office/word/2006/wordml", "wne");
map.put("http://schemas.microsoft.com/office/word/2010/wordprocessingShape", "wps");
map.put(MS_OFFICE_URN, "o");
map.put(MS_EXCEL_URN, "x");
map.put(MS_WORD_URN, "w10");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ public enum BodyType {
FOOTER,
FOOTNOTE,
TABLECELL,
COMMENT
COMMENT,
TEXTBOXCONTENT,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.apache.poi.xwpf.usermodel;

import java.util.List;

public interface IDrawing {
XWPFRun getParent();
List<XWPFTextBoxContent> getTextBoxContents();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.apache.poi.xwpf.usermodel;

public interface IDrawingContent {
XWPFDrawing getParent();
XWPFGraphicalObject getGraphicalObject();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.xwpf.usermodel;

import org.openxmlformats.schemas.drawingml.x2006.main.CTGraphicalObject;
import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTAnchor;

public class XWPFAnchor implements IDrawingContent {
private final CTAnchor ctAnchor;
private final XWPFDrawing parent;
private XWPFGraphicalObject graphic;

public XWPFAnchor(CTAnchor ctAnchor, XWPFDrawing parent) {
this.ctAnchor = ctAnchor;
this.parent = parent;
if (ctAnchor.getGraphic() != null) {
this.graphic = new XWPFGraphicalObject(ctAnchor.getGraphic(), this);
}
}

public XWPFGraphicalObject createGraphicalObject() {
CTGraphicalObject ctGraphicalObject = ctAnchor.addNewGraphic();
XWPFGraphicalObject graphicalObject = new XWPFGraphicalObject(ctGraphicalObject, this);
graphic = graphicalObject;
return graphicalObject;
}

public CTAnchor getCTAnchor() {
return ctAnchor;
}

public XWPFDrawing getParent() {
return parent;
}

public XWPFGraphicalObject getGraphicalObject() {
return graphic;
}
}
155 changes: 155 additions & 0 deletions poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFDrawing.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.xwpf.usermodel;

import org.apache.xmlbeans.XmlCursor;
import org.apache.xmlbeans.XmlObject;
import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTAnchor;
import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTInline;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDrawing;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

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

public class XWPFDrawing implements IDrawing {
private final CTDrawing ctDrawing;
private final XWPFRun parent;
private final List<IDrawingContent> drawingContents = new ArrayList<>();

public XWPFDrawing(CTDrawing ctDrawing, XWPFRun parent) {
this.ctDrawing = ctDrawing;
this.parent = parent;

try (XmlCursor cursor = ctDrawing.newCursor()) {
cursor.selectPath("./*");
while (cursor.toNextSelection()) {
XmlObject child = cursor.getObject();
if (child instanceof CTAnchor) {
drawingContents.add(new XWPFAnchor((CTAnchor) child, this));
} else if (child instanceof CTInline) {
drawingContents.add(new XWPFInline((CTInline) child, this));
}
}
}
}

public XWPFAnchor createAnchor() {
CTAnchor ctAnchor = ctDrawing.addNewAnchor();
XWPFAnchor anchor = new XWPFAnchor(ctAnchor, this);
drawingContents.add(anchor);
return anchor;
}

public XWPFInline createInline() {
CTInline ctInline = ctDrawing.addNewInline();
XWPFInline inline = new XWPFInline(ctInline, this);
drawingContents.add(inline);
return inline;
}

public CTDrawing getCtDrawing() {
return ctDrawing;
}

public XWPFRun getParent() {
return parent;
}

public List<IDrawingContent> getDrawingContents() {
return drawingContents;
}

public List<XWPFGraphicalObject> getGraphics() {
List<XWPFGraphicalObject> graphics = new ArrayList<>();
for (IDrawingContent iDrawingContent : drawingContents) {
XWPFGraphicalObject graphic = iDrawingContent.getGraphicalObject();
if (graphic == null) {
continue;
}
graphics.add(graphic);
}
return graphics;
}

public List<XWPFGraphicalObjectData> getGraphicData() {
List<XWPFGraphicalObjectData> graphicalObjectData = new ArrayList<>();
for (XWPFGraphicalObject graphic : getGraphics()) {
XWPFGraphicalObjectData graphicData = graphic.getGraphicalObjectData();
if (graphicData == null) {
continue;
}
graphicalObjectData.add(graphicData);
}
return graphicalObjectData;
}

public List<XWPFPicture> getPictures() {
List<XWPFPicture> pictures = new ArrayList<>();
for (XWPFGraphicalObjectData graphicDatum : getGraphicData()) {
XWPFPicture picture = graphicDatum.getPicture();
if (picture == null) {
continue;
}
pictures.add(picture);
}
return pictures;
}

public List<XWPFWordprocessingShape> getWordprocessingShapes() {
List<XWPFWordprocessingShape> wordprocessingShapes = new ArrayList<>();
for (XWPFGraphicalObjectData graphicDatum : getGraphicData()) {
XWPFWordprocessingShape wordprocessingShape = graphicDatum.getWordprocessingShape();
if (wordprocessingShape == null) {
continue;
}
wordprocessingShapes.add(wordprocessingShape);
}
return wordprocessingShapes;
}

public List<XWPFTextBoxContent> getTextBoxContents() {
List<XWPFTextBoxContent> textBoxContents = new ArrayList<>();
for (XWPFWordprocessingShape wordprocessingShape : getWordprocessingShapes()) {
XWPFTextBoxContent textBoxContent = wordprocessingShape.getTextBoxContent();
if (textBoxContent == null) {
continue;
}
textBoxContents.add(textBoxContent);
}
return textBoxContents;
}

@Override
public String toString() {
StringBuilder text = new StringBuilder();
XmlObject[] ts = ctDrawing.selectPath("declare namespace w='http://schemas.openxmlformats.org/wordprocessingml/2006/main' .//w:t");
for (XmlObject t : ts) {
NodeList kids = t.getDomNode().getChildNodes();
for (int n = 0; n < kids.getLength(); n++) {
if (kids.item(n) instanceof Text) {
if (text.length() > 0) {
text.append("\n");
}
text.append(kids.item(n).getNodeValue());
}
}
}
return text.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.xwpf.usermodel;

import org.openxmlformats.schemas.drawingml.x2006.main.CTGraphicalObject;
import org.openxmlformats.schemas.drawingml.x2006.main.CTGraphicalObjectData;

public class XWPFGraphicalObject {
private final CTGraphicalObject ctGraphicalObject;
private final IDrawingContent parent;
private XWPFGraphicalObjectData graphicalObjectData;

public XWPFGraphicalObject(CTGraphicalObject ctGraphicalObject, IDrawingContent parent) {
this.ctGraphicalObject = ctGraphicalObject;
this.parent = parent;
if (ctGraphicalObject.getGraphicData() != null) {
this.graphicalObjectData = new XWPFGraphicalObjectData(ctGraphicalObject.getGraphicData(), this);
}
}

public XWPFGraphicalObjectData createGraphicalObjectData() {
CTGraphicalObjectData ctGraphicalObjectData = ctGraphicalObject.addNewGraphicData();
XWPFGraphicalObjectData graphicalObjectData = new XWPFGraphicalObjectData(ctGraphicalObjectData, this);
this.graphicalObjectData = graphicalObjectData;
return graphicalObjectData;
}

public CTGraphicalObject getCTGraphicalObject() {
return ctGraphicalObject;
}

public IDrawingContent getParent() {
return parent;
}

public XWPFGraphicalObjectData getGraphicalObjectData() {
return graphicalObjectData;
}
}
Loading

0 comments on commit 6ee240b

Please sign in to comment.