1 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 ; 24 25 26 35 public class ParsedContextURLProtocolHandler extends AbstractParsedURLProtocolHandler { 36 private static Context context = null; 37 38 42 public static final void setContext(final Context newContext) { 43 if (ParsedContextURLProtocolHandler.context == null) { 44 ParsedContextURLProtocolHandler.context = newContext; 45 } 46 } 47 48 52 public ParsedContextURLProtocolHandler() { 53 super("context"); 54 } 55 56 60 public ParsedURLData parseURL(String uri) { 61 ParsedURLData urldata = null; 62 try { 63 String path = uri.substring("context:/".length()); 64 urldata = new ParsedURLData(ParsedContextURLProtocolHandler.context.getResource(path)); 65 } catch (MalformedURLException mue) { 66 StringBuffer baseFile = new StringBuffer (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 96 public ParsedURLData parseURL(ParsedURL base, String uri) { 97 StringBuffer newURI = new StringBuffer ("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 |