Sonar Fixes

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1870856 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andreas Beeker 2019-12-05 00:18:36 +00:00
parent 08159e66a7
commit 9f2f9dc793
11 changed files with 223 additions and 273 deletions

View File

@ -20,18 +20,18 @@ package org.apache.poi.hssf.usermodel.examples;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor; import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFPatriarch; import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.ClientAnchor.AnchorType; import org.apache.poi.ss.usermodel.ClientAnchor.AnchorType;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.util.IOUtils;
/** /**
@ -675,29 +675,9 @@ public class AddDimensionedImage {
* interrupted. * interrupted.
*/ */
private byte[] imageToBytes(String imageFilename) throws IOException { private byte[] imageToBytes(String imageFilename) throws IOException {
File imageFile; File imageFile = new File(imageFilename);
FileInputStream fis = null; try (FileInputStream fis = new FileInputStream(imageFile)) {
ByteArrayOutputStream bos; return IOUtils.toByteArray(fis);
int read;
try {
imageFile = new File(imageFilename);
fis = new FileInputStream(imageFile);
bos = new ByteArrayOutputStream();
while((read = fis.read()) != -1) {
bos.write(read);
}
return(bos.toByteArray());
}
finally {
if(fis != null) {
try {
fis.close();
fis = null;
}
catch(IOException ioEx) {
// Nothing to do here
}
}
} }
} }
@ -720,42 +700,22 @@ public class AddDimensionedImage {
* *
* @param args the command line arguments * @param args the command line arguments
*/ */
public static void main(String[] args) { public static void main(String[] args) throws IOException {
String imageFile;
String outputFile;
FileOutputStream fos = null;
HSSFSheet sheet;
try {
if(args.length < 2){ if(args.length < 2){
System.err.println("Usage: AddDimensionedImage imageFile outputFile"); System.err.println("Usage: AddDimensionedImage imageFile outputFile");
return; return;
} }
imageFile = args[0]; String imageFile = args[0];
outputFile = args[1]; String outputFile = args[1];
try (HSSFWorkbook workbook = new HSSFWorkbook()) { try (HSSFWorkbook workbook = new HSSFWorkbook()) {
sheet = workbook.createSheet("Picture Test"); HSSFSheet sheet = workbook.createSheet("Picture Test");
new AddDimensionedImage().addImageToSheet("A1", sheet, new AddDimensionedImage().addImageToSheet("A1", sheet,
imageFile, 125, 125, imageFile, 125, 125,
AddDimensionedImage.EXPAND_ROW_AND_COLUMN); AddDimensionedImage.EXPAND_ROW_AND_COLUMN);
fos = new FileOutputStream(outputFile); try (FileOutputStream fos = new FileOutputStream(outputFile)) {
workbook.write(fos); workbook.write(fos);
} }
} catch(IOException ioEx) {
System.out.println("Caught an: " + ioEx.getClass().getName());
System.out.println("Message: " + ioEx.getMessage());
System.out.println("Stacktrace follows...........");
ioEx.printStackTrace(System.out);
}
finally {
try {
if(fos != null) {
fos.close();
fos = null;
}
} catch(IOException ioEx) {
// I G N O R E
}
} }
} }
@ -785,11 +745,11 @@ public class AddDimensionedImage {
* @author Mark Beardsley [mas at apache.org] * @author Mark Beardsley [mas at apache.org]
* @version 1.00 5th August 2009. * @version 1.00 5th August 2009.
*/ */
public class ClientAnchorDetail { public static class ClientAnchorDetail {
public int fromIndex; private int fromIndex;
public int toIndex; private int toIndex;
public int inset; private int inset;
/** /**
* Create a new instance of the ClientAnchorDetail class using the * Create a new instance of the ClientAnchorDetail class using the
@ -857,17 +817,13 @@ public class AddDimensionedImage {
* Additional constants. * Additional constants.
* widthUnits2Millimetres() and millimetres2Units() methods. * widthUnits2Millimetres() and millimetres2Units() methods.
*/ */
public static class ConvertImageUnits { private static final class ConvertImageUnits {
// Each cell conatins a fixed number of co-ordinate points; this number // Each cell conatins a fixed number of co-ordinate points; this number
// does not vary with row height or column width or with font. These two // does not vary with row height or column width or with font. These two
// constants are defined below. // constants are defined below.
public static final int TOTAL_COLUMN_COORDINATE_POSITIONS = 1023; // MB public static final int TOTAL_COLUMN_COORDINATE_POSITIONS = 1023; // MB
public static final int TOTAL_ROW_COORDINATE_POSITIONS = 255; // MB public static final int TOTAL_ROW_COORDINATE_POSITIONS = 255; // MB
// The resoultion of an image can be expressed as a specific number
// of pixels per inch. Displays and printers differ but 96 pixels per
// inch is an acceptable standard to beging with.
public static final int PIXELS_PER_INCH = 96; // MB
// Cnstants that defines how many pixels and points there are in a // Cnstants that defines how many pixels and points there are in a
// millimetre. These values are required for the conversion algorithm. // millimetre. These values are required for the conversion algorithm.
public static final double PIXELS_PER_MILLIMETRES = 3.78; // MB public static final double PIXELS_PER_MILLIMETRES = 3.78; // MB
@ -880,13 +836,11 @@ public class AddDimensionedImage {
public static final double CELL_BORDER_WIDTH_MILLIMETRES = 2.0D; // MB public static final double CELL_BORDER_WIDTH_MILLIMETRES = 2.0D; // MB
public static final short EXCEL_COLUMN_WIDTH_FACTOR = 256; public static final short EXCEL_COLUMN_WIDTH_FACTOR = 256;
public static final int UNIT_OFFSET_LENGTH = 7; public static final int UNIT_OFFSET_LENGTH = 7;
public static final int[] UNIT_OFFSET_MAP = new int[] private static final int[] UNIT_OFFSET_MAP = new int[]
{ 0, 36, 73, 109, 146, 182, 219 }; { 0, 36, 73, 109, 146, 182, 219 };
/** /**
* pixel units to excel width units(units of 1/256th of a character width) * pixel units to excel width units(units of 1/256th of a character width)
* @param pxs
* @return
*/ */
public static short pixel2WidthUnits(int pxs) { public static short pixel2WidthUnits(int pxs) {
short widthUnits = (short) (EXCEL_COLUMN_WIDTH_FACTOR * short widthUnits = (short) (EXCEL_COLUMN_WIDTH_FACTOR *
@ -898,9 +852,6 @@ public class AddDimensionedImage {
/** /**
* excel width units(units of 1/256th of a character width) to pixel * excel width units(units of 1/256th of a character width) to pixel
* units. * units.
*
* @param widthUnits
* @return
*/ */
public static int widthUnits2Pixel(short widthUnits) { public static int widthUnits2Pixel(short widthUnits) {
int pixels = (widthUnits / EXCEL_COLUMN_WIDTH_FACTOR) int pixels = (widthUnits / EXCEL_COLUMN_WIDTH_FACTOR)

View File

@ -20,7 +20,6 @@ package org.apache.poi.hssf.usermodel.examples;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.Locale;
import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFCellStyle;
@ -57,7 +56,8 @@ public final class HSSFReadWrite {
* rows/cells. * rows/cells.
*/ */
private static void testCreateSampleSheet(String outputFilename) throws IOException { private static void testCreateSampleSheet(String outputFilename) throws IOException {
try (HSSFWorkbook wb = new HSSFWorkbook()) { try (HSSFWorkbook wb = new HSSFWorkbook();
FileOutputStream out = new FileOutputStream(outputFilename)) {
HSSFSheet s = wb.createSheet(); HSSFSheet s = wb.createSheet();
HSSFCellStyle cs = wb.createCellStyle(); HSSFCellStyle cs = wb.createCellStyle();
HSSFCellStyle cs2 = wb.createCellStyle(); HSSFCellStyle cs2 = wb.createCellStyle();
@ -121,11 +121,9 @@ public final class HSSFReadWrite {
wb.removeSheetAt(1); wb.removeSheetAt(1);
// end deleted sheet // end deleted sheet
try (FileOutputStream out = new FileOutputStream(outputFilename)) {
wb.write(out); wb.write(out);
} }
} }
}
/** /**
* Method main * Method main
@ -147,14 +145,13 @@ public final class HSSFReadWrite {
* "MODIFIED CELL" then writes it out. Hence this is "modify test 1". If you * "MODIFIED CELL" then writes it out. Hence this is "modify test 1". If you
* take the output from the write test, you'll have a valid scenario. * take the output from the write test, you'll have a valid scenario.
*/ */
public static void main(String[] args) { public static void main(String[] args) throws Exception {
if (args.length < 1) { if (args.length < 1) {
System.err.println("At least one argument expected"); System.err.println("At least one argument expected");
return; return;
} }
String fileName = args[0]; String fileName = args[0];
try {
if (args.length < 2) { if (args.length < 2) {
try (HSSFWorkbook wb = HSSFReadWrite.readFile(fileName)) { try (HSSFWorkbook wb = HSSFReadWrite.readFile(fileName)) {
@ -163,8 +160,7 @@ public final class HSSFReadWrite {
for (int k = 0; k < wb.getNumberOfSheets(); k++) { for (int k = 0; k < wb.getNumberOfSheets(); k++) {
HSSFSheet sheet = wb.getSheetAt(k); HSSFSheet sheet = wb.getSheetAt(k);
int rows = sheet.getPhysicalNumberOfRows(); int rows = sheet.getPhysicalNumberOfRows();
System.out.println("Sheet " + k + " \"" + wb.getSheetName(k) + "\" has " + rows System.out.println("Sheet " + k + " \"" + wb.getSheetName(k) + "\" has " + rows + " row(s).");
+ " row(s).");
for (int r = 0; r < rows; r++) { for (int r = 0; r < rows; r++) {
HSSFRow row = sheet.getRow(r); HSSFRow row = sheet.getRow(r);
if (row == null) { if (row == null) {
@ -206,56 +202,47 @@ public final class HSSFReadWrite {
default: default:
value = "UNKNOWN value of type " + cell.getCellType(); value = "UNKNOWN value of type " + cell.getCellType();
} }
System.out.println("CELL col=" + cell.getColumnIndex() + " VALUE=" System.out.println("CELL col=" + cell.getColumnIndex() + " VALUE=" + value);
+ value);
} }
} }
} }
} }
} }
} else if (args.length == 2) { } else if (args.length == 2) {
if (args[1].toLowerCase(Locale.ROOT).equals("write")) { if ("write".equalsIgnoreCase(args[1])) {
System.out.println("Write mode"); System.out.println("Write mode");
long time = System.currentTimeMillis(); long time = System.currentTimeMillis();
HSSFReadWrite.testCreateSampleSheet(fileName); HSSFReadWrite.testCreateSampleSheet(fileName);
System.out.println("" + (System.currentTimeMillis() - time) System.out.println("" + (System.currentTimeMillis() - time) + " ms generation time");
+ " ms generation time");
} else { } else {
System.out.println("readwrite test"); System.out.println("readwrite test");
try (HSSFWorkbook wb = HSSFReadWrite.readFile(fileName)) { try (HSSFWorkbook wb = HSSFReadWrite.readFile(fileName);
try (FileOutputStream stream = new FileOutputStream(args[1])) { FileOutputStream stream = new FileOutputStream(args[1])) {
wb.write(stream); wb.write(stream);
} }
} }
} } else if (args.length == 3 && "modify1".equalsIgnoreCase(args[2])) {
} else if (args.length == 3 && args[2].equalsIgnoreCase("modify1")) {
// delete row 0-24, row 74 - 99 && change cell 3 on row 39 to string "MODIFIED CELL!!" // delete row 0-24, row 74 - 99 && change cell 3 on row 39 to string "MODIFIED CELL!!"
try (HSSFWorkbook wb = HSSFReadWrite.readFile(fileName)) { try (HSSFWorkbook wb = HSSFReadWrite.readFile(fileName);
FileOutputStream stream = new FileOutputStream(args[1])) {
HSSFSheet sheet = wb.getSheetAt(0); HSSFSheet sheet = wb.getSheetAt(0);
for (int k = 0; k < 25; k++) { for (int k = 0; k < 25; k++) {
HSSFRow row = sheet.getRow(k); HSSFRow row = sheet.getRow(k);
sheet.removeRow(row); sheet.removeRow(row);
} }
for (int k = 74; k < 100; k++) { for (int k = 74; k < 100; k++) {
HSSFRow row = sheet.getRow(k); HSSFRow row = sheet.getRow(k);
sheet.removeRow(row); sheet.removeRow(row);
} }
HSSFRow row = sheet.getRow(39); HSSFRow row = sheet.getRow(39);
HSSFCell cell = row.getCell(3); HSSFCell cell = row.getCell(3);
cell.setCellValue("MODIFIED CELL!!!!!"); cell.setCellValue("MODIFIED CELL!!!!!");
try (FileOutputStream stream = new FileOutputStream(args[1])) {
wb.write(stream); wb.write(stream);
} }
} }
} }
} catch (Exception e) {
e.printStackTrace();
}
}
} }

View File

@ -70,6 +70,7 @@ public class SViewer extends JApplet {
* Initialize the applet * Initialize the applet
*/ */
@Override @Override
@SuppressWarnings("squid:S1148")
public void init() { public void init() {
try { try {
jbInit(); jbInit();

View File

@ -17,13 +17,37 @@
package org.apache.poi.hssf.view; package org.apache.poi.hssf.view;
import java.awt.*; import java.awt.AWTEvent;
import java.awt.event.*; import java.awt.BorderLayout;
import java.io.*; import java.awt.Dimension;
import javax.swing.*; import java.awt.Graphics;
import javax.swing.table.*; import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.*; import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
/** /**
* This class presents the sheets to the user. * This class presents the sheets to the user.
@ -144,7 +168,7 @@ public void paint(Graphics g) {
* The default is to popup a menu when the event occurs over a tab * The default is to popup a menu when the event occurs over a tab
*/ */
private class TabListener implements MouseListener { private class TabListener implements MouseListener {
public JPopupMenu popup; private final JPopupMenu popup;
public TabListener() { public TabListener() {
popup = new JPopupMenu("Sheet"); popup = new JPopupMenu("Sheet");
popup.add(createInsertSheetAction()); popup.add(createInsertSheetAction());

View File

@ -863,9 +863,9 @@ public class AddDimensionedImage {
*/ */
public class ClientAnchorDetail { public class ClientAnchorDetail {
public int fromIndex; private int fromIndex;
public int toIndex; private int toIndex;
public int inset; private int inset;
/** /**
* Create a new instance of the ClientAnchorDetail class using the * Create a new instance of the ClientAnchorDetail class using the
@ -938,26 +938,25 @@ public class AddDimensionedImage {
// Each cell conatins a fixed number of co-ordinate points; this number // Each cell conatins a fixed number of co-ordinate points; this number
// does not vary with row height or column width or with font. These two // does not vary with row height or column width or with font. These two
// constants are defined below. // constants are defined below.
public static final int TOTAL_COLUMN_COORDINATE_POSITIONS = 1023; // MB public static final int TOTAL_COLUMN_COORDINATE_POSITIONS = 1023;
public static final int TOTAL_ROW_COORDINATE_POSITIONS = 255; // MB public static final int TOTAL_ROW_COORDINATE_POSITIONS = 255;
// The resoultion of an image can be expressed as a specific number // The resoultion of an image can be expressed as a specific number
// of pixels per inch. Displays and printers differ but 96 pixels per // of pixels per inch. Displays and printers differ but 96 pixels per
// inch is an acceptable standard to beging with. // inch is an acceptable standard to beging with.
public static final int PIXELS_PER_INCH = 96; // MB public static final int PIXELS_PER_INCH = 96;
// Cnstants that defines how many pixels and points there are in a // Cnstants that defines how many pixels and points there are in a
// millimetre. These values are required for the conversion algorithm. // millimetre. These values are required for the conversion algorithm.
public static final double PIXELS_PER_MILLIMETRES = 3.78; // MB public static final double PIXELS_PER_MILLIMETRES = 3.78;
public static final double POINTS_PER_MILLIMETRE = 2.83; // MB public static final double POINTS_PER_MILLIMETRE = 2.83;
// The column width returned by HSSF and the width of a picture when // The column width returned by HSSF and the width of a picture when
// positioned to exactly cover one cell are different by almost exactly // positioned to exactly cover one cell are different by almost exactly
// 2mm - give or take rounding errors. This constant allows that // 2mm - give or take rounding errors. This constant allows that
// additional amount to be accounted for when calculating how many // additional amount to be accounted for when calculating how many
// celles the image ought to overlie. // celles the image ought to overlie.
public static final double CELL_BORDER_WIDTH_MILLIMETRES = 2.0D; // MB public static final double CELL_BORDER_WIDTH_MILLIMETRES = 2.0D;
public static final short EXCEL_COLUMN_WIDTH_FACTOR = 256; public static final short EXCEL_COLUMN_WIDTH_FACTOR = 256;
public static final int UNIT_OFFSET_LENGTH = 7; public static final int UNIT_OFFSET_LENGTH = 7;
public static final int[] UNIT_OFFSET_MAP = new int[] private static final int[] UNIT_OFFSET_MAP = { 0, 36, 73, 109, 146, 182, 219 };
{ 0, 36, 73, 109, 146, 182, 219 };
/** /**
* pixel units to excel width units(units of 1/256th of a character width) * pixel units to excel width units(units of 1/256th of a character width)
@ -1007,13 +1006,5 @@ public class AddDimensionedImage {
return(ConvertImageUnits.pixel2WidthUnits((int)(millimetres * return(ConvertImageUnits.pixel2WidthUnits((int)(millimetres *
ConvertImageUnits.PIXELS_PER_MILLIMETRES))); ConvertImageUnits.PIXELS_PER_MILLIMETRES)));
} }
public static int pointsToPixels(double points) {
return (int) Math.round(points / 72D * PIXELS_PER_INCH);
}
public static double pointsToMillimeters(double points) {
return points / 72D * 25.4;
}
} }
} }

View File

@ -148,9 +148,9 @@ public class CheckFunctionsSupported {
public static class FormulaEvaluationProblems { public static class FormulaEvaluationProblems {
/** Which used functions are unsupported by POI at this time */ /** Which used functions are unsupported by POI at this time */
public Set<String> unsupportedFunctions; private final Set<String> unsupportedFunctions;
/** Which cells had unevaluatable formulas, and why? */ /** Which cells had unevaluatable formulas, and why? */
public Map<CellReference,Exception> unevaluatableCells; private final Map<CellReference,Exception> unevaluatableCells;
protected FormulaEvaluationProblems(Set<String> unsupportedFunctions, protected FormulaEvaluationProblems(Set<String> unsupportedFunctions,
Map<CellReference, Exception> unevaluatableCells) { Map<CellReference, Exception> unevaluatableCells) {

View File

@ -87,19 +87,14 @@ public class ChartFromScratch {
Double[] values1 = listCountries.toArray(new Double[0]); Double[] values1 = listCountries.toArray(new Double[0]);
Double[] values2 = listSpeakers.toArray(new Double[0]); Double[] values2 = listSpeakers.toArray(new Double[0]);
try (XWPFDocument doc = new XWPFDocument()) { try (XWPFDocument doc = new XWPFDocument();
OutputStream out = new FileOutputStream("chart-from-scratch.docx")) {
XWPFChart chart = doc.createChart(XDDFChart.DEFAULT_WIDTH * 10, XDDFChart.DEFAULT_HEIGHT * 15); XWPFChart chart = doc.createChart(XDDFChart.DEFAULT_WIDTH * 10, XDDFChart.DEFAULT_HEIGHT * 15);
setBarData(chart, chartTitle, series, categories, values1, values2); setBarData(chart, chartTitle, series, categories, values1, values2);
// save the result // save the result
try (OutputStream out = new FileOutputStream("chart-from-scratch.docx")) {
doc.write(out); doc.write(out);
} }
} }
catch(Exception e)
{
e.printStackTrace();
}
}
System.out.println("Done"); System.out.println("Done");
} }

View File

@ -17,11 +17,21 @@
package org.apache.poi.util; package org.apache.poi.util;
import static javax.xml.XMLConstants.ACCESS_EXTERNAL_DTD;
import static javax.xml.XMLConstants.ACCESS_EXTERNAL_SCHEMA;
import static javax.xml.XMLConstants.ACCESS_EXTERNAL_STYLESHEET;
import static javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING;
import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI;
import static javax.xml.stream.XMLInputFactory.IS_NAMESPACE_AWARE;
import static javax.xml.stream.XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES;
import static javax.xml.stream.XMLInputFactory.IS_VALIDATING;
import static javax.xml.stream.XMLInputFactory.SUPPORT_DTD;
import static javax.xml.stream.XMLOutputFactory.IS_REPAIRING_NAMESPACES;
import java.io.StringReader; import java.io.StringReader;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.ParserConfigurationException;
@ -44,6 +54,8 @@ import org.xml.sax.XMLReader;
/** /**
* Helper methods for working with javax.xml classes. * Helper methods for working with javax.xml classes.
*
* @see <a href="https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html">OWASP XXE</a>
*/ */
@Internal @Internal
public final class XMLHelper { public final class XMLHelper {
@ -86,20 +98,18 @@ public final class XMLHelper {
/** /**
* Creates a new DocumentBuilderFactory, with sensible defaults * Creates a new DocumentBuilderFactory, with sensible defaults
*
* @see <a href="https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html">OWASP XXE</a>
*/ */
@SuppressWarnings({"squid:S2755"}) @SuppressWarnings({"squid:S2755"})
public static DocumentBuilderFactory getDocumentBuilderFactory() { public static DocumentBuilderFactory getDocumentBuilderFactory() {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true); factory.setNamespaceAware(true);
// this doesn't appear to work, and we still need to limit // this doesn't appear to work, and we still need to limit
// entity expansions to 1 in trySetXercesSecurityManager // entity expansions to 1 in trySet(XercesSecurityManager)
factory.setExpandEntityReferences(false); factory.setExpandEntityReferences(false);
factory.setValidating(false); factory.setValidating(false);
trySet(factory::setFeature, XMLConstants.FEATURE_SECURE_PROCESSING, true); trySet(factory::setFeature, FEATURE_SECURE_PROCESSING, true);
trySet(factory::setAttribute, XMLConstants.ACCESS_EXTERNAL_SCHEMA, ""); trySet(factory::setAttribute, ACCESS_EXTERNAL_SCHEMA, "");
trySet(factory::setAttribute, XMLConstants.ACCESS_EXTERNAL_DTD, ""); trySet(factory::setAttribute, ACCESS_EXTERNAL_DTD, "");
trySet(factory::setFeature, FEATURE_EXTERNAL_ENTITIES, false); trySet(factory::setFeature, FEATURE_EXTERNAL_ENTITIES, false);
trySet(factory::setFeature, FEATURE_PARAMETER_ENTITIES, false); trySet(factory::setFeature, FEATURE_PARAMETER_ENTITIES, false);
trySet(factory::setFeature, FEATURE_LOAD_EXTERNAL_DTD, false); trySet(factory::setFeature, FEATURE_LOAD_EXTERNAL_DTD, false);
@ -134,14 +144,16 @@ public final class XMLHelper {
} }
} }
@SuppressWarnings("squid:S2755")
public static SAXParserFactory getSaxParserFactory() { public static SAXParserFactory getSaxParserFactory() {
try { try {
SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(false); factory.setValidating(false);
factory.setNamespaceAware(true); factory.setNamespaceAware(true);
trySet(factory::setFeature, XMLConstants.FEATURE_SECURE_PROCESSING, true); trySet(factory::setFeature, FEATURE_SECURE_PROCESSING, true);
trySet(factory::setFeature, FEATURE_LOAD_DTD_GRAMMAR, false); trySet(factory::setFeature, FEATURE_LOAD_DTD_GRAMMAR, false);
trySet(factory::setFeature, FEATURE_LOAD_EXTERNAL_DTD, false); trySet(factory::setFeature, FEATURE_LOAD_EXTERNAL_DTD, false);
trySet(factory::setFeature, FEATURE_EXTERNAL_ENTITIES, false);
return factory; return factory;
} catch (RuntimeException | Error re) { // NOSONAR } catch (RuntimeException | Error re) { // NOSONAR
// this also catches NoClassDefFoundError, which may be due to a local class path issue // this also catches NoClassDefFoundError, which may be due to a local class path issue
@ -161,7 +173,8 @@ public final class XMLHelper {
public static XMLReader newXMLReader() throws SAXException, ParserConfigurationException { public static XMLReader newXMLReader() throws SAXException, ParserConfigurationException {
XMLReader xmlReader = saxFactory.newSAXParser().getXMLReader(); XMLReader xmlReader = saxFactory.newSAXParser().getXMLReader();
xmlReader.setEntityResolver(XMLHelper::ignoreEntity); xmlReader.setEntityResolver(XMLHelper::ignoreEntity);
trySet(xmlReader::setFeature, XMLConstants.FEATURE_SECURE_PROCESSING, true); trySet(xmlReader::setFeature, FEATURE_SECURE_PROCESSING, true);
trySet(xmlReader::setFeature, FEATURE_EXTERNAL_ENTITIES, false);
Object manager = getXercesSecurityManager(); Object manager = getXercesSecurityManager();
if (manager == null || !trySet(xmlReader::setProperty, PROPERTY_SECURITY_MANAGER, manager)) { if (manager == null || !trySet(xmlReader::setProperty, PROPERTY_SECURITY_MANAGER, manager)) {
// separate old version of Xerces not found => use the builtin way of setting the property // separate old version of Xerces not found => use the builtin way of setting the property
@ -176,10 +189,10 @@ public final class XMLHelper {
@SuppressWarnings({"squid:S2755"}) @SuppressWarnings({"squid:S2755"})
public static XMLInputFactory newXMLInputFactory() { public static XMLInputFactory newXMLInputFactory() {
XMLInputFactory factory = XMLInputFactory.newInstance(); XMLInputFactory factory = XMLInputFactory.newInstance();
trySet(factory::setProperty, XMLInputFactory.IS_NAMESPACE_AWARE, true); trySet(factory::setProperty, IS_NAMESPACE_AWARE, true);
trySet(factory::setProperty, XMLInputFactory.IS_VALIDATING, false); trySet(factory::setProperty, IS_VALIDATING, false);
trySet(factory::setProperty, XMLInputFactory.SUPPORT_DTD, false); trySet(factory::setProperty, SUPPORT_DTD, false);
trySet(factory::setProperty, XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false); trySet(factory::setProperty, IS_SUPPORTING_EXTERNAL_ENTITIES, false);
return factory; return factory;
} }
@ -188,7 +201,7 @@ public final class XMLHelper {
*/ */
public static XMLOutputFactory newXMLOutputFactory() { public static XMLOutputFactory newXMLOutputFactory() {
XMLOutputFactory factory = XMLOutputFactory.newInstance(); XMLOutputFactory factory = XMLOutputFactory.newInstance();
trySet(factory::setProperty, XMLOutputFactory.IS_REPAIRING_NAMESPACES, true); trySet(factory::setProperty, IS_REPAIRING_NAMESPACES, true);
return factory; return factory;
} }
@ -202,7 +215,9 @@ public final class XMLHelper {
public static TransformerFactory getTransformerFactory() { public static TransformerFactory getTransformerFactory() {
TransformerFactory factory = TransformerFactory.newInstance(); TransformerFactory factory = TransformerFactory.newInstance();
trySet(factory::setFeature, XMLConstants.FEATURE_SECURE_PROCESSING, true); trySet(factory::setFeature, FEATURE_SECURE_PROCESSING, true);
trySet(factory::setAttribute, ACCESS_EXTERNAL_DTD, "");
trySet(factory::setAttribute, ACCESS_EXTERNAL_STYLESHEET, "");
return factory; return factory;
} }
@ -216,10 +231,10 @@ public final class XMLHelper {
} }
public static SchemaFactory getSchemaFactory() { public static SchemaFactory getSchemaFactory() {
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); SchemaFactory factory = SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI);
trySet(factory::setFeature, XMLConstants.FEATURE_SECURE_PROCESSING, true); trySet(factory::setFeature, FEATURE_SECURE_PROCESSING, true);
trySet(factory::setProperty, XMLConstants.ACCESS_EXTERNAL_DTD, ""); trySet(factory::setProperty, ACCESS_EXTERNAL_DTD, "");
trySet(factory::setProperty, XMLConstants.ACCESS_EXTERNAL_SCHEMA, ""); trySet(factory::setProperty, ACCESS_EXTERNAL_SCHEMA, "");
return factory; return factory;
} }

View File

@ -27,7 +27,6 @@ import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys; import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer; import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource; import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamResult;
@ -178,11 +177,8 @@ public class WordToTextConverter extends AbstractWordConverter
DOMSource domSource = new DOMSource( getDocument() ); DOMSource domSource = new DOMSource( getDocument() );
StreamResult streamResult = new StreamResult( stringWriter ); StreamResult streamResult = new StreamResult( stringWriter );
TransformerFactory tf = TransformerFactory.newInstance(); Transformer serializer = XMLHelper.newTransformer();
Transformer serializer = tf.newTransformer();
// TODO set encoding from a command argument // TODO set encoding from a command argument
serializer.setOutputProperty( OutputKeys.ENCODING, "UTF-8" );
serializer.setOutputProperty( OutputKeys.INDENT, "no" );
serializer.setOutputProperty( OutputKeys.METHOD, "text" ); serializer.setOutputProperty( OutputKeys.METHOD, "text" );
serializer.transform( domSource, streamResult ); serializer.transform( domSource, streamResult );

View File

@ -26,7 +26,6 @@ import java.util.List;
import javax.xml.transform.OutputKeys; import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer; import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource; import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamResult;
@ -106,10 +105,7 @@ public class TestExcelConverterSuite
StringWriter stringWriter = new StringWriter(); StringWriter stringWriter = new StringWriter();
Transformer transformer = TransformerFactory.newInstance() Transformer transformer = XMLHelper.newTransformer();
.newTransformer();
transformer.setOutputProperty( OutputKeys.ENCODING, "utf-8" );
transformer.setOutputProperty( OutputKeys.INDENT, "no" );
transformer.setOutputProperty( OutputKeys.METHOD, "html" ); transformer.setOutputProperty( OutputKeys.METHOD, "html" );
transformer.transform( transformer.transform(
new DOMSource( excelToHtmlConverter.getDocument() ), new DOMSource( excelToHtmlConverter.getDocument() ),

View File

@ -26,7 +26,6 @@ import java.util.List;
import javax.xml.transform.OutputKeys; import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer; import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource; import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamResult;
@ -105,10 +104,7 @@ public class TestWordToConverterSuite
StringWriter stringWriter = new StringWriter(); StringWriter stringWriter = new StringWriter();
Transformer transformer = TransformerFactory.newInstance() Transformer transformer = XMLHelper.newTransformer();
.newTransformer();
transformer.setOutputProperty( OutputKeys.ENCODING, "utf-8" );
transformer.setOutputProperty( OutputKeys.INDENT, "false" );
transformer.setOutputProperty( OutputKeys.METHOD, "html" ); transformer.setOutputProperty( OutputKeys.METHOD, "html" );
transformer.transform( transformer.transform(
new DOMSource( wordToHtmlConverter.getDocument() ), new DOMSource( wordToHtmlConverter.getDocument() ),
@ -134,9 +130,7 @@ public class TestWordToConverterSuite
StringWriter stringWriter = new StringWriter(); StringWriter stringWriter = new StringWriter();
Transformer transformer = TransformerFactory.newInstance() Transformer transformer = XMLHelper.newTransformer();
.newTransformer();
transformer.setOutputProperty( OutputKeys.ENCODING, "utf-8" );
transformer.setOutputProperty( OutputKeys.INDENT, "yes" ); transformer.setOutputProperty( OutputKeys.INDENT, "yes" );
transformer.setOutputProperty( OutputKeys.METHOD, "text" ); transformer.setOutputProperty( OutputKeys.METHOD, "text" );
transformer.transform( transformer.transform(