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: */
017: package org.apache.cocoon.components.url;
018:
019: import org.apache.batik.util.AbstractParsedURLProtocolHandler;
020: import org.apache.batik.util.ParsedURL;
021: import org.apache.batik.util.ParsedURLData;
022: import org.apache.cocoon.environment.Context;
023:
024: import java.net.MalformedURLException;
025:
026: /**
027: * Provide an extension to Batik to handle the "context:" protocol. This class
028: * assumes it will live in a separate classloader as the Context is set statically.
029: * Batik uses the Jar file Services extension, so the class is instantiated in
030: * an uncontrolled manner (as far as Cocoon is concerned).
031: *
032: * @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
033: * @version CVS $Id: ParsedContextURLProtocolHandler.java 433543 2006-08-22 06:22:54Z crossley $
034: */
035: public class ParsedContextURLProtocolHandler extends
036: AbstractParsedURLProtocolHandler {
037: private static Context context = null;
038:
039: /**
040: * Set the ServletContext for this protocol. If it does not exist, you will
041: * get NullPointerExceptions!
042: */
043: public static final void setContext(final Context newContext) {
044: if (ParsedContextURLProtocolHandler.context == null) {
045: ParsedContextURLProtocolHandler.context = newContext;
046: }
047: }
048:
049: /**
050: * Create a new instance, this doesn't do much beyond register the type of
051: * protocol we handle.
052: */
053: public ParsedContextURLProtocolHandler() {
054: super ("context");
055: }
056:
057: /**
058: * Getbase.getPath() the ParsedURLData for the context. Absolute URIs are specified like
059: * "context://".
060: */
061: public ParsedURLData parseURL(String uri) {
062: ParsedURLData urldata = null;
063: try {
064: String path = uri.substring("context:/".length());
065: urldata = new ParsedURLData(
066: ParsedContextURLProtocolHandler.context
067: .getResource(path));
068: } catch (MalformedURLException mue) {
069: StringBuffer baseFile = new StringBuffer(
070: ParsedContextURLProtocolHandler.context
071: .getRealPath("/"));
072:
073: if (!baseFile.toString().endsWith("/")) {
074: baseFile.append("/");
075: }
076:
077: baseFile.append(baseFile);
078: baseFile.append(uri.substring("context://".length()));
079:
080: urldata = new ParsedURLData();
081: urldata.protocol = "file";
082: urldata.path = baseFile.toString();
083: }
084:
085: if ("file".equals(urldata.protocol)) {
086: urldata.host = null;
087: urldata.port = -1;
088: } else if (null == urldata.host) {
089: urldata.port = -1;
090: } else if (urldata.port < 0) {
091: urldata.host = null;
092: }
093:
094: return urldata;
095: }
096:
097: /**
098: * The build the relative URL. Relative URIs are specified like "context:".
099: */
100: public ParsedURLData parseURL(ParsedURL base, String uri) {
101: StringBuffer newURI = new StringBuffer("context://");
102: newURI.append(base.getPath());
103:
104: if (!newURI.toString().endsWith("/")) {
105: newURI.append("/");
106: }
107:
108: newURI.append(uri.substring("context:".length()));
109:
110: return this.parseURL(newURI.toString());
111: }
112: }
|