1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.scxml2.env;
18
19 import java.io.Serializable;
20 import java.net.MalformedURLException;
21 import java.net.URL;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.commons.scxml2.PathResolver;
26
27 /**
28 * A PathResolver implementation that resolves against a base URL.
29 *
30 * @see org.apache.commons.scxml2.PathResolver
31 */
32 public class URLResolver implements PathResolver, Serializable {
33
34 /** Serial version UID. */
35 private static final long serialVersionUID = 1L;
36
37 /** Implementation independent log category. */
38 private Log log = LogFactory.getLog(PathResolver.class);
39
40 /** The base URL to resolve against. */
41 private URL baseURL = null;
42
43 /**
44 * Constructor.
45 *
46 * @param baseURL The base URL to resolve against
47 */
48 public URLResolver(final URL baseURL) {
49 this.baseURL = baseURL;
50 }
51
52 /**
53 * Uses URL(URL, String) constructor to combine URL's.
54 * @see org.apache.commons.scxml2.PathResolver#resolvePath(java.lang.String)
55 */
56 public String resolvePath(final String ctxPath) {
57 URL combined;
58 try {
59 combined = new URL(baseURL, ctxPath);
60 return combined.toString();
61 } catch (MalformedURLException e) {
62 log.error("Malformed URL", e);
63 }
64 return null;
65 }
66
67 /**
68 * @see org.apache.commons.scxml2.PathResolver#getResolver(java.lang.String)
69 */
70 public PathResolver getResolver(final String ctxPath) {
71 URL combined;
72 try {
73 combined = new URL(baseURL, ctxPath);
74 return new URLResolver(combined);
75 } catch (MalformedURLException e) {
76 log.error("Malformed URL", e);
77 }
78 return null;
79 }
80
81 }
82