poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kiwiwi...@apache.org
Subject svn commit: r1882394 [2/2] - in /poi: site/src/documentation/content/xdocs/ trunk/ trunk/src/java/org/apache/poi/common/usermodel/ trunk/src/multimodule/ooxml-lite/java9/ trunk/src/multimodule/ooxml-schemas/java9/ trunk/src/ooxml/java/org/apache/poi/oo...
Date Sat, 10 Oct 2020 23:33:26 GMT
Added: poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFSignatureLine.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFSignatureLine.java?rev=1882394&view=auto
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFSignatureLine.java (added)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFSignatureLine.java Sat Oct
10 23:33:26 2020
@@ -0,0 +1,91 @@
+/* ====================================================================
+   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 javax.xml.namespace.QName;
+
+import com.microsoft.schemas.office.office.CTSignatureLine;
+import com.microsoft.schemas.vml.CTImageData;
+import org.apache.poi.common.usermodel.PictureType;
+import org.apache.poi.ooxml.util.XPathHelper;
+import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
+import org.apache.poi.poifs.crypt.dsig.SignatureLine;
+import org.apache.xmlbeans.XmlException;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPicture;
+
+public class XWPFSignatureLine extends SignatureLine {
+    static final String NS_OOXML_WP_MAIN = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
+    private static final String MS_VML_URN = "urn:schemas-microsoft-com:vml";
+
+    private CTSignatureLine line;
+
+    public void parse(XWPFDocument doc) throws XmlException {
+        line = XPathHelper.selectProperty(doc.getDocument(), CTSignatureLine.class, null,
+            new QName[]{new QName(NS_OOXML_WP_MAIN, "body")},
+            new QName[]{new QName(NS_OOXML_WP_MAIN, "p")},
+            new QName[]{new QName(NS_OOXML_WP_MAIN, "r")},
+            new QName[]{new QName(NS_OOXML_WP_MAIN, "pict")},
+            new QName[]{new QName(MS_VML_URN, "shape")},
+            new QName[]{QNAME_SIGNATURE_LINE});
+        if (line != null) {
+            setSignatureShape(line);
+            parse();
+        }
+    }
+
+    public void add(XWPFParagraph paragraph) {
+        XWPFRun r = paragraph.createRun();
+        CTPicture pict = r.getCTR().addNewPict();
+        add(pict, (image, type) -> paragraph.getDocument().addPictureData(image, mapType(type)));
+    }
+
+    @Override
+    protected void setRelationId(CTImageData imageData, String relId) {
+        imageData.setId2(relId);
+    }
+
+    private static int mapType(PictureType type) throws InvalidFormatException {
+        switch (type) {
+            case BMP:
+                return Document.PICTURE_TYPE_BMP;
+            case DIB:
+                return Document.PICTURE_TYPE_DIB;
+            case EMF:
+                return Document.PICTURE_TYPE_EMF;
+            case EPS:
+                return Document.PICTURE_TYPE_EPS;
+            case GIF:
+                return Document.PICTURE_TYPE_GIF;
+            case JPEG:
+                return Document.PICTURE_TYPE_JPEG;
+            case PICT:
+                return Document.PICTURE_TYPE_PICT;
+            case PNG:
+                return Document.PICTURE_TYPE_PNG;
+            case TIFF:
+                return Document.PICTURE_TYPE_TIFF;
+            case WMF:
+                return Document.PICTURE_TYPE_WMF;
+            case WPG:
+                return Document.PICTURE_TYPE_WPG;
+            default:
+                throw new InvalidFormatException("Unsupported picture format "+type);
+        }
+    }
+}

Propchange: poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFSignatureLine.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: poi/trunk/src/ooxml/resources/org/apache/poi/schemas/ooxmlSchemas.xsdconfig
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/resources/org/apache/poi/schemas/ooxmlSchemas.xsdconfig?rev=1882394&r1=1882393&r2=1882394&view=diff
==============================================================================
--- poi/trunk/src/ooxml/resources/org/apache/poi/schemas/ooxmlSchemas.xsdconfig (original)
+++ poi/trunk/src/ooxml/resources/org/apache/poi/schemas/ooxmlSchemas.xsdconfig Sat Oct 10
23:33:26 2020
@@ -43,4 +43,8 @@
     <xb:package>com.microsoft.schemas.compatibility</xb:package>
   </xb:namespace>
 
+  <xb:namespace uri="urn:schemas-poi-apache-org:vmldrawing">
+    <xb:package>org.apache.poi.schemas.vmldrawing</xb:package>
+  </xb:namespace>
+
 </xb:config>
\ No newline at end of file

Added: poi/trunk/src/ooxml/resources/org/apache/poi/schemas/vmlDrawing.xsd
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/resources/org/apache/poi/schemas/vmlDrawing.xsd?rev=1882394&view=auto
==============================================================================
--- poi/trunk/src/ooxml/resources/org/apache/poi/schemas/vmlDrawing.xsd (added)
+++ poi/trunk/src/ooxml/resources/org/apache/poi/schemas/vmlDrawing.xsd Sat Oct 10 23:33:26
2020
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="utf-8"?>
+<xsd:schema
+  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
+  xmlns="urn:schemas-poi-apache-org:vmldrawing"
+  targetNamespace="urn:schemas-poi-apache-org:vmldrawing"
+>
+  <xsd:import namespace="urn:schemas-microsoft-com:vml" schemaLocation="vml-main.xsd"/>
+  <xsd:import namespace="urn:schemas-microsoft-com:office:office" schemaLocation="vml-officeDrawing.xsd"/>
+  <xsd:import namespace="urn:schemas-microsoft-com:office:excel" schemaLocation="vml-spreadsheetDrawing.xsd"/>
+  <xsd:element name="xml" type="CT_XML"/>
+  <xsd:complexType name="CT_XML">
+      <xsd:choice maxOccurs="unbounded">
+        <xsd:any namespace="urn:schemas-microsoft-com:office:office"/>
+        <xsd:any namespace="urn:schemas-microsoft-com:vml"/>
+        <xsd:any namespace="urn:schemas-microsoft-com:office:excel"/>
+      </xsd:choice>
+  </xsd:complexType>
+</xsd:schema>

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/poifs/crypt/dsig/TestSignatureInfo.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/poifs/crypt/dsig/TestSignatureInfo.java?rev=1882394&r1=1882393&r2=1882394&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/poifs/crypt/dsig/TestSignatureInfo.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/poifs/crypt/dsig/TestSignatureInfo.java Sat
Oct 10 23:33:26 2020
@@ -62,11 +62,14 @@ import java.security.cert.X509Certificat
 import java.security.interfaces.RSAPublicKey;
 import java.security.spec.RSAKeyGenParameterSpec;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Calendar;
 import java.util.Collections;
 import java.util.Date;
 import java.util.Iterator;
 import java.util.List;
+import java.util.function.BiFunction;
+import java.util.function.Supplier;
 
 import javax.xml.crypto.MarshalException;
 import javax.xml.crypto.dsig.CanonicalizationMethod;
@@ -76,6 +79,7 @@ import javax.xml.crypto.dsig.dom.DOMSign
 import org.apache.jcp.xml.dsig.internal.dom.DOMSignedInfo;
 import org.apache.poi.EncryptedDocumentException;
 import org.apache.poi.POIDataSamples;
+import org.apache.poi.ooxml.POIXMLDocument;
 import org.apache.poi.ooxml.util.DocumentHelper;
 import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
 import org.apache.poi.openxml4j.opc.OPCPackage;
@@ -98,9 +102,16 @@ import org.apache.poi.util.IOUtils;
 import org.apache.poi.util.LocaleUtil;
 import org.apache.poi.util.POILogFactory;
 import org.apache.poi.util.POILogger;
+import org.apache.poi.util.TempFile;
 import org.apache.poi.xssf.streaming.SXSSFWorkbook;
+import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
+import org.apache.poi.xssf.usermodel.XSSFSheet;
+import org.apache.poi.xssf.usermodel.XSSFSignatureLine;
 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+import org.apache.poi.xwpf.usermodel.XWPFDocument;
+import org.apache.poi.xwpf.usermodel.XWPFSignatureLine;
 import org.apache.xmlbeans.SystemProperties;
+import org.apache.xmlbeans.XmlException;
 import org.apache.xmlbeans.XmlObject;
 import org.bouncycastle.asn1.DEROctetString;
 import org.bouncycastle.asn1.ocsp.OCSPObjectIdentifiers;
@@ -855,6 +866,100 @@ public class TestSignatureInfo {
         assertEquals(CanonicalizationMethod.INCLUSIVE, sic.getCanonicalizationMethod());
     }
 
+    private interface XmlDocumentPackageInit {
+        POIXMLDocument init(SignatureLine line, OPCPackage pkg) throws IOException, XmlException;
+    }
+
+    @Test
+    public void testSignatureImage() throws Exception {
+        initKeyPair();
+
+        List<Supplier<SignatureLine>> lines = Arrays.asList(XSSFSignatureLine::new,
XWPFSignatureLine::new);
+        for (Supplier<SignatureLine> sup : lines) {
+            SignatureLine line = sup.get();
+            line.setSuggestedSigner("Jack Sparrow");
+            line.setSuggestedSigner2("Captain");
+            line.setSuggestedSignerEmail("jack.bl@ck.perl");
+            line.setInvalidStamp("Bungling!");
+            line.setPlainSignature(testdata.readFile("jack-sign.emf"));
+
+            String[] ext = { "" };
+            BiFunction<SignatureLine,String[],POIXMLDocument> init =
+                (line instanceof XSSFSignatureLine)
+                ? this::initSignatureImageXSSF
+                : this::initSignatureImageXWPF;
+
+            File signDoc;
+            try (POIXMLDocument xmlDoc = init.apply(line,ext)) {
+                signDoc = TempFile.createTempFile("visual-signature", ext[0]);
+                try (FileOutputStream fos = new FileOutputStream(signDoc)) {
+                    xmlDoc.write(fos);
+                }
+            }
+
+            try (OPCPackage pkg = OPCPackage.open(signDoc, PackageAccess.READ_WRITE)) {
+                SignatureConfig sic = new SignatureConfig();
+                sic.setKey(keyPair.getPrivate());
+                sic.setSigningCertificateChain(Collections.singletonList(x509));
+
+                line.updateSignatureConfig(sic);
+
+                sic.setDigestAlgo(HashAlgorithm.sha1);
+                SignatureInfo si = new SignatureInfo();
+                si.setOpcPackage(pkg);
+                si.setSignatureConfig(sic);
+                // hash > sha1 doesn't work in excel viewer ...
+                si.confirmSignature();
+            }
+
+            XmlDocumentPackageInit reinit =
+                (line instanceof XSSFSignatureLine)
+                ? this::initSignatureImageXSSF
+                : this::initSignatureImageXWPF;
+
+            try (OPCPackage pkg = OPCPackage.open(signDoc, PackageAccess.READ)) {
+                SignatureLine line2 = sup.get();
+                try (POIXMLDocument doc = reinit.init(line2, pkg)) {
+                    line2.parse();
+                    assertEquals(line.getSuggestedSigner(), line2.getSuggestedSigner());
+                    assertEquals(line.getSuggestedSigner2(), line2.getSuggestedSigner2());
+                    assertEquals(line.getSuggestedSignerEmail(), line2.getSuggestedSignerEmail());
+                }
+
+                pkg.revert();
+            }
+        }
+    }
+
+    private XWPFDocument initSignatureImageXWPF(SignatureLine line, String[] ext) {
+        XWPFDocument doc = new XWPFDocument();
+        ((XWPFSignatureLine)line).add(doc.createParagraph());
+        ext[0] = ".docx";
+        return doc;
+    }
+
+    private XWPFDocument initSignatureImageXWPF(SignatureLine line, OPCPackage pkg) throws
IOException, XmlException {
+        XWPFDocument doc = new XWPFDocument(pkg);
+        ((XWPFSignatureLine)line).parse(doc);
+        return doc;
+    }
+
+    private XSSFWorkbook initSignatureImageXSSF(SignatureLine line, String[] ext) {
+        XSSFWorkbook xls = new XSSFWorkbook();
+        XSSFSheet sheet = xls.createSheet();
+        XSSFClientAnchor anchor = new XSSFClientAnchor(0,0,0,0,3,3,8,13);
+        ((XSSFSignatureLine)line).add(sheet, anchor);
+        ext[0] = ".xlsx";
+        return xls;
+    }
+
+    private XSSFWorkbook initSignatureImageXSSF(SignatureLine line, OPCPackage pkg) throws
IOException, XmlException {
+        XSSFWorkbook xls = new XSSFWorkbook(pkg);
+        ((XSSFSignatureLine)line).parse(xls.getSheetAt(0));
+        return xls;
+    }
+
+
     private SignatureConfig prepareConfig(String pfxInput) throws Exception {
         initKeyPair(pfxInput);
 

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFVMLDrawing.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFVMLDrawing.java?rev=1882394&r1=1882393&r2=1882394&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFVMLDrawing.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFVMLDrawing.java Sat
Oct 10 23:33:26 2020
@@ -16,6 +16,8 @@
 ==================================================================== */
 package org.apache.poi.xssf.usermodel;
 
