[Yanel-dev] SerializerFactory and ResourceResolver patch

Evaldas Taroza etaroza at optaros.com
Fri Jan 18 09:03:02 CET 2008


I added TEXT serializer, it will use the internal Xalan text serializer.

I added Constants class, where I suggest to put all the constants, in =

order to minimize hardcoded values in Yanel.

Modified ResourceResovler. The problem was that such request didn't work =

properly:
yanelresource:/en/sitetree.xml?yanel.resource.viewid=3Dmyview
The problem was, that even though the parameters were passed to the =

resolved resource, the view was not detected, so Resource.getView(null) =

would be called instead of Resource.getView("myview").

Evaldas

-- =

+41 79 616 53 76
Optaros - www.optaros.com
-------------- next part --------------
Index: java/org/wyona/yanel/core/Constants.java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- java/org/wyona/yanel/core/Constants.java	(revision 0)
+++ java/org/wyona/yanel/core/Constants.java	(revision 0)
@@ -0,0 +1,25 @@
+package org.wyona.yanel.core;
+
+
+public abstract class Constants {
+    /**
+     * General parameters and values for passing requests to Yanel (e.g. h=
ttp://.../yanel/resource.xml?param=3Dvalue) =

+     * */
+    public static interface Request{
+        /**
+         * Controls which view a viewable resource should choose
+         * */
+        public static final String YANEL_RESOURCE_VIEWID =3D "yanel.resour=
ce.viewid";
+        /**
+         * The value for the default view
+         * */
+        public static final String DEFAULT_VIEW_ID =3D "default";
+        =

+        /**
+         * The value for the source view
+         * */
+        public static final String SOURCE_VIEW_ID =3D "source";
+    }
+    =

+    private Constants(){};
+}

Property changes on: java\org\wyona\yanel\core\Constants.java
___________________________________________________________________
Name: svn:eol-style
   + LF

Index: java/org/wyona/yanel/core/serialization/SerializerFactory.java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- java/org/wyona/yanel/core/serialization/SerializerFactory.java	(revisio=
n 30347)
+++ java/org/wyona/yanel/core/serialization/SerializerFactory.java	(working=
 copy)
@@ -2,66 +2,84 @@
 =

 import java.util.Properties;
 import org.apache.log4j.Category;
+import org.apache.log4j.Logger;
+import org.apache.xml.serializer.Method;
 import org.apache.xml.serializer.OutputPropertiesFactory;
 import org.apache.xml.serializer.Serializer;
