You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by cz...@apache.org on 2002/08/07 15:15:01 UTC

cvs commit: xml-cocoon2/src/java/org/apache/cocoon/xml XMLUtils.java

cziegeler    2002/08/07 06:15:01

  Modified:    src/java/org/apache/cocoon/generation RequestGenerator.java
               src/java/org/apache/cocoon/xml XMLUtils.java
  Log:
  Updating code as suggested by Vadim
  
  Revision  Changes    Path
  1.8       +4 -3      xml-cocoon2/src/java/org/apache/cocoon/generation/RequestGenerator.java
  
  Index: RequestGenerator.java
  ===================================================================
  RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/generation/RequestGenerator.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- RequestGenerator.java	7 Aug 2002 06:50:05 -0000	1.7
  +++ RequestGenerator.java	7 Aug 2002 13:15:01 -0000	1.8
  @@ -58,6 +58,7 @@
   import org.apache.cocoon.environment.ObjectModelHelper;
   import org.apache.cocoon.environment.Request;
   import org.apache.cocoon.environment.SourceResolver;
  +import org.apache.cocoon.xml.XMLUtils;
   import org.xml.sax.SAXException;
   import org.xml.sax.helpers.AttributesImpl;
   
  @@ -103,7 +104,7 @@
   
           global_container_encoding = parameters.getParameter("container-encoding", "ISO-8859-1");
           global_form_encoding = parameters.getParameter("form-encoding", null);
  -        global_generate_attributes = "false no off".indexOf(parameters.getParameter("generate-attributes", "false")) < 0;
  +        global_generate_attributes = parameters.getParameterAsBoolean("generate-attributes", false);
       }
   
       public void setup(SourceResolver resolver, Map objectModel, String src, Parameters par)
  @@ -202,7 +203,7 @@
                   if (value!=null) {
                       this.data("      ");
                       this.start("value",attr);
  -                    this.data(String.valueOf( value ));
  +                    XMLUtils.valueOf(this.contentHandler, value );
                       this.end("value");
                       this.data("\n");
                   }
  
  
  
  1.9       +143 -15   xml-cocoon2/src/java/org/apache/cocoon/xml/XMLUtils.java
  
  Index: XMLUtils.java
  ===================================================================
  RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/xml/XMLUtils.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- XMLUtils.java	1 Jul 2002 14:43:17 -0000	1.8
  +++ XMLUtils.java	7 Aug 2002 13:15:01 -0000	1.9
  @@ -52,28 +52,27 @@
   
   import java.io.StringWriter;
   import java.util.ArrayList;
  +import java.util.Collection;
  +import java.util.Iterator;
   import java.util.Properties;
   
  -import org.w3c.dom.Attr;
  -import org.w3c.dom.Document;
  -import org.w3c.dom.Element;
  -import org.w3c.dom.Node;
  -import org.w3c.dom.NamedNodeMap;
  -import org.w3c.dom.NodeList;
  -
  -import org.xml.sax.ContentHandler;
  -import org.xml.sax.ext.LexicalHandler;
  -
  -import org.apache.cocoon.ProcessingException;
  -
  +import javax.xml.transform.OutputKeys;
   import javax.xml.transform.Transformer;
  -import javax.xml.transform.TransformerException;
   import javax.xml.transform.TransformerFactory;
  -import javax.xml.transform.OutputKeys;
   import javax.xml.transform.dom.DOMSource;
   import javax.xml.transform.stream.StreamResult;
   import javax.xml.transform.stream.StreamSource;
   
  +import org.apache.cocoon.ProcessingException;
  +import org.apache.cocoon.xml.dom.DOMStreamer;
  +import org.w3c.dom.Document;
  +import org.w3c.dom.Element;
  +import org.w3c.dom.NamedNodeMap;
  +import org.w3c.dom.Node;
  +import org.xml.sax.ContentHandler;
  +import org.xml.sax.SAXException;
  +import org.xml.sax.ext.LexicalHandler;
  +
   /**
    * XML utility methods.
    *
  @@ -241,6 +240,135 @@
           } catch (javax.xml.transform.TransformerException local) {
               throw new ProcessingException("TransformerException: " + local, local);
           }
  +    }
  +
  +    /**
  +     * Add string data
  +     *
  +     * @param contentHandler The SAX content handler
  +     * @param data The string data
  +     */
  +    public static void data(ContentHandler contentHandler,
  +                                 String data)
  +    throws SAXException {
  +        contentHandler.characters(data.toCharArray(), 0, data.length());
  +    }
  +
  +    /**
  +     * Implementation of &lt;xsp:expr&gt; for <code>String</code> :
  +     * outputs characters representing the value.
  +     *
  +     * @param contentHandler the SAX content handler
  +     * @param text the value
  +     */
  +    public static void valueOf(ContentHandler contentHandler, String text) 
  +    throws SAXException {
  +        if (text != null) {
  +            data(contentHandler, text);
  +        }
  +    }
  +
  +    /**
  +     * Implementation of &lt;xsp:expr&gt; for <code>XMLizable</code> :
  +     * outputs the value by calling <code>v.toSax(contentHandler)</code>.
  +     *
  +     * @param contentHandler the SAX content handler
  +     * @param v the XML fragment
  +     */
  +    public static void valueOf(ContentHandler contentHandler, XMLizable v) 
  +    throws SAXException {
  +        if (v != null) {
  +            try { 
  +                v.toSAX(contentHandler);
  +            } catch(ProcessingException e) {
  +                throw new SAXException(e);
  +            }
  +        }
  +    }
  +
  +    /**
  +     * Implementation of &lt;xsp:expr&gt; for <code>org.w3c.dom.Node</code> :
  +     * converts the Node to a SAX event stream.
  +     *
  +     * @param contentHandler the SAX content handler
  +     * @param v the value
  +     */
  +    public static void valueOf(ContentHandler contentHandler, Node v) 
  +    throws SAXException {
  +        if (v != null) {
  +            DOMStreamer streamer = new DOMStreamer(contentHandler);
  +            streamer.stream(v);
  +        }
  +    }
  +
  +    /**
  +     * Implementation of &lt;xsp:expr&gt; for <code>java.util.Collection</code> :
  +     * outputs the value by calling <code>xspExpr()</code> on each element of the
  +     * collection.
  +     *
  +     * @param contentHandler the SAX content handler
  +     * @param v the XML fragment
  +     */
  +    public static void valueOf(ContentHandler contentHandler, 
  +                                 Collection v) 
  +    throws SAXException {
  +        if (v != null) {
  +            Iterator iterator = v.iterator();
  +            while (iterator.hasNext()) {
  +                valueOf(contentHandler, iterator.next());
  +            }
  +        }
  +     }
  +
  +    /**
  +     * Implementation of &lt;xsp:expr&gt; for <code>Object</code> depending on its class :
  +     * <ul>
  +     * <li>if it's an array, call <code>xspExpr()</code> on all its elements,</li>
  +     * <li>if it's class has a specific <code>xspExpr()</code>implementation, use it,</li>
  +     * <li>else, output it's string representation.</li>
  +     * </ul>
  +     *
  +     * @param contentHandler the SAX content handler
  +     * @param v the value
  +     */
  +    public static void valueOf(ContentHandler contentHandler, Object v) 
  +    throws SAXException {
  +        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(contentHandler, elements[i]);
  +            }
  +            return;
  +         }
  +
  +         // Check handled object types in case they were not typed in the XSP
  +
  +         // XMLizable
  +         if (v instanceof XMLizable) {
  +             valueOf(contentHandler, (XMLizable)v);
  +             return;
  +         }
  +
  +         // Node
  +         if (v instanceof Node) {
  +             valueOf(contentHandler, (Node)v);
  +             return;
  +         }
  +
  +         // Collection
  +         if (v instanceof Collection) {
  +             valueOf(contentHandler, (Collection)v);
  +             return;
  +         }
  +
  +         // Give up: hope it's a string or has a meaningful string representation
  +         data(contentHandler, String.valueOf(v));
       }
   
   }
  
  
  

----------------------------------------------------------------------
In case of troubles, e-mail:     webmaster@xml.apache.org
To unsubscribe, e-mail:          cocoon-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: cocoon-cvs-help@xml.apache.org