cziegeler    2002/09/23 05:03:27

  Modified:    src/java/org/apache/cocoon/xml/dom DOMUtil.java
               src/java/org/apache/cocoon/webapps/session/context
                        RequestSessionContext.java
  Log:
  Better evaluation of request attributes for use in session contexts
  
  Revision  Changes    Path
  1.3       +120 -1    xml-cocoon2/src/java/org/apache/cocoon/xml/dom/DOMUtil.java
  
  Index: DOMUtil.java
  ===================================================================
  RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/xml/dom/DOMUtil.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- DOMUtil.java      19 Apr 2002 11:02:59 -0000      1.2
  +++ DOMUtil.java      23 Sep 2002 12:03:27 -0000      1.3
  @@ -52,6 +52,7 @@
   
   import org.apache.excalibur.source.SourceParameters;
   import org.apache.avalon.excalibur.xml.Parser;
  +import org.apache.avalon.excalibur.xml.XMLizable;
   import org.apache.avalon.excalibur.xml.xpath.XPathProcessor;
   import org.apache.cocoon.ProcessingException;
   import org.apache.cocoon.components.xpath.XPathUtil;
  @@ -61,6 +62,8 @@
   import org.xml.sax.SAXException;
   import org.xml.sax.helpers.AttributesImpl;
   import java.io.*;
  +import java.util.Collection;
  +import java.util.Iterator;
   
   /**
    *  This class is an utitity class for miscellaneous DOM functions, like
  @@ -445,5 +448,121 @@
           return ok;
       }
   
  +    /**
  +     * Implementation for <code>String</code> :
  +     * outputs characters representing the value.
  +     *
  +     * @param parent The node getting the value
  +     * @param text   the value
  +     */
  +    public static void valueOf(Node parent, String text) 
  +    throws ProcessingException {
  +        if (text != null) {
  +            parent.appendChild(parent.getOwnerDocument().createTextNode(text));
  +        }
  +    }
  +
  +    /**
  +     * Implementation for <code>XMLizable</code> :
  +     * outputs the value by calling <code>v.toSax(contentHandler)</code>.
  +     *
  +     * @param parent The node getting the value
  +     * @param v the XML fragment
  +     */
  +    public static void valueOf(Node parent, XMLizable v) 
  +    throws ProcessingException {
  +        if (v != null) {
  +            DOMBuilder builder = new DOMBuilder(parent);
  +            try { 
  +                v.toSAX(builder);
  +            } catch(SAXException e) {
  +                throw new ProcessingException(e);
  +            }
  +        }
  +    }
  +
  +    /**
  +     * Implementation for <code>org.w3c.dom.Node</code> :
  +     * converts the Node to a SAX event stream.
  +     *
  +     * @param parent The node getting the value
  +     * @param v the value
  +     */
  +    public static void valueOf(Node parent, Node v) 
  +    throws ProcessingException {
  +        if (v != null) {
  +            parent.appendChild(parent.getOwnerDocument().importNode(v, true));
  +        }
  +    }
  +
  +    /**
  +     * Implementation for <code>java.util.Collection</code> :
  +     * outputs the value by calling <code>xspExpr()</code> on each element of the
  +     * collection.
  +     *
  +     * @param parent The node getting the value
  +     * @param v the XML fragment
  +     */
  +    public static void valueOf(Node parent, 
  +                                 Collection v) 
  +    throws ProcessingException {
  +        if (v != null) {
  +            Iterator iterator = v.iterator();
  +            while (iterator.hasNext()) {
  +                valueOf(parent, iterator.next());
  +            }
  +        }
  +     }
  +
  +    /**
  +     * Implementation for <code>Object</code> depending on its class :
  +     * <ul>
  +     * <li>if it's an array, call {@link valueOf(Node, Object)} on all its 
elements,</li>
  +     * <li>if it's class has a specific {@link valueOf(Node, Object)} 
implementation, use it,</li>
  +     * <li>else, output it's string representation.</li>
  +     * </ul>
  +     *
  +     * @param parent The node getting the value
  +     * @param v the value
  +     */
  +    public static void valueOf(Node parent, Object v) 
  +    throws ProcessingException {
  +        if (v == null) {
  +            return;
  +        }
  +
  +        // Array: recurse over each element
  +        if (v.getClass().isArray()) {
  +            Object[] elements = (Object[]) v;
  +
  +            for (int i = 0; i < elements.length; i++) {
  +                valueOf(parent, elements[i]);
  +            }
  +            return;
  +         }
  +
  +         // Check handled object types in case they were not typed in the XSP
  +
  +         // XMLizable
  +         if (v instanceof XMLizable) {
  +             valueOf(parent, (XMLizable)v);
  +             return;
  +         }
  +
  +         // Node
  +         if (v instanceof Node) {
  +             valueOf(parent, (Node)v);
  +             return;
  +         }
  +
  +         // Collection
  +         if (v instanceof Collection) {
  +             valueOf(parent, (Collection)v);
  +             return;
  +         }
  +
  +         // Give up: hope it's a string or has a meaningful string representation
  +         valueOf(parent, String.valueOf(v));
  +    }
   
   }
  
  
  
  1.6       +6 -5      
