Hi Ross. Ross Gardler: > That would be good, but don't hold your breath. Patches welcome (and > help available on the dev list).
The attached patch to Cocoon should do something like I requested. It's completely untested, and I didn't even compile it (it wasn't immediately obvious to me how to use Cocoon's build system, since I know nothing of Maven). It adds a parameter that can be given to the LinkRewriterTransformer to tell it to let a fragment identifier pass through, or not. I made the default "false", so that fragids are not given to the rewriter, since I imagine that would be the more common case. Also, I'm not sure why there are two copies of the link rewriter stuff in Cocoon's repository (one under src/blocks/linkrewriter, and another under blocks/cocoon-linkrewriter); they seem to be identical except for the $Id$ string. Thanks, Cameron -- Cameron McCormack ICQ: 26955922 cam (at) mcc.id.au MSN: cam (at) mcc.id.au http://mcc.id.au/ JBR: heycam (at) jabber.org
--- src/blocks/linkrewriter/trunk/java/org/apache/cocoon/transformation/LinkRewriterTransformer.java 2006-05-10 10:19:39.000000000 +1000 +++ src/blocks/linkrewriter/trunk/java/org/apache/cocoon/transformation/LinkRewriterTransformer.java.new 2006-05-10 10:19:03.000000000 +1000 @@ -172,6 +172,13 @@ * <dd>String to use for links with a correct InputModule prefix, but no value * therein. Defaults to the original URI.</dd> * + * <dt>fragid-passthrough</dt> + * <dd>Boolean that, when <code>true</code>, specifies that a fragment + * identifier on the URI reference will be passed through to the rewriter, + * and when <code>false</code>, specifies that it will be stripped off + * before being sent to the rewriter, and then appended to the rewritten + * URI.</dd> + * * <dt>namespace-uri</dt> * <dd>The namespace uri of elements whose attributes are considered for * transformation. Defaults to the empty namespace ("").</dd> @@ -240,6 +247,7 @@ private String origInSchemes; private String origOutSchemes; private String origNamespaceURI; + private String origFragidPassthrough; /** * A map where keys are those attributes which are considered 'links'. @@ -275,6 +283,13 @@ private Set outSchemes; /** + * Whether to pass the fragment identifier part of the URI reference + * to the transformer, or split it off and append it back to the + * transformed URI. + */ + private boolean fragidPassthrough; + + /** * A map of attributes considered 'links' and corresponding RE expression * or NO_REGEXP object. Recreated once per invocation or copied from * origLinkAttrs based on setup() method parameters. @@ -295,6 +310,7 @@ this.origBadLinkStr = conf.getChild("bad-link-str").getValue(null); this.origInSchemes = conf.getChild("schemes").getValue(""); this.origOutSchemes = conf.getChild("exclude-schemes").getValue("http https ftp news mailto"); + this.origFragidPassthrough = conf.getChild("fragid-passthrough").getValueAsBoolean(false); this.origNamespaceURI = conf.getChild("namespace-uri").getValue(NAMESPACE); @@ -364,6 +380,8 @@ this.inSchemes = split(parameters.getParameter("schemes", this.origInSchemes), " "); this.outSchemes = split(parameters.getParameter("exclude-schemes", this.origOutSchemes), " "); + this.fragidPassthrough = parameters.getParameterAsBoolean("fragid-passthrough", this.origFragidPassthrough); + this.linkAttrs = this.origLinkAttrs; if (parameters.isParameter("link-attrs")) { try { @@ -378,6 +396,7 @@ getLogger().debug("link-attrs = " + linkAttrs); getLogger().debug("schemes = " + inSchemes); getLogger().debug("exclude-schemes = " + outSchemes); + getLogger().debug("fragid-passthrough = " + fragidPassthrough); getLogger().debug("namespace-uri = " + namespaceURI); } @@ -542,10 +561,19 @@ */ private String createTransformedLink(String oldLink) { String newLink = null; - int i = oldLink.indexOf(":"); + int i = oldLink.indexOf(':'); if (i != -1) { String scheme = oldLink.substring(0, i); - String addr = oldLink.substring(i + 1); + String addr; + String fragid; + int j = oldLink.indexOf('#'); + if (fragidPassthrough || j == -1) { + addr = oldLink.substring(i + 1); + fragid = null; + } else { + addr = oldLink.substring(i + 1, j - i - 1); + fragid = oldLink.substring(j); + } if (outSchemes.contains(scheme)) { if (getLogger().isDebugEnabled()) { getLogger().debug("Ignoring link '" + oldLink + "'"); @@ -559,6 +587,9 @@ scheme, addr, (badLinkStr != null? badLinkStr: scheme + ":" + addr)); + if (fragid != null) { + newLink += fragid; + } if (getLogger().isDebugEnabled()) { getLogger().debug("Converted link '" + oldLink + "' to '" + newLink + "'"); }