Ignore:
Timestamp:
2018-02-19T23:17:09+01:00 (7 years ago)
Author:
donvip
Message:

fix #josm15980 - bad support of empty cells in Excel files

Location:
applications/editors/josm/plugins/opendata/includes/org/apache/poi
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/opendata/includes/org/apache/poi/hssf/usermodel/HSSFRow.java

    r28000 r34072  
    1818package org.apache.poi.hssf.usermodel;
    1919
    20 import java.util.Iterator;
    21 import java.util.NoSuchElementException;
    22 
    2320import org.apache.poi.hssf.record.CellValueRecordInterface;
    2421import org.apache.poi.hssf.record.RowRecord;
    2522import org.apache.poi.ss.SpreadsheetVersion;
    26 import org.apache.poi.ss.usermodel.Cell;
    2723import org.apache.poi.ss.usermodel.Row;
    2824
     
    10298     *   the maximum number of columns supported by the Excel binary format (.xls)
    10399     */
     100    @Override
    104101    public HSSFCell createCell(int column)
    105102    {
     
    119116     *   the maximum number of columns supported by the Excel binary format (.xls)
    120117     */
     118    @Override
    121119    public HSSFCell createCell(int columnIndex, int type)
    122120    {
     
    164162     * @throws IndexOutOfBoundsException if the row number is not within the range 0-65535.
    165163     */
     164    @Override
    166165    public void setRowNum(int rowIndex) {
    167166        int maxrow = SpreadsheetVersion.EXCEL97.getLastRowIndex();
     
    180179     * @return the row number (0 based)
    181180     */
     181    @Override
    182182    public int getRowNum()
    183183    {
     
    190190     * @return the HSSFSheet that owns this row
    191191     */
     192    @Override
    192193    public HSSFSheet getSheet()
    193194    {
    194195        return sheet;
    195196    }
    196 
    197197
    198198    /**
     
    240240    }
    241241
    242 
    243242    /**
    244243     * Get the hssfcell representing a given column (logical cell)
     
    250249     * @return HSSFCell representing that column or null if undefined.
    251250     */
     251    @Override
    252252    public HSSFCell getCell(int cellnum) {
    253253        return getCell(cellnum, book.getMissingCellPolicy());
     
    263263     * @return representing that column or null if undefined + policy allows.
    264264     */
     265    @Override
    265266    public HSSFCell getCell(int cellnum, MissingCellPolicy policy) {
    266267        HSSFCell cell = retrieveCell(cellnum);
     
    285286
    286287    /**
     288     * Gets the index of the last cell contained in this row <b>PLUS ONE</b>. The result also
     289     * happens to be the 1-based column number of the last cell.  This value can be used as a
     290     * standard upper bound when iterating over cells:
     291     * <pre>
     292     * short minColIx = row.getFirstCellNum();
     293     * short maxColIx = row.getLastCellNum();
     294     * for(short colIx=minColIx; colIx&lt;maxColIx; colIx++) {
     295     *   HSSFCell cell = row.getCell(colIx);
     296     *   if(cell == null) {
     297     *     continue;
     298     *   }
     299     *   //... do something with cell
     300     * }
     301     * </pre>
     302     *
     303     * @return short representing the last logical cell in the row <b>PLUS ONE</b>, or -1 if the
     304     *  row does not contain any cells.
     305     */
     306    @Override
     307    public short getLastCellNum() {
     308        if (row.isEmpty()) {
     309            return -1;
     310        }
     311        return (short) row.getLastCol();
     312    }
     313
     314    /**
    287315     * get the lowlevel RowRecord represented by this object - should only be called
    288316     * by other parts of the high level API
     
    290318     * @return RowRecord this row represents
    291319     */
    292 
    293320    protected RowRecord getRowRecord()
    294321    {
     
    296323    }
    297324
    298     /**
    299      * @return cell iterator of the physically defined cells.
    300      * Note that the 4th element might well not be cell 4, as the iterator
    301      *  will not return un-defined (null) cells.
    302      * Call getCellNum() on the returned cells to know which cell they are.
    303      * As this only ever works on physically defined cells,
    304      *  the {@link org.apache.poi.ss.usermodel.Row.MissingCellPolicy} has no effect.
    305      */
    306     public Iterator<Cell> cellIterator()
    307     {
    308       return new CellIterator();
    309     }
    310     /**
    311      * Alias for {@link #cellIterator} to allow
    312      *  foreach loops
    313      */
    314     public Iterator<Cell> iterator() {
    315        return cellIterator();
    316     }
    317 
    318     /**
    319      * An iterator over the (physical) cells in the row.
    320      */
    321     private class CellIterator implements Iterator<Cell> {
    322       int thisId=-1;
    323       int nextId=-1;
    324 
    325       public CellIterator()
    326       {
    327         findNext();
    328       }
    329 
    330       public boolean hasNext() {
    331         return nextId<cells.length;
    332       }
    333 
    334       public Cell next() {
    335           if (!hasNext())
    336               throw new NoSuchElementException("At last element");
    337         HSSFCell cell=cells[nextId];
    338         thisId=nextId;
    339         findNext();
    340         return cell;
    341       }
    342 
    343       public void remove() {
    344           if (thisId == -1)
    345               throw new IllegalStateException("remove() called before next()");
    346         cells[thisId]=null;
    347       }
    348 
    349       private void findNext()
    350       {
    351         int i=nextId+1;
    352         for(;i<cells.length;i++)
    353         {
    354           if(cells[i]!=null) break;
    355         }
    356         nextId=i;
    357       }
    358 
    359     }
    360 
    361 
     325    @Override
    362326    public boolean equals(Object obj)
    363327    {
  • applications/editors/josm/plugins/opendata/includes/org/apache/poi/ss/usermodel/Row.java

    r28000 r34072  
    1818package org.apache.poi.ss.usermodel;
    1919
    20 import java.util.Iterator;
    21 
    2220/**
    2321 * High level representation of a row of a spreadsheet.
    2422 */
    25 public interface Row extends Iterable<Cell> {
     23public interface Row {
    2624
    2725    /**
     
    7573     */
    7674    Cell getCell(int cellnum);
    77    
     75
    7876    /**
    7977     * Returns the cell at the given (0 based) index, with the specified {@link org.apache.poi.ss.usermodel.Row.MissingCellPolicy}
     
    8886
    8987    /**
    90      * @return Cell iterator of the physically defined cells.  Note element 4 may
    91      * actually be row cell depending on how many are defined!
     88     * Gets the index of the last cell contained in this row <b>PLUS ONE</b>. The result also
     89     * happens to be the 1-based column number of the last cell.  This value can be used as a
     90     * standard upper bound when iterating over cells:
     91     * <pre>
     92     * short minColIx = row.getFirstCellNum();
     93     * short maxColIx = row.getLastCellNum();
     94     * for(short colIx=minColIx; colIx&lt;maxColIx; colIx++) {
     95     *   Cell cell = row.getCell(colIx);
     96     *   if(cell == null) {
     97     *     continue;
     98     *   }
     99     *   //... do something with cell
     100     * }
     101     * </pre>
     102     *
     103     * @return short representing the last logical cell in the row <b>PLUS ONE</b>,
     104     *   or -1 if the row does not contain any cells.
    92105     */
    93     Iterator<Cell> cellIterator();
     106    short getLastCellNum();
    94107
    95108    /**
Note: See TracChangeset for help on using the changeset viewer.