From 65ccec11cb7d8806b0da6c9c8dead6706621c1e7 Mon Sep 17 00:00:00 2001
From: Andreas Beeker
Date: Sun, 5 Apr 2020 20:50:40 +0000
Subject: [PATCH] Sonar fixes - String literals should not be duplicated
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1876163 13f79535-47bb-0310-9956-ffa450edef68
---
.../poi/hslf/examples/ApacheconEU08.java | 1 +
.../apache/poi/ss/examples/BusinessPlan.java | 1 +
.../poi/ss/examples/ConditionalFormats.java | 1 +
.../poi/ss/examples/ExcelComparator.java | 21 ++--
.../poi/ss/examples/LoanCalculator.java | 26 ++--
.../poi/ss/examples/SSPerformanceTest.java | 3 +-
.../apache/poi/ss/examples/TimesheetDemo.java | 26 ++--
.../apache/poi/ss/examples/html/ToHtml.java | 1 +
.../apache/poi/ss/excelant/ExcelAntTest.java | 115 ++++++++----------
.../org/apache/poi/POIReadOnlyDocument.java | 16 ++-
.../common/usermodel/fonts/FontCharset.java | 15 +--
.../apache/poi/hpsf/ClassIDPredefined.java | 1 +
.../apache/poi/hssf/dev/FormulaViewer.java | 94 ++++++--------
13 files changed, 160 insertions(+), 161 deletions(-)
diff --git a/src/examples/src/org/apache/poi/hslf/examples/ApacheconEU08.java b/src/examples/src/org/apache/poi/hslf/examples/ApacheconEU08.java
index 41ccbe0b03..517e99b044 100644
--- a/src/examples/src/org/apache/poi/hslf/examples/ApacheconEU08.java
+++ b/src/examples/src/org/apache/poi/hslf/examples/ApacheconEU08.java
@@ -48,6 +48,7 @@ import org.apache.poi.sl.usermodel.VerticalAlignment;
*
* @author Yegor Kozlov
*/
+@SuppressWarnings("java:S1192")
public final class ApacheconEU08 {
public static void main(String[] args) throws IOException {
diff --git a/src/examples/src/org/apache/poi/ss/examples/BusinessPlan.java b/src/examples/src/org/apache/poi/ss/examples/BusinessPlan.java
index 4415bfd5f2..fbbcab895d 100644
--- a/src/examples/src/org/apache/poi/ss/examples/BusinessPlan.java
+++ b/src/examples/src/org/apache/poi/ss/examples/BusinessPlan.java
@@ -45,6 +45,7 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
*
* @author Yegor Kozlov
*/
+@SuppressWarnings("java:S1192")
public class BusinessPlan {
private static final String[] titles = {
diff --git a/src/examples/src/org/apache/poi/ss/examples/ConditionalFormats.java b/src/examples/src/org/apache/poi/ss/examples/ConditionalFormats.java
index 654e837a42..28a861b6c2 100644
--- a/src/examples/src/org/apache/poi/ss/examples/ConditionalFormats.java
+++ b/src/examples/src/org/apache/poi/ss/examples/ConditionalFormats.java
@@ -57,6 +57,7 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
* http://www.contextures.com/xlcondformat03.html
*
*/
+@SuppressWarnings("java:S1192")
public class ConditionalFormats {
/**
diff --git a/src/examples/src/org/apache/poi/ss/examples/ExcelComparator.java b/src/examples/src/org/apache/poi/ss/examples/ExcelComparator.java
index b6bad2b4c0..4dfd6f5302 100644
--- a/src/examples/src/org/apache/poi/ss/examples/ExcelComparator.java
+++ b/src/examples/src/org/apache/poi/ss/examples/ExcelComparator.java
@@ -80,8 +80,9 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
* }
*
*/
+@SuppressWarnings("java:S1192")
public class ExcelComparator {
-
+
private static final String CELL_DATA_DOES_NOT_MATCH = "Cell Data does not Match ::";
private static final String CELL_FONT_ATTRIBUTES_DOES_NOT_MATCH = "Cell Font Attributes does not Match ::";
@@ -91,7 +92,7 @@ public class ExcelComparator {
Row row;
Cell cell;
}
-
+
List listOfDifferences = new ArrayList<>();
public static void main(String[] args) throws Exception {
@@ -108,7 +109,7 @@ public class ExcelComparator {
}
}
}
-
+
/**
* Utility to compare Excel File Contents cell by cell for all sheets.
*
@@ -231,16 +232,16 @@ public class ExcelComparator {
if (loc2.workbook.getNumberOfSheets() <= i) {
return;
}
-
+
loc1.sheet = loc1.workbook.getSheetAt(i);
loc2.sheet = loc2.workbook.getSheetAt(i);
Iterator ri1 = loc1.sheet.rowIterator();
Iterator ri2 = loc2.sheet.rowIterator();
-
+
int num1 = (ri1.hasNext()) ? ri1.next().getPhysicalNumberOfCells() : 0;
int num2 = (ri2.hasNext()) ? ri2.next().getPhysicalNumberOfCells() : 0;
-
+
if (num1 != num2) {
String str = String.format(Locale.ROOT, "%s\nworkbook1 -> %s [%d] != workbook2 -> %s [%d]",
"Number Of Columns does not Match ::",
@@ -263,7 +264,7 @@ public class ExcelComparator {
loc1.sheet = loc1.workbook.getSheetAt(i);
loc2.sheet = loc2.workbook.getSheetAt(i);
-
+
int num1 = loc1.sheet.getPhysicalNumberOfRows();
int num2 = loc2.sheet.getPhysicalNumberOfRows();
@@ -292,7 +293,7 @@ public class ExcelComparator {
);
listOfDifferences.add(str);
-
+
}
}
@@ -313,7 +314,7 @@ public class ExcelComparator {
for (int i = 0; i < loc1.workbook.getNumberOfSheets(); i++) {
String name1 = loc1.workbook.getSheetName(i);
String name2 = (loc2.workbook.getNumberOfSheets() > i) ? loc2.workbook.getSheetName(i) : "";
-
+
if (!name1.equals(name2)) {
String str = String.format(Locale.ROOT, "%s\nworkbook1 -> %s [%d] != workbook2 -> %s [%d]",
"Name of the sheets do not match ::", name1, i+1, name2, i+1
@@ -461,7 +462,7 @@ public class ExcelComparator {
Color col = loc.cell.getCellStyle().getFillForegroundColorColor();
return (col instanceof XSSFColor) ? ((XSSFColor)col).getARGBHex() : "NO COLOR";
}
-
+
/**
* Checks if cell file back ground matches.
*/
diff --git a/src/examples/src/org/apache/poi/ss/examples/LoanCalculator.java b/src/examples/src/org/apache/poi/ss/examples/LoanCalculator.java
index 7dc92ee49b..4714090bf1 100644
--- a/src/examples/src/org/apache/poi/ss/examples/LoanCalculator.java
+++ b/src/examples/src/org/apache/poi/ss/examples/LoanCalculator.java
@@ -17,14 +17,25 @@
package org.apache.poi.ss.examples;
-import org.apache.poi.xssf.usermodel.*;
-import org.apache.poi.ss.usermodel.*;
-import org.apache.poi.ss.util.CellRangeAddress;
-import org.apache.poi.hssf.usermodel.HSSFWorkbook;
-
-import java.util.Map;
-import java.util.HashMap;
import java.io.FileOutputStream;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.ss.usermodel.BorderStyle;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.CellStyle;
+import org.apache.poi.ss.usermodel.FillPatternType;
+import org.apache.poi.ss.usermodel.Font;
+import org.apache.poi.ss.usermodel.HorizontalAlignment;
+import org.apache.poi.ss.usermodel.IndexedColors;
+import org.apache.poi.ss.usermodel.Name;
+import org.apache.poi.ss.usermodel.PrintSetup;
+import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.ss.util.CellRangeAddress;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
/**
* Simple Loan Calculator. Demonstrates advance usage of cell formulas and named ranges.
@@ -34,6 +45,7 @@ import java.io.FileOutputStream;
*
* @author Yegor Kozlov
*/
+@SuppressWarnings("java:S1192")
public class LoanCalculator {
public static void main(String[] args) throws Exception {
diff --git a/src/examples/src/org/apache/poi/ss/examples/SSPerformanceTest.java b/src/examples/src/org/apache/poi/ss/examples/SSPerformanceTest.java
index 92aba4d307..8484c0998c 100644
--- a/src/examples/src/org/apache/poi/ss/examples/SSPerformanceTest.java
+++ b/src/examples/src/org/apache/poi/ss/examples/SSPerformanceTest.java
@@ -25,8 +25,8 @@ import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
-import org.apache.poi.ooxml.POIXMLTypeLoader;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.ooxml.POIXMLTypeLoader;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
@@ -42,6 +42,7 @@ import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+@SuppressWarnings("java:S1192")
public class SSPerformanceTest {
public static void main(String[] args) throws IOException {
if (args.length < 4) {
diff --git a/src/examples/src/org/apache/poi/ss/examples/TimesheetDemo.java b/src/examples/src/org/apache/poi/ss/examples/TimesheetDemo.java
index 6ab8a29438..5d65bd3539 100644
--- a/src/examples/src/org/apache/poi/ss/examples/TimesheetDemo.java
+++ b/src/examples/src/org/apache/poi/ss/examples/TimesheetDemo.java
@@ -17,14 +17,25 @@
package org.apache.poi.ss.examples;
-import org.apache.poi.xssf.usermodel.*;
-import org.apache.poi.ss.util.CellRangeAddress;
-import org.apache.poi.ss.usermodel.*;
-import org.apache.poi.hssf.usermodel.HSSFWorkbook;
-
-import java.util.Map;
-import java.util.HashMap;
import java.io.FileOutputStream;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.ss.usermodel.BorderStyle;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.CellStyle;
+import org.apache.poi.ss.usermodel.FillPatternType;
+import org.apache.poi.ss.usermodel.Font;
+import org.apache.poi.ss.usermodel.HorizontalAlignment;
+import org.apache.poi.ss.usermodel.IndexedColors;
+import org.apache.poi.ss.usermodel.PrintSetup;
+import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.VerticalAlignment;
+import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.ss.util.CellRangeAddress;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
/**
* A weekly timesheet created using Apache POI.
@@ -33,6 +44,7 @@ import java.io.FileOutputStream;
*
* @author Yegor Kozlov
*/
+@SuppressWarnings("java:S1192")
public class TimesheetDemo {
private static final String[] titles = {
"Person", "ID", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun",
diff --git a/src/examples/src/org/apache/poi/ss/examples/html/ToHtml.java b/src/examples/src/org/apache/poi/ss/examples/html/ToHtml.java
index 74221715a9..874bce4d76 100644
--- a/src/examples/src/org/apache/poi/ss/examples/html/ToHtml.java
+++ b/src/examples/src/org/apache/poi/ss/examples/html/ToHtml.java
@@ -53,6 +53,7 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
* This example shows how to display a spreadsheet in HTML using the classes for
* spreadsheet display.
*/
+@SuppressWarnings("java:S1192")
public class ToHtml {
private final Workbook wb;
private final Appendable output;
diff --git a/src/excelant/java/org/apache/poi/ss/excelant/ExcelAntTest.java b/src/excelant/java/org/apache/poi/ss/excelant/ExcelAntTest.java
index 586f855667..2fd45cab15 100644
--- a/src/excelant/java/org/apache/poi/ss/excelant/ExcelAntTest.java
+++ b/src/excelant/java/org/apache/poi/ss/excelant/ExcelAntTest.java
@@ -19,6 +19,7 @@ package org.apache.poi.ss.excelant;
import java.util.Iterator;
import java.util.LinkedList;
+import java.util.function.Supplier;
import org.apache.poi.ss.excelant.util.ExcelAntEvaluationResult;
import org.apache.poi.ss.excelant.util.ExcelAntWorkbookUtil;
@@ -30,131 +31,126 @@ import org.apache.tools.ant.Task;
* This class represents a single test. In order for the test any and all
* ExcelAntEvaluateCell evaluations must pass. Therefore it is recommended
* that you use only 1 evaluator but you can use more if you choose.
- *
+ *
* @author Jon Svede ( jon [at] loquatic [dot] com )
* @author Brian Bush ( brian [dot] bush [at] nrel [dot] gov )
*
*/
+@SuppressWarnings("unused")
public class ExcelAntTest extends Task{
private LinkedList evaluators;
-
+
private LinkedList testTasks;
-
+
private String name;
-
+
private double globalPrecision;
-
+
private boolean showSuccessDetails;
-
+
private boolean showFailureDetail;
LinkedList failureMessages;
-
+
private ExcelAntWorkbookUtil workbookUtil;
-
+
private boolean passed = true;
-
+
public ExcelAntTest() {
evaluators = new LinkedList<>();
failureMessages = new LinkedList<>();
testTasks = new LinkedList<>();
}
-
+
public void setPrecision( double precision ) {
globalPrecision = precision;
}
-
+
public void setWorkbookUtil( ExcelAntWorkbookUtil wbUtil ) {
workbookUtil = wbUtil;
}
-
-
+
+
public void setShowFailureDetail( boolean value ) {
showFailureDetail = value;
}
-
+
public void setName( String nm ) {
name = nm;
}
-
+
public String getName() {
return name;
}
-
+
public void setShowSuccessDetails( boolean details ) {
showSuccessDetails = details;
}
-
+
public boolean showSuccessDetails() {
return showSuccessDetails;
}
-
+
public void addSetDouble( ExcelAntSetDoubleCell setter ) {
addSetter( setter );
}
-
+
public void addSetString( ExcelAntSetStringCell setter ){
addSetter( setter );
}
-
+
public void addSetFormula( ExcelAntSetFormulaCell setter ) {
addSetter( setter );
}
-
+
public void addHandler( ExcelAntHandlerTask handler ) {
testTasks.add( handler );
}
-
+
private void addSetter( ExcelAntSet setter ) {
-// setters.add( setter );
testTasks.add( setter );
}
-
+
public void addEvaluate( ExcelAntEvaluateCell evaluator ) {
-// evaluators.add( evaluator );
testTasks.add( evaluator );
}
-
-// public LinkedList getSetters() {
-// return setters;
-// }
protected LinkedList getEvaluators() {
return evaluators;
}
-
+
@Override
public void execute() throws BuildException {
-
+
Iterator taskIt = testTasks.iterator();
int testCount = evaluators.size();
int failureCount = 0;
-
+
// roll over all sub task elements in one loop. This allows the
- // ordering of the sub elements to be considered.
+ // ordering of the sub elements to be considered.
while( taskIt.hasNext() ) {
Task task = taskIt.next();
-
+
// log( task.getClass().getName(), Project.MSG_INFO );
-
+
if( task instanceof ExcelAntSet ) {
ExcelAntSet set = (ExcelAntSet) task;
set.setWorkbookUtil(workbookUtil);
set.execute();
}
-
+
if( task instanceof ExcelAntHandlerTask ) {
ExcelAntHandlerTask handler = (ExcelAntHandlerTask)task;
handler.setEAWorkbookUtil(workbookUtil );
handler.execute();
}
-
+
if (task instanceof ExcelAntEvaluateCell ) {
ExcelAntEvaluateCell eval = (ExcelAntEvaluateCell)task;
eval.setWorkbookUtil( workbookUtil );
-
+
if( globalPrecision > 0 ) {
log( "setting globalPrecision to " + globalPrecision + " in the evaluator", Project.MSG_VERBOSE );
eval.setGlobalPrecision( globalPrecision );
@@ -163,48 +159,41 @@ public class ExcelAntTest extends Task{
try {
eval.execute();
ExcelAntEvaluationResult result = eval.getResult();
- if( result.didTestPass() &&
- !result.evaluationCompleteWithError()) {
+
+ Supplier details = () ->
+ result.getCellName() + ". It evaluated to " +
+ result.getReturnValue() + " when the value of " +
+ eval.getExpectedValue() + " with precision of " +
+ eval.getPrecision();
+
+ if( result.didTestPass() && !result.evaluationCompleteWithError()) {
if(showSuccessDetails) {
- log("Succeeded when evaluating " +
- result.getCellName() + ". It evaluated to " +
- result.getReturnValue() + " when the value of " +
- eval.getExpectedValue() + " with precision of " +
- eval.getPrecision(), Project.MSG_INFO );
+ log("Succeeded when evaluating " + details.get(), Project.MSG_INFO );
}
} else {
if(showFailureDetail) {
- failureMessages.add( "\tFailed to evaluate cell " +
- result.getCellName() + ". It evaluated to " +
- result.getReturnValue() + " when the value of " +
- eval.getExpectedValue() + " with precision of " +
- eval.getPrecision() + " was expected." );
-
+ failureMessages.add( "\tFailed to evaluate cell " + details.get() + " was expected." );
}
passed = false;
failureCount++;
-
+
if(eval.requiredToPass()) {
- throw new BuildException( "\tFailed to evaluate cell " +
- result.getCellName() + ". It evaluated to " +
- result.getReturnValue() + " when the value of " +
- eval.getExpectedValue() + " with precision of " +
- eval.getPrecision() + " was expected." );
+ throw new BuildException( "\tFailed to evaluate cell " + details.get() + " was expected." );
}
}
} catch( NullPointerException npe ) {
// this means the cell reference in the test is bad.
- log( "Cell assignment " + eval.getCell() + " in test " + getName() +
+ log( "Cell assignment " + eval.getCell() + " in test " + getName() +
" appears to point to an empy cell. Please check the " +
" reference in the ant script.", Project.MSG_ERR );
}
}
}
-
+
if(!passed) {
- log( "Test named " + name + " failed because " + failureCount +
- " of " + testCount + " evaluations failed to " +
- "evaluate correctly.",
+ log( "Test named " + name + " failed because " + failureCount +
+ " of " + testCount + " evaluations failed to " +
+ "evaluate correctly.",
Project.MSG_ERR );
if(showFailureDetail && failureMessages.size() > 0 ) {
for (String failureMessage : failureMessages) {
@@ -215,7 +204,7 @@ public class ExcelAntTest extends Task{
}
public boolean didTestPass() {
-
+
return passed;
}
}
diff --git a/src/java/org/apache/poi/POIReadOnlyDocument.java b/src/java/org/apache/poi/POIReadOnlyDocument.java
index dfe44d42f7..5f250704db 100644
--- a/src/java/org/apache/poi/POIReadOnlyDocument.java
+++ b/src/java/org/apache/poi/POIReadOnlyDocument.java
@@ -26,7 +26,7 @@ import org.apache.poi.poifs.filesystem.POIFSFileSystem;
/**
* This holds the common functionality for all read-only
* POI Document classes, i.e. ones which don't support writing.
- *
+ *
* @since POI 3.15 beta 3
*/
public abstract class POIReadOnlyDocument extends POIDocument {
@@ -39,29 +39,33 @@ public abstract class POIReadOnlyDocument extends POIDocument {
/**
* Note - writing is not yet supported for this file format, sorry.
- *
+ *
* @throws IllegalStateException If you call the method, as writing is not supported
*/
@Override
public void write() {
- throw new IllegalStateException("Writing is not yet implemented for this Document Format");
+ notImplemented();
}
/**
* Note - writing is not yet supported for this file format, sorry.
- *
+ *
* @throws IllegalStateException If you call the method, as writing is not supported
*/
@Override
public void write(File file) {
- throw new IllegalStateException("Writing is not yet implemented for this Document Format");
+ notImplemented();
}
/**
* Note - writing is not yet supported for this file format, sorry.
- *
+ *
* @throws IllegalStateException If you call the method, as writing is not supported
*/
@Override
public void write(OutputStream out) {
+ notImplemented();
+ }
+
+ private static void notImplemented() {
throw new IllegalStateException("Writing is not yet implemented for this Document Format");
}
}
diff --git a/src/java/org/apache/poi/common/usermodel/fonts/FontCharset.java b/src/java/org/apache/poi/common/usermodel/fonts/FontCharset.java
index 32915149f2..28df6cad4e 100644
--- a/src/java/org/apache/poi/common/usermodel/fonts/FontCharset.java
+++ b/src/java/org/apache/poi/common/usermodel/fonts/FontCharset.java
@@ -24,11 +24,12 @@ import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
/**
- * Charset represents the basic set of characters associated with a font (that it can display), and
+ * Charset represents the basic set of characters associated with a font (that it can display), and
* corresponds to the ANSI codepage (8-bit or DBCS) of that character set used by a given language.
- *
+ *
* @since POI 3.17-beta2
*/
+@SuppressWarnings("java:S1192")
public enum FontCharset {
/** Specifies the English character set. */
ANSI(0x00000000, "Cp1252"),
@@ -80,17 +81,17 @@ public enum FontCharset {
OEM(0x000000FF, "Cp1252");
private static FontCharset[] _table = new FontCharset[256];
-
+
private int nativeId;
private Charset charset;
-
+
static {
for (FontCharset c : values()) {
_table[c.getNativeId()] = c;
}
}
-
+
FontCharset(int flag, String javaCharsetName) {
this.nativeId = flag;
if (javaCharsetName.length() > 0) {
@@ -113,12 +114,12 @@ public enum FontCharset {
public Charset getCharset() {
return charset;
}
-
+
public int getNativeId() {
return nativeId;
}
public static FontCharset valueOf(int value){
- return (value < 0 || value >= _table.length) ? null :_table[value];
+ return (value < 0 || value >= _table.length) ? null : _table[value];
}
}
\ No newline at end of file
diff --git a/src/java/org/apache/poi/hpsf/ClassIDPredefined.java b/src/java/org/apache/poi/hpsf/ClassIDPredefined.java
index 61454769ec..61d6566c66 100644
--- a/src/java/org/apache/poi/hpsf/ClassIDPredefined.java
+++ b/src/java/org/apache/poi/hpsf/ClassIDPredefined.java
@@ -20,6 +20,7 @@ package org.apache.poi.hpsf;
import java.util.HashMap;
import java.util.Map;
+@SuppressWarnings("java:S1192")
public enum ClassIDPredefined {
/** OLE 1.0 package manager */
OLE_V1_PACKAGE ("{0003000C-0000-0000-C000-000000000046}", ".bin", null),
diff --git a/src/java/org/apache/poi/hssf/dev/FormulaViewer.java b/src/java/org/apache/poi/hssf/dev/FormulaViewer.java
index d53d37aa67..bc7cc9cc84 100644
--- a/src/java/org/apache/poi/hssf/dev/FormulaViewer.java
+++ b/src/java/org/apache/poi/hssf/dev/FormulaViewer.java
@@ -24,7 +24,6 @@ import java.util.List;
import org.apache.poi.hssf.model.HSSFFormulaParser;
import org.apache.poi.hssf.record.FormulaRecord;
-import org.apache.poi.hssf.record.Record;
import org.apache.poi.hssf.record.RecordFactory;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.formula.ptg.ExpPtg;
@@ -51,8 +50,8 @@ public class FormulaViewer
/**
* Method run
- *
- * @throws IOException if the file contained errors
+ *
+ * @throws IOException if the file contained errors
*/
public void run() throws IOException {
try (POIFSFileSystem fs = new POIFSFileSystem(new File(file), true)) {
@@ -71,61 +70,36 @@ public class FormulaViewer
}
}
}
-
+
private void listFormula(FormulaRecord record) {
- String sep="~";
Ptg[] tokens= record.getParsedExpression();
- Ptg token;
int numptgs = tokens.length;
- String numArg;
- token = tokens[numptgs-1];
- if (token instanceof FuncPtg) {
- numArg = String.valueOf(numptgs-1);
- } else {
- numArg = String.valueOf(-1);
- }
-
- StringBuilder buf = new StringBuilder();
-
- if (token instanceof ExpPtg) return;
- buf.append(token.toFormulaString());
- buf.append(sep);
- switch (token.getPtgClass()) {
- case Ptg.CLASS_REF :
- buf.append("REF");
- break;
- case Ptg.CLASS_VALUE :
- buf.append("VALUE");
- break;
- case Ptg.CLASS_ARRAY :
- buf.append("ARRAY");
- break;
- default:
- throwInvalidRVAToken(token);
- }
-
- buf.append(sep);
- if (numptgs>1) {
- token = tokens[numptgs-2];
- switch (token.getPtgClass()) {
- case Ptg.CLASS_REF :
- buf.append("REF");
- break;
- case Ptg.CLASS_VALUE :
- buf.append("VALUE");
- break;
- case Ptg.CLASS_ARRAY :
- buf.append("ARRAY");
- break;
- default:
- throwInvalidRVAToken(token);
- }
- }else {
- buf.append("VALUE");
- }
- buf.append(sep);
- buf.append(numArg);
- System.out.println(buf);
+ final Ptg lastToken = tokens[numptgs-1];
+
+ if (lastToken instanceof ExpPtg) return;
+
+ String buf = String.join("~",
+ lastToken.toFormulaString(),
+ mapToken(lastToken),
+ (numptgs > 1 ? mapToken(tokens[numptgs - 2]) : "VALUE"),
+ String.valueOf(lastToken instanceof FuncPtg ? numptgs-1 : -1)
+ );
+
+ System.out.println(buf);
+ }
+
+ private static String mapToken(Ptg token) {
+ switch (token.getPtgClass()) {
+ case Ptg.CLASS_REF :
+ return "REF";
+ case Ptg.CLASS_VALUE :
+ return "VALUE";
+ case Ptg.CLASS_ARRAY :
+ return "ARRAY";
+ default:
+ throwInvalidRVAToken(token);
+ return "";
+ }
}
/**
@@ -167,15 +141,15 @@ public class FormulaViewer
throwInvalidRVAToken(token);
}
buf.append(' ');
- }
+ }
return buf.toString();
}
-
+
private static void throwInvalidRVAToken(Ptg token) {
throw new IllegalStateException("Invalid RVA type (" + token.getPtgClass() + "). This should never happen.");
}
-
-
+
+
private static String composeFormula(FormulaRecord record)
{
return HSSFFormulaParser.toFormulaString(null, record.getParsedExpression());
@@ -191,7 +165,7 @@ public class FormulaViewer
{
this.file = file;
}
-
+
public void setList(boolean list) {
this.list=list;
}
--
2.39.5