From d51ca537ed0f4f945093669d71ac55ca69239111 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Wed, 4 Sep 2019 16:55:00 +0000 Subject: [PATCH] [bug-63725] commons-compress 1.19 git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1866402 13f79535-47bb-0310-9956-ffa450edef68 --- .classpath | 2 +- build.gradle | 2 +- build.xml | 5 +-- maven/poi-ooxml.pom | 2 +- sonar/ooxml/pom.xml | 2 +- .../apache/poi/ooxml/util/DocumentHelper.java | 36 +++++++++++++++++-- .../dsig/facets/XAdESSignatureFacet.java | 25 +++++++++++-- .../org/apache/poi/POIDataSamples.java | 5 ++- 8 files changed, 68 insertions(+), 11 deletions(-) diff --git a/.classpath b/.classpath index a93222b2ea..3ec3aa2132 100644 --- a/.classpath +++ b/.classpath @@ -40,7 +40,7 @@ - + diff --git a/build.gradle b/build.gradle index 430f948b5e..1123e4790f 100644 --- a/build.gradle +++ b/build.gradle @@ -233,7 +233,7 @@ project('ooxml') { compile 'org.apache.xmlbeans:xmlbeans:3.1.0' compile 'org.apache.commons:commons-collections4:4.4' compile 'org.apache.commons:commons-math3:3.6.1' - compile 'org.apache.commons:commons-compress:1.18' + compile 'org.apache.commons:commons-compress:1.19' compile 'org.apache.santuario:xmlsec:2.1.2' compile 'org.bouncycastle:bcpkix-jdk15on:1.62' compile 'com.github.virtuald:curvesapi:1.06' diff --git a/build.xml b/build.xml index 9536ac79c8..5bb7230bd0 100644 --- a/build.xml +++ b/build.xml @@ -244,9 +244,9 @@ under the License. - + + value="${repository.m2}/maven2/org/apache/commons/commons-compress/1.19/commons-compress-1.19.jar"/> @@ -652,6 +652,7 @@ under the License. + diff --git a/maven/poi-ooxml.pom b/maven/poi-ooxml.pom index b885c8c4be..e4b40365dc 100644 --- a/maven/poi-ooxml.pom +++ b/maven/poi-ooxml.pom @@ -72,7 +72,7 @@ org.apache.commons commons-compress - 1.18 + 1.19 com.github.virtuald diff --git a/sonar/ooxml/pom.xml b/sonar/ooxml/pom.xml index 4737fd05ac..c778f439b6 100644 --- a/sonar/ooxml/pom.xml +++ b/sonar/ooxml/pom.xml @@ -152,7 +152,7 @@ org.apache.commons commons-compress - 1.18 + 1.19 com.github.virtuald diff --git a/src/ooxml/java/org/apache/poi/ooxml/util/DocumentHelper.java b/src/ooxml/java/org/apache/poi/ooxml/util/DocumentHelper.java index ce8c4d0463..8fe643d764 100644 --- a/src/ooxml/java/org/apache/poi/ooxml/util/DocumentHelper.java +++ b/src/ooxml/java/org/apache/poi/ooxml/util/DocumentHelper.java @@ -19,6 +19,7 @@ package org.apache.poi.ooxml.util; import java.io.IOException; import java.io.InputStream; +import java.io.StringWriter; import java.lang.reflect.Method; import java.util.concurrent.TimeUnit; @@ -27,11 +28,15 @@ import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.stream.events.Namespace; +import javax.xml.transform.OutputKeys; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; import org.apache.poi.util.POILogFactory; import org.apache.poi.util.POILogger; -import org.w3c.dom.Document; -import org.w3c.dom.Element; +import org.w3c.dom.*; import org.xml.sax.ErrorHandler; import org.xml.sax.InputSource; import org.xml.sax.SAXException; @@ -97,6 +102,33 @@ public final class DocumentHelper { } } + public static String domToString(Node node) throws Exception { + TransformerFactory tf = TransformerFactory.newInstance(); + Transformer t = tf.newTransformer(); + t.setOutputProperty(OutputKeys.INDENT, "yes"); + StringWriter sw = new StringWriter(); + t.transform(new DOMSource(node), new StreamResult(sw)); + return sw.toString(); + } + + public static Attr findIdAttr(Element e, String name) throws Exception { + Attr att = e.getAttributeNode("Id"); + if(att != null && name.equals(att.getValue())) { + return att; + } + NodeList nl = e.getChildNodes(); + for (int i = 0; i < nl.getLength(); i++) { + Node child = nl.item(i); + if (child instanceof Element) { + Attr x = findIdAttr((Element)child, name); + if (x != null) { + return x; + } + } + } + return null; + } + static final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); static { documentBuilderFactory.setNamespaceAware(true); diff --git a/src/ooxml/java/org/apache/poi/poifs/crypt/dsig/facets/XAdESSignatureFacet.java b/src/ooxml/java/org/apache/poi/poifs/crypt/dsig/facets/XAdESSignatureFacet.java index c8f04e8824..d8da2e1f03 100644 --- a/src/ooxml/java/org/apache/poi/poifs/crypt/dsig/facets/XAdESSignatureFacet.java +++ b/src/ooxml/java/org/apache/poi/poifs/crypt/dsig/facets/XAdESSignatureFacet.java @@ -70,8 +70,7 @@ import org.etsi.uri.x01903.v13.SignedSignaturePropertiesType; import org.etsi.uri.x01903.v13.SignerRoleType; import org.w3.x2000.x09.xmldsig.DigestMethodType; import org.w3.x2000.x09.xmldsig.X509IssuerSerialType; -import org.w3c.dom.Document; -import org.w3c.dom.Node; +import org.w3c.dom.*; /** * XAdES Signature Facet. Implements XAdES v1.4.1 which is compatible with XAdES @@ -224,10 +223,32 @@ public class XAdESSignatureFacet extends SignatureFacet { private XMLObject addXadesObject(Document document, QualifyingPropertiesType qualifyingProperties) { Node qualDocElSrc = qualifyingProperties.getDomNode(); Node qualDocEl = document.importNode(qualDocElSrc, true); + markIds(qualDocEl); List xadesObjectContent = Arrays.asList(new DOMStructure(qualDocEl)); return getSignatureFactory().newXMLObject(xadesObjectContent, null, null, null); } + private void markIds(Node node) { + if (node instanceof Element) { + markIds((Element)node); + } else if (node instanceof Document) { + markIds(((Document)node).getDocumentElement()); + } + } + + private void markIds(Element element) { + if (element != null) { + Attr att = element.getAttributeNode("Id"); + if (att != null) { + element.setIdAttributeNode(att, true); + } + NodeList nl = element.getChildNodes(); + for (int i = 0; i < nl.getLength(); i++) { + markIds(nl.item(i)); + } + } + } + private Reference addXadesReference() throws XMLSignatureException { List transforms = singletonList(newTransform(CanonicalizationMethod.INCLUSIVE)); return newReference("#"+signatureConfig.getXadesSignatureId(), transforms, XADES_TYPE, null, null); diff --git a/src/testcases/org/apache/poi/POIDataSamples.java b/src/testcases/org/apache/poi/POIDataSamples.java index 92db8fe18a..c571686f51 100644 --- a/src/testcases/org/apache/poi/POIDataSamples.java +++ b/src/testcases/org/apache/poi/POIDataSamples.java @@ -116,7 +116,10 @@ public final class POIDataSamples { return _instHSMF; } - public static POIDataSamples getXmlDSignInstance(){ + public static POIDataSamples getXmlDSignInstance() { + if (System.getProperty(TEST_PROPERTY) == null) { + System.setProperty(TEST_PROPERTY, "../../test-data"); + } if(_instXmlDSign == null) _instXmlDSign = new POIDataSamples("xmldsign"); return _instXmlDSign; }