[Yanel-commits] rev 23409 - in public/yanel/trunk/src/core/java/org/wyona/yanel/core: . source transformation

michi at wyona.com michi at wyona.com
Mon Mar 26 15:41:48 CEST 2007


Author: michi
Date: 2007-03-26 15:41:47 +0200 (Mon, 26 Mar 2007)
New Revision: 23409

Added:
   public/yanel/trunk/src/core/java/org/wyona/yanel/core/source/
   public/yanel/trunk/src/core/java/org/wyona/yanel/core/source/ResourceResolver.java
   public/yanel/trunk/src/core/java/org/wyona/yanel/core/source/SourceException.java
   public/yanel/trunk/src/core/java/org/wyona/yanel/core/source/SourceResolver.java
   public/yanel/trunk/src/core/java/org/wyona/yanel/core/transformation/XIncludeTransformer.java
Log:
xinclude transformer added

Added: public/yanel/trunk/src/core/java/org/wyona/yanel/core/source/ResourceResolver.java
===================================================================
--- public/yanel/trunk/src/core/java/org/wyona/yanel/core/source/ResourceResolver.java	2007-03-26 13:30:06 UTC (rev 23408)
+++ public/yanel/trunk/src/core/java/org/wyona/yanel/core/source/ResourceResolver.java	2007-03-26 13:41:47 UTC (rev 23409)
@@ -0,0 +1,71 @@
+package org.wyona.yanel.core.source;
+
+import org.apache.log4j.Category;
+import org.wyona.yanel.core.Path;
+import org.wyona.yanel.core.Resource;
+import org.wyona.yanel.core.ResourceManager;
+import org.wyona.yanel.core.Yanel;
+import org.wyona.yanel.core.api.attributes.ViewableV1;
+import org.wyona.yanel.core.api.attributes.ViewableV2;
+import org.wyona.yanel.core.attributes.viewable.View;
+import org.wyona.yanel.core.util.ResourceAttributeHelper;
+import org.xml.sax.InputSource;
+
+/**
+ * Source resolver for yanel resources.
+ * It returns an InputSource made from the view of the resource with
+ * the given uri. It will look for the resource in the realm of the resource
+ * which is passed to the constructor of this ResourceResolver.
+ * 
+ * It uses the following URI syntax:
+ * yanelresource:/path/to/resource
+ * 
+ * TODO: support relative uris?
+ */
+public class ResourceResolver implements SourceResolver {
+
+    private static Category log = Category.getInstance(ResourceResolver.class);
+    
+    private static final String SCHEME = "yanelresource";
+
+    private Resource resource;
+    
+    public ResourceResolver(Resource resource) {
+        this.resource = resource;
+    }
+    
+    /**
+     * @see org.wyona.yanel.core.source.SourceResolver#resolve(java.lang.String)
+     */
+    public InputSource resolve(String uri) throws SourceException {
+        log.debug("resolving: " + uri);
+        String prefix = SCHEME + ":";
+        if (!uri.startsWith(prefix)) {
+            log.error("unknown scheme: " + uri);
+            return null;
+        }
+        try {
+            uri = uri.substring(prefix.length());
+            ResourceManager manager = Yanel.getInstance().getResourceManager();
+            Resource targetResource = manager.getResource(resource.getRequest(), resource.getResponse(), 
+                    resource.getRealm(), uri);
+            if (ResourceAttributeHelper.hasAttributeImplemented(targetResource, "Viewable", "1")) {
+                String viewV1path = resource.getRealm().getMountPoint() + uri.substring(1);
+                log.debug("including document: " + viewV1path);
+                View view = ((ViewableV1) targetResource).getView(new Path(viewV1path), null);
+                return new InputSource(view.getInputStream());
+            } else if (ResourceAttributeHelper.hasAttributeImplemented(targetResource, "Viewable", "2")) {
+                View view = ((ViewableV2) targetResource).getView(null);
+                return new InputSource(view.getInputStream());
+            } else {
+                log.warn("Resource is not viewable: " + uri);
+                return null;
+            }
+        } catch (Exception e) {
+            String errorMsg = "Could not resolve URI: " + uri + ": " + e.toString();
+            log.error(errorMsg, e);
+            throw new SourceException(errorMsg, e);
+        }
+    }
+    
+}

Added: public/yanel/trunk/src/core/java/org/wyona/yanel/core/source/SourceException.java
===================================================================
--- public/yanel/trunk/src/core/java/org/wyona/yanel/core/source/SourceException.java	2007-03-26 13:30:06 UTC (rev 23408)
+++ public/yanel/trunk/src/core/java/org/wyona/yanel/core/source/SourceException.java	2007-03-26 13:41:47 UTC (rev 23409)
@@ -0,0 +1,36 @@
+package org.wyona.yanel.core.source;
+
+/**
+ * 
+ */
+public class SourceException extends Exception {
+
+    /**
+     *
+     */
+    public SourceException() {
+        super();
+    }
+
+    /**
+     *
+     */
+    public SourceException(Throwable t) {
+        super(t);
+    }
+
+    /**
+     *
+     */
+    public SourceException(String s) {
+        super(s);
+    }
+
+    /**
+     *
+     */
+    public SourceException(String s, Throwable t) {
+        super(s, t);
+    }
+
+}

