KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.apache.cocoon.environment.Context;
22
23 import java.net.MalformedURLException JavaDoc;
24
25
26 /**
27  * Provide an extension to Batik to handle the "context:" protocol. This class
28  * assumes it will live in a separate classloader as the Context is set statically.
29  * Batik uses the Jar file Services extension, so the class is instantiated in
30  * an uncontrolled manner (as far as Cocoon is concerned).
31  *
32  * @author <a HREF="mailto:bloritsch@apache.org">Berin Loritsch</a>
33  * @version CVS $Id: ParsedContextURLProtocolHandler.java 30932 2004-07-29 17:35:38Z vgritsenko $
34  */

35 public class ParsedContextURLProtocolHandler extends AbstractParsedURLProtocolHandler {
36     private static Context context = null;
37
38     /**
39      * Set the ServletContext for this protocol. If it does not exist, you will
40      * get NullPointerExceptions!
41      */

42     public static final void setContext(final Context newContext) {
43         if (ParsedContextURLProtocolHandler.context == null) {
44             ParsedContextURLProtocolHandler.context = newContext;
45         }
46     }
47
48     /**
49      * Create a new instance, this doesn't do much beyond register the type of
50      * protocol we handle.
51      */

52     public ParsedContextURLProtocolHandler() {
53         super("context");
54     }
55
56     /**
57      * Getbase.getPath() the ParsedURLData for the context. Absolute URIs are specified like
58      * "context://".
59      */

60     public ParsedURLData parseURL(String JavaDoc uri) {
61         ParsedURLData urldata = null;
62         try {
63             String JavaDoc path = uri.substring("context:/".length());
64             urldata = new ParsedURLData(ParsedContextURLProtocolHandler.context.getResource(path));
65         } catch (MalformedURLException JavaDoc mue) {
66             StringBuffer JavaDoc baseFile = new StringBuffer JavaDoc(ParsedContextURLProtocolHandler
67                                                      .context.getRealPath("/"));
68
69             if (!baseFile.toString().endsWith("/")) {
70                 baseFile.append("/");
71             }
72
73             baseFile.append(baseFile);
74             baseFile.append(uri.substring("context://".length()));
75
76             urldata = new ParsedURLData();
77             urldata.protocol = "file";
78             urldata.path = baseFile.toString();
79         }
80
81         if ("file".equals(urldata.protocol)) {
82             urldata.host = null;
83             urldata.port = -1;
84         } else if (null == urldata.host) {
85             urldata.port = -1;
86         } else if (urldata.port < 0) {
87             urldata.host = null;
88         }
89
90         return urldata;
91     }
92
93     /**
94      * The build the relative URL. Relative URIs are specified like "context:".
95      */

96     public ParsedURLData parseURL(ParsedURL base, String JavaDoc uri) {
97         StringBuffer JavaDoc newURI = new StringBuffer JavaDoc("context://");
98         newURI.append(base.getPath());
99
100         if (!newURI.toString().endsWith("/")) {
101             newURI.append("/");
102         }
103
104         newURI.append(uri.substring("context:".length()));
105
106         return this.parseURL(newURI.toString());
107     }
108 }
109
Popular Tags