@Override
public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException {
- if (localName.toLowerCase(Locale.US).equals(SHEET)) {
+ if (localName.equalsIgnoreCase(SHEET)) {
String name = null;
String id = null;
for (int i = 0; i < attrs.getLength(); i++) {
- if (attrs.getLocalName(i).toLowerCase(Locale.US).equals(NAME)) {
+ final String attrName = attrs.getLocalName(i);
+ if (attrName.equalsIgnoreCase(NAME)) {
name = attrs.getValue(i);
- } else if (attrs.getLocalName(i).toLowerCase(Locale.US).equals(ID)) {
+ } else if (attrName.equalsIgnoreCase(ID)) {
id = attrs.getValue(i);
}
+ }
+ if (name != null && id != null) {
sheetRefs.add(new XSSFSheetRef(id, name));
}
}
package org.apache.poi.xssf.eventusermodel;
import static org.apache.poi.POITestCase.assertContains;
+import static org.apache.poi.POITestCase.assertNotContained;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;
+import java.util.Set;
+import java.util.HashSet;
import org.apache.poi.POIDataSamples;
import org.apache.poi.POIXMLException;
pkg.close();
}
+
+ /**
+ * bug 61304: Call to XSSFReader.getSheetsData() returns duplicate sheets.
+ *
+ * The problem seems to be caused only by those xlsx files which have a specific
+ * order of the attributes inside the <sheet> tag of workbook.xml
+ *
+ * Example (which causes the problems):
+ * <sheet name="Sheet6" r:id="rId6" sheetId="4"/>
+ *
+ * While this one works correctly:
+ * <sheet name="Sheet6" sheetId="4" r:id="rId6"/>
+ */
+ public void test61034() throws Exception {
+ OPCPackage pkg = XSSFTestDataSamples.openSamplePackage("61034.xlsx");
+ XSSFReader reader = new XSSFReader(pkg);
+ XSSFReader.SheetIterator iter = (XSSFReader.SheetIterator) reader.getSheetsData();
+ Set<String> seen = new HashSet<String>();
+ while (iter.hasNext()) {
+ InputStream stream = iter.next();
+ String sheetName = iter.getSheetName();
+ assertNotContained(seen, sheetName);
+ seen.add(sheetName);
+ stream.close();
+ }
+ pkg.close();
+ }
}
import static org.hamcrest.CoreMatchers.startsWith;
import static org.hamcrest.CoreMatchers.endsWith;
import static org.hamcrest.CoreMatchers.not;
+import static org.hamcrest.CoreMatchers.hasItem;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Locale;
import java.util.Map;
+import java.util.Set;
import org.apache.poi.util.SuppressForbidden;
import org.apache.poi.util.Internal;
}
fail("Unable to find " + key + " in " + map);
}
+
+ public static <T> void assertNotContained(Set<T> set, T element) {
+ assertThat(set, not(hasItem(element)));
+ /*if (set.contains(element)) {
+ fail("Set should not contain " + element);
+ }*/
+ }
/**
* Utility method to get the value of a private/protected field.