From 7c160ea77d7e4fed37d74e05c7c1d547c5c9af9e Mon Sep 17 00:00:00 2001 From: Joerg Pietschmann Date: Sat, 20 Jul 2002 12:50:00 +0000 Subject: [PATCH] Better validity checking for table FOs. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/fop-0_20_2-maintain@195013 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/fop/fo/flow/AbstractTableBody.java | 259 ++++++++++++++++++ src/org/apache/fop/fo/flow/TableBody.java | 229 +--------------- src/org/apache/fop/fo/flow/TableCell.java | 7 +- src/org/apache/fop/fo/flow/TableColumn.java | 7 +- src/org/apache/fop/fo/flow/TableRow.java | 8 +- 5 files changed, 279 insertions(+), 231 deletions(-) create mode 100644 src/org/apache/fop/fo/flow/AbstractTableBody.java diff --git a/src/org/apache/fop/fo/flow/AbstractTableBody.java b/src/org/apache/fop/fo/flow/AbstractTableBody.java new file mode 100644 index 000000000..c814730d5 --- /dev/null +++ b/src/org/apache/fop/fo/flow/AbstractTableBody.java @@ -0,0 +1,259 @@ +/* + * $Id$ + * Copyright (C) 2001 The Apache Software Foundation. All rights reserved. + * For details on use and redistribution please refer to the + * LICENSE file included with these sources. + */ + +package org.apache.fop.fo.flow; + +// FOP +import org.apache.fop.fo.*; +import org.apache.fop.fo.properties.*; +import org.apache.fop.datatypes.*; +import org.apache.fop.layout.*; +import org.apache.fop.apps.FOPException; + +// Java +import java.util.Vector; +import java.util.Enumeration; + +public abstract class AbstractTableBody extends FObj { + + int spaceBefore; + int spaceAfter; + String id; + + Vector columns; + RowSpanMgr rowSpanMgr; // manage information about spanning rows + + AreaContainer areaContainer; + + public AbstractTableBody(FObj parent, PropertyList propertyList) + throws FOPException { + super(parent, propertyList); + if (!(parent instanceof Table)) { + throw new FOPException("A table body must be child of fo:table," + + " not " + parent.getName()); + } + } + + public void setColumns(Vector columns) { + this.columns = columns; + } + + public void setYPosition(int value) { + areaContainer.setYPosition(value); + } + + public int getYPosition() { + return areaContainer.getCurrentYPosition(); + } + + public int getHeight() { + return areaContainer.getHeight() + spaceBefore + spaceAfter; + } + + public Status layout(Area area) throws FOPException { + if (this.marker == BREAK_AFTER) { + return new Status(Status.OK); + } + + if (this.marker == START) { + + // Common Accessibility Properties + AccessibilityProps mAccProps = propMgr.getAccessibilityProps(); + + // Common Aural Properties + AuralProps mAurProps = propMgr.getAuralProps(); + + // Common Border, Padding, and Background Properties + BorderAndPadding bap = propMgr.getBorderAndPadding(); + BackgroundProps bProps = propMgr.getBackgroundProps(); + + // Common Relative Position Properties + RelativePositionProps mRelProps = propMgr.getRelativePositionProps(); + + // this.properties.get("id"); + + this.spaceBefore = + this.properties.get("space-before.optimum").getLength().mvalue(); + this.spaceAfter = + this.properties.get("space-after.optimum").getLength().mvalue(); + this.id = this.properties.get("id").getString(); + + area.getIDReferences().createID(id); + + if (area instanceof BlockArea) { + area.end(); + } + + if (rowSpanMgr == null) { + rowSpanMgr = new RowSpanMgr(columns.size()); + } + + // if (this.isInListBody) { + // startIndent += bodyIndent + distanceBetweenStarts; + // } + + this.marker = 0; + + } + + if ((spaceBefore != 0) && (this.marker == 0)) { + area.increaseHeight(spaceBefore); + } + + if (marker == 0) { + // configure id + area.getIDReferences().configureID(id, area); + } + + int spaceLeft = area.spaceLeft(); + + /* + * Note: the parent FO must be a Table. The parent Area is the Block + * type area created by the Table, which is also a reference area. + * The content "width" (IPD) of the TableBody is the same as that + * of the containing table area, and its relative position is 0,0. + * Strictly speaking (CR), this FO should generate no areas! + */ + this.areaContainer = + new AreaContainer(propMgr.getFontState(area.getFontInfo()), 0, + area.getContentHeight(), + area.getContentWidth(), // IPD + area.spaceLeft(), Position.RELATIVE); + areaContainer.foCreator = this; // G Seshadri + areaContainer.setPage(area.getPage()); + areaContainer.setBackground(propMgr.getBackgroundProps()); + areaContainer.setBorderAndPadding(propMgr.getBorderAndPadding()); + areaContainer.start(); + + areaContainer.setAbsoluteHeight(area.getAbsoluteHeight()); + areaContainer.setIDReferences(area.getIDReferences()); + + Vector keepWith = new Vector(); + int numChildren = this.children.size(); + TableRow lastRow = null; + boolean endKeepGroup = true; + for (int i = this.marker; i < numChildren; i++) { + Object child = children.elementAt(i); + if (!(child instanceof TableRow)) { + throw new FOPException("Currently only Table Rows are supported in table body, header and footer"); + } + TableRow row = (TableRow)child; + + row.setRowSpanMgr(rowSpanMgr); + row.setColumns(columns); + row.doSetup(areaContainer); + if (row.getKeepWithPrevious().getType() + != KeepValue.KEEP_WITH_AUTO && lastRow != null + && keepWith.indexOf(lastRow) + == -1) { + keepWith.addElement(lastRow); + } else { + if (endKeepGroup && keepWith.size() > 0) { + keepWith = new Vector(); + } + } + + Status status; + if ((status = row.layout(areaContainer)).isIncomplete()) { + // BUG!!! don't distinguish between break-before and after! + if (status.isPageBreak()) { + this.marker = i; + area.addChild(areaContainer); + // areaContainer.end(); + + area.increaseHeight(areaContainer.getHeight()); + area.setAbsoluteHeight(areaContainer.getAbsoluteHeight()); + if (i == numChildren - 1) { + this.marker = BREAK_AFTER; + if (spaceAfter != 0) { + area.increaseHeight(spaceAfter); + } + } + return status; + } + if ((keepWith.size() > 0) + && (!rowSpanMgr.ignoreKeeps())) { + // && status.getCode() == Status.AREA_FULL_NONE + // FIXME!!! Handle rows spans!!! + row.removeLayout(areaContainer); + for (Enumeration e = keepWith.elements(); + e.hasMoreElements(); ) { + TableRow tr = (TableRow)e.nextElement(); + tr.removeLayout(areaContainer); + i--; + } + if (i == 0) { + resetMarker(); + + // Fix for infinite loop bug if keeps are too big for page + rowSpanMgr.setIgnoreKeeps(true); + + return new Status(Status.AREA_FULL_NONE); + } + } + this.marker = i; + if ((i != 0) && (status.getCode() == Status.AREA_FULL_NONE)) { + status = new Status(Status.AREA_FULL_SOME); + } + if (!((i == 0) && (areaContainer.getContentHeight() <= 0))) { + area.addChild(areaContainer); + // areaContainer.end(); + + area.increaseHeight(areaContainer.getHeight()); + area.setAbsoluteHeight(areaContainer.getAbsoluteHeight()); + } + + // Fix for infinite loop bug if spanned rows are too big for page + rowSpanMgr.setIgnoreKeeps(true); + + return status; + } else if (status.getCode() == Status.KEEP_WITH_NEXT + || rowSpanMgr.hasUnfinishedSpans()) { + keepWith.addElement(row); + endKeepGroup = false; + } else { + endKeepGroup = true; + } + lastRow = row; + area.setMaxHeight(area.getMaxHeight() - spaceLeft + + this.areaContainer.getMaxHeight()); + spaceLeft = area.spaceLeft(); + } + area.addChild(areaContainer); + areaContainer.end(); + + area.increaseHeight(areaContainer.getHeight()); + + area.setAbsoluteHeight(areaContainer.getAbsoluteHeight()); + + if (spaceAfter != 0) { + area.increaseHeight(spaceAfter); + area.setMaxHeight(area.getMaxHeight() - spaceAfter); + } + + if (area instanceof BlockArea) { + area.start(); + } + + return new Status(Status.OK); + } + + public void removeLayout(Area area) { + if (areaContainer != null) { + area.removeChild(areaContainer); + } + if (spaceBefore != 0) { + area.increaseHeight(-spaceBefore); + } + if (spaceAfter != 0) { + area.increaseHeight(-spaceAfter); + } + this.resetMarker(); + this.removeID(area.getIDReferences()); + } + +} diff --git a/src/org/apache/fop/fo/flow/TableBody.java b/src/org/apache/fop/fo/flow/TableBody.java index 10dba40c8..f153bfe7b 100644 --- a/src/org/apache/fop/fo/flow/TableBody.java +++ b/src/org/apache/fop/fo/flow/TableBody.java @@ -18,7 +18,7 @@ import org.apache.fop.apps.FOPException; import java.util.Vector; import java.util.Enumeration; -public class TableBody extends FObj { +public class TableBody extends AbstractTableBody { public static class Maker extends FObj.Maker { public FObj make(FObj parent, @@ -32,236 +32,9 @@ public class TableBody extends FObj { return new TableBody.Maker(); } - int spaceBefore; - int spaceAfter; - String id; - - Vector columns; - RowSpanMgr rowSpanMgr; // manage information about spanning rows - - AreaContainer areaContainer; - public TableBody(FObj parent, PropertyList propertyList) { super(parent, propertyList); this.name = "fo:table-body"; } - public void setColumns(Vector columns) { - this.columns = columns; - } - - public void setYPosition(int value) { - areaContainer.setYPosition(value); - } - - public int getYPosition() { - return areaContainer.getCurrentYPosition(); - } - - public int getHeight() { - return areaContainer.getHeight() + spaceBefore + spaceAfter; - } - - public Status layout(Area area) throws FOPException { - if (this.marker == BREAK_AFTER) { - return new Status(Status.OK); - } - - if (this.marker == START) { - - // Common Accessibility Properties - AccessibilityProps mAccProps = propMgr.getAccessibilityProps(); - - // Common Aural Properties - AuralProps mAurProps = propMgr.getAuralProps(); - - // Common Border, Padding, and Background Properties - BorderAndPadding bap = propMgr.getBorderAndPadding(); - BackgroundProps bProps = propMgr.getBackgroundProps(); - - // Common Relative Position Properties - RelativePositionProps mRelProps = propMgr.getRelativePositionProps(); - - // this.properties.get("id"); - - this.spaceBefore = - this.properties.get("space-before.optimum").getLength().mvalue(); - this.spaceAfter = - this.properties.get("space-after.optimum").getLength().mvalue(); - this.id = this.properties.get("id").getString(); - - area.getIDReferences().createID(id); - - if (area instanceof BlockArea) { - area.end(); - } - - if (rowSpanMgr == null) { - rowSpanMgr = new RowSpanMgr(columns.size()); - } - - // if (this.isInListBody) { - // startIndent += bodyIndent + distanceBetweenStarts; - // } - - this.marker = 0; - - } - - if ((spaceBefore != 0) && (this.marker == 0)) { - area.increaseHeight(spaceBefore); - } - - if (marker == 0) { - // configure id - area.getIDReferences().configureID(id, area); - } - - int spaceLeft = area.spaceLeft(); - - /* - * Note: the parent FO must be a Table. The parent Area is the Block - * type area created by the Table, which is also a reference area. - * The content "width" (IPD) of the TableBody is the same as that - * of the containing table area, and its relative position is 0,0. - * Strictly speaking (CR), this FO should generate no areas! - */ - this.areaContainer = - new AreaContainer(propMgr.getFontState(area.getFontInfo()), 0, - area.getContentHeight(), - area.getContentWidth(), // IPD - area.spaceLeft(), Position.RELATIVE); - areaContainer.foCreator = this; // G Seshadri - areaContainer.setPage(area.getPage()); - areaContainer.setBackground(propMgr.getBackgroundProps()); - areaContainer.setBorderAndPadding(propMgr.getBorderAndPadding()); - areaContainer.start(); - - areaContainer.setAbsoluteHeight(area.getAbsoluteHeight()); - areaContainer.setIDReferences(area.getIDReferences()); - - Vector keepWith = new Vector(); - int numChildren = this.children.size(); - TableRow lastRow = null; - boolean endKeepGroup = true; - for (int i = this.marker; i < numChildren; i++) { - Object child = children.elementAt(i); - if (!(child instanceof TableRow)) { - throw new FOPException("Currently only Table Rows are supported in table body, header and footer"); - } - TableRow row = (TableRow)child; - - row.setRowSpanMgr(rowSpanMgr); - row.setColumns(columns); - row.doSetup(areaContainer); - if (row.getKeepWithPrevious().getType() - != KeepValue.KEEP_WITH_AUTO && lastRow != null - && keepWith.indexOf(lastRow) - == -1) { - keepWith.addElement(lastRow); - } else { - if (endKeepGroup && keepWith.size() > 0) { - keepWith = new Vector(); - } - } - - Status status; - if ((status = row.layout(areaContainer)).isIncomplete()) { - // BUG!!! don't distinguish between break-before and after! - if (status.isPageBreak()) { - this.marker = i; - area.addChild(areaContainer); - // areaContainer.end(); - - area.increaseHeight(areaContainer.getHeight()); - area.setAbsoluteHeight(areaContainer.getAbsoluteHeight()); - if (i == numChildren - 1) { - this.marker = BREAK_AFTER; - if (spaceAfter != 0) { - area.increaseHeight(spaceAfter); - } - } - return status; - } - if ((keepWith.size() > 0) - && (!rowSpanMgr.ignoreKeeps())) { - // && status.getCode() == Status.AREA_FULL_NONE - // FIXME!!! Handle rows spans!!! - row.removeLayout(areaContainer); - for (Enumeration e = keepWith.elements(); - e.hasMoreElements(); ) { - TableRow tr = (TableRow)e.nextElement(); - tr.removeLayout(areaContainer); - i--; - } - if (i == 0) { - resetMarker(); - - // Fix for infinite loop bug if keeps are too big for page - rowSpanMgr.setIgnoreKeeps(true); - - return new Status(Status.AREA_FULL_NONE); - } - } - this.marker = i; - if ((i != 0) && (status.getCode() == Status.AREA_FULL_NONE)) { - status = new Status(Status.AREA_FULL_SOME); - } - if (!((i == 0) && (areaContainer.getContentHeight() <= 0))) { - area.addChild(areaContainer); - // areaContainer.end(); - - area.increaseHeight(areaContainer.getHeight()); - area.setAbsoluteHeight(areaContainer.getAbsoluteHeight()); - } - - // Fix for infinite loop bug if spanned rows are too big for page - rowSpanMgr.setIgnoreKeeps(true); - - return status; - } else if (status.getCode() == Status.KEEP_WITH_NEXT - || rowSpanMgr.hasUnfinishedSpans()) { - keepWith.addElement(row); - endKeepGroup = false; - } else { - endKeepGroup = true; - } - lastRow = row; - area.setMaxHeight(area.getMaxHeight() - spaceLeft - + this.areaContainer.getMaxHeight()); - spaceLeft = area.spaceLeft(); - } - area.addChild(areaContainer); - areaContainer.end(); - - area.increaseHeight(areaContainer.getHeight()); - - area.setAbsoluteHeight(areaContainer.getAbsoluteHeight()); - - if (spaceAfter != 0) { - area.increaseHeight(spaceAfter); - area.setMaxHeight(area.getMaxHeight() - spaceAfter); - } - - if (area instanceof BlockArea) { - area.start(); - } - - return new Status(Status.OK); - } - - public void removeLayout(Area area) { - if (areaContainer != null) { - area.removeChild(areaContainer); - } - if (spaceBefore != 0) { - area.increaseHeight(-spaceBefore); - } - if (spaceAfter != 0) { - area.increaseHeight(-spaceAfter); - } - this.resetMarker(); - this.removeID(area.getIDReferences()); - } - } diff --git a/src/org/apache/fop/fo/flow/TableCell.java b/src/org/apache/fop/fo/flow/TableCell.java index ba728190f..83571ae4a 100644 --- a/src/org/apache/fop/fo/flow/TableCell.java +++ b/src/org/apache/fop/fo/flow/TableCell.java @@ -95,9 +95,14 @@ public class TableCell extends FObj { AreaContainer cellArea; - public TableCell(FObj parent, PropertyList propertyList) { + public TableCell(FObj parent, PropertyList propertyList) + throws FOPException { super(parent, propertyList); this.name = "fo:table-cell"; + if (!(parent instanceof TableRow)) { + throw new FOPException("A table cell must be child of fo:table-row," + + " not " + parent.getName()); + } doSetup(); // init some basic property values } diff --git a/src/org/apache/fop/fo/flow/TableColumn.java b/src/org/apache/fop/fo/flow/TableColumn.java index 11e2a5e3d..2a3b4a0e1 100644 --- a/src/org/apache/fop/fo/flow/TableColumn.java +++ b/src/org/apache/fop/fo/flow/TableColumn.java @@ -38,9 +38,14 @@ public class TableColumn extends FObj { return new TableColumn.Maker(); } - public TableColumn(FObj parent, PropertyList propertyList) { + public TableColumn(FObj parent, PropertyList propertyList) + throws FOPException { super(parent, propertyList); this.name = "fo:table-column"; + if (!(parent instanceof Table)) { + throw new FOPException("A table column must be child of fo:table, not " + + parent.getName()); + } } public Length getColumnWidthAsLength() { diff --git a/src/org/apache/fop/fo/flow/TableRow.java b/src/org/apache/fop/fo/flow/TableRow.java index 83762c8b9..2eb4e75bd 100644 --- a/src/org/apache/fop/fo/flow/TableRow.java +++ b/src/org/apache/fop/fo/flow/TableRow.java @@ -171,9 +171,15 @@ public class TableRow extends FObj { } - public TableRow(FObj parent, PropertyList propertyList) { + public TableRow(FObj parent, PropertyList propertyList) + throws FOPException { super(parent, propertyList); this.name = "fo:table-row"; + if (!(parent instanceof AbstractTableBody)) { + throw new FOPException("A table row must be child of fo:table-body," + + " fo:table-header or fo:table-footer, not " + + parent.getName()); + } } public void setColumns(Vector columns) { -- 2.39.5