From 9b9a3dbf45f004b63de48735b708693c8e9cf6cb Mon Sep 17 00:00:00 2001 From: Andreas Beeker Date: Thu, 19 Nov 2015 01:44:38 +0000 Subject: [PATCH] findbugs fixes git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1715087 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/ss/formula/FormulaParser.java | 7 +- .../org/apache/poi/hdgf/dev/VSDDumper.java | 142 ++++--- .../poi/hslf/dev/SlideShowRecordDumper.java | 389 +++++++++--------- .../apache/poi/hssf/usermodel/HSSFChart.java | 65 ++- 4 files changed, 320 insertions(+), 283 deletions(-) diff --git a/src/java/org/apache/poi/ss/formula/FormulaParser.java b/src/java/org/apache/poi/ss/formula/FormulaParser.java index dddf2bbbbf..f2430a90be 100644 --- a/src/java/org/apache/poi/ss/formula/FormulaParser.java +++ b/src/java/org/apache/poi/ss/formula/FormulaParser.java @@ -462,15 +462,12 @@ public final class FormulaParser { // reset and let caller use explicit range operator resetPointer(colonPos); if (!part1.isCell()) { - String prefix; - if (sheetIden == null) { - prefix = ""; - } else { + String prefix = ""; + if (sheetIden != null) { prefix = "'" + sheetIden.getSheetIdentifier().getName() + '!'; } throw new FormulaParseException(prefix + part1.getRep() + "' is not a proper reference."); } - return createAreaRefParseNode(sheetIden, part1, part2); } return createAreaRefParseNode(sheetIden, part1, part2); } diff --git a/src/scratchpad/src/org/apache/poi/hdgf/dev/VSDDumper.java b/src/scratchpad/src/org/apache/poi/hdgf/dev/VSDDumper.java index 40a8a7b31e..62f2b36e59 100644 --- a/src/scratchpad/src/org/apache/poi/hdgf/dev/VSDDumper.java +++ b/src/scratchpad/src/org/apache/poi/hdgf/dev/VSDDumper.java @@ -18,6 +18,8 @@ package org.apache.poi.hdgf.dev; import java.io.File; +import java.io.PrintStream; +import java.util.Arrays; import org.apache.poi.hdgf.HDGFDiagram; import org.apache.poi.hdgf.chunks.Chunk; @@ -33,6 +35,15 @@ import org.apache.poi.poifs.filesystem.NPOIFSFileSystem; * of a Visio file */ public final class VSDDumper { + final static String tabs = "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"; + + final PrintStream ps; + final HDGFDiagram hdgf; + VSDDumper(PrintStream ps, HDGFDiagram hdgf) { + this.ps = ps; + this.hdgf = hdgf; + } + public static void main(String[] args) throws Exception { if(args.length == 0) { System.err.println("Use:"); @@ -40,84 +51,87 @@ public final class VSDDumper { System.exit(1); } - HDGFDiagram hdgf = new HDGFDiagram( - new NPOIFSFileSystem(new File(args[0])) - ); + NPOIFSFileSystem poifs = new NPOIFSFileSystem(new File(args[0])); + HDGFDiagram hdgf = new HDGFDiagram(poifs); - System.out.println("Opened " + args[0]); - System.out.println("The document claims a size of " + - hdgf.getDocumentSize() + " (" + - Long.toHexString(hdgf.getDocumentSize()) + ")"); - System.out.println(); - - dumpStream(hdgf.getTrailerStream(), 0); + PrintStream ps = System.out; + ps.println("Opened " + args[0]); + VSDDumper vd = new VSDDumper(ps, hdgf); + vd.dumpFile(); + + poifs.close(); } - public static void dumpStream(Stream stream, int indent) { - String ind = ""; - for(int i=0; i= 8) { - for(int i=0; i<8; i++) { - if(i>0) ds += ", "; - ds += db[i]; - } - } - System.out.println(ind + " First few bytes are\t" + ds); + String ds = (db.length >= 8) ? Arrays.toString(db) : "[]"; + dumpVal("First few bytes are", ds, indent+1); - if(stream instanceof PointerContainingStream) { - PointerContainingStream pcs = (PointerContainingStream)stream; - System.out.println(ind + " Has " + - pcs.getPointedToStreams().length + " children:"); + if (stream instanceof PointerContainingStream) { + Stream streams[] = ((PointerContainingStream)stream).getPointedToStreams(); + dumpVal("Nbr of children", streams.length, indent+1); - for(int i=0; i"); - System.err.println("Valid Options:"); - System.err.println("-escher\t\t: dump contents of escher records"); - System.err.println("-verbose\t: dump binary contents of each record"); - } - - - /** - * Constructs a Powerpoint dump from fileName. Parses the document - * and dumps out the contents - * - * @param fileName The name of the file to read. - * @throws IOException if there is a problem while parsing the document. - */ - public SlideShowRecordDumper(String fileName, boolean verbose, boolean escher) throws IOException - { - optVerbose = verbose; - optEscher = escher; - doc = new HSLFSlideShowImpl(fileName); - } - - - public void printDump() throws IOException { - // Prints out the records in the tree - walkTree(0,0,doc.getRecords()); - } + final static String tabs = "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"; + + private boolean optVerbose; + private boolean optEscher; + private HSLFSlideShowImpl doc; + private final PrintStream ps; + + /** + * right now this function takes one parameter: a ppt file, and outputs a + * dump of what it contains + */ + public static void main(String args[]) throws IOException { + String filename = ""; + boolean verbose = false; + boolean escher = false; + + int ndx = 0; + for (; ndx < args.length; ndx++) { + if (!args[ndx].substring(0, 1).equals("-")) + break; + + if (args[ndx].equals("-escher")) { + escher = true; + } else if (args[ndx].equals("-verbose")) { + verbose = true; + } else { + printUsage(); + return; + } + } - public String makeHex(int number, int padding) { - String hex = Integer.toHexString(number).toUpperCase(Locale.ROOT); - while(hex.length() < padding) { - hex = "0" + hex; - } - return hex; - } + // parsed any options, expect exactly one remaining arg (filename) + if (ndx != args.length - 1) { + printUsage(); + return; + } - public String reverseHex(String s) { - StringBuffer ret = new StringBuffer(); + filename = args[ndx]; + + SlideShowRecordDumper foo = new SlideShowRecordDumper(System.out, + filename, verbose, escher); + + foo.printDump(); + } + + public static void printUsage() { + System.err.println("Usage: SlideShowRecordDumper [-escher] [-verbose] "); + System.err.println("Valid Options:"); + System.err.println("-escher\t\t: dump contents of escher records"); + System.err.println("-verbose\t: dump binary contents of each record"); + } + + /** + * Constructs a Powerpoint dump from fileName. Parses the document + * and dumps out the contents + * + * @param fileName The name of the file to read. + * @throws IOException if there is a problem while parsing the document. + */ + public SlideShowRecordDumper(PrintStream ps, String fileName, boolean verbose, boolean escher) + throws IOException { + this.ps = ps; + optVerbose = verbose; + optEscher = escher; + doc = new HSLFSlideShowImpl(fileName); + } + + + public void printDump() throws IOException { + // Prints out the records in the tree + walkTree(0, 0, doc.getRecords(), 0); + } + + public String makeHex(int number, int padding) { + String hex = Integer.toHexString(number).toUpperCase(Locale.ROOT); + while (hex.length() < padding) { + hex = "0" + hex; + } + return hex; + } - // Get to a multiple of two - if((s.length() / 2) * 2 != s.length()) { s = "0" + s; } + public String reverseHex(String s) { + StringBuilder ret = new StringBuilder(); - // Break up into blocks - char[] c = s.toCharArray(); - for(int i=c.length; i>0; i-=2) { - ret.append(c[i-2]); - ret.append(c[i-1]); - if(i != 2) { ret.append(' '); } - } - return ret.toString(); - } - - public int getDiskLen(Record r) throws IOException { - if (r == null) return 0; + // Get to a multiple of two + int pos = 0; + if ((s.length() & 1) == 1) { + ret.append(0); + pos++; + } + for (char c : s.toCharArray()) { + if (pos > 0 && (pos & 1) == 0) { + ret.append(' '); + } + ret.append(c); + pos++; + } - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - r.writeOut(baos); - byte[] b = baos.toByteArray(); - return b.length; - } + return ret.toString(); + } - public String getPrintableRecordContents(Record r) throws IOException { - if (r==null) return "<>"; + public int getDiskLen(Record r) throws IOException { + int diskLen = 0; + if (r != null) { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + r.writeOut(baos); + diskLen = baos.size(); + } + return diskLen; + } - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - r.writeOut(baos); - byte[] b = baos.toByteArray(); - return HexDump.dump(b, 0, 0); - } + public String getPrintableRecordContents(Record r) throws IOException { + if (r == null) { + return "<>"; + } - public String printEscherRecord( EscherRecord er ) { - String nl = System.getProperty( "line.separator" ); - StringBuffer buf = new StringBuffer(); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + r.writeOut(baos); + byte[] b = baos.toByteArray(); + return HexDump.dump(b, 0, 0); + } + public void printEscherRecord(EscherRecord er, int indent) { if (er instanceof EscherContainerRecord) { - buf.append(printEscherContainerRecord( (EscherContainerRecord)er )); + printEscherContainerRecord( (EscherContainerRecord)er, indent ); } else if (er instanceof EscherTextboxRecord) { - buf.append("EscherTextboxRecord:" + nl); - - EscherTextboxWrapper etw = new EscherTextboxWrapper((EscherTextboxRecord)er); - Record children[] = etw.getChildRecords(); - for (int j=0; j 0 && (children[j-1] instanceof TextCharsAtom || - children[j-1] instanceof TextBytesAtom)) { - - int size = (children[j-1] instanceof TextCharsAtom) ? - ((TextCharsAtom)children[j-1]).getText().length() : - ((TextBytesAtom)children[j-1]).getText().length(); - - StyleTextPropAtom tsp = (StyleTextPropAtom)children[j]; - tsp.setParentTextSize(size); - - } else { - buf.append("Error! Couldn't find preceding TextAtom for style\n"); - } - - buf.append(children[j].toString() + nl ); - } else { - buf.append(children[j].toString() + nl ); - } - } + printEscherTextBox( (EscherTextboxRecord)er, indent ); } else { - buf.append( er.toString() ); + ps.print( tabs.substring(0, indent) ); + ps.println( er.toString() ); } - return buf.toString(); - } - - public String printEscherContainerRecord( EscherContainerRecord ecr ) { - String indent = ""; - - String nl = System.getProperty( "line.separator" ); - - StringBuffer children = new StringBuffer(); - int count = 0; - for ( Iterator iterator = ecr.getChildIterator(); iterator.hasNext(); ) - { - if (count < 1) { - children.append( " children: " + nl ); + } + + private void printEscherTextBox( EscherTextboxRecord tbRecord, int indent ) { + String ind = tabs.substring(0, indent); + ps.println(ind+"EscherTextboxRecord:"); + + EscherTextboxWrapper etw = new EscherTextboxWrapper(tbRecord); + Record prevChild = null; + for (Record child : etw.getChildRecords()) { + if (child instanceof StyleTextPropAtom) { + // need preceding Text[Chars|Bytes]Atom to initialize the data structure + String text = null; + if (prevChild instanceof TextCharsAtom) { + text = ((TextCharsAtom)prevChild).getText(); + } else if (prevChild instanceof TextBytesAtom) { + text = ((TextBytesAtom)prevChild).getText(); + } else { + ps.println(ind+"Error! Couldn't find preceding TextAtom for style"); + continue; + } + + StyleTextPropAtom tsp = (StyleTextPropAtom)child; + tsp.setParentTextSize(text.length()); } - String newIndent = " "; - - EscherRecord record = iterator.next(); - children.append(newIndent + "Child " + count + ":" + nl); - - children.append( printEscherRecord(record) ); - + ps.println(ind+child.toString()); + prevChild = child; + } + + } + + private void printEscherContainerRecord( EscherContainerRecord ecr, int indent ) { + String ind = tabs.substring(0, indent); + ps.println(ind + ecr.getClass().getName() + " (" + ecr.getRecordName() + "):"); + ps.println(ind + " isContainer: " + ecr.isContainerRecord()); + ps.println(ind + " options: 0x" + HexDump.toHex( ecr.getOptions() )); + ps.println(ind + " recordId: 0x" + HexDump.toHex( ecr.getRecordId() )); + + List childRecords = ecr.getChildRecords(); + ps.println(ind + " numchildren: " + childRecords.size()); + ps.println(ind + " children: "); + int count = 0; + for ( EscherRecord record : childRecords ) { + ps.println(ind + " Child " + count + ":"); + printEscherRecord(record, indent+1); count++; } + } - return - indent + ecr.getClass().getName() + " (" + ecr.getRecordName() + "):" + nl + - indent + " isContainer: " + ecr.isContainerRecord() + nl + - indent + " options: 0x" + HexDump.toHex( ecr.getOptions() ) + nl + - indent + " recordId: 0x" + HexDump.toHex( ecr.getRecordId() ) + nl + - indent + " numchildren: " + ecr.getChildRecords().size() + nl + - indent + children.toString(); - } + public void walkTree(int depth, int pos, Record[] records, int indent) throws IOException { + String ind = tabs.substring(0, indent); - public void walkTree(int depth, int pos, Record[] records) throws IOException { - int indent = depth; - String ind = ""; - for(int i=0; i c = r.getClass(); @@ -254,10 +253,10 @@ public final class SlideShowRecordDumper { } // Display the record - System.out.println(ind + "At position " + pos + " (" + makeHex(pos,6) + "):"); - System.out.println(ind + " Record is of type " + cname); - System.out.println(ind + " Type is " + r.getRecordType() + " (" + hexType + " -> " + rHexType + " )"); - System.out.println(ind + " Len is " + (len-8) + " (" + makeHex((len-8),8) + "), on disk len is " + len ); + ps.println(ind + "At position " + pos + " (" + makeHex(pos,6) + "):"); + ps.println(ind + " Record is of type " + cname); + ps.println(ind + " Type is " + r.getRecordType() + " (" + hexType + " -> " + rHexType + " )"); + ps.println(ind + " Len is " + (len-8) + " (" + makeHex((len-8),8) + "), on disk len is " + len ); // print additional information for drawings and atoms if (optEscher && cname.equals("PPDrawing")) { @@ -270,18 +269,18 @@ public final class SlideShowRecordDumper { EscherRecord er = factory.createRecord(b, 0); er.fillFields(b, 0, factory); - System.out.println( printEscherRecord( er ) ); + printEscherRecord( er, indent+1 ); } else if(optVerbose && r.getChildRecords() == null) { String recData = getPrintableRecordContents(r); - System.out.println(ind + recData ); + ps.println(ind + recData ); } - System.out.println(); + ps.println(); // If it has children, show them if(r.getChildRecords() != null) { - walkTree((depth+3),pos+8,r.getChildRecords()); + walkTree((depth+3),pos+8,r.getChildRecords(), indent+1); } // Wind on the position marker diff --git a/src/scratchpad/src/org/apache/poi/hssf/usermodel/HSSFChart.java b/src/scratchpad/src/org/apache/poi/hssf/usermodel/HSSFChart.java index c5b51aff54..e08fb2d74b 100644 --- a/src/scratchpad/src/org/apache/poi/hssf/usermodel/HSSFChart.java +++ b/src/scratchpad/src/org/apache/poi/hssf/usermodel/HSSFChart.java @@ -21,7 +21,6 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.List; -import org.apache.poi.hssf.record.chart.*; import org.apache.poi.hssf.record.BOFRecord; import org.apache.poi.hssf.record.DimensionsRecord; import org.apache.poi.hssf.record.EOFRecord; @@ -35,6 +34,38 @@ import org.apache.poi.hssf.record.RecordBase; import org.apache.poi.hssf.record.SCLRecord; import org.apache.poi.hssf.record.UnknownRecord; import org.apache.poi.hssf.record.VCenterRecord; +import org.apache.poi.hssf.record.chart.AreaFormatRecord; +import org.apache.poi.hssf.record.chart.AxisLineFormatRecord; +import org.apache.poi.hssf.record.chart.AxisOptionsRecord; +import org.apache.poi.hssf.record.chart.AxisParentRecord; +import org.apache.poi.hssf.record.chart.AxisRecord; +import org.apache.poi.hssf.record.chart.AxisUsedRecord; +import org.apache.poi.hssf.record.chart.BarRecord; +import org.apache.poi.hssf.record.chart.BeginRecord; +import org.apache.poi.hssf.record.chart.CategorySeriesAxisRecord; +import org.apache.poi.hssf.record.chart.ChartFormatRecord; +import org.apache.poi.hssf.record.chart.ChartRecord; +import org.apache.poi.hssf.record.chart.ChartTitleFormatRecord; +import org.apache.poi.hssf.record.chart.DataFormatRecord; +import org.apache.poi.hssf.record.chart.DefaultDataLabelTextPropertiesRecord; +import org.apache.poi.hssf.record.chart.EndRecord; +import org.apache.poi.hssf.record.chart.FontBasisRecord; +import org.apache.poi.hssf.record.chart.FontIndexRecord; +import org.apache.poi.hssf.record.chart.FrameRecord; +import org.apache.poi.hssf.record.chart.LegendRecord; +import org.apache.poi.hssf.record.chart.LineFormatRecord; +import org.apache.poi.hssf.record.chart.LinkedDataRecord; +import org.apache.poi.hssf.record.chart.PlotAreaRecord; +import org.apache.poi.hssf.record.chart.PlotGrowthRecord; +import org.apache.poi.hssf.record.chart.SeriesIndexRecord; +import org.apache.poi.hssf.record.chart.SeriesRecord; +import org.apache.poi.hssf.record.chart.SeriesTextRecord; +import org.apache.poi.hssf.record.chart.SeriesToChartGroupRecord; +import org.apache.poi.hssf.record.chart.SheetPropertiesRecord; +import org.apache.poi.hssf.record.chart.TextRecord; +import org.apache.poi.hssf.record.chart.TickRecord; +import org.apache.poi.hssf.record.chart.UnitsRecord; +import org.apache.poi.hssf.record.chart.ValueRangeRecord; import org.apache.poi.ss.formula.ptg.Area3DPtg; import org.apache.poi.ss.formula.ptg.AreaPtgBase; import org.apache.poi.ss.formula.ptg.Ptg; @@ -51,7 +82,8 @@ public final class HSSFChart { private ChartRecord chartRecord; private LegendRecord legendRecord; - private ChartTitleFormatRecord chartTitleFormat; + @SuppressWarnings("unused") + private ChartTitleFormatRecord chartTitleFormat; private SeriesTextRecord chartTitleText; private List valueRanges = new ArrayList(); @@ -111,7 +143,7 @@ public final class HSSFChart { * NOTE: Does not yet work... checking it in just so others * can take a look. */ - public void createBarChart( HSSFWorkbook workbook, HSSFSheet sheet ) + public void createBarChart( HSSFWorkbook workbook, HSSFSheet parentSheet ) { List records = new ArrayList(); @@ -175,7 +207,7 @@ public final class HSSFChart { - sheet.insertChartRecords( records ); + parentSheet.insertChartRecords( records ); workbook.insertChartRecord(); } @@ -201,7 +233,7 @@ public final class HSSFChart { } else if(r instanceof LegendRecord) { lastChart.legendRecord = (LegendRecord)r; } else if(r instanceof SeriesRecord) { - HSSFSeries series = lastChart.new HSSFSeries( (SeriesRecord)r ); + HSSFSeries series = new HSSFSeries( (SeriesRecord)r ); lastChart.series.add(series); lastSeries = series; } else if(r instanceof ChartTitleFormatRecord) { @@ -213,8 +245,7 @@ public final class HSSFChart { SeriesTextRecord str = (SeriesTextRecord)r; if(lastChart.legendRecord == null && lastChart.series.size() > 0) { - HSSFSeries series = (HSSFSeries) - lastChart.series.get(lastChart.series.size()-1); + HSSFSeries series = lastChart.series.get(lastChart.series.size()-1); series.seriesTitleText = str; } else { lastChart.chartTitleText = str; @@ -244,8 +275,7 @@ public final class HSSFChart { } } - return (HSSFChart[]) - charts.toArray( new HSSFChart[charts.size()] ); + return charts.toArray( new HSSFChart[charts.size()] ); } /** Get the X offset of the chart */ @@ -270,8 +300,7 @@ public final class HSSFChart { * Returns the series of the chart */ public HSSFSeries[] getSeries() { - return (HSSFSeries[]) - series.toArray(new HSSFSeries[series.size()]); + return series.toArray(new HSSFSeries[series.size()]); } /** @@ -307,7 +336,7 @@ public final class HSSFChart { * @param minorUnit minor unit value; Double.NaN - automatic; null - no change */ public void setValueRange( int axisIndex, Double minimum, Double maximum, Double majorUnit, Double minorUnit){ - ValueRangeRecord valueRange = (ValueRangeRecord)valueRanges.get( axisIndex ); + ValueRangeRecord valueRange = valueRanges.get( axisIndex ); if( valueRange == null ) return; if( minimum != null ){ valueRange.setAutomaticMinimum(minimum.isNaN()); @@ -962,7 +991,7 @@ public final class HSSFChart { /** * A series in a chart */ - public class HSSFSeries { + public static class HSSFSeries { private SeriesRecord series; private SeriesTextRecord seriesTitleText; private LinkedDataRecord dataName; @@ -1225,13 +1254,13 @@ public final class HSSFChart { newSeries = new HSSFSeries(seriesRecord); newRecord = seriesRecord; } else if (record instanceof LinkedDataRecord) { - LinkedDataRecord linkedDataRecord = (LinkedDataRecord) ((LinkedDataRecord)record).clone(); + LinkedDataRecord linkedDataRecord = ((LinkedDataRecord)record).clone(); if (newSeries != null) { newSeries.insertData(linkedDataRecord); } newRecord = linkedDataRecord; } else if (record instanceof DataFormatRecord) { - DataFormatRecord dataFormatRecord = (DataFormatRecord) ((DataFormatRecord)record).clone(); + DataFormatRecord dataFormatRecord = ((DataFormatRecord)record).clone(); dataFormatRecord.setSeriesIndex((short)seriesIdx) ; dataFormatRecord.setSeriesNumber((short)seriesIdx) ; @@ -1267,8 +1296,7 @@ public final class HSSFChart { return newSeries; } - public boolean removeSeries(HSSFSeries series) { - int idx = 0; + public boolean removeSeries(HSSFSeries remSeries) { int deep = 0; int chartDeep = -1; int lastSeriesDeep = -1; @@ -1282,7 +1310,6 @@ public final class HSSFChart { Iterator iter = records.iterator(); while (iter.hasNext()) { RecordBase record = iter.next(); - idx++; if (record instanceof BeginRecord) { deep++; @@ -1311,7 +1338,7 @@ public final class HSSFChart { } } else if (record instanceof SeriesRecord) { if (chartEntered) { - if (series.series == record) { + if (remSeries.series == record) { lastSeriesDeep = deep; removeSeries = true; } else { -- 2.39.5