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 */
017package org.apache.commons.scxml2.env;
018
019import java.io.Serializable;
020import java.net.MalformedURLException;
021import java.net.URL;
022
023import org.apache.commons.logging.Log;
024import org.apache.commons.logging.LogFactory;
025import org.apache.commons.scxml2.PathResolver;
026
027/**
028 * A PathResolver implementation that resolves against a base URL.
029 *
030 * @see org.apache.commons.scxml2.PathResolver
031 */
032public class URLResolver implements PathResolver, Serializable {
033
034    /** Serial version UID. */
035    private static final long serialVersionUID = 1L;
036
037    /** Implementation independent log category. */
038    private Log log = LogFactory.getLog(PathResolver.class);
039
040    /** The base URL to resolve against. */
041    private URL baseURL = null;
042
043    /**
044     * Constructor.
045     *
046     * @param baseURL The base URL to resolve against
047     */
048    public URLResolver(final URL baseURL) {
049        this.baseURL = baseURL;
050    }
051
052    /**
053     * Uses URL(URL, String) constructor to combine URL's.
054     * @see org.apache.commons.scxml2.PathResolver#resolvePath(java.lang.String)
055     */
056    public String resolvePath(final String ctxPath) {
057        URL combined;
058        try {
059            combined = new URL(baseURL, ctxPath);
060            return combined.toString();
061        } catch (MalformedURLException e) {
062            log.error("Malformed URL", e);
063        }
064        return null;
065    }
066
067    /**
068     * @see org.apache.commons.scxml2.PathResolver#getResolver(java.lang.String)
069     */
070    public PathResolver getResolver(final String ctxPath) {
071        URL combined;
072        try {
073            combined = new URL(baseURL, ctxPath);
074            return new URLResolver(combined);
075        } catch (MalformedURLException e) {
076            log.error("Malformed URL", e);
077        }
078        return null;
079    }
080
081}
082