vgritsenko    2002/10/22 20:39:15

  Modified:    src/java/org/apache/cocoon/xml Tag: cocoon_2_0_3_branch
                        XMLUtils.java
  Log:
  Add xsp:expr stuff to the XMLUtil (backported from 2.1)
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.6.2.2   +133 -4    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.6.2.1
  retrieving revision 1.6.2.2
  diff -u -r1.6.2.1 -r1.6.2.2
  --- XMLUtils.java     11 Oct 2002 06:30:41 -0000      1.6.2.1
  +++ XMLUtils.java     23 Oct 2002 03:39:15 -0000      1.6.2.2
  @@ -53,21 +53,22 @@
   import java.io.StringWriter;
   import java.util.ArrayList;
   import java.util.Properties;
  +import java.util.Collection;
  +import java.util.Iterator;
   
  -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.SAXException;
   import org.xml.sax.ext.LexicalHandler;
   
   import org.apache.cocoon.ProcessingException;
  +import org.apache.cocoon.xml.dom.DOMStreamer;
   
   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;
  @@ -244,4 +245,132 @@
           }
       }
   
  +    /**
  +     * 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:     [EMAIL PROTECTED]
To unsubscribe, e-mail:          [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to