From c96db64e3c572fffba1257bdffc3e1f4abbbb16f Mon Sep 17 00:00:00 2001 From: Danny Muid Date: Wed, 18 Feb 2004 16:48:17 +0000 Subject: [PATCH] Removed variable that stores the number of merged regions in its record since the array should be the basis for this number. Less manual index tracking, the better. Thanks for problem report! PR: 27005 Reported by: skalchihin@mgsm.ru git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/branches/REL_2_BRANCH@353510 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/hssf/record/MergeCellsRecord.java | 33 +++++++++---------- .../poi/hssf/usermodel/TestCloneSheet.java | 15 ++++++--- 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/src/java/org/apache/poi/hssf/record/MergeCellsRecord.java b/src/java/org/apache/poi/hssf/record/MergeCellsRecord.java index 0d0b2684d..05e1ac548 100644 --- a/src/java/org/apache/poi/hssf/record/MergeCellsRecord.java +++ b/src/java/org/apache/poi/hssf/record/MergeCellsRecord.java @@ -61,19 +61,18 @@ import java.util.Iterator; import org.apache.poi.util.LittleEndian; /** - * Title: Merged Cells Record

+ * Title: Merged Cells Record + *
* Description: Optional record defining a square area of cells to "merged" into - * one cell.

- * REFERENCE: NONE (UNDOCUMENTED PRESENTLY)

+ * one cell.
+ * REFERENCE: NONE (UNDOCUMENTED PRESENTLY)
* @author Andrew C. Oliver (acoliver at apache dot org) * @version 2.0-pre */ - public class MergeCellsRecord extends Record { public final static short sid = 0xe5; - private short field_1_num_areas; private ArrayList field_2_regions; public MergeCellsRecord() @@ -109,11 +108,11 @@ public class MergeCellsRecord protected void fillFields(byte [] data, short size, int offset) { - field_1_num_areas = LittleEndian.getShort(data, 0 + offset); - field_2_regions = new ArrayList(field_1_num_areas + 10); + short numAreas = LittleEndian.getShort(data, 0 + offset); + field_2_regions = new ArrayList(numAreas + 10); int pos = 2; - for (int k = 0; k < field_1_num_areas; k++) + for (int k = 0; k < numAreas; k++) { MergedRegion region = new MergedRegion(LittleEndian @@ -135,7 +134,9 @@ public class MergeCellsRecord public short getNumAreas() { - return field_1_num_areas; + //if the array size is larger than a short (65536), the record can't hold that many merges anyway + if (field_2_regions == null) return 0; + return (short)field_2_regions.size(); } /** @@ -143,13 +144,14 @@ public class MergeCellsRecord * it will be incremented automatically or decremented when an area is removed. If * you are setting this to 0 then you are a terrible person. Just remove the record. * (just kidding about you being a terrible person..hehe) - * + * @deprecated We now link the size to the actual array of merged regions + * @see #getNumAreas() * @param numareas number of areas */ public void setNumAreas(short numareas) { - field_1_num_areas = numareas; + } /** @@ -175,7 +177,6 @@ public class MergeCellsRecord colto); field_2_regions.add(region); - field_1_num_areas++; return field_2_regions.size() - 1; } @@ -187,7 +188,6 @@ public class MergeCellsRecord public void removeAreaAt(int area) { field_2_regions.remove(area); - field_1_num_areas--; } /** @@ -246,9 +246,9 @@ public class MergeCellsRecord retval.append("[MERGEDCELLS]").append("\n"); retval.append(" .sid =").append(sid).append("\n"); - retval.append(" .numregions =").append(field_1_num_areas) + retval.append(" .numregions =").append(getNumAreas()) .append("\n"); - for (int k = 0; k < field_1_num_areas; k++) + for (int k = 0; k < getNumAreas(); k++) { MergedRegion region = ( MergedRegion ) field_2_regions.get(k); @@ -325,8 +325,7 @@ public class MergeCellsRecord } public Object clone() { - MergeCellsRecord rec = new MergeCellsRecord(); - rec.field_1_num_areas = field_1_num_areas; + MergeCellsRecord rec = new MergeCellsRecord(); rec.field_2_regions = new ArrayList(); Iterator iterator = field_2_regions.iterator(); while (iterator.hasNext()) { diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestCloneSheet.java b/src/testcases/org/apache/poi/hssf/usermodel/TestCloneSheet.java index 2901bfe76..dd4a824a1 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestCloneSheet.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestCloneSheet.java @@ -2,7 +2,7 @@ /* ==================================================================== * The Apache Software License, Version 1.1 * - * Copyright (c) 2003 The Apache Software Foundation. All rights + * Copyright (c) 2004 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without @@ -55,10 +55,14 @@ package org.apache.poi.hssf.usermodel; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; + import junit.framework.TestCase; -import org.apache.poi.hssf.usermodel.HSSFSheet; -import org.apache.poi.hssf.usermodel.HSSFWorkbook; + import org.apache.poi.hssf.util.Region; +import org.apache.poi.poifs.filesystem.POIFSFileSystem; /** * Test the ability to clone a sheet. @@ -77,7 +81,10 @@ public class TestCloneSheet extends TestCase { HSSFWorkbook b = new HSSFWorkbook(); HSSFSheet s = b.createSheet("Test"); s.addMergedRegion(new Region((short)0,(short)0,(short)1,(short)1)); - b.cloneSheet(0); + HSSFSheet clonedSheet = b.cloneSheet(0); + + assertEquals("One merged area", 1, clonedSheet.getNumMergedRegions()); + } catch(Exception e){e.printStackTrace();fail(e.getMessage());} }