Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions poi-ooxml/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ dependencies {
// see https://logging.apache.org/log4j/2.x/log4j-slf4j-impl/
testImplementation "org.apache.logging.log4j:log4j-slf4j18-impl:${log4jVersion}"

testRuntimeOnly 'net.sf.saxon:Saxon-HE:10.6'

broken("org.apache.xmlgraphics:batik-script:${batikVersion}"){
exclude group: 'xalan', module: 'xalan'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more

package org.apache.poi.xssf.extractor;

import static org.apache.poi.xssf.usermodel.XSSFRelation.NS_SPREADSHEETML;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand All @@ -25,13 +26,16 @@ Licensed to the Apache Software Foundation (ASF) under one or more

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.*;
import java.util.regex.Pattern;

import javax.xml.namespace.NamespaceContext;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
import org.apache.poi.ooxml.POIXMLDocumentPart;
Expand All @@ -46,6 +50,9 @@ Licensed to the Apache Software Foundation (ASF) under one or more
import org.apache.poi.xssf.usermodel.XSSFMap;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.jupiter.api.Test;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

Expand Down Expand Up @@ -186,26 +193,30 @@ void testMultiTable() throws Exception {
assertNotNull(map);

XSSFExportToXml exporter = new XSSFExportToXml(map);
UnsynchronizedByteArrayOutputStream os = new UnsynchronizedByteArrayOutputStream();
exporter.exportToXML(os, true);
String xml = os.toString("UTF-8");

String xml;
try (UnsynchronizedByteArrayOutputStream os = new UnsynchronizedByteArrayOutputStream()) {
exporter.exportToXML(os, true);
xml = os.toString("UTF-8");
}
assertNotNull(xml);

String[] regexConditions = {
"<MapInfo", "</MapInfo>",
"<Schema ID=\"1\" Namespace=\"\" SchemaRef=\"\"/>",
"<Schema ID=\"4\" Namespace=\"\" SchemaRef=\"\"/>",
"DataBinding",
"Map Append=\"false\" AutoFit=\"false\" ID=\"1\"",
"Map Append=\"false\" AutoFit=\"false\" ID=\"5\"",
};

for (String condition : regexConditions) {
Pattern pattern = Pattern.compile(condition);
Matcher matcher = pattern.matcher(xml);
assertTrue(matcher.find());
}
Document xmlDoc = XMLHelper.newDocumentBuilder().parse(new InputSource(new StringReader(xml)));

XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xpath = xpathFactory.newXPath();
xpath.setNamespaceContext(new XPathNSContext());
assertNotNull(xpath.evaluate("/ss:MapInfo", xmlDoc, XPathConstants.NODE));
assertNotNull(xpath.evaluate(
"/ss:MapInfo/ss:Schema[@ID=\"1\" and @Namespace=\"\" and @SchemaRef=\"\"]", xmlDoc, XPathConstants.NODE));
assertNotNull(xpath.evaluate(
"/ss:MapInfo/ss:Schema[@ID=\"4\" and @Namespace=\"\" and @SchemaRef=\"\"]", xmlDoc, XPathConstants.NODE));
NodeList databindingList = (NodeList) xpath.evaluate(
"/ss:MapInfo/ss:Map/ss:DataBinding", xmlDoc, XPathConstants.NODESET);
assertEquals(5, databindingList.getLength());
assertNotNull(xpath.evaluate(
"/ss:MapInfo/ss:Map[@ID=\"1\" and @Append=\"false\" and @AutoFit=\"false\"]", xmlDoc, XPathConstants.NODE));
assertNotNull(xpath.evaluate(
"/ss:MapInfo/ss:Map[@ID=\"5\" and @Append=\"false\" and @AutoFit=\"false\"]", xmlDoc, XPathConstants.NODE));
}

found = true;
Expand Down Expand Up @@ -665,4 +676,24 @@ void testXXEInSchema() throws Exception {
}
}
}

