From f49f4dba40b8b280988f3e81e1b61bbf71ca3863 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Tue, 12 Dec 2006 18:21:41 +0000 Subject: [PATCH] When sorting the contents of a directory, do so in a way that doesn't upset Excel when it comes to Macros. Correct logic from Bill, and fix+test from Yegor. See bug #39234 git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@486265 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/poifs/property/DirectoryProperty.java | 33 +++- .../org/apache/poi/hssf/data/39234.xls | Bin 0 -> 24064 bytes .../poifs/filesystem/TestPropertySorter.java | 152 ++++++++++++++++++ 3 files changed, 182 insertions(+), 3 deletions(-) create mode 100644 src/testcases/org/apache/poi/hssf/data/39234.xls create mode 100644 src/testcases/org/apache/poi/poifs/filesystem/TestPropertySorter.java diff --git a/src/java/org/apache/poi/poifs/property/DirectoryProperty.java b/src/java/org/apache/poi/poifs/property/DirectoryProperty.java index 5681ad5f03..e0d817cf07 100644 --- a/src/java/org/apache/poi/poifs/property/DirectoryProperty.java +++ b/src/java/org/apache/poi/poifs/property/DirectoryProperty.java @@ -127,7 +127,7 @@ public class DirectoryProperty return result; } - private class PropertyComparator + public static class PropertyComparator implements Comparator { @@ -162,13 +162,40 @@ public class DirectoryProperty public int compare(Object o1, Object o2) { + String VBA_PROJECT = "_VBA_PROJECT"; String name1 = (( Property ) o1).getName(); String name2 = (( Property ) o2).getName(); - int result = name1.length() - name2.length(); + int result = name1.length() - name2.length(); if (result == 0) { - result = name1.compareTo(name2); + // _VBA_PROJECT, it seems, will always come last + if (name1.compareTo(VBA_PROJECT) == 0) + result = 1; + else if (name2.compareTo(VBA_PROJECT) == 0) + result = -1; + else + { + if (name1.startsWith("__") && name2.startsWith("__")) + { + // Betweeen __SRP_0 and __SRP_1 just sort as normal + result = name1.compareToIgnoreCase(name2); + } + else if (name1.startsWith("__")) + { + // If only name1 is __XXX then this will be placed after name2 + result = 1; + } + else if (name2.startsWith("__")) + { + // If only name2 is __XXX then this will be placed after name1 + result = -1; + } + else + // result = name1.compareTo(name2); + // The default case is to sort names ignoring case + result = name1.compareToIgnoreCase(name2); + } } return result; } diff --git a/src/testcases/org/apache/poi/hssf/data/39234.xls b/src/testcases/org/apache/poi/hssf/data/39234.xls new file mode 100644 index 0000000000000000000000000000000000000000..20fc9a0011870caf1cd439324fbd16d665e13208 GIT binary patch literal 24064 zcmeHP3qV}Oxt?=&frUU^2#HBZ;tAwI62N@`0S$&_Ss)Pt4dl^^mn^VJFbj+9qA}*N zNwl@C*1TG6wQa1`YIChoTdnpX=Gvy#rar2@Y9A&>TkWl_di7R&t$V+J<_vq5EU;i} zwKoIbnK}Qw{`qI-pXb@fAIm&`(_Pa~up#C=W@7I}QdzQruE0&;#>$uqhY5Hu5{U>R zV{o1}A0Y|+4ZK=VdD(y&NCK$NQ-D<93?L1d0HgyMz(fGm&!z&IKo&3wm<;3tQ-Cvp zX+Rz@9moM@05gGEz*&F>%m#9SIlw|-E-(+64=ezvZ7tI60@#JR`y(Xc4>hYRSpa__ z)P;xj!0uz0LBfffDQGdWfTRyJ3q2Vd;7?XV)@zF z$i0tkV(pxdO(ND9o>@#)sI5sJ5{;@-ZX3gh+>Mm6xh>Ghvh384;~AO%z@gA7`4jI_j}Ptt zlpcvGUjHY(KsvzK|Ec^W2igBgKadVcr2mtiARR#cUG{&{C8PsLFOUw9{l5tAvjH1m z2ONMCC zXat&oj{($5nt>Kz6|fpu15i32hy4lQ0-zPR5coHs4OjYz4Lf+kqWG7)Z6C7L$|MA15D6 zd1L@i6JsaPA`Y7$;_c!EglR=i>5P5cw;|A{ogdiZ4_u=CqfHg(!K+Z)96%$jP?FKo zRm?Yj0m=i)8N^Y`#;)L+K;NeJKw#62@SKR|MiHPC@SMb-ck(2@&z}|W*^Ixl&TW}P zjf18$Yt>2~?i&8g<*5kZ%K*^g6~2F`q2pgQNFXx zGoWCh;Bx2W<`v}S+1eJI+nO(5S{KYR&&F_#fmM>=f5A#$=Y_F>axtFIj0P&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O#z`0MdaBU?M=_tguPnW$EwP zuqOj_rGmb#qcGES8_v__Lnr}S>qU)d6LNjWFZ-p+QAB?8DH^oGps5mXro%R4<&kQ2@S4CK$Rdhz_Lky{7n z!H2vkDF$h>^Y*tHpM|&*`Yhb(T9F z4hLE=0=OG$D{{-r%3bBfE=PHp5!3;isu0}fbU5A48fUct=(X;OTz8q<@FcV z%0SLYyPz%*4B`4?$k&72Ds5TM`oM)5X@fn+UU9jz%;~5uEw&Z9YszhfcDuW#u*`#A zXtTL&?sB``W-D=RI~SA0a|dJDc*-=M2l2VT4+$FkV+D#*pz+6IXGwupTDrJE%SaO) zvI1U%|8AY?h)Z=2rJ@5OZA!X8D{&&x;?iw`PVT~gN$O1?$(tQ64fMz;0=c4{i_tq$DirectoryProperty . + *

