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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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$ */
  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. protected Object clone() {
  61. TablePart clone = (TablePart) super.clone();
  62. clone.rowGroups = new LinkedList(rowGroups);
  63. return clone;
  64. }
  65. /** {@inheritDoc} */
  66. public void bind(PropertyList pList) throws FOPException {
  67. commonBorderPaddingBackground = pList.getBorderPaddingBackgroundProps();
  68. super.bind(pList);
  69. }
  70. /** {@inheritDoc} */
  71. public void processNode(String elementName, Locator locator,
  72. Attributes attlist, PropertyList pList)
  73. throws FOPException {
  74. super.processNode(elementName, locator, attlist, pList);
  75. if (!inMarker()) {
  76. Table t = getTable();
  77. if (t.hasExplicitColumns()) {
  78. int size = t.getNumberOfColumns();
  79. pendingSpans = new ArrayList(size);
  80. for (int i = 0; i < size; i++) {
  81. pendingSpans.add(null);
  82. }
  83. } else {
  84. pendingSpans = new ArrayList();
  85. }
  86. columnNumberManager = new ColumnNumberManager();
  87. }
  88. }
  89. /** {@inheritDoc} */
  90. public void finalizeNode() throws FOPException {
  91. if (!inMarker()) {
  92. pendingSpans = null;
  93. columnNumberManager = null;
  94. }
  95. if (!(tableRowsFound || tableCellsFound)) {
  96. missingChildElementError("marker* (table-row+|table-cell+)", true);
  97. getParent().removeChild(this);
  98. } else {
  99. finishLastRowGroup();
  100. }
  101. }
  102. /** {@inheritDoc} */
  103. TablePart getTablePart() {
  104. return this;
  105. }
  106. protected void finishLastRowGroup() throws ValidationException {
  107. if (!inMarker()) {
  108. RowGroupBuilder rowGroupBuilder = getTable().getRowGroupBuilder();
  109. if (tableRowsFound) {
  110. rowGroupBuilder.endTableRow();
  111. } else if (!lastCellEndsRow) {
  112. rowGroupBuilder.endRow(this);
  113. }
  114. try {
  115. rowGroupBuilder.endTablePart();
  116. } catch (ValidationException e) {
  117. e.setLocator(locator);
  118. throw e;
  119. }
  120. }
  121. }
  122. /**
  123. * {@inheritDoc}
  124. * <br>XSL Content Model: marker* (table-row+|table-cell+)
  125. */
  126. protected void validateChildNode(Locator loc, String nsURI, String localName)
  127. throws ValidationException {
  128. if (FO_URI.equals(nsURI)) {
  129. if (localName.equals("marker")) {
  130. if (tableRowsFound || tableCellsFound) {
  131. nodesOutOfOrderError(loc, "fo:marker", "(table-row+|table-cell+)");
  132. }
  133. } else if (localName.equals("table-row")) {
  134. tableRowsFound = true;
  135. if (tableCellsFound) {
  136. TableEventProducer eventProducer = TableEventProducer.Provider.get(
  137. getUserAgent().getEventBroadcaster());
  138. eventProducer.noMixRowsAndCells(this, getName(), getLocator());
  139. }
  140. } else if (localName.equals("table-cell")) {
  141. tableCellsFound = true;
  142. if (tableRowsFound) {
  143. TableEventProducer eventProducer = TableEventProducer.Provider.get(
  144. getUserAgent().getEventBroadcaster());
  145. eventProducer.noMixRowsAndCells(this, getName(), getLocator());
  146. }
  147. } else {
  148. invalidChildError(loc, nsURI, localName);
  149. }
  150. }
  151. }
  152. /** {@inheritDoc} */
  153. protected void addChildNode(FONode child) throws FOPException {
  154. if (!inMarker()) {
  155. switch (child.getNameId()) {
  156. case FO_TABLE_ROW:
  157. if (!rowsStarted) {
  158. getTable().getRowGroupBuilder().startTablePart(this);
  159. } else {
  160. columnNumberManager.prepareForNextRow(pendingSpans);
  161. getTable().getRowGroupBuilder().endTableRow();
  162. }
  163. rowsStarted = true;
  164. getTable().getRowGroupBuilder().startTableRow((TableRow)child);
  165. break;
  166. case FO_TABLE_CELL:
  167. if (!rowsStarted) {
  168. getTable().getRowGroupBuilder().startTablePart(this);
  169. }
  170. rowsStarted = true;
  171. TableCell cell = (TableCell) child;
  172. addTableCellChild(cell, firstRow);
  173. lastCellEndsRow = cell.endsRow();
  174. if (lastCellEndsRow) {
  175. firstRow = false;
  176. columnNumberManager.prepareForNextRow(pendingSpans);
  177. getTable().getRowGroupBuilder().endRow(this);
  178. }
  179. break;
  180. default:
  181. //nop
  182. }
  183. }
  184. //TODO: possible performance problems in case of large tables...
  185. //If the number of children grows significantly large, the default
  186. //implementation in FObj will get slower and slower...
  187. super.addChildNode(child);
  188. }
  189. void addRowGroup(List rowGroup) {
  190. rowGroups.add(rowGroup);
  191. }
  192. public List getRowGroups() {
  193. return rowGroups;
  194. }
  195. /**
  196. * Get the {@link CommonBorderPaddingBackground} instance attached
  197. * to this TableBody.
  198. * @return the {@link CommonBorderPaddingBackground} instance.
  199. */
  200. public CommonBorderPaddingBackground getCommonBorderPaddingBackground() {
  201. return commonBorderPaddingBackground;
  202. }
  203. /**
  204. * @param obj table row in question
  205. * @return true if the given table row is the first row of this body.
  206. */
  207. public boolean isFirst(TableRow obj) {
  208. return (firstChild == null
  209. || firstChild == obj);
  210. }
  211. void signalNewRow() {
  212. if (rowsStarted) {
  213. firstRow = false;
  214. if (!lastCellEndsRow) {
  215. columnNumberManager.prepareForNextRow(pendingSpans);
  216. getTable().getRowGroupBuilder().endRow(this);
  217. }
  218. }
  219. }
  220. }