xml-cocoon2/src/java/org/apache/cocoon/webapps/session/context/RequestSessionContext.java
  
  Index: RequestSessionContext.java
  ===================================================================
  RCS file: 
/home/cvs/xml-cocoon2/src/java/org/apache/cocoon/webapps/session/context/RequestSessionContext.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- RequestSessionContext.java        30 Jul 2002 01:44:19 -0000      1.5
  +++ RequestSessionContext.java        23 Sep 2002 12:03:27 -0000      1.6
  @@ -66,6 +66,7 @@
   import org.apache.cocoon.webapps.session.connector.Resource;
   import org.apache.cocoon.webapps.session.xml.XMLUtil;
   import org.apache.cocoon.xml.IncludeXMLConsumer;
  +import org.apache.cocoon.xml.dom.DOMUtil;
   
   import org.w3c.dom.Document;
   import org.w3c.dom.DocumentFragment;
  @@ -291,7 +292,7 @@
               attrName = (String) all.nextElement();
               attr = doc.createElementNS(null, attrName);
               attrElement.appendChild(attr);
  -            attr.appendChild(this.createTextNode(doc, 
this.request.getAttribute(attrName).toString()));
  +            DOMUtil.valueOf(attr, this.request.getAttribute(attrName));
           }
       }
   
  @@ -418,13 +419,13 @@
   
                   try {
                       // create "/parametervalues" entry
  -                    element = 
doc.createElementNS(SessionConstants.SESSION_NAMESPACE_URI, "cocoon:param");
  +                    element = 
doc.createElementNS(SessionConstants.SESSION_NAMESPACE_URI, "session:param");
                       parameterValuesElement.appendChild(element);
                       parameter = element;
  -                    element = 
doc.createElementNS(SessionConstants.SESSION_NAMESPACE_URI, "cocoon:name");
  +                    element = 
doc.createElementNS(SessionConstants.SESSION_NAMESPACE_URI, "session:name");
                       parameter.appendChild(element);
                       element.appendChild(doc.createTextNode(parameterName));
  -                    element = 
doc.createElementNS(SessionConstants.SESSION_NAMESPACE_URI, "cocoon:value");
  +                    element = 
doc.createElementNS(SessionConstants.SESSION_NAMESPACE_URI, "session:value");
                       parameter.appendChild(element);
                       element.appendChild(valueNode.cloneNode(true));
                   } catch (Exception local) {
  
  
  

----------------------------------------------------------------------
In case of troubles, e-mail:     [EMAIL PROTECTED]
To unsubscribe, e-mail:          [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to