Skip to content

Commit

Permalink
Merge pull request #78 from tan9/feature/configurable-xml-reader
Browse files Browse the repository at this point in the history
Fixes #77 Disable DTD support on XMLInputFactory.
  • Loading branch information
miachm authored Sep 13, 2023
2 parents f6e3207 + d5b5a28 commit 6e1cabd
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@
<version>1.2.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.woodstox</groupId>
<artifactId>woodstox-core</artifactId>
<version>6.5.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<distributionManagement>
Expand Down
Binary file added resources/metadataWithDtd.ods
Binary file not shown.
4 changes: 4 additions & 0 deletions src/com/github/miachm/sods/XmlReaderEventImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ class XmlReaderEventImpl implements XmlReader {
@Override
public XmlReaderInstanceEventImpl load(InputStream in) throws IOException {
try {
// #77 Make the ODS files with DTD work on the JBoss-bundled Woodstox,
// or ensure they work on another StAX implementation set to validate by default.
inputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE);

reader = inputFactory.createXMLStreamReader(in);
// Skip start of document
try {
Expand Down
44 changes: 44 additions & 0 deletions tests/com/github/miachm/sods/XMLInputFactoriesTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.github.miachm.sods;

import org.testng.SkipException;
import org.testng.annotations.Test;

import javax.xml.stream.XMLInputFactory;
import java.io.File;

import static org.testng.AssertJUnit.*;

public class XMLInputFactoriesTest {

// Disable this test by default, because XmlReaderEventImpl stores a static XMLInputFactory
// instance, which is not reset after the test.
@Test(enabled = false)
public void testLoadUsingBundledXMLInputFactory() throws Exception {
testUsingFactory("com.sun.xml.internal.stream.XMLInputFactoryImpl");
}

@Test
public void testLoadUsingWoodstoxXMLInputFactory() throws Exception {
testUsingFactory("com.ctc.wstx.stax.WstxInputFactory");
}

private void testUsingFactory(String factoryName) throws Exception {
// Skip test if factory is not available
try {
Class.forName(factoryName);
} catch (ClassNotFoundException e) {
throw new SkipException(factoryName + " not found, ignore this test.");
}

try {
System.setProperty(XMLInputFactory.class.getName(), factoryName);

SpreadSheet spread = new SpreadSheet(new File("resources/metadataWithDtd.ods"));
assertEquals(spread.getNumSheets(),1);

} finally {
System.clearProperty(XMLInputFactory.class.getName());
}
}

}

0 comments on commit 6e1cabd

Please sign in to comment.