[Yanel-commits] rev 23449 - in public/yanel/trunk/src/resources: . navigation navigation/src navigation/src/build navigation/src/java navigation/src/java/org navigation/src/java/org/wyona navigation/src/java/org/wyona/yanel navigation/src/java/org/wyona/yanel/impl navigation/src/java/org/wyona/yanel/impl/resources

michi at wyona.com michi at wyona.com
Wed Mar 28 00:14:03 CEST 2007


Author: michi
Date: 2007-03-28 00:14:02 +0200 (Wed, 28 Mar 2007)
New Revision: 23449

Added:
   public/yanel/trunk/src/resources/navigation/
   public/yanel/trunk/src/resources/navigation/build.xml
   public/yanel/trunk/src/resources/navigation/resource.xml
   public/yanel/trunk/src/resources/navigation/src/
   public/yanel/trunk/src/resources/navigation/src/build/
   public/yanel/trunk/src/resources/navigation/src/build/build.properties
   public/yanel/trunk/src/resources/navigation/src/build/dependencies.xml
   public/yanel/trunk/src/resources/navigation/src/java/
   public/yanel/trunk/src/resources/navigation/src/java/org/
   public/yanel/trunk/src/resources/navigation/src/java/org/wyona/
   public/yanel/trunk/src/resources/navigation/src/java/org/wyona/yanel/
   public/yanel/trunk/src/resources/navigation/src/java/org/wyona/yanel/impl/
   public/yanel/trunk/src/resources/navigation/src/java/org/wyona/yanel/impl/resources/
   public/yanel/trunk/src/resources/navigation/src/java/org/wyona/yanel/impl/resources/NavigationResource.java
Log:
navigation resource added

Added: public/yanel/trunk/src/resources/navigation/build.xml
===================================================================
--- public/yanel/trunk/src/resources/navigation/build.xml	2007-03-27 22:11:41 UTC (rev 23448)
+++ public/yanel/trunk/src/resources/navigation/build.xml	2007-03-27 22:14:02 UTC (rev 23449)
@@ -0,0 +1,48 @@
+<?xml version="1.0"?>
+
+<project name="yanel-resource" default="compile">
+
+  <import file="src/build/dependencies.xml"/>
+
+  <target name="init" description="Initialize build" depends="dependencies">
+    <echo>Initialize build</echo>
+
+    <dirname property="resource.home.dir" file="${ant.file}"/>
+    <echo>Resource Home Directory: ${resource.home.dir}</echo>
+
+    <property file="${resource.home.dir}/src/build/local.build.properties"/>
+    <property file="${resource.home.dir}/src/build/build.properties"/>
+
+    <path id="classpath">
+      <pathelement path="${maven2.cp}"/>
+    </path>
+    <property name="resource.build.dir" value="${resource.home.dir}/build"/>
+    <property name="resource.classes.dir" value="${resource.build.dir}/classes"/>
+    <property name="resource.lib.dir" value="${resource.build.dir}/lib"/>
+    <property name="resource.java.dir" value="${resource.home.dir}/src/java"/>
+  </target>
+
+  <target name="compile" description="Compile Java classes" depends="init">
+
+    <mkdir dir="${resource.classes.dir}"/>
+    <javac srcdir="${resource.java.dir}" destdir="${resource.classes.dir}"
+      classpathref="classpath"
+      debug="true"
+    />
+    <mkdir dir="${resource.lib.dir}"/>
+    <jar destfile="${resource.lib.dir}/yanel-resource-${resource.name}-${resource.version}.jar"
+      basedir="${resource.classes.dir}"
+    />
+  </target>
+
+  <target name="clean" depends="init">
+    <delete dir="${resource.build.dir}"/>
+  </target>
+
+  <target name="copy-dependencies" depends="init">
+    <copy todir="${build.dir}/webapps/${servlet.context.prefix}/WEB-INF/lib">
+      <fileset refid="maven2.resource.fileset"/>
+      <mapper type="flatten"/>
+    </copy>
+  </target>
+</project>