+ * In particular it is important to serialize ROOT._VBA_PROJECT_CUR.VBA node. + * See bug 39234 in bugzilla. Thanks to Bill Seddon for providing the solution. + *

+ * + * @author Yegor Kozlov + */ +public class TestPropertySorter extends TestCase { + + //the correct order of entries in the test file + protected static final String[] _entries = { + "dir", "JML", "UTIL", "Loader", "Sheet1", "Sheet2", "Sheet3", + "__SRP_0", "__SRP_1", "__SRP_2", "__SRP_3", "__SRP_4", "__SRP_5", + "ThisWorkbook", "_VBA_PROJECT", + }; + + protected File testFile; + + public void setUp(){ + String home = System.getProperty("HSSF.testdata.path"); + testFile = new File(home + "/39234.xls"); + } + + /** + * Test sorting of properties in DirectoryProperty + */ + public void testSortProperties() throws IOException { + InputStream is = new FileInputStream(testFile); + POIFSFileSystem fs = new POIFSFileSystem(is); + is.close(); + Property[] props = getVBAProperties(fs); + + assertEquals(_entries.length, props.length); + + // (1). See that there is a problem with the old case-sensitive property comparartor + Arrays.sort(props, new CaseSensitivePropertyComparator()); + try { + for (int i = 0; i < props.length; i++) { + assertEquals(_entries[i], props[i].getName()); + } + fail("case-sensitive property comparator returns properties in wrong order"); + } catch (ComparisonFailure e){ + ; // as expected + } + + // (2) Verify that the fixed proeprty comparator works right + Arrays.sort(props, new DirectoryProperty.PropertyComparator()); + for (int i = 0; i < props.length; i++) { + assertEquals(_entries[i], props[i].getName()); + } + } + + /** + * Serialize file system and verify that the order of properties is the same as in the original file. + */ + public void testSerialization() throws IOException { + InputStream is = new FileInputStream(testFile); + POIFSFileSystem fs = new POIFSFileSystem(is); + is.close(); + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + fs.writeFilesystem(out); + out.close(); + is = new ByteArrayInputStream(out.toByteArray()); + fs = new POIFSFileSystem(is); + is.close(); + Property[] props = getVBAProperties(fs); + Arrays.sort(props, new DirectoryProperty.PropertyComparator()); + + assertEquals(_entries.length, props.length); + for (int i = 0; i < props.length; i++) { + assertEquals(_entries[i], props[i].getName()); + } + } + + /** + * @return array of properties read from ROOT._VBA_PROJECT_CUR.VBA node + */ + protected Property[] getVBAProperties(POIFSFileSystem fs) throws IOException { + String _VBA_PROJECT_CUR = "_VBA_PROJECT_CUR"; + String VBA = "VBA"; + + DirectoryEntry root = fs.getRoot(); + DirectoryEntry vba_project = (DirectoryEntry)root.getEntry(_VBA_PROJECT_CUR); + + DirectoryNode vba = (DirectoryNode)vba_project.getEntry(VBA); + DirectoryProperty p = (DirectoryProperty)vba.getProperty(); + + ArrayList lst = new ArrayList(); + for (Iterator it = p.getChildren(); it.hasNext();){ + Property ch = (Property)it.next(); + lst.add(ch); + } + return (Property [])lst.toArray(new Property[ 0 ]); + } + + /** + * Old version of case-sensitive PropertyComparator to demonstrate the problem + */ + private class CaseSensitivePropertyComparator implements Comparator + { + + public boolean equals(Object o) + { + return this == o; + } + + public int compare(Object o1, Object o2) + { + String name1 = (( Property ) o1).getName(); + String name2 = (( Property ) o2).getName(); + int result = name1.length() - name2.length(); + + if (result == 0) + { + result = name1.compareTo(name2); + } + return result; + } + } + +} -- 2.39.5