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

org.geotools.styling.DefaultResourceLocator 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 );

The newest version!
/*
 *    GeoTools - The Open Source Java GIS Toolkit
 *    http://geotools.org
 *
 *    (C) 2005-2008, 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.styling;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import org.geotools.util.URLs;

/**
 * Default locator for online resources. Searches by absolute URL, relative path w.r.t. to SLD
 * document or classpath.
 *
 * @author Jan De Moerloose
 */
public class DefaultResourceLocator implements ResourceLocator {

    URL sourceUrl;

    private static final java.util.logging.Logger LOGGER =
            org.geotools.util.logging.Logging.getLogger(DefaultResourceLocator.class);

    public void setSourceUrl(URL sourceUrl) {
        this.sourceUrl = sourceUrl;
    }

    public URL locateResource(String uri) {
        URL url = null;
        try {
            url = new URL(uri);

            File f = URLs.urlToFile(url);
            if (f != null && !f.isAbsolute()) {
                // ok, relative url, if the file exists when we are ok
                if (!f.exists() && sourceUrl != null) {
                    String query = getQueryPart(url);
                    URL relativeUrl = makeRelativeURL(f.getPath(), query);
                    if (relativeUrl != null) {
                        URL validated = validateRelativeURL(relativeUrl);
                        if (validated != null) {
                            url = validated;
                        }
                    }
                }
            }
        } catch (MalformedURLException mfe) {
            LOGGER.fine("Looks like " + uri + " is a relative path..");
            if (sourceUrl != null) {
                url = makeRelativeURL(uri, null);
            }
            if (url == null) {
                url = getClass().getResource(uri);
                if (url == null)
                    LOGGER.warning(
                            "can't parse " + uri + " as a java resource present in the classpath");
            }
        }
        return url;
    }

    /**
     * Variant to URL.getQuery() that won't balk at having # in the query (e.g., a color in a
     * parametric SVG reference), e.g. firestation.svg?fill=#FF0000
     */
    private String getQueryPart(URL url) {
        String external = url.toExternalForm();
        int idx = external.indexOf('?');
        if (idx == -1 || idx == external.length() - 1) {
            return null;
        } else {
            return external.substring(idx + 1);
        }
    }

    protected URL validateRelativeURL(URL relativeUrl) {
        File f;
        f = URLs.urlToFile(relativeUrl);
        if (f != null && f.exists()) {
            // bingo!
            return relativeUrl;
        }
        return null;
    }

    protected URL makeRelativeURL(String uri, String query) {
        try {
            if (query != null) {
                return new URL(sourceUrl, uri + "?" + query);
            } else {
                return new URL(sourceUrl, uri);
            }
        } catch (MalformedURLException e) {
            LOGGER.warning("can't parse " + uri + " as relative to" + sourceUrl.toExternalForm());
        }
        return null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy