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/10 10:30:59 UTC

svn commit: r473284 - in /lenya/trunk/src: modules-core/linking/java/src/org/apache/lenya/cms/linking/ modules/export/ modules/export/java/src/org/apache/lenya/cms/export/

Author: andreas
Date: Fri Nov 10 01:30:58 2006
New Revision: 473284

URL: http://svn.apache.org/viewvc?view=rev&rev=473284
Log:
Convert links to UUID syntax after importing content

Added:
    lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/linking/Link.java
Modified:
    lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/linking/LinkResolver.java
    lenya/trunk/src/modules/export/java/src/org/apache/lenya/cms/export/Import.java
    lenya/trunk/src/modules/export/module.xml

Added: 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=auto&rev=473284
==============================================================================
--- lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/linking/Link.java (added)
+++ lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/linking/Link.java Fri Nov 10 01:30:58 2006
@@ -0,0 +1,93 @@
+/*
+ * Copyright  1999-2004 The Apache Software Foundation
+ *
+ *  Licensed 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;
+
+/**
+ * A link to a document.
+ */
+public class Link {
+
+    private String uuid;
+    private String language;
+    private String revision;
+    private String area;
+    private String pubId;
+    
+    public Link() {
+    }
+    
+    public String getArea() {
+        return area;
+    }
+
+    public void setArea(String area) {
+        this.area = area;
+    }
+
+    public String getLanguage() {
+        return language;
+    }
+
+    public void setLanguage(String language) {
+        this.language = language;
+    }
+
+    public String getPubId() {
+        return pubId;
+    }
+
+    public void setPubId(String pubId) {
+        this.pubId = pubId;
+    }
+
+    public String getRevision() {
+        return revision;
+    }
+
+    public void setRevision(String revision) {
+        this.revision = revision;
+    }
+
+    public String getUuid() {
+        return uuid;
+    }
+
+    public void setUuid(String uuid) {
+        this.uuid = uuid;
+    }
+
+    public String getUri() {
+        String uri = LinkResolver.SCHEME + ":";
+        if (this.uuid != null) {
+            uri = uri + this.uuid;
+        }
+        if (this.language != null) {
+            uri = uri + ",lang=" + this.language;
+        }
+        if (this.area != null) {
+            uri = uri + ",area=" + this.area;
+        }
+        if (this.pubId != null) {
+            uri = uri + ",pub=" + this.pubId;
+        }
+        if (this.revision != null) {
+            uri = uri + ",rev=" + this.revision;
+        }
+        return uri;
+    }
+    
+}

