From 83107c9daec42ed33c1c1cbc0d6b2bf3a1ec6fca Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Tue, 15 Jan 2008 16:42:44 +0000 Subject: [PATCH] Fix bug #44201 - support cloning of DVRecord, so validation enabled sheets can be cloned git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@612152 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/changes.xml | 3 +- src/documentation/content/xdocs/status.xml | 3 +- .../record/AbstractEscherHolderRecord.java | 15 +--------- .../org/apache/poi/hssf/record/DVRecord.java | 22 +++++++++----- .../org/apache/poi/hssf/record/Record.java | 28 ++++++++++++++++++ .../org/apache/poi/hssf/data/44201.xls | Bin 0 -> 13824 bytes .../apache/poi/hssf/usermodel/TestBugs.java | 20 +++++++++++++ 7 files changed, 68 insertions(+), 23 deletions(-) create mode 100644 src/testcases/org/apache/poi/hssf/data/44201.xls diff --git a/src/documentation/content/xdocs/changes.xml b/src/documentation/content/xdocs/changes.xml index 2617fbb43b..be8df65763 100644 --- a/src/documentation/content/xdocs/changes.xml +++ b/src/documentation/content/xdocs/changes.xml @@ -36,7 +36,8 @@ - 44200 - Enable cloning of sheets with notes + 44201 - Enable cloning of sheets with data validation rules + 44200 - Enable cloning of sheets with notes 43008 - Add a moveCell method to HSSFRow, and deprecate setCellNum(), which didn't update things properly 43058 - Support setting row grouping on files from CR IX, which lack GutsRecords 31795 - Support cloning of sheets with certain drawing objects on them diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 9e93592fc4..5868b0eb2d 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -33,7 +33,8 @@ - 44200 - Enable cloning of sheets with notes + 44201 - Enable cloning of sheets with data validation rules + 44200 - Enable cloning of sheets with notes 43008 - Add a moveCell method to HSSFRow, and deprecate setCellNum(), which didn't update things properly 43058 - Support setting row grouping on files from CR IX, which lack GutsRecords 31795 - Support cloning of sheets with certain drawing objects on them diff --git a/src/java/org/apache/poi/hssf/record/AbstractEscherHolderRecord.java b/src/java/org/apache/poi/hssf/record/AbstractEscherHolderRecord.java index 0260fc915b..c5fae27ae3 100644 --- a/src/java/org/apache/poi/hssf/record/AbstractEscherHolderRecord.java +++ b/src/java/org/apache/poi/hssf/record/AbstractEscherHolderRecord.java @@ -18,7 +18,6 @@ package org.apache.poi.hssf.record; -import java.io.ByteArrayInputStream; import java.util.ArrayList; import java.util.Iterator; import java.util.List; @@ -232,19 +231,7 @@ public abstract class AbstractEscherHolderRecord public Object clone() { - // Do it via a re-serialise - // It's a cheat, but it works... - byte[] b = serialize(); - RecordInputStream rinp = new RecordInputStream( - new ByteArrayInputStream(b) - ); - rinp.nextRecord(); - - Record[] r = RecordFactory.createRecord(rinp); - if(r.length != 1) { - throw new IllegalStateException("Re-serialised a record to clone it, but got " + r.length + " records back!"); - } - return r[0]; + return cloneViaReserialise(); } public void addEscherRecord(int index, EscherRecord element) diff --git a/src/java/org/apache/poi/hssf/record/DVRecord.java b/src/java/org/apache/poi/hssf/record/DVRecord.java index 0bae009bd3..e5527d61f1 100644 --- a/src/java/org/apache/poi/hssf/record/DVRecord.java +++ b/src/java/org/apache/poi/hssf/record/DVRecord.java @@ -16,16 +16,16 @@ package org.apache.poi.hssf.record; +import java.io.IOException; +import java.util.Enumeration; +import java.util.Hashtable; +import java.util.Stack; + +import org.apache.poi.hssf.record.formula.Ptg; +import org.apache.poi.hssf.util.HSSFCellRangeAddress; import org.apache.poi.util.BitField; import org.apache.poi.util.LittleEndian; import org.apache.poi.util.StringUtil; -import org.apache.poi.hssf.util.HSSFCellRangeAddress; -import org.apache.poi.hssf.record.formula.Ptg; - -import java.io.IOException; -import java.util.Stack; -import java.util.Hashtable; -import java.util.Enumeration; /** * Title: DV Record