+import org.apache.xml.serializer.ToTextStream;
 =

 /**
  * Factory to create serializers. =

- * Currently only supports html serializers.
  */
 public class SerializerFactory {
 =

-    private static Category log =3D Category.getInstance(SerializerFactory=
.class);
+    private static Logger log =3D Logger.getLogger(SerializerFactory.class=
);
     =

     public static final int XHTML_STRICT =3D 1;
     public static final int HTML_TRANSITIONAL =3D 2;
     public static final int XML =3D 3;
+    public static final int TEXT =3D 4;
     =

     public static final String XHTML_STRICT_KEY =3D "XHTML_STRICT";
     public static final String HTML_TRANSITIONAL_KEY =3D "HTML_TRANSITIONA=
L";
     public static final String XML_KEY =3D "XML";
+    public static final String TEXT_KEY =3D "TEXT";
 =

+    /**
+     * @return HTML serializer with the format specified
+     * */
     public static Serializer getSerializer(Properties format) {
         Serializer serializer =3D new HTMLSerializer();
         serializer.setOutputFormat(format);
         return serializer;
     }
     =

+    /**
+     * Get serializer by a given key. Will return TEXT serializer when the=
 key is not recognized.
+     * */
     public static Serializer getSerializer(String key) {
         if (key.equals(XHTML_STRICT_KEY)) {
             return getSerializer(XHTML_STRICT);
         } else if (key.equals(HTML_TRANSITIONAL_KEY)) {
             return getSerializer(HTML_TRANSITIONAL);
-        } if (key.equals(XML_KEY)) {
+        } else if (key.equals(XML_KEY)) {
             return getSerializer(XML);
+        } else if (key.equals(TEXT_KEY)) {
+            return getSerializer(TEXT);
         }
-        return null;
+        return getSerializer(TEXT);
     }
     =

+    /**
+     * Get serializer by a given key. Will return TEXT serializer when the=
 key is not recognized.
+     * */
     public static Serializer getSerializer(int key) {
         Serializer serializer =3D null;
         if (key =3D=3D XHTML_STRICT) {
             serializer =3D new HTMLSerializer();
-            Properties format =3D OutputPropertiesFactory.getDefaultMethod=
Properties("html");
+            Properties format =3D OutputPropertiesFactory.getDefaultMethod=
Properties(Method.XHTML);
             format.setProperty("indent", "yes");
             format.setProperty("doctype-public", "-//W3C//DTD XHTML 1.0 St=
rict//EN");
             format.setProperty("doctype-system", "http://www.w3.org/TR/xht=
ml1/DTD/xhtml1-strict.dtd");
             serializer.setOutputFormat(format);
-        }
-        if (key =3D=3D HTML_TRANSITIONAL) {
+        }else if (key =3D=3D HTML_TRANSITIONAL) {
             serializer =3D new HTMLSerializer();
-            Properties format =3D OutputPropertiesFactory.getDefaultMethod=
Properties("html");
+            Properties format =3D OutputPropertiesFactory.getDefaultMethod=
Properties(Method.HTML);
             format.setProperty("indent", "yes");
             format.setProperty("omit-xml-declaration", "yes");
             format.setProperty("doctype-public", "-//W3C//DTD HTML 4.01 Tr=
ansitional//EN");
             format.setProperty("doctype-system", "http://www.w3.org/TR/htm=
l4/loose.dtd");
             serializer.setOutputFormat(format);
-        }
-        if (key =3D=3D XML) {
+        }else if (key =3D=3D XML) {
             serializer =3D new XMLSerializer();
-            Properties format =3D OutputPropertiesFactory.getDefaultMethod=
Properties("xml");
+            Properties format =3D OutputPropertiesFactory.getDefaultMethod=
Properties(Method.XML);
             format.setProperty("indent", "yes");
             serializer.setOutputFormat(format);
+        } else {
+            // Internal Xalan serializer
+            Properties format =3D OutputPropertiesFactory.getDefaultMethod=
Properties(Method.TEXT);
+            serializer =3D new ToTextStream();
+            serializer.setOutputFormat(format);
         }
         return serializer;
     }
Index: java/org/wyona/yanel/core/source/ResourceResolver.java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- java/org/wyona/yanel/core/source/ResourceResolver.java	(revision 30347)
+++ java/org/wyona/yanel/core/source/ResourceResolver.java	(working copy)
@@ -3,6 +3,7 @@
 import java.util.HashMap;
 =

 import org.apache.log4j.Category;
+import org.wyona.yanel.core.Constants;
 import org.wyona.yanel.core.Path;
 import org.wyona.yanel.core.Resource;
 import org.wyona.yanel.core.ResourceManager;
@@ -58,15 +59,19 @@
             Resource targetResource =3D manager.getResource(resource.getEn=
vironment(), =

                     resource.getRealm(), uri);
             targetResource.setParameters(parameters);
+            =

+            // Check which if there is a view requested for the resource
+            String viewid =3D String.valueOf(parameters.get(Constants.Requ=
est.YANEL_RESOURCE_VIEWID));
+            =

             if (ResourceAttributeHelper.hasAttributeImplemented(targetReso=
urce, "Viewable", "1")) {
                 String viewV1path =3D resource.getRealm().getMountPoint() =
+ uri.substring(1);
                 if (log.isDebugEnabled()) {
                     log.debug("including document: " + viewV1path);
                 }
-                View view =3D ((ViewableV1) targetResource).getView(new Pa=
th(viewV1path), null);
+                View view =3D ((ViewableV1) targetResource).getView(new Pa=
th(viewV1path), viewid);
                 return new StreamSource(view.getInputStream());
             } else if (ResourceAttributeHelper.hasAttributeImplemented(tar=
getResource, "Viewable", "2")) {
-                View view =3D ((ViewableV2) targetResource).getView(null);
+                View view =3D ((ViewableV2) targetResource).getView(viewid=
);
                 if (!view.isResponse()) {
                     throw new Exception("Reading from the response has not=
 been implemented yet!");
                 } else {


More information about the Yanel-development mailing list