Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

FromTableColumnFunction.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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.expr;
  19. import java.util.List;
  20. import org.apache.fop.fo.Constants;
  21. import org.apache.fop.fo.FOPropertyMapping;
  22. import org.apache.fop.fo.FObj;
  23. import org.apache.fop.fo.flow.table.ColumnNumberManager;
  24. import org.apache.fop.fo.flow.table.Table;
  25. import org.apache.fop.fo.flow.table.TableCell;
  26. import org.apache.fop.fo.flow.table.TableColumn;
  27. import org.apache.fop.fo.flow.table.TableFObj;
  28. import org.apache.fop.fo.properties.Property;
  29. /**
  30. * Class modelling the from-table-column Property Value function. See Sec.
  31. * 5.10.4 of the XSL-FO spec.
  32. */
  33. public class FromTableColumnFunction extends FunctionBase {
  34. /** {@inheritDoc} */
  35. public int getRequiredArgsCount() {
  36. return 0;
  37. }
  38. @Override
  39. /** {@inheritDoc} */
  40. public int getOptionalArgsCount() {
  41. return 1;
  42. }
  43. @Override
  44. /** {@inheritDoc} */
  45. public Property getOptionalArgDefault(int index, PropertyInfo pi) throws PropertyException {
  46. if ( index == 0 ) {
  47. return getPropertyName ( pi );
  48. } else {
  49. return super.getOptionalArgDefault ( index, pi );
  50. }
  51. }
  52. /** {@inheritDoc} */
  53. public Property eval(Property[] args, PropertyInfo pInfo) throws PropertyException {
  54. FObj fo = pInfo.getPropertyList().getFObj();
  55. /* obtain property Id for the property for which the function is being
  56. * evaluated */
  57. int propId = 0;
  58. if (args.length == 0) {
  59. propId = pInfo.getPropertyMaker().getPropId();
  60. } else {
  61. String propName = args[0].getString();
  62. propId = FOPropertyMapping.getPropertyId(propName);
  63. }
  64. /* make sure we have a correct property id ... */
  65. if (propId != -1) {
  66. /* obtain column number for which the function is being evaluated: */
  67. int columnNumber = -1;
  68. int span = 0;
  69. if (fo.getNameId() != Constants.FO_TABLE_CELL) {
  70. // climb up to the nearest cell
  71. do {
  72. fo = (FObj) fo.getParent();
  73. } while (fo.getNameId() != Constants.FO_TABLE_CELL
  74. && fo.getNameId() != Constants.FO_PAGE_SEQUENCE);
  75. if (fo.getNameId() == Constants.FO_TABLE_CELL) {
  76. //column-number is available on the cell
  77. columnNumber = ((TableCell) fo).getColumnNumber();
  78. span = ((TableCell) fo).getNumberColumnsSpanned();
  79. } else {
  80. //means no table-cell was found...
  81. throw new PropertyException("from-table-column() may only be used on "
  82. + "fo:table-cell or its descendants.");
  83. }
  84. } else {
  85. //column-number is only accurately available through the propertyList
  86. columnNumber = pInfo.getPropertyList().get(Constants.PR_COLUMN_NUMBER)
  87. .getNumeric().getValue();
  88. span = pInfo.getPropertyList().get(Constants.PR_NUMBER_COLUMNS_SPANNED)
  89. .getNumeric().getValue();
  90. }
  91. /* return the property from the column */
  92. Table t = ((TableFObj) fo).getTable();
  93. List cols = t.getColumns();
  94. ColumnNumberManager columnIndexManager = t.getColumnNumberManager();
  95. if (cols == null) {
  96. //no columns defined => no match: return default value
  97. return pInfo.getPropertyList().get(propId, false, true);
  98. } else {
  99. if (columnIndexManager.isColumnNumberUsed(columnNumber)) {
  100. //easiest case: exact match
  101. return ((TableColumn) cols.get(columnNumber - 1)).getProperty(propId);
  102. } else {
  103. //no exact match: try all spans...
  104. while (--span > 0 && !columnIndexManager.isColumnNumberUsed(++columnNumber)) {
  105. //nop: just increment/decrement
  106. }
  107. if (columnIndexManager.isColumnNumberUsed(columnNumber)) {
  108. return ((TableColumn) cols.get(columnNumber - 1)).getProperty(propId);
  109. } else {
  110. //no match: return default value
  111. return pInfo.getPropertyList().get(propId, false, true);
  112. }
  113. }
  114. }
  115. } else {
  116. throw new PropertyException("Incorrect parameter to from-table-column() function");
  117. }
  118. }
  119. }