]> source.dussan.org Git - xmlgraphics-fop.git/commitdiff
Fix for NPE in Cell.java (getBorders() can return null).
authorJeremias Maerki <jeremias@apache.org>
Thu, 28 Apr 2005 13:44:01 +0000 (13:44 +0000)
committerJeremias Maerki <jeremias@apache.org>
Thu, 28 Apr 2005 13:44:01 +0000 (13:44 +0000)
Added fetching of row groups as per definition in http://wiki.apache.org/xmlgraphics-fop/TableLayout/KnuthElementsForTables.

git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/Temp_KnuthStylePageBreaking@198596 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/fop/layoutmgr/table/Cell.java
src/java/org/apache/fop/layoutmgr/table/GridUnit.java
src/java/org/apache/fop/layoutmgr/table/TableContentLayoutManager.java
src/java/org/apache/fop/layoutmgr/table/TableRowIterator.java

index 7419ba1e6a04e292dfd59660c41bdd45b539d61a..c875e350595ef307d3efe179818c383539844553 100644 (file)
@@ -434,7 +434,7 @@ public class Cell extends BlockStackingLayoutManager implements BlockLevelLayout
                     int lastRowHeight = 0;
                     for (int x = 0; x < gridUnits.length; x++) {
                         GridUnit gu = gridUnits[x];
-                        if (!gu.getBorders().hasBorder()) {
+                        if (!gu.hasBorders()) {
                             continue;
                         }
                         
index 034c3304c7e03a08a13a30867b519dcbc22120c1..cc9ed9b36a18f7aeea46adffed74e9e2eb5d7a00 100644 (file)
@@ -153,6 +153,13 @@ public class GridUnit {
         return this.effBorders;
     }
     
+    /**
+     * @return true if the grid unit has any borders.
+     */
+    public boolean hasBorders() {
+        return (getBorders() != null) && getBorders().hasBorder();
+    }
+    
     /**
      * Assigns the borders from the given cell to this cell info. Used in
      * case of separate border model.
index 2a08b38092bc590e7297d6e83e71e4c073776888..b5df25ab42e7e078d7f65a8c26cc506e011cdee1 100644 (file)
@@ -67,10 +67,12 @@ public class TableContentLayoutManager {
         Table table = getTableLM().getTable();
         this.trIter = new TableRowIterator(table, getTableLM().getColumns(), TableRowIterator.BODY);
         if (table.getTableHeader() != null) {
-            headerIter = new TableRowIterator(table, getTableLM().getColumns(), TableRowIterator.HEADER);
+            headerIter = new TableRowIterator(table, 
+                    getTableLM().getColumns(), TableRowIterator.HEADER);
         }
         if (table.getTableFooter() != null) {
-            footerIter = new TableRowIterator(table, getTableLM().getColumns(), TableRowIterator.FOOTER);
+            footerIter = new TableRowIterator(table, 
+                    getTableLM().getColumns(), TableRowIterator.FOOTER);
         }
     }
     
@@ -86,7 +88,7 @@ public class TableContentLayoutManager {
      * @see org.apache.fop.layoutmgr.LayoutManager#getNextKnuthElements(org.apache.fop.layoutmgr.LayoutContext, int)
      */
     public LinkedList getNextKnuthElements(LayoutContext context, int alignment) {
-        log.debug("Columns: " +getTableLM().getColumns());
+        log.debug("Columns: " + getTableLM().getColumns());
         KnuthBox headerAsFirst = null;
         KnuthBox headerAsSecondToLast = null;
         KnuthBox footerAsLast = null;
@@ -164,82 +166,87 @@ public class TableContentLayoutManager {
     
     /**
      * Creates Knuth elements by iterating over a TableRowIterator.
-     * @param context
-     * @param alignment
-     * @return
+     * @param iter TableRowIterator instance to fetch rows from
+     * @param context Active LayoutContext
+     * @param alignment alignment indicator
+     * @return An element list
      */
     private LinkedList getKnuthElementsForRowIterator(TableRowIterator iter, 
             LayoutContext context, int alignment, boolean disableHeaderFooter) {
         LinkedList returnList = new LinkedList();
+        TableRowIterator.EffRow[] rowGroup = null;
         TableRowIterator.EffRow row = null;
-        while ((row = iter.getNextRow()) != null) {
-            List pgus = new java.util.ArrayList();
-            TableRow tableRow = null;
-            int maxCellHeight = 0;
-            for (int j = 0; j < row.getGridUnits().size(); j++) {
-                GridUnit gu = (GridUnit)row.getGridUnits().get(j);
-                if (gu.isPrimary() && !gu.isEmpty()) {
-                    PrimaryGridUnit primary = (PrimaryGridUnit)gu;
-                    primary.getCellLM().setParent(tableLM);
+        while ((rowGroup = iter.getNextRowGroup()) != null) {
+            for (int rgi = 0; rgi < rowGroup.length; rgi++) {
+                row = rowGroup[rgi];
+                List pgus = new java.util.ArrayList();
+                TableRow tableRow = null;
+                int maxCellHeight = 0;
+                for (int j = 0; j < row.getGridUnits().size(); j++) {
+                    GridUnit gu = (GridUnit)row.getGridUnits().get(j);
+                    if (gu.isPrimary() && !gu.isEmpty()) {
+                        PrimaryGridUnit primary = (PrimaryGridUnit)gu;
+                        primary.getCellLM().setParent(tableLM);
 
-                    //Calculate width of cell
-                    int spanWidth = 0;
-                    for (int i = primary.getStartCol(); 
-                            i < primary.getStartCol() + primary.getCell().getNumberColumnsSpanned();
-                            i++) {
-                        spanWidth += getTableLM().getColumns().getColumn(i + 1)
-                            .getColumnWidth().getValue();
-                    }
-                    log.info("spanWidth=" + spanWidth);
-                    LayoutContext childLC = new LayoutContext(0);
-                    childLC.setStackLimit(context.getStackLimit()); //necessary?
-                    childLC.setRefIPD(spanWidth);
-                    
-                    LinkedList elems = primary.getCellLM().getNextKnuthElements(childLC, alignment);
-                    primary.setElements(elems);
-                    log.debug("Elements: " + elems);
-                    int len = calcCellHeightFromContents(elems);
-                    pgus.add(primary);
-                    maxCellHeight = Math.max(maxCellHeight, len);
-                    if (len > row.getHeight().opt) {
-                        row.setHeight(new MinOptMax(len));
+                        //Calculate width of cell
+                        int spanWidth = 0;
+                        for (int i = primary.getStartCol(); 
+                                i < primary.getStartCol() + primary.getCell().getNumberColumnsSpanned();
+                                i++) {
+                            spanWidth += getTableLM().getColumns().getColumn(i + 1)
+                                .getColumnWidth().getValue();
+                        }
+                        log.info("spanWidth=" + spanWidth);
+                        LayoutContext childLC = new LayoutContext(0);
+                        childLC.setStackLimit(context.getStackLimit()); //necessary?
+                        childLC.setRefIPD(spanWidth);
+                        
+                        LinkedList elems = primary.getCellLM().getNextKnuthElements(childLC, alignment);
+                        primary.setElements(elems);
+                        log.debug("Elements: " + elems);
+                        int len = calcCellHeightFromContents(elems);
+                        pgus.add(primary);
+                        maxCellHeight = Math.max(maxCellHeight, len);
+                        if (len > row.getHeight().opt) {
+                            row.setHeight(new MinOptMax(len));
+                        }
+                        LengthRangeProperty bpd = primary.getCell().getBlockProgressionDimension();
+                        if (!bpd.getOptimum().isAuto()) {
+                            if (bpd.getOptimum().getLength().getValue() > row.getHeight().opt) {
+                                row.setHeight(new MinOptMax(bpd.getOptimum().getLength().getValue()));
+                            }
+                        }
+                        if (tableRow == null) {
+                            tableRow = primary.getRow();
+                        }
                     }
-                    LengthRangeProperty bpd = primary.getCell().getBlockProgressionDimension();
+                }
+                
+                if (tableRow != null) {
+                    LengthRangeProperty bpd = tableRow.getBlockProgressionDimension();
                     if (!bpd.getOptimum().isAuto()) {
                         if (bpd.getOptimum().getLength().getValue() > row.getHeight().opt) {
                             row.setHeight(new MinOptMax(bpd.getOptimum().getLength().getValue()));
                         }
                     }
-                    if (tableRow == null) {
-                        tableRow = primary.getRow();
-                    }
                 }
-            }
-            
-            if (tableRow != null) {
-                LengthRangeProperty bpd = tableRow.getBlockProgressionDimension();
-                if (!bpd.getOptimum().isAuto()) {
-                    if (bpd.getOptimum().getLength().getValue() > row.getHeight().opt) {
-                        row.setHeight(new MinOptMax(bpd.getOptimum().getLength().getValue()));
-                    }
+                log.debug(row);
+                
+                PrimaryGridUnit[] pguArray = new PrimaryGridUnit[pgus.size()];
+                pguArray = (PrimaryGridUnit[])pgus.toArray(pguArray);
+                LinkedList returnedList = getCombinedKnuthElementsForRow(pguArray, row, 
+                        disableHeaderFooter);
+                if (returnedList != null) {
+                    returnList.addAll(returnedList);
                 }
-            }
-            log.debug(row);
-            
-            PrimaryGridUnit[] pguArray = new PrimaryGridUnit[pgus.size()];
-            pguArray = (PrimaryGridUnit[])pgus.toArray(pguArray);
-            LinkedList returnedList = getCombinedKnuthElementsForRow(pguArray, row, 
-                    disableHeaderFooter);
-            if (returnedList != null) {
-                returnList.addAll(returnedList);
-            }
 
-            if (row.getHeight().opt > maxCellHeight) {
-                int space = row.getHeight().opt - maxCellHeight;
-                KnuthPenalty penalty = (KnuthPenalty)returnList.removeLast();
-                //Insert dummy box before penalty
-                returnList.add(new KnuthBox(space, new Position(getTableLM()), false));
-                returnList.add(penalty);
+                if (row.getHeight().opt > maxCellHeight) {
+                    int space = row.getHeight().opt - maxCellHeight;
+                    KnuthPenalty penalty = (KnuthPenalty)returnList.removeLast();
+                    //Insert dummy box before penalty
+                    returnList.add(new KnuthBox(space, new Position(getTableLM()), false));
+                    returnList.add(penalty);
+                }
             }
         }
         
@@ -322,7 +329,7 @@ public class TableContentLayoutManager {
     
     private int getNextStep(int laststep, List[] elementLists, int[] index, 
             int[] start, int[] end, int[] widths, int[] fullWidths) {
-        int backupWidths[] = new int[start.length];
+        int[] backupWidths = new int[start.length];
         System.arraycopy(widths, 0, backupWidths, 0, backupWidths.length);
         //set starting points
         for (int i = 0; i < start.length; i++) {
index feeca595335c0a055bf104d277f61e21eb861901..728190820153b661cad9b8542583d415506b34d4 100644 (file)
@@ -18,6 +18,7 @@
 
 package org.apache.fop.layoutmgr.table;
 
+import java.util.Iterator;
 import java.util.List;
 import java.util.ListIterator;
 
@@ -32,7 +33,10 @@ import org.apache.fop.fo.properties.CommonBorderPaddingBackground;
 import org.apache.fop.traits.MinOptMax;
 
 /**
- * Iterator that lets the table layout manager step over all rows of a table.
+ * <p>Iterator that lets the table layout manager step over all rows of a table.
+ * </p>
+ * <p>Note: This class is not thread-safe.
+ * </p>
  */
 public class TableRowIterator {
 
@@ -46,6 +50,7 @@ public class TableRowIterator {
     /** Logger **/
     private static Log log = LogFactory.getLog(TableRowIterator.class);
 
+    /** The table on with this instance operates. */
     protected Table table;
     private ColumnSetup columns;
     
@@ -57,6 +62,7 @@ public class TableRowIterator {
     private int currentRowIndex = -1;
     //TODO rows should later be a Jakarta Commons LinkedList so concurrent modifications while 
     //using a ListIterator are possible
+    /** List of cache rows. */
     private List rows = new java.util.ArrayList();
     //private int indexOfFirstRowInList;
     private int currentIndex = -1;
@@ -93,6 +99,44 @@ public class TableRowIterator {
         }
     }
     
+    /**
+     * Returns the next row group if any. A row group in this context is the minimum number of 
+     * consecutive rows which contains all spanned grid units of its cells.
+     * @return the next row group, or null
+     */
+    public EffRow[] getNextRowGroup() {
+        EffRow firstRowInGroup = getNextRow();
+        if (firstRowInGroup == null) {
+            return null;
+        }
+        EffRow lastRowInGroup = firstRowInGroup;
+        int lastIndex = lastRowInGroup.getIndex();
+        boolean allFinished = true;
+        do {
+            Iterator iter = lastRowInGroup.getGridUnits().iterator();
+            while (iter.hasNext()) {
+                GridUnit gu = (GridUnit)iter.next();
+                if (!gu.isLastGridUnitRowSpan()) {
+                    allFinished = false;
+                    break;
+                }
+            }
+            if (!allFinished) {
+                lastIndex = lastRowInGroup.getIndex();
+                lastRowInGroup = getNextRow();
+                if (lastRowInGroup == null) {
+                    allFinished = true;
+                }
+            }
+        } while (!allFinished);
+        int rowCount = lastIndex - firstRowInGroup.getIndex() + 1;
+        EffRow[] rowGroup = new EffRow[rowCount];
+        for (int i = 0; i < rowCount; i++) {
+            rowGroup[i] = getCachedRow(i + firstRowInGroup.getIndex());
+        }
+        return rowGroup;
+    }
+    
     public EffRow getNextRow() {
         currentIndex++;
         boolean moreRows = true;
@@ -375,5 +419,5 @@ public class TableRowIterator {
             return sb.toString();
         }
     }
-    
+
 }