@@ -502,6 +502,14 @@ public class DVRecord extends Record { return this.sid; } + + /** + * Clones the object. Uses serialisation, as the + * contents are somewhat complex + */ + public Object clone() { + return cloneViaReserialise(); + } /**@todo DVRecord = Serializare */ diff --git a/src/java/org/apache/poi/hssf/record/Record.java b/src/java/org/apache/poi/hssf/record/Record.java index 3205c57740..c1b8a0cfda 100644 --- a/src/java/org/apache/poi/hssf/record/Record.java +++ b/src/java/org/apache/poi/hssf/record/Record.java @@ -19,6 +19,8 @@ package org.apache.poi.hssf.record; +import java.io.ByteArrayInputStream; + /** * Title: Record * Description: All HSSF Records inherit from this class. It @@ -147,4 +149,30 @@ public abstract class Record public Object clone() { throw new RuntimeException("The class "+getClass().getName()+" needs to define a clone method"); } + + /** + * Clone the current record, via a call to serialise + * it, and another to create a new record from the + * bytes. + * May only be used for classes which don't have + * internal counts / ids in them. For those which + * do, a full record-aware serialise is needed, which + * allocates new ids / counts as needed. + */ + public Record cloneViaReserialise() + { + // Do it via a re-serialise + // It's a cheat, but it works... + byte[] b = serialize(); + RecordInputStream rinp = new RecordInputStream( + new ByteArrayInputStream(b) + ); + rinp.nextRecord(); + + Record[] r = RecordFactory.createRecord(rinp); + if(r.length != 1) { + throw new IllegalStateException("Re-serialised a record to clone it, but got " + r.length + " records back!"); + } + return r[0]; + } } diff --git a/src/testcases/org/apache/poi/hssf/data/44201.xls b/src/testcases/org/apache/poi/hssf/data/44201.xls new file mode 100644 index 0000000000000000000000000000000000000000..593d5aa1879668083b30a47f0434f1f21bdfe97e GIT binary patch literal 13824 zcmeHOdx%t382{bb*>zS|cXoaBve{`?aa~>0UM{V=6vl)V9_Q3?j$O zATp>7k`UA$mKi<4prlZ)=_r_=%_^a&IQOunNnt27hJlXzZjdH({98zT-H!r zIu@Raa$@Es@M1i{&WxiT6G`=$j1DxR1^wl;Gba|F$ps)*s_0U*A%zvVxkM$iF$i`2 zS2Bq+2_XG_~}_+x>W=DPi1XM^h}kk$p9zw2_4%mNGTp_JIv8-7K<45BeoC74rB>c9HZ)tA`67Az-;fwGrMi=ETZA-1V zxmYNH-MBC>B;AGeNI9z-hv(RfeX4w^Giu9;PQ~-MgsqV&K`)GN$tBwx%+&TihI&JZ zWCyiO!XE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BSFhFj^Yvd zK(G0@+7F6-jfa4Dqs{{5~hx`hpPrvLq>w>zVqSIS@ckwF=eP^MzUqcjb zGn>Te=C>c6s^`LyM)v~hZ>ff4Jqq^KW0z!pJ=}|J=gwU0bGZs AnE(I) literal 0 HcmV?d00001 diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java b/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java index a3154e3063..006b60c3dd 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java @@ -934,6 +934,26 @@ extends TestCase { } + /** + * Bug 44201: Sheet not cloneable when validation added to excel cell + */ + public void test44201() throws Exception { + FileInputStream in = new FileInputStream(new File(cwd, "44201.xls")); + HSSFWorkbook wb = new HSSFWorkbook(in); + in.close(); + + wb.cloneSheet(0); + assertTrue("No Exceptions while cloning sheet", true); + + //serialize and read again + ByteArrayOutputStream out = new ByteArrayOutputStream(); + wb.write(out); + out.close(); + + wb = new HSSFWorkbook(new ByteArrayInputStream(out.toByteArray())); + assertTrue("No Exceptions while reading file", true); + + } } -- 2.39.5