fix old xerces errors because of not available disallow-doctype parser feature

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1849336 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andreas Beeker 2018-12-19 19:14:55 +00:00
parent 4199a2f0a3
commit 6486fb7c2d
4 changed files with 35 additions and 10 deletions

View File

@ -18,21 +18,28 @@
package org.apache.poi.openxml4j.opc;
import java.io.InputStream;
import java.net.URL;
import org.apache.poi.ooxml.util.POIXMLConstants;
import org.apache.poi.openxml4j.OpenXML4JTestDataSamples;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.internal.ContentType;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import static org.junit.Assert.*;
import javax.xml.parsers.DocumentBuilderFactory;
/**
* Tests for content type (ContentType class).
*
* @author Julien Chable
*/
public final class TestContentType {
@Rule
public ExpectedException exception = ExpectedException.none();
/**
* Check rule M1.13: Package implementers shall only create and only
* recognize parts with a content type; format designers shall specify a
@ -144,9 +151,14 @@ public final class TestContentType {
/**
* OOXML content types don't need entities and we shouldn't
* barf if we get one from a third party system that added them
* (expected = InvalidFormatException.class)
*/
@Test(expected = InvalidFormatException.class)
@Test
public void testFileWithContentTypeEntities() throws Exception {
if (!isOldXercesActive()) {
exception.expect(InvalidFormatException.class);
}
InputStream is = OpenXML4JTestDataSamples.openSampleStream("ContentTypeHasEntities.ooxml");
OPCPackage.open(is);
}
@ -225,4 +237,13 @@ public final class TestContentType {
private static void assertContains(String needle, String haystack) {
assertTrue(haystack.contains(needle));
}
public static boolean isOldXercesActive() {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
dbf.setFeature(POIXMLConstants.FEATURE_DISALLOW_DOCTYPE_DECL, true);
return false;
} catch (Exception|AbstractMethodError ignored) {}
return true;
}
}

View File

@ -36,6 +36,7 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;
import org.openxmlformats.schemas.officeDocument.x2006.customProperties.CTProperty;
import static org.apache.poi.openxml4j.opc.TestContentType.isOldXercesActive;
import static org.junit.Assert.*;
public final class TestPackageCoreProperties {
@ -249,9 +250,9 @@ public final class TestPackageCoreProperties {
// Get the Core Properties
PackagePropertiesPart props = (PackagePropertiesPart)p.getPackageProperties();
// used to resolve a vale but now we ignore DTD entities for security reasons
assertFalse(props.getCreatorProperty().isPresent());
// used to resolve a value but now we ignore DTD entities for security reasons
assertEquals(isOldXercesActive(), props.getCreatorProperty().isPresent());
p.close();
}

View File

@ -17,6 +17,8 @@
package org.apache.poi.openxml4j.opc;
import static org.apache.poi.openxml4j.opc.TestContentType.isOldXercesActive;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
@ -418,8 +420,8 @@ public class TestRelationships extends TestCase {
if (pr.getRelationshipType().equals(PackageRelationshipTypes.EXTENDED_PROPERTIES))
foundExtPropRel = true;
}
assertFalse("Core/Doc Relationship not found in " + p.getRelationships(), foundDocRel);
assertFalse("Core Props Relationship not found in " + p.getRelationships(), foundCorePropRel);
assertFalse("Ext Props Relationship not found in " + p.getRelationships(), foundExtPropRel);
assertEquals("Core/Doc Relationship not found in " + p.getRelationships(), isOldXercesActive(), foundDocRel);
assertEquals("Core Props Relationship not found in " + p.getRelationships(), isOldXercesActive(), foundCorePropRel);
assertEquals("Ext Props Relationship not found in " + p.getRelationships(), isOldXercesActive(), foundExtPropRel);
}
}

View File

@ -17,6 +17,7 @@
package org.apache.poi.xssf.usermodel;
import static org.apache.poi.openxml4j.opc.TestContentType.isOldXercesActive;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
@ -1969,7 +1970,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues {
fail("should have thrown SAXParseException");
} catch (SAXParseException e) {
assertNotNull(e.getMessage());
assertTrue(e.getMessage().contains("DOCTYPE is disallowed when the feature"));
assertNotEquals(isOldXercesActive(), e.getMessage().contains("DOCTYPE is disallowed when the feature"));
}
}