diff --git a/src/java/org/apache/poi/common/usermodel/HyperlinkType.java b/src/java/org/apache/poi/common/usermodel/HyperlinkType.java index dc88dc560..41de80062 100644 --- a/src/java/org/apache/poi/common/usermodel/HyperlinkType.java +++ b/src/java/org/apache/poi/common/usermodel/HyperlinkType.java @@ -57,16 +57,16 @@ public enum HyperlinkType { /** * The codes don't have any real meaning. - * There bytes that are read in and written out from HSSF, HSLF, XSSF, and XSLF are different + * They are bytes that are read in and written out from HSSF, HSLF, XSSF, and XSLF are different * that the codes here. * These codes only exist to assist in transitioning from using ints to enums. * - * @param code + * @param code The unique number for this type. * @deprecated POI 3.15 beta 3 */ @Internal(since="3.15 beta 3") @Deprecated - private HyperlinkType(int code) { + HyperlinkType(int code) { this.code = code; } diff --git a/src/java/org/apache/poi/hssf/record/WindowOneRecord.java b/src/java/org/apache/poi/hssf/record/WindowOneRecord.java index e2b9e2c4d..440348e37 100644 --- a/src/java/org/apache/poi/hssf/record/WindowOneRecord.java +++ b/src/java/org/apache/poi/hssf/record/WindowOneRecord.java @@ -43,6 +43,7 @@ public final class WindowOneRecord extends StandardRecord { BitFieldFactory.getInstance(0x01); // is this window is hidden static final private BitField iconic = BitFieldFactory.getInstance(0x02); // is this window is an icon + @SuppressWarnings("unused") static final private BitField reserved = BitFieldFactory.getInstance(0x04); // reserved static final private BitField hscroll = BitFieldFactory.getInstance(0x08); // display horizontal scrollbar @@ -353,41 +354,37 @@ public final class WindowOneRecord extends StandardRecord { return field_9_tab_width_ratio; } - public String toString() - { - StringBuffer buffer = new StringBuffer(); - - buffer.append("[WINDOW1]\n"); - buffer.append(" .h_hold = ") - .append(Integer.toHexString(getHorizontalHold())).append("\n"); - buffer.append(" .v_hold = ") - .append(Integer.toHexString(getVerticalHold())).append("\n"); - buffer.append(" .width = ") - .append(Integer.toHexString(getWidth())).append("\n"); - buffer.append(" .height = ") - .append(Integer.toHexString(getHeight())).append("\n"); - buffer.append(" .options = ") - .append(Integer.toHexString(getOptions())).append("\n"); - buffer.append(" .hidden = ").append(getHidden()) - .append("\n"); - buffer.append(" .iconic = ").append(getIconic()) - .append("\n"); - buffer.append(" .hscroll = ") - .append(getDisplayHorizontalScrollbar()).append("\n"); - buffer.append(" .vscroll = ") - .append(getDisplayVerticalScrollbar()).append("\n"); - buffer.append(" .tabs = ").append(getDisplayTabs()) - .append("\n"); - buffer.append(" .activeSheet = ") - .append(Integer.toHexString(getActiveSheetIndex())).append("\n"); - buffer.append(" .firstVisibleTab = ") - .append(Integer.toHexString(getFirstVisibleTab())).append("\n"); - buffer.append(" .numselectedtabs = ") - .append(Integer.toHexString(getNumSelectedTabs())).append("\n"); - buffer.append(" .tabwidthratio = ") - .append(Integer.toHexString(getTabWidthRatio())).append("\n"); - buffer.append("[/WINDOW1]\n"); - return buffer.toString(); + public String toString() { + return "[WINDOW1]\n" + + " .h_hold = " + + Integer.toHexString(getHorizontalHold()) + "\n" + + " .v_hold = " + + Integer.toHexString(getVerticalHold()) + "\n" + + " .width = " + + Integer.toHexString(getWidth()) + "\n" + + " .height = " + + Integer.toHexString(getHeight()) + "\n" + + " .options = " + + Integer.toHexString(getOptions()) + "\n" + + " .hidden = " + getHidden() + + "\n" + + " .iconic = " + getIconic() + + "\n" + + " .hscroll = " + + getDisplayHorizontalScrollbar() + "\n" + + " .vscroll = " + + getDisplayVerticalScrollbar() + "\n" + + " .tabs = " + getDisplayTabs() + + "\n" + + " .activeSheet = " + + Integer.toHexString(getActiveSheetIndex()) + "\n" + + " .firstVisibleTab = " + + Integer.toHexString(getFirstVisibleTab()) + "\n" + + " .numselectedtabs = " + + Integer.toHexString(getNumSelectedTabs()) + "\n" + + " .tabwidthratio = " + + Integer.toHexString(getTabWidthRatio()) + "\n" + + "[/WINDOW1]\n"; } public void serialize(LittleEndianOutput out) { diff --git a/src/java/org/apache/poi/util/BlockingInputStream.java b/src/java/org/apache/poi/util/BlockingInputStream.java index 62d2c7e6a..298c6aa7f 100644 --- a/src/java/org/apache/poi/util/BlockingInputStream.java +++ b/src/java/org/apache/poi/util/BlockingInputStream.java @@ -28,7 +28,7 @@ import java.io.IOException; * data from slow (ie, non FileInputStream) sources, for example when * reading an OLE2 Document over a network. * - * Possible extentions: add a timeout. Curently a call to read(byte[]) on this + * Possible extensions: add a timeout. Currently a call to read(byte[]) on this * class is blocking, so use at your own peril if your underlying stream blocks. * * @author Jens Gerhard diff --git a/src/java/org/apache/poi/util/FontMetricsDumper.java b/src/java/org/apache/poi/util/FontMetricsDumper.java index 10818c1ad..514764657 100644 --- a/src/java/org/apache/poi/util/FontMetricsDumper.java +++ b/src/java/org/apache/poi/util/FontMetricsDumper.java @@ -36,40 +36,33 @@ public class FontMetricsDumper Properties props = new Properties(); Font[] allFonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts(); - for ( int i = 0; i < allFonts.length; i++ ) - { - String fontName = allFonts[i].getFontName(); + for (Font allFont : allFonts) { + String fontName = allFont.getFontName(); Font font = new Font(fontName, Font.BOLD, 10); FontMetrics fontMetrics = Toolkit.getDefaultToolkit().getFontMetrics(font); int fontHeight = fontMetrics.getHeight(); - props.setProperty("font." + fontName + ".height", fontHeight+""); - StringBuffer characters = new StringBuffer(); - for (char c = 'a'; c <= 'z'; c++) - { - characters.append( c + ", " ); + props.setProperty("font." + fontName + ".height", fontHeight + ""); + StringBuilder characters = new StringBuilder(); + for (char c = 'a'; c <= 'z'; c++) { + characters.append(c).append(", "); } - for (char c = 'A'; c <= 'Z'; c++) - { - characters.append( c + ", " ); + for (char c = 'A'; c <= 'Z'; c++) { + characters.append(c).append(", "); } - for (char c = '0'; c <= '9'; c++) - { - characters.append( c + ", " ); + for (char c = '0'; c <= '9'; c++) { + characters.append(c).append(", "); } - StringBuffer widths = new StringBuffer(); - for (char c = 'a'; c <= 'z'; c++) - { - widths.append( fontMetrics.getWidths()[c] + ", " ); + StringBuilder widths = new StringBuilder(); + for (char c = 'a'; c <= 'z'; c++) { + widths.append(fontMetrics.getWidths()[c]).append(", "); } - for (char c = 'A'; c <= 'Z'; c++) - { - widths.append( fontMetrics.getWidths()[c] + ", " ); + for (char c = 'A'; c <= 'Z'; c++) { + widths.append(fontMetrics.getWidths()[c]).append(", "); } - for (char c = '0'; c <= '9'; c++) - { - widths.append( fontMetrics.getWidths()[c] + ", " ); + for (char c = '0'; c <= '9'; c++) { + widths.append(fontMetrics.getWidths()[c]).append(", "); } props.setProperty("font." + fontName + ".characters", characters.toString()); props.setProperty("font." + fontName + ".widths", widths.toString()); diff --git a/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Range.java b/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Range.java index d01373742..10b3f963c 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Range.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Range.java @@ -265,7 +265,6 @@ public class Range { // TODO -instantiable superclass // This means there's nested stuff, so we // can just zap the lot text = text.substring(0, first13) + text.substring(last15 + 1); - continue; } return text; @@ -648,8 +647,8 @@ public class Range { // TODO -instantiable superclass public void replaceText(String pPlaceHolder, String pValue, int pOffset) { int absPlaceHolderIndex = getStartOffset() + pOffset; - Range subRange = new Range(absPlaceHolderIndex, (absPlaceHolderIndex + pPlaceHolder - .length()), this); + Range subRange = new Range(absPlaceHolderIndex, + (absPlaceHolderIndex + pPlaceHolder.length()), this); subRange.insertBefore(pValue); // re-create the sub-range so we can delete it @@ -669,15 +668,14 @@ public class Range { // TODO -instantiable superclass * The replacement text (e.g., "Apache Software Foundation") */ public void replaceText(String pPlaceHolder, String pValue) { - boolean keepLooking = true; - while (keepLooking) { - + while (true) { String text = text(); int offset = text.indexOf(pPlaceHolder); - if (offset >= 0) + if (offset >= 0) { replaceText(pPlaceHolder, pValue, offset); - else - keepLooking = false; + } else { + break; + } } } @@ -726,10 +724,8 @@ public class Range { // TODO -instantiable superclass istd = papx.getIstd(); } - CharacterRun chp = new CharacterRun( chpx, _doc.getStyleSheet(), istd, - this ); - - return chp; + return new CharacterRun( chpx, _doc.getStyleSheet(), istd, + this); } /** @@ -742,8 +738,7 @@ public class Range { // TODO -instantiable superclass public Section getSection(int index) { initSections(); SEPX sepx = _sections.get(index + _sectionStart); - Section sep = new Section(sepx, this); - return sep; + return new Section(sepx, this); } /** @@ -949,8 +944,6 @@ public class Range { // TODO -instantiable superclass * * @param rpl * A list of property nodes. - * @param min - * A hint on where to start looking. * @param start * The starting character offset. * @param end 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 7bd9d55d9..efa219beb 100644 --- a/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestBugs.java +++ b/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestBugs.java @@ -39,7 +39,6 @@ import org.apache.poi.util.POILogger; import java.io.ByteArrayOutputStream; import java.io.File; -import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.Arrays; @@ -130,7 +129,6 @@ public class TestBugs extends TestCase /** * Bug 33519 - HWPF fails to read a file - * @throws IOException */ public void test33519() throws IOException { @@ -139,7 +137,6 @@ public class TestBugs extends TestCase /** * Bug 34898 - WordExtractor doesn't read the whole string from the file - * @throws IOException */ public void test34898() throws IOException { @@ -180,7 +177,6 @@ public class TestBugs extends TestCase /** * Bug 44331 - HWPFDocument.write destroys fields - * @throws IOException */ @SuppressWarnings("deprecation") public void test44431() throws IOException @@ -209,7 +205,6 @@ public class TestBugs extends TestCase /** * Bug 44331 - HWPFDocument.write destroys fields - * @throws IOException */ public void test44431_2() throws IOException { @@ -237,7 +232,6 @@ public class TestBugs extends TestCase /** * Bug 45473 - HWPF cannot read file after save - * @throws IOException */ public void test45473() throws IOException { @@ -292,7 +286,6 @@ public class TestBugs extends TestCase /** * [RESOLVED FIXED] Bug 46817 - Regression: Text from some table cells * missing - * @throws IOException */ public void test46817() throws IOException { @@ -307,7 +300,6 @@ public class TestBugs extends TestCase * [FAILING] Bug 47286 - Word documents saves in wrong format if source * contains form elements * - * @throws IOException */ @SuppressWarnings("deprecation") public void test47286() throws IOException @@ -526,7 +518,6 @@ public class TestBugs extends TestCase /** * [FAILING] Bug 50955 - error while retrieving the text file - * @throws IOException */ public void test50955() throws IOException { @@ -571,9 +562,6 @@ public class TestBugs extends TestCase /** * [RESOLVED FIXED] Bug 51604 - replace text fails for doc (poi 3.8 beta * release from download site ) - * - * @throws IOException - * @throws FileNotFoundException */ public void test51604p2() throws Exception { @@ -581,18 +569,7 @@ public class TestBugs extends TestCase Range range = doc.getRange(); int numParagraph = range.numParagraphs(); - for (int i = 0; i < numParagraph; i++ ) - { - Paragraph paragraph = range.getParagraph(i); - int numCharRuns = paragraph.numCharacterRuns(); - for (int j = 0; j < numCharRuns; j++ ) - { - CharacterRun charRun = paragraph.getCharacterRun(j); - String text = charRun.text(); - if (text.contains("Header" ) ) - charRun.replaceText(text, "added"); - } - } + replaceText(range, numParagraph); doc = HWPFTestDataSamples.writeOutAndReadBack(doc); final FileInformationBlock fileInformationBlock = doc @@ -610,6 +587,21 @@ public class TestBugs extends TestCase assertEquals(doc.getText().length(), totalLength); } + private void replaceText(Range range, int numParagraph) { + for (int i = 0; i < numParagraph; i++ ) + { + Paragraph paragraph = range.getParagraph(i); + int numCharRuns = paragraph.numCharacterRuns(); + for (int j = 0; j < numCharRuns; j++ ) + { + CharacterRun charRun = paragraph.getCharacterRun(j); + String text = charRun.text(); + if (text.contains("Header" ) ) + charRun.replaceText(text, "added"); + } + } + } + /** * [RESOLVED FIXED] Bug 51604 - replace text fails for doc (poi 3.8 beta * release from download site ) @@ -632,18 +624,7 @@ public class TestBugs extends TestCase Range range = doc.getRange(); int numParagraph = range.numParagraphs(); - for (int i = 0; i < numParagraph; i++ ) - { - Paragraph paragraph = range.getParagraph(i); - int numCharRuns = paragraph.numCharacterRuns(); - for (int j = 0; j < numCharRuns; j++ ) - { - CharacterRun charRun = paragraph.getCharacterRun(j); - String text = charRun.text(); - if (text.contains("Header" ) ) - charRun.replaceText(text, "added"); - } - } + replaceText(range, numParagraph); doc = HWPFTestDataSamples.writeOutAndReadBack(doc); @@ -675,7 +656,6 @@ public class TestBugs extends TestCase /** * Bug 51678 - Extracting text from Bug51524.zip is slow Bug 51524 - * PapBinTable constructor is slow - * @throws IOException */ public void test51678And51524() throws IOException { @@ -822,34 +802,21 @@ public class TestBugs extends TestCase private int section2BottomMargin = 1440; private final int section2NumColumns = 3; + @SuppressWarnings("SuspiciousNameCombination") public void testHWPFSections() { - HWPFDocument document = null; - Paragraph para = null; - Section section = null; - Range overallRange = null; - int numParas = 0; - int numSections = 0; - document = HWPFTestDataSamples.openSampleFile("Bug53453Section.doc"); - overallRange = document.getOverallRange(); - numParas = overallRange.numParagraphs(); + HWPFDocument document = HWPFTestDataSamples.openSampleFile("Bug53453Section.doc"); + Range overallRange = document.getOverallRange(); + int numParas = overallRange.numParagraphs(); for(int i = 0; i < numParas; i++) { - para = overallRange.getParagraph(i); - numSections = para.numSections(); + Paragraph para = overallRange.getParagraph(i); + int numSections = para.numSections(); for(int j = 0; j < numSections; j++) { - section = para.getSection(j); + Section section = para.getSection(j); if(para.text().trim().equals("Section1")) { - assertEquals(section1BottomMargin, section.getMarginBottom()); - assertEquals(section1LeftMargin, section.getMarginLeft()); - assertEquals(section1RightMargin, section.getMarginRight()); - assertEquals(section1TopMargin, section.getMarginTop()); - assertEquals(section1NumColumns, section.getNumColumns()); + assertSection1Margin(section); } else if(para.text().trim().equals("Section2")) { - assertEquals(section2BottomMargin, section.getMarginBottom()); - assertEquals(section2LeftMargin, section.getMarginLeft()); - assertEquals(section2RightMargin, section.getMarginRight()); - assertEquals(section2TopMargin, section.getMarginTop()); - assertEquals(section2NumColumns, section.getNumColumns()); + assertSection2Margin(section); // Change the margin widths this.section2BottomMargin = (int)(1.5 * AbstractWordUtils.TWIPS_PER_INCH); @@ -869,53 +836,70 @@ public class TestBugs extends TestCase overallRange = document.getOverallRange(); numParas = overallRange.numParagraphs(); for(int i = 0; i < numParas; i++) { - para = overallRange.getParagraph(i); - numSections = para.numSections(); + Paragraph para = overallRange.getParagraph(i); + int numSections = para.numSections(); for(int j = 0; j < numSections; j++) { - section = para.getSection(j); + Section section = para.getSection(j); if(para.text().trim().equals("Section1")) { // No changes to the margins in Section1 - assertEquals(section1BottomMargin, section.getMarginBottom()); - assertEquals(section1LeftMargin, section.getMarginLeft()); - assertEquals(section1RightMargin, section.getMarginRight()); - assertEquals(section1TopMargin, section.getMarginTop()); - assertEquals(section1NumColumns, section.getNumColumns()); + assertSection1Margin(section); } else if(para.text().trim().equals("Section2")) { // The margins in Section2 have kept the new settings. - assertEquals(section2BottomMargin, section.getMarginBottom()); - assertEquals(section2LeftMargin, section.getMarginLeft()); - assertEquals(section2RightMargin, section.getMarginRight()); - assertEquals(section2TopMargin, section.getMarginTop()); - assertEquals(section2NumColumns, section.getNumColumns()); + assertSection2Margin(section); } } } } + @SuppressWarnings("Duplicates") + private void assertSection1Margin(Section section) { + assertEquals(section1BottomMargin, section.getMarginBottom()); + assertEquals(section1LeftMargin, section.getMarginLeft()); + assertEquals(section1RightMargin, section.getMarginRight()); + assertEquals(section1TopMargin, section.getMarginTop()); + assertEquals(section1NumColumns, section.getNumColumns()); + } + + @SuppressWarnings("Duplicates") + private void assertSection2Margin(Section section) { + assertEquals(section2BottomMargin, section.getMarginBottom()); + assertEquals(section2LeftMargin, section.getMarginLeft()); + assertEquals(section2RightMargin, section.getMarginRight()); + assertEquals(section2TopMargin, section.getMarginTop()); + assertEquals(section2NumColumns, section.getNumColumns()); + } + public void testRegressionIn315beta2() { HWPFDocument hwpfDocument = HWPFTestDataSamples.openSampleFile("cap.stanford.edu_profiles_viewbiosketch_facultyid=4009&name=m_maciver.doc"); assertNotNull(hwpfDocument); } - public void DISABLEDtest57603SevenRowTable() throws Exception { - HWPFDocument hwpfDocument = HWPFTestDataSamples.openSampleFile("57603-seven_columns.doc"); - HWPFDocument hwpfDocument2 = HWPFTestDataSamples.writeOutAndReadBack(hwpfDocument); - assertNotNull(hwpfDocument2); - } - - public void test57843() throws IOException { + public void test57603SevenRowTable() throws Exception { try { - File f = POIDataSamples.getDocumentInstance().getFile("57843.doc"); - boolean readOnly = true; - POIFSFileSystem fs = new POIFSFileSystem(f, readOnly); - HWPFOldDocument doc = new HWPFOldDocument(fs); - assertNotNull(doc); - doc.close(); - fs.close(); - fixed("57843"); + HWPFDocument hwpfDocument = HWPFTestDataSamples.openSampleFile("57603-seven_columns.doc"); + HWPFDocument hwpfDocument2 = HWPFTestDataSamples.writeOutAndReadBack(hwpfDocument); + assertNotNull(hwpfDocument2); + hwpfDocument2.close(); + hwpfDocument.close(); + fixed("57603"); } catch (ArrayIndexOutOfBoundsException e) { // expected until this bug is fixed } } + + public void test57843() throws IOException { + File f = POIDataSamples.getDocumentInstance().getFile("57843.doc"); + POIFSFileSystem fs = new POIFSFileSystem(f, true); + try { + HWPFOldDocument doc = new HWPFOldDocument(fs); + assertNotNull(doc); + doc.close(); + fixed("57843"); + } catch (ArrayIndexOutOfBoundsException e) { + // expected until this bug is fixed + } finally { + fs.close(); + } + } } diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java b/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java index 454c1da68..1308b2c11 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java @@ -95,6 +95,7 @@ import org.junit.Test; * YK: If a bug can be tested in terms of common ss interfaces, * define the test in the base class {@link BaseTestBugzillaIssues} */ +@SuppressWarnings("deprecation") public final class TestBugs extends BaseTestBugzillaIssues { // to not affect other tests running in the same JVM @After @@ -894,7 +895,6 @@ public final class TestBugs extends BaseTestBugzillaIssues { /** * Problems with extracting check boxes from * HSSFObjectData - * @throws Exception */ @Test(expected=FileNotFoundException.class) public void bug44840() throws Exception { @@ -943,33 +943,15 @@ public final class TestBugs extends BaseTestBugzillaIssues { // Check all names fit within range, and use // DeletedArea3DPtg InternalWorkbook w = wb1.getWorkbook(); - for(int i=0; i