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 2009/02/10 23:34:46 UTC

svn commit: r743140 - in /lenya/trunk/org.apache.lenya.core.resourcetype: ./ src/ src/main/ src/main/java/ src/main/java/org/ src/main/java/org/apache/ src/main/java/org/apache/lenya/ src/main/java/org/apache/lenya/cms/ src/main/java/org/apache/lenya/c...

Author: andreas
Date: Tue Feb 10 22:34:45 2009
New Revision: 743140

URL: http://svn.apache.org/viewvc?rev=743140&view=rev
Log:
Adding resourcetype module.

Added:
    lenya/trunk/org.apache.lenya.core.resourcetype/
    lenya/trunk/org.apache.lenya.core.resourcetype/pom.xml
    lenya/trunk/org.apache.lenya.core.resourcetype/src/
    lenya/trunk/org.apache.lenya.core.resourcetype/src/main/
    lenya/trunk/org.apache.lenya.core.resourcetype/src/main/java/
    lenya/trunk/org.apache.lenya.core.resourcetype/src/main/java/org/
    lenya/trunk/org.apache.lenya.core.resourcetype/src/main/java/org/apache/
    lenya/trunk/org.apache.lenya.core.resourcetype/src/main/java/org/apache/lenya/
    lenya/trunk/org.apache.lenya.core.resourcetype/src/main/java/org/apache/lenya/cms/
    lenya/trunk/org.apache.lenya.core.resourcetype/src/main/java/org/apache/lenya/cms/publication/
    lenya/trunk/org.apache.lenya.core.resourcetype/src/main/java/org/apache/lenya/cms/publication/ResourceTypeFactory.java
    lenya/trunk/org.apache.lenya.core.resourcetype/src/main/java/org/apache/lenya/cms/publication/ResourceTypeImpl.java
    lenya/trunk/org.apache.lenya.core.resourcetype/src/main/resources/
    lenya/trunk/org.apache.lenya.core.resourcetype/src/main/resources/META-INF/
    lenya/trunk/org.apache.lenya.core.resourcetype/src/main/resources/META-INF/cocoon/
    lenya/trunk/org.apache.lenya.core.resourcetype/src/main/resources/META-INF/cocoon/spring/
    lenya/trunk/org.apache.lenya.core.resourcetype/src/main/resources/META-INF/cocoon/spring/lenya-core-resourcetype-components.xml

Added: lenya/trunk/org.apache.lenya.core.resourcetype/pom.xml
URL: http://svn.apache.org/viewvc/lenya/trunk/org.apache.lenya.core.resourcetype/pom.xml?rev=743140&view=auto
==============================================================================
--- lenya/trunk/org.apache.lenya.core.resourcetype/pom.xml (added)
+++ lenya/trunk/org.apache.lenya.core.resourcetype/pom.xml Tue Feb 10 22:34:45 2009
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+  <parent>
+    <groupId>org.apache.lenya</groupId>
+    <artifactId>lenya-parent</artifactId>
+    <version>2.2.0-SNAPSHOT</version>
+    <relativePath>../org.apache.lenya.parent/pom.xml</relativePath>
+  </parent>
+  <groupId>org.apache.lenya</groupId>
+  <artifactId>lenya-core-resourcetype</artifactId>
+  <packaging>jar</packaging>
+  <name>Apache Lenya Resource Type</name>
+  
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.avalon.framework</groupId>
+      <artifactId>avalon-framework-api</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.lenya</groupId>
+      <artifactId>lenya-core-api</artifactId>
+    </dependency>
+  </dependencies>
+</project>

