You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TablePart.java 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id: TableBody.java 655614 2008-05-12 19:37:39Z vhennebert $ */
  18. package org.apache.fop.fo.flow.table;
  19. import java.util.ArrayList;
  20. import java.util.LinkedList;
  21. import java.util.List;
  22. import org.xml.sax.Attributes;
  23. import org.xml.sax.Locator;
  24. import org.apache.fop.apps.FOPException;
  25. import org.apache.fop.fo.FONode;
  26. import org.apache.fop.fo.PropertyList;
  27. import org.apache.fop.fo.ValidationException;
  28. import org.apache.fop.fo.properties.CommonBorderPaddingBackground;
  29. /**
  30. * An abstract base class modelling a TablePart
  31. * (i.e. fo:table-header, fo:table-footer and fo:table-body).
  32. */
  33. public abstract class TablePart extends TableCellContainer {
  34. // The value of properties relevant for fo:table-body.
  35. private CommonBorderPaddingBackground commonBorderPaddingBackground;
  36. // Unused but valid items, commented out for performance:
  37. // private CommonAccessibility commonAccessibility;
  38. // private CommonAural commonAural;
  39. // private CommonRelativePosition commonRelativePosition;
  40. // private int visibility;
  41. // End of property values
  42. /**
  43. * used for validation
  44. */
  45. protected boolean tableRowsFound = false;
  46. protected boolean tableCellsFound = false;
  47. private boolean firstRow = true;
  48. private boolean rowsStarted = false;
  49. private boolean lastCellEndsRow = true;
  50. private List rowGroups = new LinkedList();
  51. /**
  52. * Create a TablePart instance with the given {@link FONode}
  53. * as parent.
  54. * @param parent FONode that is the parent of the object
  55. */
  56. public TablePart(FONode parent) {
  57. super(parent);
  58. }
  59. /** {@inheritDoc} */
  60. public void bind(PropertyList pList) throws FOPException {
  61. commonBorderPaddingBackground = pList.getBorderPaddingBackgroundProps();
  62. super.bind(pList);
  63. }
  64. /** {@inheritDoc} */
  65. public void processNode(String elementName, Locator locator,
  66. Attributes attlist, PropertyList pList)
  67. throws FOPException {
  68. super.processNode(elementName, locator, attlist, pList);
  69. if (!inMarker()) {
  70. Table t = getTable();
  71. if (t.hasExplicitColumns()) {
  72. int size = t.getNumberOfColumns();
  73. pendingSpans = new ArrayList(size);
  74. for (int i = 0; i < size; i++) {
  75. pendingSpans.add(null);
  76. }
  77. } else {
  78. pendingSpans = new ArrayList();
  79. }
  80. columnNumberManager = new ColumnNumberManager();
  81. }
  82. }
  83. /** {@inheritDoc} */
  84. public void finalizeNode() throws FOPException {
  85. if (!inMarker()) {
  86. pendingSpans = null;
  87. columnNumberManager = null;
  88. }
  89. if (!(tableRowsFound || tableCellsFound)) {
  90. missingChildElementError("marker* (table-row+|table-cell+)", true);
  91. getParent().removeChild(this);
  92. } else {
  93. finishLastRowGroup();
  94. }
  95. }
  96. /** {@inheritDoc} */
  97. TablePart getTablePart() {
  98. return this;
  99. }
  100. protected void finishLastRowGroup() throws ValidationException {
  101. if (!inMarker()) {
  102. RowGroupBuilder rowGroupBuilder = getTable().getRowGroupBuilder();
  103. if (tableRowsFound) {
  104. rowGroupBuilder.endTableRow();
  105. } else if (!lastCellEndsRow) {
  106. rowGroupBuilder.endRow(this);
  107. }
  108. try {
  109. rowGroupBuilder.endTablePart();
  110. } catch (ValidationException e) {
  111. e.setLocator(locator);
  112. throw e;
  113. }
  114. }
  115. }
  116. /**
  117. * {@inheritDoc}
  118. * <br>XSL Content Model: marker* (table-row+|table-cell+)
  119. */
  120. protected void validateChildNode(Locator loc, String nsURI, String localName)
  121. throws ValidationException {
  122. if (FO_URI.equals(nsURI)) {
  123. if (localName.equals("marker")) {
  124. if (tableRowsFound || tableCellsFound) {
  125. nodesOutOfOrderError(loc, "fo:marker", "(table-row+|table-cell+)");
  126. }
  127. } else if (localName.equals("table-row")) {
  128. tableRowsFound = true;
  129. if (tableCellsFound) {
  130. TableEventProducer eventProducer = TableEventProducer.Provider.get(
  131. getUserAgent().getEventBroadcaster());
  132. eventProducer.noMixRowsAndCells(this, getName(), getLocator());
  133. }
  134. } else if (localName.equals("table-cell")) {
  135. tableCellsFound = true;
  136. if (tableRowsFound) {
  137. TableEventProducer eventProducer = TableEventProducer.Provider.get(
  138. getUserAgent().getEventBroadcaster());
  139. eventProducer.noMixRowsAndCells(this, getName(), getLocator());
  140. }
  141. } else {
  142. invalidChildError(loc, nsURI, localName);
  143. }
  144. }
  145. }
  146. /** {@inheritDoc} */
  147. protected void addChildNode(FONode child) throws FOPException {
  148. if (!inMarker()) {
  149. switch (child.getNameId()) {
  150. case FO_TABLE_ROW:
  151. if (!rowsStarted) {
  152. getTable().getRowGroupBuilder().startTablePart(this);
  153. } else {
  154. columnNumberManager.prepareForNextRow(pendingSpans);
  155. getTable().getRowGroupBuilder().endTableRow();
  156. }
  157. rowsStarted = true;
  158. getTable().getRowGroupBuilder().startTableRow((TableRow)child);
  159. break;
  160. case FO_TABLE_CELL:
  161. if (!rowsStarted) {
  162. getTable().getRowGroupBuilder().startTablePart(this);
  163. }
  164. rowsStarted = true;
  165. TableCell cell = (TableCell) child;
  166. addTableCellChild(cell, firstRow);
  167. lastCellEndsRow = cell.endsRow();
  168. if (lastCellEndsRow) {
  169. firstRow = false;
  170. columnNumberManager.prepareForNextRow(pendingSpans);
  171. getTable().getRowGroupBuilder().endRow(this);
  172. }
  173. break;
  174. default:
  175. //nop
  176. }
  177. }
  178. super.addChildNode(child);
  179. }
  180. void addRowGroup(List rowGroup) {
  181. rowGroups.add(rowGroup);
  182. }
  183. public List getRowGroups() {
  184. return rowGroups;
  185. }
  186. /**
  187. * Get the {@link CommonBorderPaddingBackground} instance attached
  188. * to this TableBody.
  189. * @return the {@link CommonBorderPaddingBackground} instance.
  190. */
  191. public CommonBorderPaddingBackground getCommonBorderPaddingBackground() {
  192. return commonBorderPaddingBackground;
  193. }
  194. /**
  195. * @param obj table row in question
  196. * @return true if the given table row is the first row of this body.
  197. */
  198. public boolean isFirst(TableRow obj) {
  199. return (firstChild == null
  200. || firstChild == obj);
  201. }
  202. void signalNewRow() {
  203. if (rowsStarted) {
  204. firstRow = false;
  205. if (!lastCellEndsRow) {
  206. columnNumberManager.prepareForNextRow(pendingSpans);
  207. getTable().getRowGroupBuilder().endRow(this);
  208. }
  209. }
  210. }
  211. }