All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.geotools.data.util.ComplexAttributeConverterFactory Maven / Gradle / Ivy

Go to download

The main module contains the GeoTools public interfaces that are used by other GeoTools modules (and GeoTools applications). Where possible we make use industry standard terms as provided by OGC and ISO standards. The formal GeoTools public api consists of gt-metadata, jts and the gt-main module. The main module contains the default implementations that are available provided to other GeoTools modules using our factory system. Factories are obtained from an appropriate FactoryFinder, giving applications a chance configure the factory used using the Factory Hints facilities. FilterFactory ff = CommonFactoryFinder.getFilterFactory(); Expression expr = ff.add( expression1, expression2 ); If you find yourself using implementation specific classes chances are you doing it wrong: Expression expr = new AddImpl( expression1, expressiom2 );

There is a newer version: 24.2-oss84-1
Show newest version
/*
 *    GeoTools - The Open Source Java GIS Toolkit
 *    http://geotools.org
 *
 *    (C) 2009-2011, Open Source Geospatial Foundation (OSGeo)
 *
 *    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.data.util;

import java.util.Collection;
import org.geotools.feature.AttributeImpl;
import org.geotools.filter.identity.FeatureIdImpl;
import org.geotools.util.Converter;
import org.geotools.util.ConverterFactory;
import org.geotools.util.Converters;
import org.geotools.util.factory.Hints;
import org.opengis.feature.Attribute;
import org.opengis.feature.ComplexAttribute;
import org.opengis.feature.GeometryAttribute;
import org.opengis.feature.Property;
import org.opengis.filter.identity.FeatureId;

/**
 * This converter retrieves the values out of attributes.
 *
 * @author Rini Angreani (CSIRO Earth Science and Resource Engineering)
 * @author Niels Charlier
 */
public class ComplexAttributeConverterFactory implements ConverterFactory {

    public Converter createConverter(Class source, Class target, Hints hints) {
        if (ComplexAttribute.class.isAssignableFrom(source)) {
            return new Converter() {
                public Object convert(Object source, Class target) throws Exception {
                    if (source instanceof ComplexAttribute) {
                        Collection valueMap =
                                ((ComplexAttribute) source).getValue();
                        if (valueMap.isEmpty() || valueMap.size() > 1) {
                            return null;
                        } else {
                            // there should only be one value
                            source = valueMap.iterator().next();
                            if (AttributeImpl.class.equals(source.getClass())) {
                                return Converters.convert(((Attribute) source).getValue(), target);
                            }
                        }
                    }
                    return null;
                }
            };
        }

        // GeometryAttribute unwrapper
        if (GeometryAttribute.class.isAssignableFrom(source)) {
            return new Converter() {
                public Object convert(Object source, Class target) throws Exception {
                    if (source instanceof GeometryAttribute) {
                        return Converters.convert(((GeometryAttribute) source).getValue(), target);
                    }
                    return null;
                }
            };
        }

        // String to FeatureId comparison
        if (FeatureId.class.isAssignableFrom(target) && String.class.isAssignableFrom(source)) {
            return new Converter() {
                public Object convert(Object source, Class target) {
                    if (source != null) {
                        return new FeatureIdImpl((String) source);
                    }
                    return null;
                }
            };
        }

        // gets the value of an attribute and tries to convert it to a string
        if (Attribute.class.isAssignableFrom(source)) {
            return new Converter() {
                public Object convert(Object source, Class target) {
                    if (source instanceof Attribute) {
                        // get the attribute value
                        Attribute attribute = (Attribute) source;
                        Object value = attribute.getValue();
                        // let the available converters do their job
                        return Converters.convert(value, target);
                    }
                    // this should not happen, anyway we can only handle attributes
                    return null;
                }
            };
        }

        // handles the conversion of a list of attributes to string
        if (Collection.class.isAssignableFrom(source) && target == String.class) {
            return new Converter() {
                public Object convert(Object source, Class target) {
                    if (!isCollectionOf(source, Attribute.class)) {
                        // not a collection of attributes, we are done
                        return null;
                    }
                    // all attributes values will be concatenated and separated by a coma
                    StringBuilder builder = new StringBuilder();
                    Collection collection = (Collection) source;
                    for (Object element : collection) {
                        if (element == null) {
                            // well we got a NULL element, let's keep track of it
                            builder.append("NULL, ");
                        } else {
                            // we delegate the conversion to converters since we may be dealing wih
                            // a subtype of attribute
                            builder.append(Converters.convert(element, String.class));
                            builder.append(", ");
                        }
                    }
                    if (builder.length() == 0) {
                        // no attributes added, we are done
                        return "";
                    }
                    // remove the extra coma and space
                    builder.delete(builder.length() - 2, builder.length());
                    return builder.toString();
                }
            };
        }

        return null;
    }

    /**
     * Helper method that just checks if the provided source is a collection of objects and that all
     * the objects are either NULL or that the expected type is assignable from them.
     */
    private boolean isCollectionOf(Object source, Class expected) {
        if (!(source instanceof Collection)) {
            // not a collection, we are done
            return false;
        }
        // let's check all the elements
        Collection collection = (Collection) source;
        for (Object element : collection) {
            if (!(element != null && expected.isAssignableFrom(element.getClass()))) {
                // non NULL element and not compatible with he expected type
                return false;
            }
        }
        // all non NULL elements are compatible with he expected type
        return true;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy