/* * Geotools2 - OpenSource mapping toolkit * http://geotools.org * (C) 2002, Geotools Project Managment Committee (PMC) * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; * version 2.1 of the License. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * */ package org.geotools.feature; import org.geotools.feature.AttributeType; import org.geotools.filter.CompareFilter; import org.geotools.filter.Filter; import org.geotools.filter.LengthFunction; import org.geotools.filter.LiteralExpression; import org.geotools.filter.LogicFilter; import java.util.Iterator; /** *

* Proposal: AttributeType utilities class. *

* @author Luca S. Percich, AMA-MI * @source $URL$ */ public class AttributeTypes { // Returned by getFieldLength() functions when field length is undefined public static final int FIELD_LENGTH_UNDEFINED = 0; /** *

* Returns the field length defined via a restrinction for an attribute * type *

* *

* This should be considered the maximum allowed length for the given * attribute, which means: * *

*

* * @param attr The attribute type * @param defaultLength The default field length * * @return The defined field length, or defaultLength if no maximum length * has been defined. */ public static int getFieldLength(AttributeType attr, int defaultLength) { int length = getFieldLengthFromFilter(attr.getRestriction()); if (length == FIELD_LENGTH_UNDEFINED) { length = defaultLength; } return length; } /** *

* Returns the field length defined via a restrinction for an attribute * type *

* * @param attr * * @return the defined field length, or FIELD_LENGTH_UNDEFINED if no * maximum length has been defined. */ public static int getFieldLength(AttributeType attr) { return getFieldLength(attr, FIELD_LENGTH_UNDEFINED); } /** *

* Obtains a field length from a filter possibly containing a * LengthFunction expression *

* * @param filter the given filter * * @return The maximum field length found in the filter, or * FIELD_LENGTH_UNDEFINED if none was found; */ public static int getFieldLengthFromFilter(Filter filter) { int length = FIELD_LENGTH_UNDEFINED; if ((filter != null) && (filter != Filter.ALL) && (filter != Filter.NONE)) { short filterType = filter.getFilterType(); if ((filterType == Filter.COMPARE_LESS_THAN) || (filterType == Filter.COMPARE_LESS_THAN_EQUAL) || (filterType == Filter.COMPARE_EQUALS)) { try { CompareFilter cf = (CompareFilter) filter; if (cf.getLeftValue() instanceof LengthFunction) { length = Integer.parseInt(((LiteralExpression) cf.getRightValue()).getLiteral() .toString()); } else { if (cf.getRightValue() instanceof LengthFunction) { length = Integer.parseInt(((LiteralExpression) cf .getLeftValue()).getLiteral() .toString()); } } if (filterType == Filter.COMPARE_LESS_THAN) { length--; } } catch (NumberFormatException e) { } // In case of a complex filter, looks for the maximum defined length in filter } else if ((filterType == Filter.LOGIC_AND) || (filterType == Filter.LOGIC_OR)) { for (Iterator it = ((LogicFilter) filter).getFilterIterator(); it.hasNext();) { Filter subFilter = (Filter) it.next(); int subLength = getFieldLengthFromFilter(subFilter); if (subLength > length) { length = subLength; } } } } return length; } }