Added: public/yanel/trunk/src/core/java/org/wyona/yanel/core/source/SourceResolver.java
===================================================================
--- public/yanel/trunk/src/core/java/org/wyona/yanel/core/source/SourceResolver.java	2007-03-26 13:30:06 UTC (rev 23408)
+++ public/yanel/trunk/src/core/java/org/wyona/yanel/core/source/SourceResolver.java	2007-03-26 13:41:47 UTC (rev 23409)
@@ -0,0 +1,13 @@
+package org.wyona.yanel.core.source;
+
+import org.xml.sax.InputSource;
+
+/**
+ * Resolves a URI to a InputSource.
+ * TODO: make this more generic, i.e. return a more generic source object.
+ */
+public interface SourceResolver {
+
+    InputSource resolve(String uri) throws SourceException;
+    
+}

Added: public/yanel/trunk/src/core/java/org/wyona/yanel/core/transformation/XIncludeTransformer.java
===================================================================
--- public/yanel/trunk/src/core/java/org/wyona/yanel/core/transformation/XIncludeTransformer.java	2007-03-26 13:30:06 UTC (rev 23408)
+++ public/yanel/trunk/src/core/java/org/wyona/yanel/core/transformation/XIncludeTransformer.java	2007-03-26 13:41:47 UTC (rev 23409)
@@ -0,0 +1,84 @@
+package org.wyona.yanel.core.transformation;
+
+import org.apache.log4j.Category;
+import org.apache.xml.resolver.tools.CatalogResolver;
+import org.wyona.yanel.core.source.SourceResolver;
+import org.xml.sax.Attributes;
+import org.xml.sax.SAXException;
+import org.xml.sax.XMLReader;
+import org.xml.sax.helpers.XMLReaderFactory;
+
+/**
+ * Transformer to include xml into a sax stream.
+ * It will resolve <xi:include href="bla.xml"/>. 
+ * The namespace uri is http://www.w3.org/2001/XInclude
+ * This is not a complete implementation of the xinclude specification.
+ * @see http://www.w3.org/TR/xinclude/
+ */
+public class XIncludeTransformer extends AbstractTransformer {
+
+    private static Category log = Category.getInstance(XIncludeTransformer.class);
+
+    public static final String NS_URI = "http://www.w3.org/2001/XInclude";
+    
+    private SourceResolver resolver;
+    private boolean ignoreDocumentEvent;
+    private boolean insideIncludeElement;
+    
+    public XIncludeTransformer() {
+        this.ignoreDocumentEvent = false;
+        this.insideIncludeElement = false;
+    }
+
+    public void setResolver(SourceResolver resolver) {
+        this.resolver = resolver;
+    }
+
+    public void startDocument() throws SAXException {
+        if (!this.ignoreDocumentEvent) {
+            super.startDocument();
+        }
+    }
+    
+    public void endDocument() throws SAXException {
+        if (!this.ignoreDocumentEvent) {
+            super.endDocument();
+        }
+    }
+    
+    public void startElement(String namespaceURI, String localName, String qName, Attributes attrs) throws SAXException {
+        
+        if (namespaceURI.equals(NS_URI) && localName.equals("include")) {
+            String href = attrs.getValue("href");
+         
+            XMLReader xmlReader = XMLReaderFactory.createXMLReader();
+            CatalogResolver catalogResolver = new CatalogResolver();
+            xmlReader.setEntityResolver(catalogResolver);
+            xmlReader.setContentHandler(this);
+            
+            this.ignoreDocumentEvent = true;
+            try {
+                xmlReader.parse(resolver.resolve(href));
+            } catch (Exception e) {
+                log.error("XInclude error for href: " + href + ":  " + e, e);
+                throw new SAXException(e);
+            }
+            this.ignoreDocumentEvent = false;
+            this.insideIncludeElement = true;
+
+        } else {
+            if (!this.insideIncludeElement) {
+                super.startElement(namespaceURI, localName, qName, attrs);
+            }
+        }
+    }
+
+    public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
+        if (!this.insideIncludeElement) {
+            super.endElement(namespaceURI, localName, qName);
+        } else if (namespaceURI.equals(NS_URI) && localName.equals("include")) {
+            this.insideIncludeElement = false;
+        }
+    }
+
+}




More information about the Yanel-commits mailing list