diff options
author | Andreas Beeker <kiwiwings@apache.org> | 2019-12-03 21:56:47 +0000 |
---|---|---|
committer | Andreas Beeker <kiwiwings@apache.org> | 2019-12-03 21:56:47 +0000 |
commit | 08159e66a738c291572745cee8cc3617777dc2b5 (patch) | |
tree | 849e1239faa41d3493e57a0141a1c3d6b598fd9f /src/scratchpad | |
parent | dd3279df30eb9656c3a72f4a6a2445f777c7dd5a (diff) | |
download | poi-08159e66a738c291572745cee8cc3617777dc2b5.tar.gz poi-08159e66a738c291572745cee8cc3617777dc2b5.zip |
Sonar Fixes + Refactor scattered XML initializations to XMLHelper
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1870769 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/scratchpad')
10 files changed, 54 insertions, 109 deletions
diff --git a/src/scratchpad/src/org/apache/poi/hssf/converter/ExcelToFoConverter.java b/src/scratchpad/src/org/apache/poi/hssf/converter/ExcelToFoConverter.java index a8d2832240..0bdaf57c65 100644 --- a/src/scratchpad/src/org/apache/poi/hssf/converter/ExcelToFoConverter.java +++ b/src/scratchpad/src/org/apache/poi/hssf/converter/ExcelToFoConverter.java @@ -20,9 +20,7 @@ import java.io.File; import java.util.ArrayList; import java.util.List; -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; @@ -92,12 +90,8 @@ public class ExcelToFoConverter extends AbstractExcelConverter DOMSource domSource = new DOMSource( doc ); StreamResult streamResult = new StreamResult( new File(args[1]) ); - TransformerFactory tf = TransformerFactory.newInstance(); - Transformer serializer = tf.newTransformer(); // TODO set encoding from a command argument - serializer.setOutputProperty( OutputKeys.ENCODING, "UTF-8" ); - serializer.setOutputProperty( OutputKeys.INDENT, "no" ); - serializer.setOutputProperty( OutputKeys.METHOD, "xml" ); + Transformer serializer = XMLHelper.newTransformer(); serializer.transform( domSource, streamResult ); } @@ -113,8 +107,7 @@ public class ExcelToFoConverter extends AbstractExcelConverter final HSSFWorkbook workbook = AbstractExcelUtils.loadXls( xlsFile ); try { ExcelToFoConverter excelToHtmlConverter = new ExcelToFoConverter( - XMLHelper.getDocumentBuilderFactory().newDocumentBuilder() - .newDocument() ); + XMLHelper.newDocumentBuilder().newDocument() ); excelToHtmlConverter.processWorkbook( workbook ); return excelToHtmlConverter.getDocument(); } finally { diff --git a/src/scratchpad/src/org/apache/poi/hssf/converter/ExcelToHtmlConverter.java b/src/scratchpad/src/org/apache/poi/hssf/converter/ExcelToHtmlConverter.java index 2e173e15d4..e3f5e9aaff 100644 --- a/src/scratchpad/src/org/apache/poi/hssf/converter/ExcelToHtmlConverter.java +++ b/src/scratchpad/src/org/apache/poi/hssf/converter/ExcelToHtmlConverter.java @@ -27,7 +27,6 @@ import java.util.Map; import javax.xml.parsers.ParserConfigurationException; 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; @@ -85,11 +84,8 @@ public class ExcelToHtmlConverter extends AbstractExcelConverter { DOMSource domSource = new DOMSource( doc ); StreamResult streamResult = new StreamResult( new File(args[1]) ); - TransformerFactory tf = TransformerFactory.newInstance(); - Transformer serializer = tf.newTransformer(); + Transformer serializer = XMLHelper.newTransformer(); // TODO set encoding from a command argument - serializer.setOutputProperty( OutputKeys.ENCODING, "UTF-8" ); - serializer.setOutputProperty( OutputKeys.INDENT, "no" ); serializer.setOutputProperty( OutputKeys.METHOD, "html" ); serializer.transform( domSource, streamResult ); } @@ -133,8 +129,7 @@ public class ExcelToHtmlConverter extends AbstractExcelConverter { */ public static Document process( HSSFWorkbook workbook ) throws IOException, ParserConfigurationException { ExcelToHtmlConverter excelToHtmlConverter = new ExcelToHtmlConverter( - XMLHelper.getDocumentBuilderFactory().newDocumentBuilder() - .newDocument() ); + XMLHelper.newDocumentBuilder().newDocument() ); excelToHtmlConverter.processWorkbook( workbook ); return excelToHtmlConverter.getDocument(); } diff --git a/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToFoConverter.java b/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToFoConverter.java index ad51d4b7cf..9f2bbdba80 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToFoConverter.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToFoConverter.java @@ -24,9 +24,7 @@ import java.util.Set; import java.util.concurrent.atomic.AtomicInteger; import javax.xml.parsers.DocumentBuilder; -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; @@ -86,17 +84,14 @@ public class WordToFoConverter extends AbstractWordConverter DOMSource domSource = new DOMSource( doc ); StreamResult streamResult = new StreamResult( new File( args[1] ) ); - TransformerFactory tf = TransformerFactory.newInstance(); - Transformer serializer = tf.newTransformer(); // TODO set encoding from a command argument - serializer.setOutputProperty( OutputKeys.ENCODING, "UTF-8" ); - serializer.setOutputProperty( OutputKeys.INDENT, "yes" ); + Transformer serializer = XMLHelper.newTransformer(); serializer.transform( domSource, streamResult ); } static Document process( File docFile ) throws Exception { - final DocumentBuilder docBuild = XMLHelper.getDocumentBuilderFactory().newDocumentBuilder(); + final DocumentBuilder docBuild = XMLHelper.newDocumentBuilder(); try (final HWPFDocumentCore hwpfDocument = WordToFoUtils.loadDoc( docFile )) { WordToFoConverter wordToFoConverter = new WordToFoConverter(docBuild.newDocument()); wordToFoConverter.processDocument(hwpfDocument); diff --git a/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToHtmlConverter.java b/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToHtmlConverter.java index 784fb5028c..d3d4f6e57b 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToHtmlConverter.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToHtmlConverter.java @@ -30,7 +30,6 @@ import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; -import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; @@ -154,18 +153,15 @@ public class WordToHtmlConverter extends AbstractWordConverter DOMSource domSource = new DOMSource( doc ); StreamResult streamResult = new StreamResult( new File(args[1]) ); - TransformerFactory tf = TransformerFactory.newInstance(); - Transformer serializer = tf.newTransformer(); + Transformer serializer = XMLHelper.newTransformer(); // TODO set encoding from a command argument - serializer.setOutputProperty( OutputKeys.ENCODING, "UTF-8" ); - serializer.setOutputProperty( OutputKeys.INDENT, "yes" ); serializer.setOutputProperty( OutputKeys.METHOD, "html" ); serializer.transform( domSource, streamResult ); } static Document process( File docFile ) throws IOException, ParserConfigurationException { - final DocumentBuilder docBuild = XMLHelper.getDocumentBuilderFactory().newDocumentBuilder(); + final DocumentBuilder docBuild = XMLHelper.newDocumentBuilder(); try (final HWPFDocumentCore wordDocument = AbstractWordUtils.loadDoc( docFile )) { WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(docBuild.newDocument()); wordToHtmlConverter.processDocument(wordDocument); diff --git a/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToTextConverter.java b/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToTextConverter.java index 399029233d..3103c81869 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToTextConverter.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToTextConverter.java @@ -76,7 +76,7 @@ public class WordToTextConverter extends AbstractWordConverter throws Exception { WordToTextConverter wordToTextConverter = new WordToTextConverter( - XMLHelper.getDocumentBuilderFactory().newDocumentBuilder().newDocument() ); + XMLHelper.newDocumentBuilder().newDocument() ); wordToTextConverter.processDocument( wordDocument ); return wordToTextConverter.getText(); } @@ -105,11 +105,8 @@ public class WordToTextConverter extends AbstractWordConverter DOMSource domSource = new DOMSource( doc ); StreamResult streamResult = new StreamResult( new File( args[1] ) ); - TransformerFactory tf = TransformerFactory.newInstance(); - Transformer serializer = tf.newTransformer(); + Transformer serializer = XMLHelper.newTransformer(); // TODO set encoding from a command argument - serializer.setOutputProperty( OutputKeys.ENCODING, "UTF-8" ); - serializer.setOutputProperty( OutputKeys.INDENT, "no" ); serializer.setOutputProperty( OutputKeys.METHOD, "text" ); serializer.transform( domSource, streamResult ); } @@ -117,7 +114,7 @@ public class WordToTextConverter extends AbstractWordConverter private static Document process( File docFile ) throws IOException, ParserConfigurationException { try (final HWPFDocumentCore wordDocument = AbstractWordUtils.loadDoc( docFile )) { WordToTextConverter wordToTextConverter = new WordToTextConverter( - XMLHelper.getDocumentBuilderFactory().newDocumentBuilder().newDocument()); + XMLHelper.newDocumentBuilder().newDocument()); wordToTextConverter.processDocument(wordDocument); return wordToTextConverter.getDocument(); } @@ -141,8 +138,7 @@ public class WordToTextConverter extends AbstractWordConverter public WordToTextConverter() throws ParserConfigurationException { this.textDocumentFacade = new TextDocumentFacade( - XMLHelper.getDocumentBuilderFactory().newDocumentBuilder() - .newDocument() ); + XMLHelper.newDocumentBuilder().newDocument() ); } /** diff --git a/src/scratchpad/testcases/org/apache/poi/hssf/converter/TestExcelConverterSuite.java b/src/scratchpad/testcases/org/apache/poi/hssf/converter/TestExcelConverterSuite.java index a398e8e024..cdf362acc9 100644 --- a/src/scratchpad/testcases/org/apache/poi/hssf/converter/TestExcelConverterSuite.java +++ b/src/scratchpad/testcases/org/apache/poi/hssf/converter/TestExcelConverterSuite.java @@ -16,26 +16,26 @@ ==================================================================== */ package org.apache.poi.hssf.converter; -import org.apache.poi.POIDataSamples; -import org.apache.poi.hssf.usermodel.HSSFWorkbook; -import org.apache.poi.util.XMLHelper; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; +import static org.junit.Assert.assertNotNull; -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 java.io.File; -import java.io.FilenameFilter; import java.io.StringWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import static org.junit.Assert.assertNotNull; +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.POIDataSamples; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.util.XMLHelper; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; @RunWith(Parameterized.class) public class TestExcelConverterSuite @@ -53,15 +53,7 @@ public class TestExcelConverterSuite List<Object[]> files = new ArrayList<>(); File directory = POIDataSamples.getDocumentInstance().getFile( "../spreadsheet" ); - for ( final File child : directory.listFiles( new FilenameFilter() - { - @Override - public boolean accept( File dir, String name ) - { - return name.endsWith( ".xls" ) && !failingFiles.contains( name ); - } - } ) ) - { + for ( final File child : directory.listFiles((dir,name) -> name.endsWith( ".xls" ) && !failingFiles.contains( name ))) { files.add(new Object[] { child }); } @@ -84,16 +76,12 @@ public class TestExcelConverterSuite } ExcelToHtmlConverter excelToHtmlConverter = new ExcelToHtmlConverter( - XMLHelper.getDocumentBuilderFactory().newDocumentBuilder().newDocument() ); + XMLHelper.newDocumentBuilder().newDocument() ); excelToHtmlConverter.processWorkbook( workbook ); StringWriter stringWriter = new StringWriter(); - Transformer transformer = TransformerFactory.newInstance() - .newTransformer(); - transformer.setOutputProperty( OutputKeys.ENCODING, "utf-8" ); - transformer.setOutputProperty( OutputKeys.INDENT, "yes" ); - transformer.setOutputProperty( OutputKeys.METHOD, "xml" ); + Transformer transformer = XMLHelper.newTransformer(); transformer.transform( new DOMSource( excelToHtmlConverter.getDocument() ), new StreamResult( stringWriter ) ); @@ -113,7 +101,7 @@ public class TestExcelConverterSuite } ExcelToHtmlConverter excelToHtmlConverter = new ExcelToHtmlConverter( - XMLHelper.getDocumentBuilderFactory().newDocumentBuilder().newDocument() ); + XMLHelper.newDocumentBuilder().newDocument() ); excelToHtmlConverter.processWorkbook( workbook ); StringWriter stringWriter = new StringWriter(); diff --git a/src/scratchpad/testcases/org/apache/poi/hwpf/converter/TestWordToConverterSuite.java b/src/scratchpad/testcases/org/apache/poi/hwpf/converter/TestWordToConverterSuite.java index 3525ba78b3..1a8d5b8b80 100644 --- a/src/scratchpad/testcases/org/apache/poi/hwpf/converter/TestWordToConverterSuite.java +++ b/src/scratchpad/testcases/org/apache/poi/hwpf/converter/TestWordToConverterSuite.java @@ -18,18 +18,18 @@ package org.apache.poi.hwpf.converter; import static org.junit.Assert.assertNotNull; -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 java.io.File; -import java.io.FilenameFilter; import java.io.StringWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +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.POIDataSamples; import org.apache.poi.hwpf.HWPFDocumentCore; import org.apache.poi.util.XMLHelper; @@ -55,15 +55,7 @@ public class TestWordToConverterSuite List<Object[]> files = new ArrayList<>(); File directory = POIDataSamples.getDocumentInstance().getFile( "../document" ); - for ( final File child : directory.listFiles( new FilenameFilter() - { - @Override - public boolean accept( File dir, String name ) - { - return name.endsWith( ".doc" ) && !failingFiles.contains( name ); - } - } ) ) - { + for ( final File child : directory.listFiles((dir,name) -> name.endsWith( ".doc" ) && !failingFiles.contains( name ))) { files.add(new Object[] { child }); } @@ -83,15 +75,12 @@ public class TestWordToConverterSuite } WordToFoConverter wordToFoConverter = new WordToFoConverter( - XMLHelper.getDocumentBuilderFactory().newDocumentBuilder().newDocument() ); + XMLHelper.newDocumentBuilder().newDocument() ); wordToFoConverter.processDocument( hwpfDocument ); StringWriter stringWriter = new StringWriter(); - Transformer transformer = TransformerFactory.newInstance() - .newTransformer(); - transformer.setOutputProperty( OutputKeys.ENCODING, "utf-8" ); - transformer.setOutputProperty( OutputKeys.INDENT, "false" ); + Transformer transformer = XMLHelper.newTransformer(); transformer.transform( new DOMSource( wordToFoConverter.getDocument() ), new StreamResult( stringWriter ) ); @@ -111,7 +100,7 @@ public class TestWordToConverterSuite } WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter( - XMLHelper.getDocumentBuilderFactory().newDocumentBuilder().newDocument() ); + XMLHelper.newDocumentBuilder().newDocument() ); wordToHtmlConverter.processDocument( hwpfDocument ); StringWriter stringWriter = new StringWriter(); @@ -140,7 +129,7 @@ public class TestWordToConverterSuite } WordToTextConverter wordToTextConverter = new WordToTextConverter( - XMLHelper.getDocumentBuilderFactory().newDocumentBuilder().newDocument() ); + XMLHelper.newDocumentBuilder().newDocument() ); wordToTextConverter.processDocument( wordDocument ); StringWriter stringWriter = new StringWriter(); diff --git a/src/scratchpad/testcases/org/apache/poi/hwpf/converter/TestWordToFoConverter.java b/src/scratchpad/testcases/org/apache/poi/hwpf/converter/TestWordToFoConverter.java index cd8ced23c9..54970dce8a 100644 --- a/src/scratchpad/testcases/org/apache/poi/hwpf/converter/TestWordToFoConverter.java +++ b/src/scratchpad/testcases/org/apache/poi/hwpf/converter/TestWordToFoConverter.java @@ -22,9 +22,7 @@ import static org.apache.poi.POITestCase.assertContains; import java.io.StringWriter; -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; @@ -45,14 +43,12 @@ public class TestWordToFoConverter .getDocumentInstance().openResourceAsStream( sampleFileName ) ); WordToFoConverter wordToFoConverter = new WordToFoConverter( - XMLHelper.getDocumentBuilderFactory().newDocumentBuilder().newDocument() ); + XMLHelper.newDocumentBuilder().newDocument() ); wordToFoConverter.processDocument( hwpfDocument ); StringWriter stringWriter = new StringWriter(); - Transformer transformer = TransformerFactory.newInstance() - .newTransformer(); - transformer.setOutputProperty( OutputKeys.INDENT, "yes" ); + Transformer transformer = XMLHelper.newTransformer(); transformer.transform( new DOMSource( wordToFoConverter.getDocument() ), new StreamResult( stringWriter ) ); diff --git a/src/scratchpad/testcases/org/apache/poi/hwpf/converter/TestWordToHtmlConverter.java b/src/scratchpad/testcases/org/apache/poi/hwpf/converter/TestWordToHtmlConverter.java index b1884f748d..673ac68163 100644 --- a/src/scratchpad/testcases/org/apache/poi/hwpf/converter/TestWordToHtmlConverter.java +++ b/src/scratchpad/testcases/org/apache/poi/hwpf/converter/TestWordToHtmlConverter.java @@ -16,21 +16,21 @@ ==================================================================== */ package org.apache.poi.hwpf.converter; -import org.apache.poi.POIDataSamples; -import org.apache.poi.hwpf.HWPFDocument; -import org.apache.poi.util.XMLHelper; -import org.junit.Test; -import org.w3c.dom.Document; +import static org.apache.poi.POITestCase.assertContains; +import static org.junit.Assert.assertFalse; + +import java.io.StringWriter; 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 java.io.StringWriter; -import static org.apache.poi.POITestCase.assertContains; -import static org.junit.Assert.assertFalse; +import org.apache.poi.POIDataSamples; +import org.apache.poi.hwpf.HWPFDocument; +import org.apache.poi.util.XMLHelper; +import org.junit.Test; +import org.w3c.dom.Document; /** * Test cases for {@link WordToHtmlConverter} @@ -45,7 +45,7 @@ public class TestWordToHtmlConverter { HWPFDocument hwpfDocument = new HWPFDocument(POIDataSamples .getDocumentInstance().openResourceAsStream(sampleFileName)); - Document newDocument = XMLHelper.getDocumentBuilderFactory().newDocumentBuilder().newDocument(); + Document newDocument = XMLHelper.newDocumentBuilder().newDocument(); WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter( newDocument); @@ -58,10 +58,7 @@ public class TestWordToHtmlConverter { StringWriter stringWriter = new StringWriter(); - Transformer transformer = TransformerFactory.newInstance() - .newTransformer(); - transformer.setOutputProperty(OutputKeys.INDENT, "yes"); - transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); + Transformer transformer = XMLHelper.newTransformer(); transformer.setOutputProperty(OutputKeys.METHOD, "html"); transformer.transform( new DOMSource(wordToHtmlConverter.getDocument()), diff --git a/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestBugs.java b/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestBugs.java index 09c6c64b33..e0a0671c77 100644 --- a/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestBugs.java +++ b/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestBugs.java @@ -899,7 +899,7 @@ public class TestBugs{ @Test public void test59322() throws Exception { try(HWPFDocument doc = HWPFTestDataSamples.openSampleFile("59322.doc")) { - Document document = XMLHelper.getDocumentBuilderFactory().newDocumentBuilder().newDocument(); + Document document = XMLHelper.newDocumentBuilder().newDocument(); WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(document); wordToHtmlConverter.processDocument(doc); assertNotNull(document); |