cziegeler 02/02/07 06:40:17 Modified: src/scratchpad/src/org/apache/cocoon/transformation EncodeURLTransformer.java Log: Improved encode transformer - it is now cacheable, if now session is available Revision Changes Path 1.3 +51 -52 xml-cocoon2/src/scratchpad/src/org/apache/cocoon/transformation/EncodeURLTransformer.java Index: EncodeURLTransformer.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/scratchpad/src/org/apache/cocoon/transformation/EncodeURLTransformer.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- EncodeURLTransformer.java 7 Feb 2002 07:41:24 -0000 1.2 +++ EncodeURLTransformer.java 7 Feb 2002 14:40:17 -0000 1.3 @@ -61,6 +61,9 @@ import org.apache.avalon.framework.configuration.ConfigurationException; import org.apache.avalon.framework.parameters.Parameters; import org.apache.cocoon.ProcessingException; +import org.apache.cocoon.caching.Cacheable; +import org.apache.cocoon.caching.CacheValidity; +import org.apache.cocoon.caching.NOPCacheValidity; import org.apache.cocoon.environment.ObjectModelHelper; import org.apache.cocoon.environment.Request; import org.apache.cocoon.environment.Response; @@ -111,7 +114,7 @@ * </pre></tt> * * @author <a href="mailto:[EMAIL PROTECTED]">Bernhard Huber</a> - * @version CVS $Id: EncodeURLTransformer.java,v 1.2 2002/02/07 07:41:24 cziegeler Exp $ + * @version CVS $Id: EncodeURLTransformer.java,v 1.3 2002/02/07 14:40:17 cziegeler Exp $ * * @cocoon:name encodeURL * @cocoon:status scratchpad @@ -161,12 +164,9 @@ private String includeNameConfigure = INCLUDE_NAME_DEFAULT; private String excludeNameConfigure = EXCLUDE_NAME_DEFAULT; - private String includeName; - private String excludeName; - private ElementAttributeMatching elementAttributeMatching; private Response response; - private Request request; + private Session session; /** @@ -183,19 +183,22 @@ public void setup(SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws ProcessingException, SAXException, IOException { - includeName = parameters.getParameter(EncodeURLTransformer.INCLUDE_NAME, this.includeNameConfigure); - excludeName = parameters.getParameter(EncodeURLTransformer.EXCLUDE_NAME, this.excludeNameConfigure); - try { - elementAttributeMatching = new ElementAttributeMatching(includeName, excludeName); - } catch (RESyntaxException reex) { - String message = "Cannot parse include-name: " + includeName + " " + + this.session = ObjectModelHelper.getRequest(objectModel).getSession( false ); + if ( null != this.session ) { + this.response = ObjectModelHelper.getResponse(objectModel); + final String includeName = parameters.getParameter(EncodeURLTransformer.INCLUDE_NAME, + this.includeNameConfigure); + final String excludeName = parameters.getParameter(EncodeURLTransformer.EXCLUDE_NAME, + this.excludeNameConfigure); + try { + this.elementAttributeMatching = new ElementAttributeMatching(includeName, excludeName); + } catch (RESyntaxException reex) { + final String message = "Cannot parse include-name: " + includeName + " " + "or exclude-name: " + excludeName + "!"; - throw new ProcessingException(message, reex); + throw new ProcessingException(message, reex); + } } - - this.response = ObjectModelHelper.getResponse(objectModel); - this.request = ObjectModelHelper.getRequest(objectModel); } @@ -235,12 +238,38 @@ public void recycle() { super.recycle(); this.response = null; - this.request = null; - elementAttributeMatching = null; + this.session = null; + this.elementAttributeMatching = null; } /** + * Generate the unique key. + * This key must be unique inside the space of this component. + * + * @return The generated key hashes the src + */ + public long generateKey() { + if ( null == this.session ) { + return 1; + } + return 0; + } + + /** + * Generate the validity object. + * + * @return The generated validity object or <code>null</code> if the + * component is currently not cacheable. + */ + public CacheValidity generateValidity() { + if ( null == this.session ) { + return NOPCacheValidity.CACHE_VALIDITY; + } + return null; + } + + /** * Start parsing an element * * @param uri of the element @@ -251,10 +280,10 @@ * @since */ public void startElement(String uri, String name, String raw, Attributes attributes) - throws SAXException { - String lname = name; + throws SAXException { - if (response != null && elementAttributeMatching != null) { + if (this.session != null && this.elementAttributeMatching != null) { + String lname = name; if (attributes != null && attributes.getLength() > 0) { AttributesImpl new_attributes = new AttributesImpl(attributes); for (int i = 0; i < new_attributes.getLength(); i++) { @@ -263,7 +292,7 @@ String value = new_attributes.getValue(i); if (elementAttributeMatching.matchesElementAttribute(lname, attr_lname)) { - String new_value = encodeURL(value, request, response); + final String new_value = response.encodeURL( value ); if (getLogger().isDebugEnabled()) { this.getLogger().debug("element/@attribute matches: " + name + "/@" + attr_lname); this.getLogger().debug("encodeURL: " + value + " -> " + new_value); @@ -280,36 +309,6 @@ super.contentHandler.startElement(uri, name, raw, attributes); } - - /** - * Encode a URL. - * <p> - * This method calculates from a given url the encoded url - * </p> - * - * @param url the URL probably without sessionid. - * @param request the request - * @param response the response - * @return String the original url inclusive the sessionid - * @since - */ - private String encodeURL(String url, Request request, Response response) { - String encoded_url; - if (response != null) { - // As some servlet-engine does not check if url has been already rewritten - Session session = request.getSession(false); - if (session != null && url.indexOf(session.getId()) > -1) { - encoded_url = url; - } else { - encoded_url = response.encodeURL(url); - } - } else { - encoded_url = url; - } - return encoded_url; - } - - /** * A helper class for matching element names, and attribute names. * @@ -319,7 +318,7 @@ * </p> * * @author <a href="mailto:[EMAIL PROTECTED]">Bernhard Huber</a> - * @version CVS $Id: EncodeURLTransformer.java,v 1.2 2002/02/07 07:41:24 cziegeler Exp $ + * @version CVS $Id: EncodeURLTransformer.java,v 1.3 2002/02/07 14:40:17 cziegeler Exp $ */ public class ElementAttributeMatching { /**
---------------------------------------------------------------------- In case of troubles, e-mail: [EMAIL PROTECTED] To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]