Added: lenya/trunk/org.apache.lenya.core.resourcetype/src/main/java/org/apache/lenya/cms/publication/ResourceTypeFactory.java
URL: http://svn.apache.org/viewvc/lenya/trunk/org.apache.lenya.core.resourcetype/src/main/java/org/apache/lenya/cms/publication/ResourceTypeFactory.java?rev=743140&view=auto
==============================================================================
--- lenya/trunk/org.apache.lenya.core.resourcetype/src/main/java/org/apache/lenya/cms/publication/ResourceTypeFactory.java (added)
+++ lenya/trunk/org.apache.lenya.core.resourcetype/src/main/java/org/apache/lenya/cms/publication/ResourceTypeFactory.java Tue Feb 10 22:34:45 2009
@@ -0,0 +1,50 @@
+/*
+ * 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.publication;
+
+import org.apache.avalon.framework.configuration.Configuration;
+import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
+import org.apache.excalibur.source.Source;
+import org.apache.excalibur.source.SourceResolver;
+
+public class ResourceTypeFactory {
+    
+    private SourceResolver sourceResolver;
+    
+    public ResourceType createResourceType(String configUri) throws Exception {
+        Source source = null;
+        try {
+            source = this.sourceResolver.resolveURI(configUri);
+            DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder();
+            Configuration config = builder.build(source.getInputStream());
+            ResourceTypeImpl type = new ResourceTypeImpl();
+            type.configure(config);
+            return type;
+        }
+        finally {
+            if (source != null) {
+                this.sourceResolver.release(source);
+            }
+        }
+    }
+
+    public void setSourceResolver(SourceResolver sourceResolver) {
+        this.sourceResolver = sourceResolver;
+    }
+
+}

Added: lenya/trunk/org.apache.lenya.core.resourcetype/src/main/java/org/apache/lenya/cms/publication/ResourceTypeImpl.java
URL: http://svn.apache.org/viewvc/lenya/trunk/org.apache.lenya.core.resourcetype/src/main/java/org/apache/lenya/cms/publication/ResourceTypeImpl.java?rev=743140&view=auto
==============================================================================
--- lenya/trunk/org.apache.lenya.core.resourcetype/src/main/java/org/apache/lenya/cms/publication/ResourceTypeImpl.java (added)
+++ lenya/trunk/org.apache.lenya.core.resourcetype/src/main/java/org/apache/lenya/cms/publication/ResourceTypeImpl.java Tue Feb 10 22:34:45 2009
@@ -0,0 +1,233 @@
+/*
+ * 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.publication;
+
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.avalon.framework.configuration.Configuration;
+import org.apache.avalon.framework.configuration.ConfigurationException;
+import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
+import org.apache.commons.lang.Validate;
+import org.apache.excalibur.source.Source;
+import org.apache.excalibur.source.SourceResolver;
+import org.apache.lenya.xml.Schema;
+
+/**
+ * Resource type.
+ * 
+ * @version $Id:$
+ */
+public class ResourceTypeImpl implements ResourceType {
+
+    /**
+     * The default sample name.
+     */
+    public static final String DEFAULT_SAMPLE_NAME = "Default Sample";
+
+    protected static final String ATTRIBUTE_URI = "uri";
+    protected static final String ATTRIBUTE_NAME = "name";
+    protected static final String ELEMENT_SCHEMA = "schema";
+    protected static final String ATTRIBUTE_NAMESPACE = "namespace";
+    protected static final String ELEMENT_REWRITE_ATTRIBUTE = "link-attribute";
+    protected static final String ATTRIBUTE_XPATH = "xpath";
+    protected static final String ELEMENT_SAMPLES = "samples";
+    protected static final String ELEMENT_SAMPLE = "sample";
+    protected static final String ATTRIBUTE_MIME_TYPE = "mime-type";
+    protected static final String ELEMENT_FORMAT = "format";
+    protected static final String ELEMENT_EXPIRES = "expires";
+    protected static final String ATTRIBUTE_SECONDS = "seconds";
+
+    private Schema schema = null;
+    private String[] linkAttributeXPaths;
+    private Map samples;
+    private String samplesUri = null;
+    private Map formats = new HashMap();
+    private long expires = 0;
+    private String name;
+
+    private SourceResolver resolver;
+
+    /**
+     * A format.
+     */
+    public static class Format {
+        private String uri;
+
+        /**
+         * @param uri The uri.
+         */
+        public Format(String uri) {
+            Validate.notNull(uri);
+            this.uri = uri;
+        }
+
+        /**
+         * @return The uri.
+         */
+        public String getURI() {
+            return this.uri;
+        }
+    }
+
+    public void configure(Configuration config) throws Exception {
+        
+        this.name = config.getAttribute(ATTRIBUTE_NAME);
+
+        Configuration schemaConf = config.getChild(ELEMENT_SCHEMA, false);
+
+        if (schemaConf != null) {
+            String uri = schemaConf.getAttribute(ATTRIBUTE_URI);
+            String language = schemaConf.getAttribute(ATTRIBUTE_NAMESPACE);
+            this.schema = new Schema(language, uri);
+        }
+
+        // determine the sample content locations.
+        Configuration samplesFileConf = config.getChild(ELEMENT_SAMPLES, false);
+        if (samplesFileConf != null) {
+            this.samplesUri = samplesFileConf.getAttribute(ATTRIBUTE_URI);
+        } else {
+            this.samples = loadSamples(config);
+        }
+
+        Configuration[] rewriteAttributeConfigs = config.getChildren(ELEMENT_REWRITE_ATTRIBUTE);
+        List xPaths = new ArrayList();
+        for (int i = 0; i < rewriteAttributeConfigs.length; i++) {
+            String xPath = rewriteAttributeConfigs[i].getAttribute(ATTRIBUTE_XPATH);
+            xPaths.add(xPath);
+        }
+        this.linkAttributeXPaths = (String[]) xPaths.toArray(new String[xPaths.size()]);
+
+        Configuration[] formatConfigs = config.getChildren(ELEMENT_FORMAT);
+        for (int i = 0; i < formatConfigs.length; i++) {
+            String name = formatConfigs[i].getAttribute(ATTRIBUTE_NAME);
+            String uri = formatConfigs[i].getAttribute(ATTRIBUTE_URI);
+            this.formats.put(name, new Format(uri));
+        }
+
+        Configuration expiresConf = config.getChild(ELEMENT_EXPIRES, false);
+        if (expiresConf != null) {
+            this.expires = expiresConf.getAttributeAsLong(ATTRIBUTE_SECONDS);
+        }
+
+    }
+
+    protected Map loadSamples(Configuration samplesParentConfig) throws ConfigurationException {
+        Configuration[] samplesConf = samplesParentConfig.getChildren(ELEMENT_SAMPLE);
+        Map samples = new LinkedHashMap();
+        for (int i = 0; i < samplesConf.length; i++) {
+            String name = samplesConf[i].getAttribute(ATTRIBUTE_NAME, DEFAULT_SAMPLE_NAME);
+            String mimeType = samplesConf[i].getAttribute(ATTRIBUTE_MIME_TYPE);
+            String uri = samplesConf[i].getAttribute(ATTRIBUTE_URI);
+            samples.put(name, new Sample(name, mimeType, uri));
+        }
+        return samples;
+    }
+
+    protected Configuration readConfiguration(String uri) throws ConfigurationException {
+        Configuration config;
+        DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder();
+        Source source = null;
+        try {
+            source = resolver.resolveURI(uri);
+            config = builder.build(source.getInputStream());
+        } catch (Exception e) {
+            throw new ConfigurationException("Loading samples from URI [" + uri + "] failed: ", e);
+        } finally {
+            if (source != null) {
+                resolver.release(source);
+            }
+        }
+        return config;
+    }
+
+    public Date getExpires() {
+        Date date = new Date();
+        date.setTime(date.getTime() + this.expires * 1000l);
+        return date;
+    }
+
+    public Schema getSchema() {
+        return this.schema;
+    }
+
+    public String[] getLinkAttributeXPaths() {
+        return this.linkAttributeXPaths;
+    }
+
+    protected Map getSamples() {
+        if (this.samplesUri == null) {
+            return this.samples;
+        } else {
+            try {
+                Configuration samplesConfig = readConfiguration(this.samplesUri);
+                return loadSamples(samplesConfig);
+            } catch (ConfigurationException e) {
+                throw new RuntimeException(e);
+            }
+
+        }
+    }
+
+    public String[] getSampleNames() {
+        Set names = getSamples().keySet();
+        return (String[]) names.toArray(new String[names.size()]);
+    }
+
+    public Sample getSample(String name) {
+        Map samples = getSamples();
+        if (!samples.containsKey(name)) {
+            throw new IllegalArgumentException("No sample with name [" + name + "] found.");
+        }
+        return (Sample) samples.get(name);
+    }
+
+    public void setName(String name) {
+        Validate.notNull(name);
+        this.name = name;
+    }
+
+    public String getName() {
+        return this.name;
+    }
+
+    public String[] getFormats() {
+        Set names = this.formats.keySet();
+        return (String[]) names.toArray(new String[names.size()]);
+    }
+
+    public String getFormatURI(String format) {
+
+        if (!this.formats.containsKey(format)) {
+            throw new RuntimeException("The resource type [" + getName()
+                    + "] does not support the format [" + format + "].");
+        }
+
+        return ((Format) this.formats.get(format)).getURI();
+    }
+
+    public String toString() {
+        return getName();
+    }
+
+}

Added: lenya/trunk/org.apache.lenya.core.resourcetype/src/main/resources/META-INF/cocoon/spring/lenya-core-resourcetype-components.xml
URL: http://svn.apache.org/viewvc/lenya/trunk/org.apache.lenya.core.resourcetype/src/main/resources/META-INF/cocoon/spring/lenya-core-resourcetype-components.xml?rev=743140&view=auto
==============================================================================
--- lenya/trunk/org.apache.lenya.core.resourcetype/src/main/resources/META-INF/cocoon/spring/lenya-core-resourcetype-components.xml (added)
+++ lenya/trunk/org.apache.lenya.core.resourcetype/src/main/resources/META-INF/cocoon/spring/lenya-core-resourcetype-components.xml Tue Feb 10 22:34:45 2009
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  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.
+-->
+<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://www.springframework.org/schema/beans
+  http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"
+  xmlns="http://www.springframework.org/schema/beans">
+
+  <bean name="org.apache.lenya.cms.publication.ResourceTypeFactory"
+    class="org.apache.lenya.cms.publication.ResourceTypeFactory">
+    <property name="sourceResolver" ref="org.apache.excalibur.source.SourceResolver"/>
+  </bean>
+  
+</beans>



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