+import static org.apache.poi.ooxml.POIXMLTypeLoader.DEFAULT_XML_OPTIONS;
+import static org.apache.poi.xssf.usermodel.XSSFVMLDrawing.QNAME_VMLDRAWING;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
@@ -27,14 +29,10 @@ import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.math.BigInteger;
+import java.util.Collections;
 import java.util.List;
 import java.util.regex.Pattern;
 
-import org.apache.poi.POIDataSamples;
-import org.apache.xmlbeans.XmlException;
-import org.apache.xmlbeans.XmlObject;
-import org.junit.Test;
-
 import com.microsoft.schemas.office.excel.CTClientData;
 import com.microsoft.schemas.office.excel.STObjectType;
 import com.microsoft.schemas.office.excel.STTrueFalseBlank;
@@ -46,6 +44,11 @@ import com.microsoft.schemas.vml.CTShape
 import com.microsoft.schemas.vml.CTShapetype;
 import com.microsoft.schemas.vml.STExt;
 import com.microsoft.schemas.vml.STTrueFalse;
+import org.apache.poi.POIDataSamples;
+import org.apache.xmlbeans.XmlException;
+import org.apache.xmlbeans.XmlObject;
+import org.apache.xmlbeans.XmlOptions;
+import org.junit.Test;
 
 public class TestXSSFVMLDrawing {
 
@@ -59,7 +62,7 @@ public class TestXSSFVMLDrawing {
         assertEquals(STExt.EDIT, layout.getExt());
         assertEquals(STExt.EDIT, layout.getIdmap().getExt());
         assertEquals("1", layout.getIdmap().getData());
-    
+
         assertTrue(items.get(1) instanceof CTShapetype);
         CTShapetype type = (CTShapetype)items.get(1);
         assertEquals("21600,21600", type.getCoordsize());
@@ -70,6 +73,7 @@ public class TestXSSFVMLDrawing {
         assertEquals(STConnectType.RECT, type.getPathArray(0).getConnecttype());
 
         CTShape shape = vml.newCommentShape();
+        items = vml.getItems();
         assertEquals(3, items.size());
         assertSame(items.get(2),  shape);
         assertEquals("#_x0000_t202", shape.getType());
@@ -110,7 +114,7 @@ public class TestXSSFVMLDrawing {
 
     @Test
     public void testFindCommentShape() throws IOException, XmlException {
-        
+
         XSSFVMLDrawing vml = new XSSFVMLDrawing();
         try (InputStream stream = POIDataSamples.getSpreadSheetInstance().openResourceAsStream("vmlDrawing1.vml"))
{
             vml.read(stream);
@@ -158,17 +162,21 @@ public class TestXSSFVMLDrawing {
         assertNull(vml.findCommentShape(0, 0));
 
     }
-    
+
     @Test
     public void testEvilUnclosedBRFixing() throws IOException, XmlException {
         XSSFVMLDrawing vml = new XSSFVMLDrawing();
         try (InputStream stream = POIDataSamples.getOpenXML4JInstance().openResourceAsStream("bug-60626.vml"))
{
             vml.read(stream);
         }
+
+        XmlOptions xopt = new XmlOptions(DEFAULT_XML_OPTIONS);
+        xopt.setSaveImplicitNamespaces(Collections.singletonMap("", QNAME_VMLDRAWING.getNamespaceURI()));
+
         Pattern p = Pattern.compile("<br/>");
         int count = 0;
         for (XmlObject xo : vml.getItems()) {
-            String[] split = p.split(xo.toString());
+            String[] split = p.split(xo.xmlText(xopt));
             count += split.length-1;
         }
         assertEquals(16, count);

Added: poi/trunk/test-data/xmldsign/jack-sign.emf
URL: http://svn.apache.org/viewvc/poi/trunk/test-data/xmldsign/jack-sign.emf?rev=1882394&view=auto
==============================================================================
Binary file - no diff available.

Propchange: poi/trunk/test-data/xmldsign/jack-sign.emf
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message