Author: andreas
Date: Thu Mar 6 15:04:08 2008
New Revision: 634446
URL: http://svn.apache.org/viewvc?rev=634446&view=rev
Log:
Allow to mark broken links by subclasses of AbstractLinkTransformer.
Modified:
lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/cocoon/transformation/AbstractLinkTransformer.java
lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/cocoon/transformation/UuidToUrlTransformer.java
lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/linking/LinkRewriter.java
Modified: lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/cocoon/transformation/AbstractLinkTransformer.java
URL: http://svn.apache.org/viewvc/lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/cocoon/transformation/AbstractLinkTransformer.java?rev=634446&r1=634445&r2=634446&view=diff
==============================================================================
--- lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/cocoon/transformation/AbstractLinkTransformer.java (original)
+++ lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/cocoon/transformation/AbstractLinkTransformer.java Thu Mar 6 15:04:08 2008
@@ -28,6 +28,7 @@
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.cocoon.transformation.AbstractSAXTransformer;
+import org.apache.lenya.ac.AccessControlException;
import org.apache.lenya.cms.linking.LinkRewriter;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
@@ -36,12 +37,17 @@
/**
* <p>
* This transformer processes all links which are configured using
- * <code><transform/></code> elements:
+ * <code><transform/></code> elements.
+ * </p>
+ * <p>
+ * If the link rewriter returns <code>null</code> for a link, an attribute can be
+ * added to the corresponding element.
* </p>
* <code><pre>
* <map:transformer ... >
* <transform namespace="http://www.w3.org/1999/xhtml" element="a" attribute="href"/>
* <transform namespace="..." ... />
+ * <markBrokenLinks attribute="..." value="..."/>
* </map:transformer>
* </pre></code>
*/
@@ -52,6 +58,10 @@
*/
private Set localNames = new HashSet();
+ private boolean markBrokenLinks;
+ private String brokenLinkAttribute;
+ private String brokenLinkValue;
+
public void configure(Configuration config) throws ConfigurationException {
super.configure(config);
Configuration[] transformConfigs = config.getChildren("transform");
@@ -70,6 +80,15 @@
}
configs.add(attrConfig);
}
+ Configuration brokenLinksConfig = config.getChild("markBrokenLinks", false);
+ if (brokenLinksConfig != null) {
+ this.brokenLinkAttribute = brokenLinksConfig.getAttribute("attribute");
+ this.brokenLinkValue = brokenLinksConfig.getAttribute("value");
+ this.markBrokenLinks = true;
+ }
+ else {
+ this.markBrokenLinks = false;
+ }
}
/**
@@ -236,7 +255,13 @@
protected void handleLink(String linkUrl, AttributeConfiguration config, AttributesImpl newAttrs)
throws Exception {
if (getLinkRewriter().matches(linkUrl)) {
- setAttribute(newAttrs, config.attribute, getLinkRewriter().rewrite(linkUrl));
+ String rewrittenUrl = getLinkRewriter().rewrite(linkUrl);
+ if (rewrittenUrl != null) {
+ setAttribute(newAttrs, config.attribute, rewrittenUrl);
+ }
+ else {
+ markBrokenLink(newAttrs, config.attribute);
+ }
}
}
@@ -283,5 +308,24 @@
* @return The link rewriter used by this transformer.
*/
protected abstract LinkRewriter getLinkRewriter();
+
+ /**
+ * Marks a link element as broken and removes the attribute which contained the URL.
+ * @param newAttrs The new attributes.
+ * @param attrName The attribute name containing the URL which could not be rewritten.
+ * @throws AccessControlException when something went wrong.
+ */
+ protected void markBrokenLink(AttributesImpl newAttrs, String attrName)
+ throws AccessControlException {
+ if (this.markBrokenLinks) {
+ if (newAttrs.getIndex(this.brokenLinkAttribute) > -1) {
+ newAttrs.removeAttribute(newAttrs.getIndex(this.brokenLinkAttribute));
+ }
+ if (newAttrs.getIndex(attrName) > -1) {
+ newAttrs.setAttribute(newAttrs.getIndex(attrName), "", attrName, attrName, "CDATA", "");
+ }
+ newAttrs.addAttribute("", this.brokenLinkAttribute, this.brokenLinkAttribute, "CDATA", this.brokenLinkValue);
+ }
+ }
}
Modified: lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/cocoon/transformation/UuidToUrlTransformer.java
URL: http://svn.apache.org/viewvc/lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/cocoon/transformation/UuidToUrlTransformer.java?rev=634446&r1=634445&r2=634446&view=diff
==============================================================================
--- lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/cocoon/transformation/UuidToUrlTransformer.java (original)
+++ lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/cocoon/transformation/UuidToUrlTransformer.java Thu Mar 6 15:04:08 2008
@@ -343,14 +343,5 @@
}
return this.validity;
}
-
- protected static class Extension {
- public Extension(String extension, long lastModified) {
- this.extension = extension;
- this.lastModified = lastModified;
- }
- protected String extension;
- protected long lastModified;
- }
}
Modified: lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/linking/LinkRewriter.java
URL: http://svn.apache.org/viewvc/lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/linking/LinkRewriter.java?rev=634446&r1=634445&r2=634446&view=diff
==============================================================================
--- lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/linking/LinkRewriter.java (original)
+++ lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/linking/LinkRewriter.java Thu Mar 6 15:04:08 2008
@@ -30,7 +30,7 @@
/**
* @param url The original URL.
- * @return The rewritten URL.
+ * @return The rewritten URL or <code>null</code> if the URL could not be rewritten.
*/
String rewrite(String url);
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.