Added: public/yanel/trunk/src/resources/navigation/resource.xml
===================================================================
--- public/yanel/trunk/src/resources/navigation/resource.xml	2007-03-27 22:11:41 UTC (rev 23448)
+++ public/yanel/trunk/src/resources/navigation/resource.xml	2007-03-27 22:14:02 UTC (rev 23449)
@@ -0,0 +1,17 @@
+<?xml version="1.0"?>
+
+<resource xmlns="http://www.wyona.org/yanel/1.0"
+  name="navigation"
+  namespace="http://www.wyona.org/yanel/resource/1.0"
+  class="org.wyona.yanel.impl.resources.NavigationResource"
+  >
+<description>
+Navigation XML for building menu etc.
+</description>
+
+<rtd>
+  <property name="mime-type"/>
+  <property name="xslt"/>
+  <property name="type"/>
+</rtd>
+</resource>

Added: public/yanel/trunk/src/resources/navigation/src/build/build.properties
===================================================================
--- public/yanel/trunk/src/resources/navigation/src/build/build.properties	2007-03-27 22:11:41 UTC (rev 23448)
+++ public/yanel/trunk/src/resources/navigation/src/build/build.properties	2007-03-27 22:14:02 UTC (rev 23449)
@@ -0,0 +1,2 @@
+resource.name=navigation
+resource.version=0.0.1-dev-rXXX

Added: public/yanel/trunk/src/resources/navigation/src/build/dependencies.xml
===================================================================
--- public/yanel/trunk/src/resources/navigation/src/build/dependencies.xml	2007-03-27 22:11:41 UTC (rev 23448)
+++ public/yanel/trunk/src/resources/navigation/src/build/dependencies.xml	2007-03-27 22:14:02 UTC (rev 23449)
@@ -0,0 +1,24 @@
+<?xml version="1.0"?>
+
+<project name="wiki-dependencies" xmlns:artifact="antlib:org.apache.maven.artifact.ant">
+
+  <target name="dependencies" description="Dependencies">
+    <!-- INFO: http://maven.apache.org/ant-tasks.html -->
+    <artifact:remoteRepository id="wyona.remote.repository" url="http://www.wyona.org/maven2/"/>
+
+    <artifact:dependencies pathId="maven2.classpath" filesetId="maven2.fileset">
+      <remoteRepository refid="wyona.remote.repository"/>
+      <dependency groupId="wyona-org-yanel" artifactId="yanel-core"
+                  version="${yanel.source.version}"/>
+    </artifact:dependencies>
+
+    <artifact:dependencies pathId="maven2.resource.classpath" filesetId="maven2.resource.fileset">
+      <remoteRepository refid="wyona.remote.repository"/>
+      <!-- No resource specific libs yet -->
+    </artifact:dependencies>
+
+    <property name="maven2.cp" refid="maven2.classpath"/>
+    <!--<echo>Maven2 classpath: ${maven2.cp}</echo>-->
+  </target>
+
+</project>

