Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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.apache.fop.apps.FOPException;
  23. import org.apache.fop.fo.FONode;
  24. import org.apache.fop.fo.PropertyList;
  25. import org.apache.fop.fo.ValidationException;
  26. import org.apache.fop.fo.properties.CommonBorderPaddingBackground;
  27. import org.xml.sax.Attributes;
  28. import org.xml.sax.Locator;
  29. /**
  30. * Class modelling the fo:table-body object.
  31. */
  32. public class TableBody extends TableCellContainer {
  33. // The value of properties relevant for fo:table-body.
  34. private CommonBorderPaddingBackground commonBorderPaddingBackground;
  35. // Unused but valid items, commented out for performance:
  36. // private CommonAccessibility commonAccessibility;
  37. // private CommonAural commonAural;
  38. // private CommonRelativePosition commonRelativePosition;
  39. // private int visibility;
  40. // End of property values
  41. /**
  42. * used for validation
  43. */
  44. protected boolean tableRowsFound = false;
  45. protected boolean tableCellsFound = false;
  46. private boolean firstRow = true;
  47. private boolean rowsStarted = false;
  48. private boolean lastCellEndsRow = true;
  49. /** The last encountered table-row. */
  50. private TableRow lastRow;
  51. private List rowGroups = new LinkedList();
  52. /**
  53. * @param parent FONode that is the parent of the object
  54. */
  55. public TableBody(FONode parent) {
  56. super(parent);
  57. }
  58. /**
  59. * {@inheritDoc}
  60. */
  61. public void bind(PropertyList pList) throws FOPException {
  62. commonBorderPaddingBackground = pList.getBorderPaddingBackgroundProps();
  63. super.bind(pList);
  64. }
  65. /**
  66. * {@inheritDoc}
  67. */
  68. public void processNode(String elementName, Locator locator,
  69. Attributes attlist, PropertyList pList)
  70. throws FOPException {
  71. if (!inMarker()) {
  72. Table t = getTable();
  73. if (t.hasExplicitColumns()) {
  74. int size = t.getNumberOfColumns();
  75. pendingSpans = new ArrayList(size);
  76. for (int i = 0; i < size; i++) {
  77. pendingSpans.add(null);
  78. }
  79. } else {
  80. pendingSpans = new ArrayList();
  81. }
  82. columnNumberManager = new ColumnNumberManager();
  83. }
  84. super.processNode(elementName, locator, attlist, pList);
  85. }
  86. /**
  87. * {@inheritDoc}
  88. */
  89. public void startOfNode() throws FOPException {
  90. super.startOfNode();
  91. getFOEventHandler().startBody(this);
  92. }
  93. /**
  94. * {@inheritDoc}
  95. */
  96. public void endOfNode() throws FOPException {
  97. if (!inMarker()) {
  98. pendingSpans = null;
  99. columnNumberManager = null;
  100. }
  101. getFOEventHandler().endBody(this);
  102. if (!(tableRowsFound || tableCellsFound)) {
  103. if (getUserAgent().validateStrictly()) {
  104. missingChildElementError("marker* (table-row+|table-cell+)");
  105. } else {
  106. log.error("fo:table-body must not be empty. "
  107. + "Expected: marker* (table-row+|table-cell+)");
  108. getParent().removeChild(this);
  109. }
  110. } else {
  111. finishLastRowGroup();
  112. }
  113. }
  114. /** {@inheritDoc} */
  115. TableBody getTablePart() {
  116. return this;
  117. }
  118. protected void finishLastRowGroup() throws ValidationException {
  119. if (!inMarker()) {
  120. RowGroupBuilder rowGroupBuilder = getTable().getRowGroupBuilder();
  121. if (tableRowsFound) {
  122. rowGroupBuilder.endTableRow();
  123. } else if (!lastCellEndsRow) {
  124. rowGroupBuilder.endRow(this);
  125. }
  126. try {
  127. rowGroupBuilder.endTablePart();
  128. } catch (ValidationException e) {
  129. e.setLocator(locator);
  130. throw e;
  131. }
  132. }
  133. }
  134. /**
  135. * {@inheritDoc} String, String)
  136. * XSL Content Model: marker* (table-row+|table-cell+)
  137. */
  138. protected void validateChildNode(Locator loc, String nsURI, String localName)
  139. throws ValidationException {
  140. if (FO_URI.equals(nsURI)) {
  141. if (localName.equals("marker")) {
  142. if (tableRowsFound || tableCellsFound) {
  143. nodesOutOfOrderError(loc, "fo:marker", "(table-row+|table-cell+)");
  144. }
  145. } else if (localName.equals("table-row")) {
  146. tableRowsFound = true;
  147. if (tableCellsFound) {
  148. invalidChildError(loc, nsURI, localName, "Either fo:table-rows"
  149. + " or fo:table-cells may be children of an " + getName()
  150. + " but not both");
  151. }
  152. } else if (localName.equals("table-cell")) {
  153. tableCellsFound = true;
  154. if (tableRowsFound) {
  155. invalidChildError(loc, nsURI, localName,
  156. "Either fo:table-rows or fo:table-cells "
  157. + "may be children of an "
  158. + getName() + " but not both");
  159. }
  160. } else {
  161. invalidChildError(loc, nsURI, localName);
  162. }
  163. } else {
  164. invalidChildError(loc, nsURI, localName);
  165. }
  166. }
  167. /**
  168. * {@inheritDoc}
  169. */
  170. protected void addChildNode(FONode child) throws FOPException {
  171. if (!inMarker()) {
  172. switch (child.getNameId()) {
  173. case FO_TABLE_ROW:
  174. if (!rowsStarted) {
  175. getTable().getRowGroupBuilder().startTablePart(this);
  176. } else {
  177. columnNumberManager.prepareForNextRow(pendingSpans);
  178. getTable().getRowGroupBuilder().endTableRow();
  179. }
  180. rowsStarted = true;
  181. lastRow = (TableRow) child;
  182. getTable().getRowGroupBuilder().startTableRow(lastRow);
  183. break;
  184. case FO_TABLE_CELL:
  185. if (!rowsStarted) {
  186. getTable().getRowGroupBuilder().startTablePart(this);
  187. }
  188. rowsStarted = true;
  189. TableCell cell = (TableCell) child;
  190. addTableCellChild(cell, firstRow);
  191. lastCellEndsRow = cell.endsRow();
  192. if (lastCellEndsRow) {
  193. firstRow = false;
  194. columnNumberManager.prepareForNextRow(pendingSpans);
  195. getTable().getRowGroupBuilder().endRow(this);
  196. }
  197. break;
  198. default:
  199. //nop
  200. }
  201. }
  202. super.addChildNode(child);
  203. }
  204. /** {inheritDoc} */
  205. protected void setCollapsedBorders() {
  206. Table table = (Table) parent;
  207. createBorder(CommonBorderPaddingBackground.START, table);
  208. createBorder(CommonBorderPaddingBackground.END, table);
  209. createBorder(CommonBorderPaddingBackground.BEFORE);
  210. createBorder(CommonBorderPaddingBackground.AFTER);
  211. }
  212. void addRowGroup(List rowGroup) {
  213. rowGroups.add(rowGroup);
  214. }
  215. public List getRowGroups() {
  216. return rowGroups;
  217. }
  218. /**
  219. * @return the Common Border, Padding, and Background Properties.
  220. */
  221. public CommonBorderPaddingBackground getCommonBorderPaddingBackground() {
  222. return commonBorderPaddingBackground;
  223. }
  224. /** {@inheritDoc} */
  225. public String getLocalName() {
  226. return "table-body";
  227. }
  228. /**
  229. * {@inheritDoc}
  230. */
  231. public int getNameId() {
  232. return FO_TABLE_BODY;
  233. }
  234. protected boolean isTableHeader() {
  235. return false;
  236. }
  237. protected boolean isTableFooter() {
  238. return false;
  239. }
  240. /**
  241. * @param obj table row in question
  242. * @return true if the given table row is the first row of this body.
  243. */
  244. public boolean isFirst(TableRow obj) {
  245. return (firstChild == null
  246. || firstChild == obj);
  247. }
  248. void signalNewRow() {
  249. if (rowsStarted) {
  250. firstRow = false;
  251. if (!lastCellEndsRow) {
  252. columnNumberManager.prepareForNextRow(pendingSpans);
  253. getTable().getRowGroupBuilder().endRow(this);
  254. }
  255. }
  256. }
  257. }