private static class DocHelperErrorHandler implements ErrorHandler {
- public void warning(SAXParseException exception) throws SAXException {
+ public void warning(SAXParseException exception) {
printError(POILogger.WARN, exception);
}
- public void error(SAXParseException exception) throws SAXException {
+ public void error(SAXParseException exception) {
printError(POILogger.ERROR, exception);
}
trySetXercesSecurityManager(documentBuilderFactory);
}
- private static void trySetFeature(DocumentBuilderFactory dbf, String feature, boolean enabled) {
+ private static void trySetFeature(@SuppressWarnings("SameParameterValue") DocumentBuilderFactory dbf, String feature, boolean enabled) {
try {
dbf.setFeature(feature, enabled);
} catch (Exception e) {
}
}
- private static void trySetXercesSecurityManager(DocumentBuilderFactory dbf) {
+ private static void trySetXercesSecurityManager(@SuppressWarnings("SameParameterValue") DocumentBuilderFactory dbf) {
// Try built-in JVM one first, standalone if not
for (String securityManagerClassName : new String[]{
//"com.sun.org.apache.xerces.internal.util.SecurityManager",
package org.apache.poi.ooxml.util;
-import java.io.IOException;
import java.io.StringReader;
import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;
return xmlReader;
}
- static final EntityResolver IGNORING_ENTITY_RESOLVER = new EntityResolver() {
- @Override
- public InputSource resolveEntity(String publicId, String systemId)
- throws SAXException, IOException {
- return new InputSource(new StringReader(""));
- }
- };
+ static final EntityResolver IGNORING_ENTITY_RESOLVER = (publicId, systemId) -> new InputSource(new StringReader(""));
private static final SAXParserFactory saxFactory;
static {
}
}
- private static void trySetSAXFeature(SAXParserFactory spf, String feature, boolean flag) {
+ private static void trySetSAXFeature(@SuppressWarnings("SameParameterValue") SAXParserFactory spf,
+ String feature, boolean flag) {
try {
spf.setFeature(feature, flag);
} catch (Exception e) {
}
}
- private static void trySetSAXFeature(XMLReader xmlReader, String feature) {
+ private static void trySetSAXFeature(XMLReader xmlReader, @SuppressWarnings("SameParameterValue") String feature) {
try {
xmlReader.setFeature(feature, true);
} catch (Exception e) {
import org.junit.Test;
import org.xml.sax.InputSource;
-import org.xml.sax.XMLReader;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import java.io.ByteArrayInputStream;
+import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotSame;
+import static org.junit.Assert.assertTrue;
public class TestDocumentHelper {
@Test
assertNotSame(documentBuilder, DocumentHelper.newDocumentBuilder());
assertTrue(documentBuilder.isNamespaceAware());
assertFalse(documentBuilder.isValidating());
- documentBuilder.parse(new InputSource(new ByteArrayInputStream("<xml></xml>".getBytes("UTF-8"))));
+ documentBuilder.parse(new InputSource(new ByteArrayInputStream("<xml></xml>".getBytes(StandardCharsets.UTF_8))));
}
@Test
int limit = 1000;
ArrayList<CompletableFuture<DocumentBuilder>> futures = new ArrayList<>();
for(int i = 0; i < limit; i++) {
- futures.add(CompletableFuture.supplyAsync(() -> {
- return DocumentHelper.newDocumentBuilder();
- }));
+ futures.add(CompletableFuture.supplyAsync(DocumentHelper::newDocumentBuilder));
}
HashSet<DocumentBuilder> dbs = new HashSet<>();
for(CompletableFuture<DocumentBuilder> future : futures) {
import static org.junit.Assert.*;
import java.io.ByteArrayInputStream;
+import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import javax.xml.XMLConstants;
-import javax.xml.parsers.DocumentBuilder;
import org.junit.Test;
import org.xml.sax.InputSource;
// ignore exceptions from old parsers that don't support these features
// (https://bz.apache.org/bugzilla/show_bug.cgi?id=62692)
}
- reader.parse(new InputSource(new ByteArrayInputStream("<xml></xml>".getBytes("UTF-8"))));
+ reader.parse(new InputSource(new ByteArrayInputStream("<xml></xml>".getBytes(StandardCharsets.UTF_8))));
}
@Test
HashSet<XMLReader> readers = new HashSet<>();
for(CompletableFuture<XMLReader> future : futures) {
XMLReader reader = future.get(10, TimeUnit.SECONDS);
- assertTrue(reader.getFeature(XMLConstants.FEATURE_SECURE_PROCESSING));
+ try {
+ assertTrue(reader.getFeature(XMLConstants.FEATURE_SECURE_PROCESSING));
+ } catch (SAXNotRecognizedException e) {
+ // can happen for older XML Parsers, e.g. we have a CI Job which runs with Xerces XML Parser
+ assertTrue("Had Exception about not-recognized SAX feature: " + e + " which is only expected" +
+ " for Xerces XML Parser, but had parser: " + reader,
+ reader.getClass().getName().contains("org.apache.xerces"));
+ }
readers.add(reader);
}
assertEquals(limit, readers.size());