KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > url > ParsedResourceURLProtocolHandler


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.components.url;
17
18 import org.apache.batik.util.AbstractParsedURLProtocolHandler;
19 import org.apache.batik.util.ParsedURL;
20 import org.apache.batik.util.ParsedURLData;
21
22 /**
23  * Provide an extension to Batik to handle the "resource:" protocol. This class
24  * uses the <code>Thread.getContextClassLoader()</code> classloader to get resources.
25  * It is safe to use this URL with multiple Cocoon webapps running.
26  *
27  * @author <a HREF="mailto:bloritsch@apache.org">Berin Loritsch</a>
28  * @version CVS $Id: ParsedResourceURLProtocolHandler.java 30932 2004-07-29 17:35:38Z vgritsenko $
29  */

30 public class ParsedResourceURLProtocolHandler extends AbstractParsedURLProtocolHandler {
31
32     /**
33      * Create a new instance, this doesn't do much beyond register the type of
34      * protocol we handle.
35      */

36     public ParsedResourceURLProtocolHandler() {
37         super("resource");
38     }
39
40     /**
41      * Getbase.getPath() the ParsedURLData for the context. Absolute URIs are specified like
42      * "resource://".
43      */

44     public ParsedURLData parseURL(String JavaDoc uri) {
45         ParsedURLData urldata = null;
46         String JavaDoc path = uri.substring("resource:/".length());
47         urldata = new ParsedURLData(Thread.currentThread().getContextClassLoader().getResource(path));
48
49         if ("file".equals(urldata.protocol)) {
50             urldata.host = null;
51             urldata.port = -1;
52         } else if (null == urldata.host) {
53             urldata.port = -1;
54         } else if (urldata.port < 0) {
55             urldata.host = null;
56         }
57
58         return urldata;
59     }
60
61     /**
62      * The build the relative URL. Relative URIs are specified like "resource:".
63      */

64     public ParsedURLData parseURL(ParsedURL base, String JavaDoc uri) {
65         StringBuffer JavaDoc newURI = new StringBuffer JavaDoc("resource://");
66         newURI.append(base.getPath());
67
68         if ( !newURI.toString().endsWith("/") ) {
69             newURI.append("/");
70         }
71
72         newURI.append(uri.substring("resource:".length()));
73
74         return this.parseURL(newURI.toString());
75     }
76 }
77
Popular Tags