001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /* $Id: FromTableColumnFunction.java 429168 2006-08-06 18:23:59Z adelmelle $ */
019:
020: package org.apache.fop.fo.expr;
021:
022: import java.util.List;
023: import org.apache.fop.fo.Constants;
024: import org.apache.fop.fo.FObj;
025: import org.apache.fop.fo.FOPropertyMapping;
026: import org.apache.fop.fo.flow.Table;
027: import org.apache.fop.fo.flow.TableCell;
028: import org.apache.fop.fo.flow.TableColumn;
029: import org.apache.fop.fo.flow.TableFObj;
030: import org.apache.fop.fo.properties.Property;
031:
032: /**
033: * Class modelling the from-table-column Property Value function. See Sec.
034: * 5.10.4 of the XSL-FO spec.
035: */
036: public class FromTableColumnFunction extends FunctionBase {
037:
038: /**
039: * @return 1 (maximum arguments for the from-table-column function)
040: */
041: public int nbArgs() {
042: return 1;
043: }
044:
045: /**
046: * @return true (allow padding of arglist with property name)
047: */
048: public boolean padArgsWithPropertyName() {
049: return true;
050: }
051:
052: /**
053: *
054: * @param args array of arguments, which should either be empty, or the
055: * first of which should contain an NCName corresponding to a property name
056: * @param pInfo PropertyInfo object to be evaluated
057: * @return the Property corresponding to the property name specified, or, if
058: * none, for the property for which the expression is being evaluated
059: * @throws PropertyException for incorrect arguments, and (for now) in all
060: * cases, because this function is not implemented
061: */
062: public Property eval(Property[] args, PropertyInfo pInfo)
063: throws PropertyException {
064:
065: FObj fo = pInfo.getPropertyList().getFObj();
066:
067: /* obtain property Id for the property for which the function is being
068: * evaluated */
069: int propId = 0;
070: if (args.length == 0) {
071: propId = pInfo.getPropertyMaker().getPropId();
072: } else {
073: String propName = args[0].getString();
074: propId = FOPropertyMapping.getPropertyId(propName);
075: }
076:
077: /* make sure we have a correct property id ... */
078: if (propId != -1) {
079: /* obtain column number for which the function is being evaluated: */
080: int columnNumber = -1;
081: int span = 0;
082: if (fo.getNameId() != Constants.FO_TABLE_CELL) {
083: // climb up to the nearest cell
084: do {
085: fo = (FObj) fo.getParent();
086: } while (fo.getNameId() != Constants.FO_TABLE_CELL
087: && fo.getNameId() != Constants.FO_PAGE_SEQUENCE);
088: if (fo.getNameId() == Constants.FO_TABLE_CELL) {
089: //column-number is available on the cell
090: columnNumber = ((TableCell) fo).getColumnNumber();
091: span = ((TableCell) fo).getNumberColumnsSpanned();
092: } else {
093: //means no table-cell was found...
094: throw new PropertyException(
095: "from-table-column() may only be used on "
096: + "fo:table-cell or its descendants.");
097: }
098: } else {
099: //column-number is only accurately available through the propertyList
100: columnNumber = pInfo.getPropertyList().get(
101: Constants.PR_COLUMN_NUMBER).getNumeric()
102: .getValue();
103: span = pInfo.getPropertyList().get(
104: Constants.PR_NUMBER_COLUMNS_SPANNED)
105: .getNumeric().getValue();
106: }
107:
108: /* return the property from the column */
109: Table t = ((TableFObj) fo).getTable();
110: List cols = t.getColumns();
111: if (cols == null) {
112: //no columns defined => no match: return default value
113: return pInfo.getPropertyList().get(propId, false, true);
114: } else {
115: if (t.isColumnNumberUsed(columnNumber)) {
116: //easiest case: exact match
117: return ((TableColumn) cols.get(columnNumber - 1))
118: .getProperty(propId);
119: } else {
120: //no exact match: try all spans...
121: while (--span > 0
122: && !t.isColumnNumberUsed(++columnNumber)) {
123: //nop: just increment/decrement
124: }
125: if (t.isColumnNumberUsed(columnNumber)) {
126: return ((TableColumn) cols
127: .get(columnNumber - 1))
128: .getProperty(propId);
129: } else {
130: //no match: return default value
131: return pInfo.getPropertyList().get(propId,
132: false, true);
133: }
134: }
135: }
136: } else {
137: throw new PropertyException(
138: "Incorrect parameter to from-table-column() function");
139: }
140: }
141:
142: }
|