Modified: lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/linking/LinkResolver.java
URL: http://svn.apache.org/viewvc/lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/linking/LinkResolver.java?view=diff&rev=473284&r1=473283&r2=473284
==============================================================================
--- lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/linking/LinkResolver.java (original)
+++ lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/linking/LinkResolver.java Fri Nov 10 01:30:58 2006
@@ -41,14 +41,17 @@
  * <code>lenya-document:&lt;uuid&gt;[,lang=...][,area=...][,rev=...][,pub=...]</code>
  */
 public interface LinkResolver {
-    
+
     String ROLE = LinkResolver.class.getName();
-    
+    String SCHEME = "lenya-document";
+
     /**
      * Resolve a link.
+     * 
      * @param currentDocument The document which contains the link.
      * @param linkUri The link URI.
-     * @return A document or <code>null</code> if the target document does not exist.
+     * @return A document or <code>null</code> if the target document does not
+     *         exist.
      * @throws MalformedURLException if the URI is invalid.
      */
     Document resolve(Document currentDocument, String linkUri) throws MalformedURLException;

Modified: lenya/trunk/src/modules/export/java/src/org/apache/lenya/cms/export/Import.java
URL: http://svn.apache.org/viewvc/lenya/trunk/src/modules/export/java/src/org/apache/lenya/cms/export/Import.java?view=diff&rev=473284&r1=473283&r2=473284
==============================================================================
--- lenya/trunk/src/modules/export/java/src/org/apache/lenya/cms/export/Import.java (original)
+++ lenya/trunk/src/modules/export/java/src/org/apache/lenya/cms/export/Import.java Fri Nov 10 01:30:58 2006
@@ -21,10 +21,14 @@
 import org.apache.avalon.framework.service.ServiceSelector;
 import org.apache.excalibur.source.SourceResolver;
 import org.apache.lenya.cms.cocoon.source.SourceUtil;
+import org.apache.lenya.cms.linking.Link;
+import org.apache.lenya.cms.linking.LinkResolver;
 import org.apache.lenya.cms.metadata.MetaData;
 import org.apache.lenya.cms.publication.Area;
 import org.apache.lenya.cms.publication.Document;
+import org.apache.lenya.cms.publication.DocumentFactory;
 import org.apache.lenya.cms.publication.DocumentManager;
+import org.apache.lenya.cms.publication.Publication;
 import org.apache.lenya.cms.publication.PublicationException;
 import org.apache.lenya.cms.publication.ResourceType;
 import org.apache.lenya.cms.publication.URLInformation;
@@ -34,7 +38,11 @@
 import org.apache.lenya.cms.usecase.AbstractUsecase;
 import org.apache.lenya.xml.DocumentHelper;
 import org.apache.lenya.xml.NamespaceHelper;
+import org.apache.xpath.XPathAPI;
+import org.w3c.dom.Attr;
 import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
 
 public class Import extends AbstractUsecase {
 
@@ -97,7 +105,8 @@
 
         Element siteElement = xml.getDocumentElement();
         importChildren(area, helper, siteElement, baseUri, "");
-
+        
+        convertLinks(area);
     }
 
     protected void importElement(Area area, NamespaceHelper helper, Element element,
@@ -130,7 +139,7 @@
         ResourceType resourceType = null;
         SourceResolver resolver = null;
         try {
-            
+
             org.w3c.dom.Document xml = SourceUtil.readDOM(metaUri, this.manager);
             NamespaceHelper helper = new NamespaceHelper(
                     "http://apache.org/cocoon/lenya/page-envelope/1.0", "", xml);
@@ -138,7 +147,7 @@
             Element internalElement = helper.getFirstChild(metaElement, "internal");
             Element resourceTypeElement = helper.getFirstChild(internalElement, "resourceType");
             String resourceTypeName = DocumentHelper.getSimpleElementText(resourceTypeElement);
-            
+
             selector = (ServiceSelector) this.manager.lookup(ResourceType.ROLE + "Selector");
             resourceType = (ResourceType) selector.select(resourceTypeName);
 
@@ -146,23 +155,23 @@
             Document newDoc;
             SiteStructure site = area.getSite();
             if (!site.contains(path) || site.getNode(path).getLanguages().length == 0) {
-                newDoc = docManager.add(getDocumentFactory(), resourceType, contentUri, area.getPublication(),
-                        area.getName(), path, language, ".html", navigationTitle, visibleInNav);
-            }
-            else {
+                newDoc = docManager.add(getDocumentFactory(), resourceType, contentUri, area
+                        .getPublication(), area.getName(), path, language, ".html",
+                        navigationTitle, visibleInNav);
+            } else {
                 SiteNode node = site.getNode(path);
                 Document doc = node.getLink(node.getLanguages()[0]).getDocument();
                 newDoc = docManager.addVersion(doc, area.getName(), language, true);
                 resolver = (SourceResolver) this.manager.lookup(SourceResolver.ROLE);
                 SourceUtil.copy(resolver, contentUri, newDoc.getSourceURI());
             }
-            
+
             String dcNamespace = "http://purl.org/dc/elements/1.1/";
-            
+
             Element dcElement = helper.getFirstChild(metaElement, "dc");
             NamespaceHelper dcHelper = new NamespaceHelper(dcNamespace, "dc", xml);
             Element[] dcElements = dcHelper.getChildren(dcElement);
-            
+
             MetaData meta = newDoc.getMetaData(dcNamespace);
             for (int i = 0; i < dcElements.length; i++) {
                 String key = dcElements[i].getLocalName();
@@ -190,6 +199,85 @@
         Element[] elements = helper.getChildren(element, "node");
         for (int i = 0; i < elements.length; i++) {
             importElement(area, helper, elements[i], baseUri, path);
+        }
+    }
+
+    protected void convertLinks(Area area) {
+        Document[] docs = area.getDocuments();
+        for (int i = 0; i < docs.length; i++) {
+            convertLinks(docs[i]);
+        }
+    }
+
+    protected void convertLinks(Document examinedDocument) {
+        boolean linksRewritten = false;
+
+        LinkResolver linkResolver = null;
+        try {
+            ResourceType type = examinedDocument.getResourceType();
+            String[] xPaths = type.getLinkAttributeXPaths();
+
+            if (xPaths.length == 0) {
+                if (getLogger().isDebugEnabled()) {
+                    getLogger().debug(
+                            "Convert links: No XPaths for resource type [" + type.getName() + "]");
+                }
+            } else {
+                linkResolver = (LinkResolver) this.manager.lookup(LinkResolver.ROLE);
+                Publication pub = examinedDocument.getPublication();
+                DocumentFactory factory = examinedDocument.getFactory();
+
+                org.w3c.dom.Document xmlDocument = SourceUtil.readDOM(examinedDocument
+                        .getSourceURI(), this.manager);
+
+                for (int xPathIndex = 0; xPathIndex < xPaths.length; xPathIndex++) {
+                    if (getLogger().isDebugEnabled()) {
+                        getLogger()
+                                .debug("Convert links: Check XPath [" + xPaths[xPathIndex] + "]");
+                    }
+                    NodeList nodes = XPathAPI.selectNodeList(xmlDocument, xPaths[xPathIndex]);
+                    for (int nodeIndex = 0; nodeIndex < nodes.getLength(); nodeIndex++) {
+                        Node node = nodes.item(nodeIndex);
+                        if (node.getNodeType() != Node.ATTRIBUTE_NODE) {
+                            throw new RuntimeException("The XPath [" + xPaths[xPathIndex]
+                                    + "] may only match attribute nodes!");
+                        }
+                        Attr attribute = (Attr) node;
+                        final String url = attribute.getValue();
+                        if (getLogger().isDebugEnabled()) {
+                            getLogger().debug("Convert links: Check URL [" + url + "]");
+                        }
+
+                        if (url.startsWith("/" + pub.getId() + "/" + examinedDocument.getArea() + "/")) {
+                            final String webappUrl = url;
+                            if (factory.isDocument(webappUrl)) {
+                                Document targetDocument = factory.getFromURL(webappUrl);
+
+                                if (getLogger().isDebugEnabled()) {
+                                    getLogger().debug(
+                                            "Convert links: Check webapp URL [" + webappUrl + "]");
+                                }
+
+                                Link link = new Link();
+                                link.setUuid(targetDocument.getUUID());
+                                attribute.setValue(link.getUri());
+                                linksRewritten = true;
+                            }
+                        }
+                    }
+                }
+
+                if (linksRewritten) {
+                    SourceUtil.writeDOM(xmlDocument, examinedDocument.getSourceURI(), this.manager);
+                }
+            }
+        } catch (Exception e) {
+            throw new RuntimeException("Error rewriting document: [" + examinedDocument
+                    + "] - source URI: [" + examinedDocument.getSourceURI() + "]", e);
+        } finally {
+            if (linkResolver != null) {
+                this.manager.release(linkResolver);
+            }
         }
     }
 

Modified: lenya/trunk/src/modules/export/module.xml
URL: http://svn.apache.org/viewvc/lenya/trunk/src/modules/export/module.xml?view=diff&rev=473284&r1=473283&r2=473284
==============================================================================
--- lenya/trunk/src/modules/export/module.xml (original)
+++ lenya/trunk/src/modules/export/module.xml Fri Nov 10 01:30:58 2006
@@ -21,6 +21,7 @@
   <id>org.apache.lenya.modules.export</id>
   <depends module="org.apache.lenya.modules.usecase"/>
   <depends module="org.apache.lenya.modules.sitetree"/>
+  <depends module="org.apache.lenya.modules.linking"/>
   <package>org.apache.lenya.modules</package>
   <version>0.1-dev</version>
   <name>Export and import</name>



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