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 2006/11/28 11:21:48 UTC

svn commit: r479977 - in /lenya/trunk/src: java/org/apache/lenya/cms/publication/util/ modules-core/linking/config/cocoon-xconf/ modules-core/linking/java/src/org/apache/lenya/cms/linking/ modules-core/linking/java/test/ modules-core/linking/java/test/...

Author: andreas
Date: Tue Nov 28 02:21:47 2006
New Revision: 479977

URL: http://svn.apache.org/viewvc?view=rev&rev=479977
Log:
Added link manager and corresponding test case

Added:
    lenya/trunk/src/modules-core/linking/config/cocoon-xconf/linkmanager.xconf
    lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/linking/LinkManager.java
    lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/linking/LinkManagerImpl.java
    lenya/trunk/src/modules-core/linking/java/test/
    lenya/trunk/src/modules-core/linking/java/test/org/
    lenya/trunk/src/modules-core/linking/java/test/org/apache/
    lenya/trunk/src/modules-core/linking/java/test/org/apache/lenya/
    lenya/trunk/src/modules-core/linking/java/test/org/apache/lenya/cms/
    lenya/trunk/src/modules-core/linking/java/test/org/apache/lenya/cms/linking/
    lenya/trunk/src/modules-core/linking/java/test/org/apache/lenya/cms/linking/LinkTest.java
Modified:
    lenya/trunk/src/java/org/apache/lenya/cms/publication/util/DocumentReferencesHelper.java
    lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/linking/Link.java
    lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/linking/LinkResolverImpl.java

Modified: lenya/trunk/src/java/org/apache/lenya/cms/publication/util/DocumentReferencesHelper.java
URL: http://svn.apache.org/viewvc/lenya/trunk/src/java/org/apache/lenya/cms/publication/util/DocumentReferencesHelper.java?view=diff&rev=479977&r1=479976&r2=479977
==============================================================================
--- lenya/trunk/src/java/org/apache/lenya/cms/publication/util/DocumentReferencesHelper.java (original)
+++ lenya/trunk/src/java/org/apache/lenya/cms/publication/util/DocumentReferencesHelper.java Tue Nov 28 02:21:47 2006
@@ -127,7 +127,7 @@
      * @throws ProcessingException if the search for references failed.
      */
     public Document[] getReferences(String area) throws ProcessingException {
-
+        
         ArrayList documents = new ArrayList();
         Publication publication = this.pageEnvelope.getPublication();
         DocumentIdToPathMapper mapper = publication.getPathMapper();

Added: lenya/trunk/src/modules-core/linking/config/cocoon-xconf/linkmanager.xconf
URL: http://svn.apache.org/viewvc/lenya/trunk/src/modules-core/linking/config/cocoon-xconf/linkmanager.xconf?view=auto&rev=479977
==============================================================================
--- lenya/trunk/src/modules-core/linking/config/cocoon-xconf/linkmanager.xconf (added)
+++ lenya/trunk/src/modules-core/linking/config/cocoon-xconf/linkmanager.xconf Tue Nov 28 02:21:47 2006
@@ -0,0 +1,26 @@
+<?xml version="1.0"?>
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one or more
+  contributor license agreements.  See the NOTICE file distributed with
+  this work for additional information regarding copyright ownership.
+  The ASF licenses this file to You under the Apache License, Version 2.0
+  (the "License"); you may not use this file except in compliance with
+  the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+-->
+
+<!-- $Id:$ -->
+
+<xconf xpath="/cocoon" unless="/cocoon/component[@role = 'org.apache.lenya.cms.linking.LinkManager']">
+
+  <component logger="" role="org.apache.lenya.cms.linking.LinkManager"
+    class="org.apache.lenya.cms.linking.LinkManagerImpl"/>
+  
+</xconf>

Modified: lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/linking/Link.java
URL: http://svn.apache.org/viewvc/lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/linking/Link.java?view=diff&rev=479977&r1=479976&r2=479977
==============================================================================
--- lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/linking/Link.java (original)
+++ lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/linking/Link.java Tue Nov 28 02:21:47 2006
@@ -17,11 +17,18 @@
  */
 package org.apache.lenya.cms.linking;
 
+import java.net.MalformedURLException;
+
+import org.apache.lenya.util.Query;
+
 /**
  * A link to a document.
  */
 public class Link {
 
+    protected static final String PAIR_DELIMITER = ",";
+    protected static final String KEY_VALUE_DELIMITER = "=";
+
     private String uuid;
     private String language;
     private String revision;
@@ -35,6 +42,34 @@
     }
     
     /**
+     * Ctor.
+     * @param linkUri The link URI.
+     * @throws MalformedURLException if the URI doesn't represent a link.
+     */
+    public Link(String linkUri) throws MalformedURLException {
+        
+        if (!linkUri.startsWith(LinkResolver.SCHEME + ":")) {
+            throw new MalformedURLException("The string [" + linkUri + "] is not a valid link URI!");
+        }
+        
+        String[] schemeAndPath = linkUri.split(":");
+        String path = schemeAndPath[1];
+
+        if (path.indexOf(PAIR_DELIMITER) > -1) {
+            int firstDelimiterIndex = path.indexOf(PAIR_DELIMITER);
+            this.uuid = path.substring(0, firstDelimiterIndex);
+            String pathQueryString = path.substring(firstDelimiterIndex + 1);
+            Query query = new Query(pathQueryString, PAIR_DELIMITER, KEY_VALUE_DELIMITER);
+            this.pubId = query.getValue("pub");
+            this.area = query.getValue("area");
+            this.language = query.getValue("lang");
+            this.revision = query.getValue("rev");
+        } else {
+            this.uuid = path;
+        }
+    }
+    
+    /**
      * @return The area.
      */
     public String getArea() {
@@ -125,6 +160,21 @@
             uri = uri + ",rev=" + this.revision;
         }
         return uri;
+    }
+
+    public boolean equals(Object obj) {
+        if (!getClass().isInstance(obj)) {
+            return false;
+        }
+        return ((Link) obj).getUri().equals(getUri());
+    }
+
+    public int hashCode() {
+        return getUri().hashCode();
+    }
+
+    public String toString() {
+        return getUri();
     }
     
 }