private class XPathNSContext implements NamespaceContext {
final Map<String,String> nsMap = new HashMap<>();

XPathNSContext() {
nsMap.put("ss", NS_SPREADSHEETML);
}

public String getNamespaceURI(String prefix) {
return nsMap.get(prefix);
}
@SuppressWarnings("rawtypes")
@Override
public Iterator getPrefixes(String val) {
return null;
}
public String getPrefix(String uri) {
return null;
}
}
}
1 change: 1 addition & 0 deletions poi/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ dependencies {
testImplementation "org.junit.jupiter:junit-jupiter-api:${junitVersion}"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${junitVersion}"
testImplementation 'org.apiguardian:apiguardian-api:1.1.2'
testRuntimeOnly 'net.sf.saxon:Saxon-HE:10.6'

// needed for locating the external references
javadocs project(':poi-ooxml')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@ Licensed to the Apache Software Foundation (ASF) under one or more

package org.apache.poi.poifs.crypt.agile;

import static org.apache.poi.poifs.crypt.agile.EncryptionDocument.getBinAttr;
import static org.apache.poi.poifs.crypt.agile.EncryptionDocument.setBinAttr;

import org.apache.poi.EncryptedDocumentException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import static org.apache.poi.poifs.crypt.agile.EncryptionDocument.*;

public class CertificateKeyEncryptor {

/**
Expand Down Expand Up @@ -60,9 +59,9 @@ public CertificateKeyEncryptor(Element certificateKey) {

void write(Element encryption) {
Document doc = encryption.getOwnerDocument();
Element keyEncryptor = (Element) encryption.appendChild(doc.createElement("keyEncryptor"));
Element keyEncryptor = (Element) encryption.appendChild(doc.createElementNS(ENC_NS, "keyEncryptor"));
keyEncryptor.setAttribute("uri", KeyEncryptor.CERT_NS);
Element encryptedKey = (Element) keyEncryptor.appendChild(doc.createElement("c:encryptedKey"));
Element encryptedKey = (Element) keyEncryptor.appendChild(doc.createElementNS(KeyEncryptor.CERT_NS, "c:encryptedKey"));

setBinAttr(encryptedKey, "encryptedKeyValue", encryptedKeyValue);
setBinAttr(encryptedKey, "x509Certificate", x509Certificate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public DataIntegrity(Element parent) {

void write(Element encryption) {
Document doc = encryption.getOwnerDocument();
Element dataIntegrity = (Element)encryption.appendChild(doc.createElement("dataIntegrity"));
Element dataIntegrity = (Element)encryption.appendChild(doc.createElementNS(ENC_NS, "dataIntegrity"));
setBinAttr(dataIntegrity, "encryptedHmacKey", encryptedHmacKey);
setBinAttr(dataIntegrity, "encryptedHmacValue", encryptedHmacValue);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void write(Document doc) {
if (dataIntegrity != null) {
dataIntegrity.write(encryption);
}
Element keyEncryptors = (Element)encryption.appendChild(doc.createElement("keyEncryptors"));
Element keyEncryptors = (Element)encryption.appendChild(doc.createElementNS(ENC_NS, "keyEncryptors"));
boolean hasPass = false;
boolean hasCert = false;
for (KeyEncryptor ke : this.keyEncryptors) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public KeyData(Element parent) {

void write(Element encryption) {
Document doc = encryption.getOwnerDocument();
Element keyData = (Element)encryption.appendChild(doc.createElement("keyData"));
Element keyData = (Element)encryption.appendChild(doc.createElementNS(ENC_NS, "keyData"));
setIntAttr(keyData, "saltSize", saltSize);
setIntAttr(keyData, "blockSize", blockSize);
setIntAttr(keyData, "keyBits", keyBits);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,16 @@ Licensed to the Apache Software Foundation (ASF) under one or more

package org.apache.poi.poifs.crypt.agile;

import static org.apache.poi.poifs.crypt.agile.EncryptionDocument.getBinAttr;
import static org.apache.poi.poifs.crypt.agile.EncryptionDocument.getIntAttr;
import static org.apache.poi.poifs.crypt.agile.EncryptionDocument.setAttr;
import static org.apache.poi.poifs.crypt.agile.EncryptionDocument.setBinAttr;
import static org.apache.poi.poifs.crypt.agile.EncryptionDocument.setIntAttr;

import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.poifs.crypt.ChainingMode;
import org.apache.poi.poifs.crypt.CipherAlgorithm;
import org.apache.poi.poifs.crypt.HashAlgorithm;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import static org.apache.poi.poifs.crypt.agile.EncryptionDocument.*;
import static org.apache.poi.poifs.crypt.agile.KeyEncryptor.PASS_NS;

public class PasswordKeyEncryptor {

/**
Expand Down Expand Up @@ -124,9 +121,9 @@ public PasswordKeyEncryptor(Element passwordKey) {

void write(Element encryption) {
Document doc = encryption.getOwnerDocument();
Element keyEncryptor = (Element) encryption.appendChild(doc.createElement("keyEncryptor"));
keyEncryptor.setAttribute("uri", KeyEncryptor.PASS_NS);
Element encryptedKey = (Element) keyEncryptor.appendChild(doc.createElement("p:encryptedKey"));
Element keyEncryptor = (Element) encryption.appendChild(doc.createElementNS(ENC_NS, "keyEncryptor"));
keyEncryptor.setAttribute("uri", PASS_NS);
Element encryptedKey = (Element) keyEncryptor.appendChild(doc.createElementNS(PASS_NS, "p:encryptedKey"));

setIntAttr(encryptedKey, "saltSize", saltSize);
setIntAttr(encryptedKey, "blockSize", blockSize);
Expand Down
Binary file modified poi/src/main/java9/module-info.class
Binary file not shown.