1 16 package org.apache.cocoon.components.url; 17 18 import org.apache.avalon.framework.context.ContextException; 19 import org.apache.avalon.framework.context.Contextualizable; 20 import org.apache.avalon.framework.logger.AbstractLogEnabled; 21 22 import org.apache.cocoon.Constants; 23 import org.apache.cocoon.environment.Context; 24 25 import java.net.MalformedURLException ; 26 import java.net.URL ; 27 28 34 public class ContextURLFactory extends AbstractLogEnabled implements URLFactory, Contextualizable { 35 36 39 protected org.apache.avalon.framework.context.Context context; 40 41 50 public URL getURL(String location) throws MalformedURLException { 51 Context envContext = null; 52 try { 53 envContext = (Context)this.context.get(Constants.CONTEXT_ENVIRONMENT_CONTEXT); 54 } catch (ContextException e){ 55 getLogger().error("ContextException in getURL",e); 56 } 57 if (envContext == null) { 58 getLogger().warn("no environment-context in application context (making an absolute URL)"); 59 return new URL (location); 60 } 61 URL u = envContext.getResource("/" + location); 62 if (u != null) 63 return u; 64 else { 65 getLogger().info(location + " could not be found. (possible context problem)"); 66 throw new MalformedURLException (location + " could not be found. (possible context problem)"); 67 } 68 } 69 70 public URL getURL(URL base, String location) throws MalformedURLException { 71 return getURL(base.toExternalForm() + location); 72 } 73 74 77 public void contextualize(org.apache.avalon.framework.context.Context context) throws ContextException { 78 if (this.context == null) { 79 this.context = context; 80 } 81 } 82 } 83 | Popular Tags |