Added: lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/linking/LinkManager.java
URL: http://svn.apache.org/viewvc/lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/linking/LinkManager.java?view=auto&rev=479977
==============================================================================
--- lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/linking/LinkManager.java (added)
+++ lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/linking/LinkManager.java Tue Nov 28 02:21:47 2006
@@ -0,0 +1,48 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ */
+package org.apache.lenya.cms.linking;
+
+import org.apache.lenya.cms.publication.Document;
+
+/**
+ * Link manager.
+ */
+public interface LinkManager {
+    
+    /**
+     * The Avalon service role.
+     */
+    String ROLE = LinkManager.class.getName();
+
+    /**
+     * Returns all links from a document.
+     * @param source The document.
+     * @return An array of links.
+     */
+    Link[] getLinksFrom(Document source);
+
+    /**
+     * Returns all documents which reference a certain document. This depends on
+     * the currently available translations of the target document and the link
+     * resolver fallback mode.
+     * @param target The target document.
+     * @return An array of documents.
+     */
+    Document[] getReferencingDocuments(Document target);
+
+}

Added: lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/linking/LinkManagerImpl.java
URL: http://svn.apache.org/viewvc/lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/linking/LinkManagerImpl.java?view=auto&rev=479977
==============================================================================
--- lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/linking/LinkManagerImpl.java (added)
+++ lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/linking/LinkManagerImpl.java Tue Nov 28 02:21:47 2006
@@ -0,0 +1,102 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ */
+package org.apache.lenya.cms.linking;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import org.apache.avalon.framework.logger.AbstractLogEnabled;
+import org.apache.avalon.framework.service.ServiceException;
+import org.apache.avalon.framework.service.ServiceManager;
+import org.apache.avalon.framework.service.Serviceable;
+import org.apache.lenya.cms.cocoon.source.SourceUtil;
+import org.apache.lenya.cms.publication.Document;
+import org.apache.xpath.XPathAPI;
+import org.w3c.dom.Attr;
+import org.w3c.dom.Node;
+import org.w3c.dom.traversal.NodeIterator;
+
+/**
+ * Link manager implementation.
+ */
+public class LinkManagerImpl extends AbstractLogEnabled implements LinkManager, Serviceable {
+
+    protected ServiceManager manager;
+
+    public Link[] getLinksFrom(Document source) {
+
+        Set links = new HashSet();
+
+        try {
+            String[] xPaths = source.getResourceType().getLinkAttributeXPaths();
+            org.w3c.dom.Document xml = SourceUtil.readDOM(source.getSourceURI(), this.manager);
+            for (int i = 0; i < xPaths.length; i++) {
+                NodeIterator iter = XPathAPI.selectNodeIterator(xml, xPaths[i]);
+                Node node;
+                while ((node = iter.nextNode()) != null) {
+                    Attr attr = (Attr) node;
+                    String uri = attr.getValue();
+                    if (isLinkUri(uri)) {
+                        links.add(new Link(uri));
+                    }
+                }
+            }
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+
+        return (Link[]) links.toArray(new Link[links.size()]);
+    }
+
+    protected boolean isLinkUri(String uri) {
+        return uri.startsWith(LinkResolver.SCHEME + ":");
+    }
+
+    public Document[] getReferencingDocuments(Document target) {
+
+        Document[] allDocs = target.area().getDocuments();
+        Set docs = new HashSet();
+
+        LinkResolver resolver = null;
+        try {
+            resolver = (LinkResolver) this.manager.lookup(LinkResolver.ROLE);
+            for (int d = 0; d < allDocs.length; d++) {
+
+                Link[] links = getLinksFrom(allDocs[d]);
+                for (int l = 0; l < links.length; l++) {
+                    LinkTarget linkTarget = resolver.resolve(allDocs[d], links[l].getUri());
+                    if (linkTarget.getDocument().equals(target)) {
+                        docs.add(allDocs[d]);
+                    }
+                }
+            }
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        } finally {
+            if (resolver != null) {
+                this.manager.release(resolver);
+            }
+        }
+        return (Document[]) docs.toArray(new Document[docs.size()]);
+    }
+
+    public void service(ServiceManager manager) throws ServiceException {
+        this.manager = manager;
+    }
+
+}

Modified: lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/linking/LinkResolverImpl.java
URL: http://svn.apache.org/viewvc/lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/linking/LinkResolverImpl.java?view=diff&rev=479977&r1=479976&r2=479977
==============================================================================
--- lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/linking/LinkResolverImpl.java (original)
+++ lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/linking/LinkResolverImpl.java Tue Nov 28 02:21:47 2006
@@ -27,7 +27,6 @@
 import org.apache.lenya.cms.publication.Document;
 import org.apache.lenya.cms.publication.Publication;
 import org.apache.lenya.cms.publication.PublicationException;
-import org.apache.lenya.util.Query;
 
 /**
  * Link resolver implemenation.
@@ -39,37 +38,23 @@
      */
     public static final String ROLE = LinkResolverImpl.class.getName();
     
-    protected static final String PAIR_DELIMITER = ",";
-    protected static final String KEY_VALUE_DELIMITER = "=";
 
     public LinkTarget resolve(Document currentDoc, String linkUri) throws MalformedURLException {
 
-        String[] schemeAndPath = linkUri.split(":");
-        String path = schemeAndPath[1];
-
-        String pubId;
-        String uuid;
-        String area;
-        String language;
-        int revision = -1;
-
-        if (path.indexOf(PAIR_DELIMITER) > -1) {
-            int firstDelimiterIndex = path.indexOf(PAIR_DELIMITER);
-            uuid = path.substring(0, firstDelimiterIndex);
-            String pathQueryString = path.substring(firstDelimiterIndex + 1);
-            Query query = new Query(pathQueryString, PAIR_DELIMITER, KEY_VALUE_DELIMITER);
-            pubId = query.getValue("pub", currentDoc.getPublication().getId());
-            area = query.getValue("area", currentDoc.getArea());
-            language = query.getValue("lang", currentDoc.getLanguage());
-            String revisionString = query.getValue("rev", null);
-            if (revisionString != null) {
-                revision = Integer.valueOf(revisionString).intValue();
-            }
-        } else {
-            uuid = path;
-            pubId = currentDoc.getPublication().getId();
-            area = currentDoc.getArea();
-            language = currentDoc.getLanguage();
+        Link link = new Link(linkUri);
+        
+        String uuid = getValue(link.getUuid(), currentDoc.getUUID());
+        String language = getValue(link.getLanguage(), currentDoc.getLanguage());
+        String revisionString = getValue(link.getRevision(), null);
+        String area = getValue(link.getArea(), currentDoc.getArea());
+        String pubId = getValue(link.getPubId(), currentDoc.getPublication().getId());
+
+        int revision;
+        if (revisionString == null) {
+            revision = -1;
+        }
+        else {
+            revision = Integer.valueOf(revisionString).intValue();
         }
 
         if (uuid.length() == 0) {
@@ -105,6 +90,15 @@
             }
         } catch (PublicationException e) {
             throw new RuntimeException(e);
+        }
+    }
+    
+    protected String getValue(String value, String defaultValue) {
+        if (value == null) {
+            return defaultValue;
+        }
+        else {
+            return value;
         }
     }
 

Added: lenya/trunk/src/modules-core/linking/java/test/org/apache/lenya/cms/linking/LinkTest.java
URL: http://svn.apache.org/viewvc/lenya/trunk/src/modules-core/linking/java/test/org/apache/lenya/cms/linking/LinkTest.java?view=auto&rev=479977
==============================================================================
--- lenya/trunk/src/modules-core/linking/java/test/org/apache/lenya/cms/linking/LinkTest.java (added)
+++ lenya/trunk/src/modules-core/linking/java/test/org/apache/lenya/cms/linking/LinkTest.java Tue Nov 28 02:21:47 2006
@@ -0,0 +1,78 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ */
+package org.apache.lenya.cms.linking;
+
+import java.util.Arrays;
+
+import org.apache.lenya.ac.impl.AbstractAccessControlTest;
+import org.apache.lenya.cms.publication.Area;
+import org.apache.lenya.cms.publication.Document;
+import org.apache.lenya.cms.publication.Publication;
+import org.apache.lenya.cms.site.SiteStructure;
+
+/**
+ * Test for link functionality.
+ */
+public class LinkTest extends AbstractAccessControlTest {
+    
+    /**
+     * Link test.
+     * @throws Exception
+     */
+    public void testLinks() throws Exception {
+        
+        Publication pub = getPublication("test");
+        Area area = pub.getArea("authoring");
+        SiteStructure site = area.getSite();
+        
+        Document source = site.getNode("/index").getLink("en").getDocument();
+        Document target = site.getNode("/tutorial").getLink("en").getDocument();
+        
+        LinkManager linkManager = null;
+        LinkResolver resolver = null;
+        try {
+            linkManager = (LinkManager) getManager().lookup(LinkManager.ROLE);
+            resolver = (LinkResolver) getManager().lookup(LinkResolver.ROLE);
+            
+            Link[] links = linkManager.getLinksFrom(source);
+            
+            boolean matched = false;
+            for (int i = 0; i < links.length; i++) {
+                LinkTarget linkTarget = resolver.resolve(source, links[i].getUri());
+                if (linkTarget.getDocument().equals(target)) {
+                    matched = true;
+                }
+            }
+            
+            assertTrue(matched);
+            
+            Document[] references = linkManager.getReferencingDocuments(target);
+            assertTrue(Arrays.asList(references).contains(source));
+        }
+        finally {
+            if (linkManager != null) {
+                getManager().release(linkManager);
+            }
+            if (resolver != null) {
+                getManager().release(resolver);
+            }
+        }
+        
+    }
+
+}



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