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 2005/12/02 10:52:54 UTC

svn commit: r351656 - in /lenya/trunk/src/java/org/apache/lenya/cms/cocoon/source: ContentSource.java ContentSourceFactory.java

Author: andreas
Date: Fri Dec  2 01:52:49 2005
New Revision: 351656

URL: http://svn.apache.org/viewcvs?rev=351656&view=rev
Log:
Repo layer: Added ContentSource to access repository content

Added:
    lenya/trunk/src/java/org/apache/lenya/cms/cocoon/source/ContentSource.java
    lenya/trunk/src/java/org/apache/lenya/cms/cocoon/source/ContentSourceFactory.java

Added: lenya/trunk/src/java/org/apache/lenya/cms/cocoon/source/ContentSource.java
URL: http://svn.apache.org/viewcvs/lenya/trunk/src/java/org/apache/lenya/cms/cocoon/source/ContentSource.java?rev=351656&view=auto
==============================================================================
--- lenya/trunk/src/java/org/apache/lenya/cms/cocoon/source/ContentSource.java (added)
+++ lenya/trunk/src/java/org/apache/lenya/cms/cocoon/source/ContentSource.java Fri Dec  2 01:52:49 2005
@@ -0,0 +1,184 @@
+/*
+ * Copyright  1999-2005 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.cocoon.source;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.PipedInputStream;
+import java.io.PipedOutputStream;
+
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+
+import org.apache.avalon.framework.container.ContainerUtil;
+import org.apache.avalon.framework.logger.LogEnabled;
+import org.apache.avalon.framework.logger.Logger;
+import org.apache.excalibur.source.Source;
+import org.apache.excalibur.source.SourceNotFoundException;
+import org.apache.excalibur.source.impl.AbstractSource;
+import org.apache.lenya.cms.repo.Document;
+import org.apache.lenya.cms.repo.RepositoryException;
+
+/**
+ * Repository source.
+ */
+public class ContentSource extends AbstractSource implements LogEnabled {
+
+    public ContentSource(Document document, Logger logger) {
+        ContainerUtil.enableLogging(this, logger);
+        this.document = document;
+    }
+
+    private Document document;
+
+    private Logger logger;
+
+    public void enableLogging(Logger logger) {
+        this.logger = logger;
+    }
+
+    protected Logger getLogger() {
+        return this.logger;
+    }
+
+    /**
+     * @see org.apache.excalibur.source.Source#exists()
+     */
+    public boolean exists() {
+        return this.document != null;
+    }
+
+    /**
+     * @see org.apache.excalibur.source.Source#getInputStream()
+     */
+    public InputStream getInputStream() throws IOException, SourceNotFoundException {
+        if (getLogger().isDebugEnabled())
+            getLogger().debug("Get InputStream for " + getURI());
+        if (!exists()) {
+            throw new SourceNotFoundException("The source [" + getURI() + "] does not exist!");
+        }
+        try {
+            return this.document.getInputStream();
+        } catch (RepositoryException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    protected InputStream convert(org.w3c.dom.Document edoc) throws IOException {
+
+        final org.w3c.dom.Document doc = edoc;
+        final PipedOutputStream pos = new PipedOutputStream();
+        PipedInputStream pis = new PipedInputStream();
+        pis.connect(pos);
+
+        (new Thread(new Runnable() {
+
+            public void run() {
+                try {
+                    transform(doc, pos);
+                } catch (TransformerException e) {
+                    throw new RuntimeException("Failed to tranform org.w3c.dom.Document to PipedOutputStream",
+                            e);
+                } finally {
+                    try {
+                        pos.close();
+                    } catch (Exception ignore) {
+                        ignore.printStackTrace();
+                    }
+                }
+            }
+        },
+                getClass().getName() + ".convert(org.w3c.dom.Document edoc)")).start();
+
+        return pis;
+    }
+
+    void transform(org.w3c.dom.Document edoc, PipedOutputStream pos) throws TransformerException {
+
+        TransformerFactory tFactory = TransformerFactory.newInstance();
+        Transformer transformer = tFactory.newTransformer();
+
+        transformer.setOutputProperty("encoding", "UTF-8");
+        transformer.setOutputProperty("indent", "yes");
+
+        transformer.transform(new DOMSource(edoc), new StreamResult(pos));
+
+    }
+
+    /**
+     * @see org.apache.excalibur.source.Source#getContentLength()
+     */
+    public long getContentLength() {
+        try {
+            return this.document.getContentLength();
+        } catch (RepositoryException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
+     * @see org.apache.excalibur.source.Source#getLastModified()
+     */
+    public long getLastModified() {
+        try {
+            return this.document.getLastModified();
+        } catch (RepositoryException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
+     * @see org.apache.excalibur.source.Source#getMimeType()
+     */
+    public String getMimeType() {
+        try {
+            return this.document.getContentNode().getDocumentType().getMimeType();
+        } catch (RepositoryException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
+     * 
+     */
+    public Source getParent() {
+        getLogger().warn("getParent() not implemented yet!");
+        return null;
+    }
+
+    /**
+     * 
+     */
+    public void makeCollection() {
+        getLogger().warn("RepositorySource().makeCollection() not implemented yet!");
+    }
+
+    /**
+     * 
+     */
+    public String getName() {
+        // Quick and dirty
+        String name = new java.io.File(getURI()).getName();
+        if (getLogger().isDebugEnabled())
+            getLogger().debug("getName(): URI: " + name);
+        return name;
+    }
+
+}

Added: lenya/trunk/src/java/org/apache/lenya/cms/cocoon/source/ContentSourceFactory.java
URL: http://svn.apache.org/viewcvs/lenya/trunk/src/java/org/apache/lenya/cms/cocoon/source/ContentSourceFactory.java?rev=351656&view=auto
==============================================================================
--- lenya/trunk/src/java/org/apache/lenya/cms/cocoon/source/ContentSourceFactory.java (added)
+++ lenya/trunk/src/java/org/apache/lenya/cms/cocoon/source/ContentSourceFactory.java Fri Dec  2 01:52:49 2005
@@ -0,0 +1,114 @@
+/*
+ * Copyright  1999-2005 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.cocoon.source;
+
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.util.Map;
+
+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.excalibur.source.Source;
+import org.apache.excalibur.source.SourceFactory;
+import org.apache.lenya.cms.repo.Area;
+import org.apache.lenya.cms.repo.ContentNode;
+import org.apache.lenya.cms.repo.Document;
+import org.apache.lenya.cms.repo.Publication;
+import org.apache.lenya.cms.repo.Repository;
+import org.apache.lenya.cms.repo.Session;
+import org.apache.lenya.cms.repo.SiteNode;
+import org.apache.lenya.cms.repo.avalon.RepositoryFactory;
+
+/**
+ * Repository source factory.
+ */
+public class ContentSourceFactory extends AbstractLogEnabled implements SourceFactory, Serviceable {
+
+    protected static final String SCHEME = "content";
+
+    public Source getSource(String location, Map parameters) throws IOException,
+            MalformedURLException {
+
+        RepositoryFactory factory = null;
+        Source source = null;
+
+        try {
+            if (this.repository == null) {
+                factory = (RepositoryFactory) this.manager.lookup(RepositoryFactory.ROLE);
+                this.repository = factory.getRepository();
+            }
+
+            Session session = this.repository.createSession();
+
+            String[] parts = location.split(":");
+
+            if (parts.length != 2) {
+                throw new MalformedURLException("The URL must be of the form [content:/locator]!");
+            }
+
+            final String scheme = parts[0];
+            if (!scheme.equals(SCHEME)) {
+                throw new MalformedURLException("The scheme must be [" + SCHEME + "]!");
+            }
+
+            final String locator = parts[1];
+            if (locator.startsWith("//")) {
+                String docIdentifier = locator.substring("//".length());
+                String[] steps = docIdentifier.split("/");
+                String pubId = steps[0];
+                String areaId = steps[1];
+                String language = steps[2];
+                String prefix = "//" + pubId + "/" + areaId + "/" + language;
+                String path = docIdentifier.substring(prefix.length());
+                Publication pub = session.getPublication(pubId);
+                Area area = pub.getArea(areaId);
+                SiteNode siteNode = area.getSite().getNode(path);
+                ContentNode contentNode = siteNode.getContentNode();
+                Document document = contentNode.getDocument(language);
+                source = new ContentSource(document, getLogger());
+            } else if (locator.startsWith("/")) {
+                throw new MalformedURLException("Only absolute locations are supported!");
+            } else {
+                throw new MalformedURLException("The locator must start with either one or two slashes.");
+            }
+
+        } catch (MalformedURLException e) {
+            throw e;
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        } finally {
+            if (factory != null) {
+                this.manager.release(factory);
+            }
+        }
+
+        return source;
+    }
+
+    public void release(Source source) {
+    }
+
+    private ServiceManager manager;
+    private Repository repository;
+
+    public void service(ServiceManager manager) throws ServiceException {
+        this.manager = manager;
+    }
+
+}



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