Added: public/yanel/trunk/src/resources/navigation/src/java/org/wyona/yanel/impl/resources/NavigationResource.java
===================================================================
--- public/yanel/trunk/src/resources/navigation/src/java/org/wyona/yanel/impl/resources/NavigationResource.java	2007-03-27 22:11:41 UTC (rev 23448)
+++ public/yanel/trunk/src/resources/navigation/src/java/org/wyona/yanel/impl/resources/NavigationResource.java	2007-03-27 22:14:02 UTC (rev 23449)
@@ -0,0 +1,440 @@
+/*
+ * Copyright 2006 Wyona
+ *
+ *  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.wyona.org/licenses/APACHE-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.wyona.yanel.impl.resources;
+
+import org.wyona.yanel.core.Path;
+import org.wyona.yanel.core.Resource;
+import org.wyona.yanel.core.ResourceConfiguration;
+import org.wyona.yanel.core.Topic;
+import org.wyona.yanel.core.Yanel;
+import org.wyona.yanel.core.api.attributes.CreatableV2;
+import org.wyona.yanel.core.api.attributes.ModifiableV2;
+import org.wyona.yanel.core.api.attributes.VersionableV2;
+import org.wyona.yanel.core.api.attributes.ViewableV1;
+import org.wyona.yanel.core.api.attributes.ViewableV2;
+import org.wyona.yanel.core.attributes.versionable.RevisionInformation;
+import org.wyona.yanel.core.attributes.viewable.View;
+import org.wyona.yanel.core.attributes.viewable.ViewDescriptor;
+
+import org.wyona.yanel.core.serialization.HTMLSerializer;
+import org.wyona.yanel.core.serialization.SerializerFactory;
+import org.wyona.yanel.core.source.ResourceResolver;
+import org.wyona.yanel.core.transformation.I18nTransformer;
+import org.wyona.yanel.core.transformation.I18nTransformer2;
+import org.wyona.yanel.core.transformation.XIncludeTransformer;
+import org.wyona.yanel.core.util.PathUtil;
+import org.wyona.yanel.core.util.ResourceAttributeHelper;
+
+import org.wyona.yarep.core.Node;
+import org.wyona.yarep.core.Repository;
+import org.wyona.yarep.core.RepositoryFactory;
+import org.wyona.yarep.core.Revision;
+import org.wyona.yarep.util.RepoPath;
+import org.xml.sax.InputSource;
+import org.xml.sax.XMLReader;
+import org.xml.sax.helpers.XMLReaderFactory;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import javax.xml.parsers.SAXParser;
+import javax.xml.parsers.SAXParserFactory;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.sax.SAXResult;
+import javax.xml.transform.sax.SAXSource;
+import javax.xml.transform.sax.SAXTransformerFactory;
+import javax.xml.transform.sax.TransformerHandler;
+import javax.xml.transform.stream.StreamSource;
+import javax.xml.transform.stream.StreamResult;
+
+import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.Reader;
+import java.io.Writer;
+import java.util.Date;
+import java.util.Properties;
+import java.util.jar.JarEntry;
+import java.util.jar.JarInputStream;
+
+import org.apache.log4j.Category;
+import org.apache.xml.resolver.tools.CatalogResolver;
+import org.apache.xml.serializer.Serializer;
+
+/**
+ *
+ */
+public class NavigationResource extends Resource implements ViewableV2, ModifiableV2 {
+
+    private static Category log = Category.getInstance(NavigationResource.class);
+
+    /**
+     *
+     */
+    public NavigationResource() {
+    }
+
+    /**
+     *
+     */
+    public ViewDescriptor[] getViewDescriptors() {
+        ViewDescriptor[] vd = new ViewDescriptor[2];
+        vd[0] = new ViewDescriptor("default");
+        // NOTE: depends on XSLT ...
+        vd[0].setMimeType(null);
+        vd[1] = new ViewDescriptor("source");
+        vd[1].setMimeType("application/xml");
+        return vd;
+    }
+
+    public View getView(String viewId) throws Exception {
+        return getView(viewId, null);
+    }
+
+    /**
+     * Generates view
+     */
+    public View getView(String viewId, String revisionName) throws Exception {
+        View defaultView = new View();
+        String mimeType = getMimeType(viewId);
+        defaultView.setMimeType(mimeType);
+
+        String siteTreePath = getResourceConfigProperty("sitetree");
+        String currentPath = (String)getParameters().get("path");
+        if (currentPath == null) currentPath = getPath();
+
+        try {
+            Repository repo = getRealm().getRepository();
+
+            if (viewId != null && viewId.equals("source")) {
+                defaultView.setInputStream(getContentXML(repo, siteTreePath));
+                defaultView.setMimeType("application/xml");
+                return defaultView;
+            }
+
+            String[] xsltPath = getXSLTPath(getPath());
+            if (xsltPath != null) {
+                
+                // create reader:
+                XMLReader xmlReader = XMLReaderFactory.createXMLReader();
+                CatalogResolver catalogResolver = new CatalogResolver();
+                xmlReader.setEntityResolver(catalogResolver);
+                
+                // create xslt transformer:
+                SAXTransformerFactory tf = (SAXTransformerFactory)TransformerFactory.newInstance();
+                
+                TransformerHandler[] xsltHandlers = new TransformerHandler[xsltPath.length];
+                for (int i = 0; i < xsltPath.length; i++) {
+                    xsltHandlers[i] = tf.newTransformerHandler(new StreamSource(repo.getNode(xsltPath[i]).getInputStream()));
+                    xsltHandlers[i].getTransformer().setParameter("yanel.path.name", PathUtil.getName(getPath()));
+                    xsltHandlers[i].getTransformer().setParameter("yanel.path", getPath());
+                    xsltHandlers[i].getTransformer().setParameter("yanel.back2context", PathUtil.backToContext(realm, getPath()));
+                    xsltHandlers[i].getTransformer().setParameter("yarep.back2realm", PathUtil.backToRealm(getPath()));
+                    String userAgent = getRequest().getHeader("User-Agent");
+                    String os = getOS(userAgent);
+                    if (os != null) xsltHandlers[i].getTransformer().setParameter("os", os);
+                    String client = getClient(userAgent);
+                    if (client != null) xsltHandlers[i].getTransformer().setParameter("client", client);
+                    xsltHandlers[i].getTransformer().setParameter("language", getLanguage());
+                    xsltHandlers[i].getTransformer().setParameter("currentPath", currentPath);
+                }
+                
+                // create i18n transformer:
+                I18nTransformer2 i18nTransformer = new I18nTransformer2("global", getLanguage(), getRealm().getDefaultLanguage());
+                i18nTransformer.setEntityResolver(catalogResolver);
+                
+                // create xinclude transformer:
+                XIncludeTransformer xIncludeTransformer = new XIncludeTransformer();
+                ResourceResolver resolver = new ResourceResolver(this);
+                xIncludeTransformer.setResolver(resolver);
+                
+                // create serializer:
+                Serializer serializer = SerializerFactory.getSerializer(SerializerFactory.XHTML_STRICT);
+                ByteArrayOutputStream baos = new ByteArrayOutputStream();
+                
+                // chain everything together (create a pipeline):
+                xmlReader.setContentHandler(xsltHandlers[0]);
+                for (int i=0; i<xsltHandlers.length-1; i++) {
+                    xsltHandlers[i].setResult(new SAXResult(xsltHandlers[i+1]));
+                }
+                xsltHandlers[xsltHandlers.length-1].setResult(new SAXResult(xIncludeTransformer));
+                xIncludeTransformer.setResult(new SAXResult(i18nTransformer));
+                i18nTransformer.setResult(new SAXResult(serializer.asContentHandler()));
+                serializer.setOutputStream(baos);
+                
+                // execute pipeline:
+                xmlReader.parse(new InputSource(getContentXML(repo, siteTreePath)));
+                
+                // write result into view:
+                defaultView.setInputStream(new ByteArrayInputStream(baos.toByteArray()));
+                return defaultView;
+            } else {
+                log.debug("Mime-Type: " + mimeType);
+                defaultView.setInputStream(getContentXML(repo, siteTreePath));
+            }
+        } catch(Exception e) {
+            log.error(e + " (" + getPath() + ", " + getRealm() + ")", e);
+            throw new Exception(e);
+        }
+
+        return defaultView;
+    }
+
+    /**
+     * Get language with the following priorization: 1) yanel.meta.language query string parameter, 2) Accept-Language header, 3) Default en
+     */
+    private String getLanguage() {
+        String language = getRequest().getParameter("yanel.meta.language");
+        if (language == null) {
+            language = getRequest().getParameter("Accept-Language");
+        }
+        if(language != null && language.length() > 0) return language;
+        return getRealm().getDefaultLanguage();
+    }
+
+
+    /**
+     *
+     */
+    private InputStream getContentXML(Repository repo, String path) throws Exception {
+        Node node = repo.getNode(path);
+        return node.getInputStream();
+    }
+
+    /**
+     * Get mime type
+     */
+    public String getMimeType(String viewId) throws Exception {
+        String mimeType = getResourceConfigProperty("mime-type");
+        if (mimeType != null) return mimeType;
+
+        String suffix = PathUtil.getSuffix(getPath());
+        if (suffix != null) {
+            log.debug("SUFFIX: " + suffix);
+            if (suffix.equals("html")) {
+                //mimeType = "text/html";
+                mimeType = "application/xhtml+xml";
+            } else if (suffix.equals("xhtml")) {
+                mimeType = "application/xhtml+xml";
+            } else if (suffix.equals("xml")) {
+                mimeType = "application/xml";
+            } else {
+                mimeType = "application/xml";
+            }
+        } else {
+            mimeType = "application/xml";
+        }
+        return mimeType;
+    }
+
+    /**
+     *
+     */
+    public Reader getReader() throws Exception {
+        return new InputStreamReader(getInputStream(), "UTF-8");
+    }
+
+    /**
+     *
+     */
+    public InputStream getInputStream() throws Exception {
+        return getRealm().getRepository().getNode(getPath()).getInputStream();
+    }
+
+    /**
+     *
+     */
+    public Writer getWriter() throws Exception {
+        log.error("Not implemented yet!");
+        return null;
+    }
+
+    /**
+     *
+     */
+    public OutputStream getOutputStream() throws Exception {
+        return getRealm().getRepository().getNode(getPath()).getOutputStream();
+    }
+
+    /**
+     *
+     */
+    public void write(InputStream in) throws Exception {
+        log.warn("Not implemented yet!");
+    }
+
+    /**
+     *
+     */
+    public long getLastModified() throws Exception {
+        Node node = getRealm().getRepository().getNode(getPath());
+        long lastModified;
+        if (node.isResource()) {
+            lastModified = node.getLastModified();
+        } else {
+            lastModified = 0;
+        }
+
+        return lastModified;
+    }
+
+    /**
+     * Get XSLT path
+     */
+    private String[] getXSLTPath(String path) throws Exception {
+        String[] xsltPath = getResourceConfigProperties("xslt");
+        if (xsltPath != null) return xsltPath;
+        log.info("No XSLT Path within: " + path);
+        return null;
+    }
+
+
+    /**
+     *
+     */
+    public boolean delete() throws Exception {
+        getRealm().getRepository().getNode(getPath()).delete();
+        return true;
+    }
+
+    public boolean exists() throws Exception {
+        log.warn("Not implemented yet!");
+        return true; 
+    }
+
+    /**
+     * Get size of generated page
+     */
+    public long getSize() throws Exception {
+        // TODO: If the XML is being transformed then the size will not be the same as the size of the node!
+/*
+        Node node = getRealm().getRepository().getNode(getPath());
+        long size;
+        if (node.isResource()) {
+            size = node.getSize();
+        } else {
+            size = 0;
+        }
+        return size;
+*/
+        return -1;
+    }
+
+    /**
+     * Get operating system
+     */
+    public String getOS(String userAgent) {
+        if (userAgent.indexOf("Linux") > 0) {
+            return "unix";
+        } else if (userAgent.indexOf("Mac OS X") > 0) {
+            return "unix";
+        } else if (userAgent.indexOf("Windows") > 0) {
+            return "windows";
+        } else {
+            log.warn("Operating System could not be recognized: " + userAgent);
+            return null;
+        }
+    }
+
+    /**
+     * Get client
+     */
+    public String getClient(String userAgent) {
+        if (userAgent.indexOf("Firefox") > 0) {
+            return "firefox";
+        } else if (userAgent.indexOf("MSIE") > 0) {
+            return "msie";
+        } else {
+            log.warn("Client could not be recognized: " + userAgent);
+            return null;
+        }
+    }
+    
+    /**
+     * Get property value from resource configuration
+     */
+    private String getResourceConfigProperty(String name) throws Exception {
+    	ResourceConfiguration rc = getConfiguration();
+    	if (rc != null) return rc.getProperty(name);
+    	return getRTI().getProperty(name);
+    }
+    
+    /**
+     * Get property value from resource configuration
+     */
+    private String[] getResourceConfigProperties(String name) throws Exception {
+    	ResourceConfiguration rc = getConfiguration();
+    	if (rc != null) return rc.getProperties(name);
+    	return getRTI().getProperties(name);
+    }
+
+
+    /**
+     *
+     */
+    public String getPropertyType(String name) {
+        log.warn("Not implemented yet!");
+        return null;
+    }
+
+    /**
+     *
+     */
+    public Object getProperty(String name) {
+        log.warn("Not implemented yet!");
+        return null;
+    }
+
+    /**
+     *
+     */
+    public String[] getPropertyNames() {
+        log.warn("Not implemented yet!");
+        return null;
+    }
+
+    /**
+     *
+     */
+    public void setProperty(String name, Object value) {
+        log.warn("Not implemented yet!");
+    }
+    
+    /**
+     * @param name
+     * @return introspection as string
+     */
+    private String getIntrospection(String name) {
+        StringBuffer sb = new StringBuffer();
+        sb.append("<?xml version=\"1.0\"?>");
+        sb.append("\n");
+        sb.append("\n<introspection xmlns=\"http://www.wyona.org/neutron/1.0\">");
+        sb.append("\n");
+        sb.append("\n  <edit mime-type=\"application/xhtml+xml\" name=\"" + name + "\">");
+        sb.append("\n    <checkout url=\"" + name + "?yanel.resource.viewid=source&amp;yanel.resource.usecase=checkout\" method=\"GET\"/>");
+        sb.append("\n    <checkin  url=\"" + name + "?yanel.resource.usecase=checkin\" method=\"PUT\"/>");
+        sb.append("\n  </edit>");
+        sb.append("\n</introspection>");
+        
+        return sb.toString();
+    }    
+}




More information about the Yanel-commits mailing list