poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cen...@apache.org
Subject svn commit: r1734863 - in /poi/trunk/src: java/org/apache/poi/hssf/usermodel/ java/org/apache/poi/ss/usermodel/ ooxml/java/org/apache/poi/xssf/streaming/ ooxml/java/org/apache/poi/xssf/usermodel/ testcases/org/apache/poi/hssf/usermodel/ testcases/org/a...
Date Sun, 13 Mar 2016 21:30:38 GMT
Author: centic
Date: Sun Mar 13 21:30:38 2016
New Revision: 1734863

URL: http://svn.apache.org/viewvc?rev=1734863&view=rev
Log:
Bug 56930: Add Workbook.getNames() to allow to query for names that appear multiple times

Modified:
    poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java
    poi/trunk/src/java/org/apache/poi/ss/usermodel/Workbook.java
    poi/trunk/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java
    poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java
    poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFName.java
    poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestNamedRange.java

Modified: poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java?rev=1734863&r1=1734862&r2=1734863&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java Sun Mar 13 21:30:38
2016
@@ -1474,6 +1474,18 @@ public final class HSSFWorkbook extends
     }
 
     @Override
+    public List<HSSFName> getNames(String name) {
+        List<HSSFName> nameList = new ArrayList<HSSFName>();
+        for(HSSFName nr : names) {
+            if(nr.getNameName().equals(name)) {
+                nameList.add(nr);
+            }
+        }
+
+        return nameList;
+    }
+
+    @Override
     public HSSFName getNameAt(int nameIndex) {
         int nNames = names.size();
         if (nNames < 1) {

Modified: poi/trunk/src/java/org/apache/poi/ss/usermodel/Workbook.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/usermodel/Workbook.java?rev=1734863&r1=1734862&r2=1734863&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/usermodel/Workbook.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/usermodel/Workbook.java Sun Mar 13 21:30:38 2016
@@ -351,6 +351,15 @@ public interface Workbook extends Closea
      * @return the defined name with the specified name. <code>null</code> if
not found.
      */
     Name getName(String name);
+
+    /**
+     * Returns all defined names with the given name.
+     *
+     * @param name the name of the defined name
+     * @return a list of the defined names with the specified name. An empty list is returned
if none is found.
+     */
+    List<? extends Name> getNames(String name);
+
     /**
      * @param nameIndex position of the named range (0-based)
      * @return the defined name at the specified index

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java?rev=1734863&r1=1734862&r2=1734863&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java Sun Mar 13 21:30:38
2016
@@ -956,6 +956,18 @@ public class SXSSFWorkbook implements Wo
     {
         return _wb.getName(name);
     }
+
+    /**
+     * Returns all defined names with the given name.
+     *
+     * @param name the name of the defined name
+     * @return a list of the defined names with the specified name. An empty list is returned
if none is found.
+     */
+    @Override
+    public List<? extends Name> getNames(String name) {
+        return _wb.getNames(name);
+    }
+
     /**
      * @param nameIndex position of the named range (0-based)
      * @return the defined name at the specified index

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java?rev=1734863&r1=1734862&r2=1734863&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java Sun Mar 13 21:30:38
2016
@@ -522,7 +522,7 @@ public class XSSFWorkbook extends POIXML
      */
     public int addPicture(InputStream is, int format) throws IOException {
         int imageNumber = getAllPictures().size() + 1;
-        XSSFPictureData img = (XSSFPictureData)createRelationship(XSSFPictureData.RELATIONS[format],
XSSFFactory.getInstance(), imageNumber, true).getDocumentPart();
+        XSSFPictureData img = createRelationship(XSSFPictureData.RELATIONS[format], XSSFFactory.getInstance(),
imageNumber, true).getDocumentPart();
         OutputStream out = img.getPackagePart().getOutputStream();
         IOUtils.copy(is, out);
         out.close();
@@ -872,7 +872,7 @@ public class XSSFWorkbook extends POIXML
             List<PackagePart> mediaParts = getPackage().getPartsByName(Pattern.compile("/xl/media/.*?"));
             pictures = new ArrayList<XSSFPictureData>(mediaParts.size());
             for(PackagePart part : mediaParts){
-                pictures.add(new XSSFPictureData(part, null));
+                pictures.add(new XSSFPictureData(part));
             }
         }
         return pictures; //YK: should return Collections.unmodifiableList(pictures);
@@ -909,6 +909,18 @@ public class XSSFWorkbook extends POIXML
     }
 
     @Override
+    public List<XSSFName> getNames(String name) {
+        List<XSSFName> names = new ArrayList<XSSFName>();
+        for(XSSFName nr : namedRanges) {
+            if(nr.getNameName().equals(name)) {
+                names.add(nr);
+            }
+        }
+
+        return names;
+    }
+
+    @Override
     public XSSFName getNameAt(int nameIndex) {
         int nNames = namedRanges.size();
         if (nNames < 1) {

Modified: poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFName.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFName.java?rev=1734863&r1=1734862&r2=1734863&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFName.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFName.java Sun Mar 13 21:30:38
2016
@@ -17,10 +17,6 @@
 
 package org.apache.poi.hssf.usermodel;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
-
 import org.apache.poi.POITestCase;
 import org.apache.poi.hssf.HSSFITestDataProvider;
 import org.apache.poi.hssf.HSSFTestDataSamples;
@@ -33,6 +29,8 @@ import org.apache.poi.ss.util.AreaRefere
 import org.apache.poi.ss.util.CellRangeAddress;
 import org.junit.Test;
 
+import static org.junit.Assert.*;
+
 /**
  * Tests various functionality having to do with {@link org.apache.poi.ss.usermodel.Name}.
  */
@@ -119,6 +117,7 @@ public final class TestHSSFName extends
         HSSFName namedRange1 = wb1.getNameAt(0);
         //Getting it sheet name
         sheetName = namedRange1.getSheetName();
+        assertNotNull(sheetName);
 
         // sanity check
         SanityChecker c = new SanityChecker();
@@ -198,6 +197,7 @@ public final class TestHSSFName extends
         workbook.close();
     }
 
+    @SuppressWarnings("deprecation")
     @Test
     public void testDeletedReference() throws Exception {
         HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("24207.xls");
@@ -233,8 +233,8 @@ public final class TestHSSFName extends
         wb.createSheet("CSCO");
 
         Ptg[] ptgs = HSSFFormulaParser.parse("CSCO!$E$71", wb, FormulaType.NAMEDRANGE, 0);
-        for (int i = 0; i < ptgs.length; i++) {
-            assertEquals('R', ptgs[i].getRVAType());
+        for (Ptg ptg : ptgs) {
+            assertEquals('R', ptg.getRVAType());
         }
         wb.close();
     }

Modified: poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestNamedRange.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestNamedRange.java?rev=1734863&r1=1734862&r2=1734863&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestNamedRange.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestNamedRange.java Sun Mar 13
21:30:38 2016
@@ -29,6 +29,8 @@ import org.apache.poi.ss.util.AreaRefere
 import org.apache.poi.ss.util.CellReference;
 import org.junit.Test;
 
+import java.util.List;
+
 /**
  * Tests of implementations of {@link org.apache.poi.ss.usermodel.Name}.
  *
@@ -643,4 +645,33 @@ public abstract class BaseTestNamedRange
         
         wb.close();
     }
+
+    @Test
+    public void testBug56930() {
+        Workbook wb = _testDataProvider.createWorkbook();
+
+        // x1 on sheet1 defines "x=1"
+        wb.createSheet("sheet1");
+        Name x1 = wb.createName();
+
+        x1.setNameName("x");
+        x1.setRefersToFormula("1");
+        x1.setSheetIndex(wb.getSheetIndex("sheet1"));
+
+        // x2 on sheet2 defines "x=2"
+        wb.createSheet("sheet2");
+        Name x2 = wb.createName();
+        x2.setNameName("x");
+        x2.setRefersToFormula("2");
+        x2.setSheetIndex(wb.getSheetIndex("sheet2"));
+
+        List<? extends Name> names = wb.getNames("x");
+        assertEquals("Had: " + names, 2, names.size());
+        assertEquals("1", names.get(0).getRefersToFormula());
+        assertEquals("2", names.get(1).getRefersToFormula());
+
+        assertEquals("1", wb.getName("x").getRefersToFormula());
+        wb.removeName("x");
+        assertEquals("2", wb.getName("x").getRefersToFormula());
+    }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message