You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lenya.apache.org by an...@apache.org on 2008/03/07 00:04:09 UTC

svn commit: r634446 - in /lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms: cocoon/transformation/AbstractLinkTransformer.java cocoon/transformation/UuidToUrlTransformer.java linking/LinkRewriter.java

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>&lt;transform/&gt;</code> elements:
+ * <code>&lt;transform/&gt;</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>
  *   &lt;map:transformer ... &gt;
  *     &lt;transform namespace=&quot;http://www.w3.org/1999/xhtml&quot; element=&quot;a&quot; attribute=&quot;href&quot;/&gt;
  *     &lt;transform namespace=&quot;...&quot; ... /&gt;
+ *     &lt;markBrokenLinks attribute=&quot;...&quot; value=&quot;...&quot;/&gt;
  *   &lt;/map:transformer&gt;
  * </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);
 



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@lenya.apache.org
For additional commands, e-mail: commits-help@lenya.apache.org