01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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.cocoon.components.url;
18:
19: import org.apache.batik.util.AbstractParsedURLProtocolHandler;
20: import org.apache.batik.util.ParsedURL;
21: import org.apache.batik.util.ParsedURLData;
22:
23: /**
24: * Provide an extension to Batik to handle the "resource:" protocol. This class
25: * uses the <code>Thread.getContextClassLoader()</code> classloader to get resources.
26: * It is safe to use this URL with multiple Cocoon webapps running.
27: *
28: * @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
29: * @version CVS $Id: ParsedResourceURLProtocolHandler.java 433543 2006-08-22 06:22:54Z crossley $
30: */
31: public class ParsedResourceURLProtocolHandler extends
32: AbstractParsedURLProtocolHandler {
33:
34: /**
35: * Create a new instance, this doesn't do much beyond register the type of
36: * protocol we handle.
37: */
38: public ParsedResourceURLProtocolHandler() {
39: super ("resource");
40: }
41:
42: /**
43: * Getbase.getPath() the ParsedURLData for the context. Absolute URIs are specified like
44: * "resource://".
45: */
46: public ParsedURLData parseURL(String uri) {
47: ParsedURLData urldata = null;
48: String path = uri.substring("resource:/".length());
49: urldata = new ParsedURLData(Thread.currentThread()
50: .getContextClassLoader().getResource(path));
51:
52: if ("file".equals(urldata.protocol)) {
53: urldata.host = null;
54: urldata.port = -1;
55: } else if (null == urldata.host) {
56: urldata.port = -1;
57: } else if (urldata.port < 0) {
58: urldata.host = null;
59: }
60:
61: return urldata;
62: }
63:
64: /**
65: * The build the relative URL. Relative URIs are specified like "resource:".
66: */
67: public ParsedURLData parseURL(ParsedURL base, String uri) {
68: StringBuffer newURI = new StringBuffer("resource://");
69: newURI.append(base.getPath());
70:
71: if (!newURI.toString().endsWith("/")) {
72: newURI.append("/");
73: }
74:
75: newURI.append(uri.substring("resource:".length()));
76:
77: return this.parseURL(newURI.toString());
78: }
79: }
|