You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by js...@apache.org on 2007/08/07 20:46:59 UTC

svn commit: r563618 - in /activemq/camel/trunk/camel-core/src/main/java/org/apache/camel: converter/NIOConverter.java converter/jaxp/XmlConverter.java processor/validation/DefaultValidationErrorHandler.java

Author: jstrachan
Date: Tue Aug  7 11:46:58 2007
New Revision: 563618

URL: http://svn.apache.org/viewvc?view=rev&rev=563618
Log:
added some extra converters

Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/NIOConverter.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/validation/DefaultValidationErrorHandler.java

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/NIOConverter.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/NIOConverter.java?view=diff&rev=563618&r1=563617&r2=563618
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/NIOConverter.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/NIOConverter.java Tue Aug  7 11:46:58 2007
@@ -16,10 +16,11 @@
  */
 package org.apache.camel.converter;
 
-import java.nio.ByteBuffer;
-
 import org.apache.camel.Converter;
 
+import java.io.InputStream;
+import java.nio.ByteBuffer;
+
 /**
  * Some core java.nio based 
  * <a href="http://activemq.apache.org/camel/type-converter.html">Type Converters</a>
@@ -86,5 +87,10 @@
         ByteBuffer buf = ByteBuffer.allocate(8);
         buf.putDouble(value);
         return buf;
+    }
+
+    @Converter
+    public static InputStream toInputStream(ByteBuffer bufferbuffer) {
+        return IOConverter.toInputStream(toByteArray(bufferbuffer));
     }
 }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java?view=diff&rev=563618&r1=563617&r2=563618
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java Tue Aug  7 11:46:58 2007
@@ -1,4 +1,5 @@
 /**
+ *
  * Licensed to the Apache Software Foundation (ASF) under one or more
  * contributor license agreements.  See the NOTICE file distributed with
  * this work for additional information regarding copyright ownership.
@@ -6,7 +7,7 @@
  * (the "License"); you may not use this file except in compliance with
  * the License.  You may obtain a copy of the License at
  *
- *      http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
@@ -16,15 +17,16 @@
  */
 package org.apache.camel.converter.jaxp;
 
-import java.io.ByteArrayInputStream;
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.Reader;
-import java.io.StringReader;
-import java.io.StringWriter;
-import java.lang.reflect.Constructor;
+import org.apache.camel.Converter;
+import org.apache.camel.converter.IOConverter;
+import org.apache.camel.converter.NIOConverter;
+import org.apache.camel.util.ObjectHelper;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+import org.xml.sax.XMLReader;
 
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
@@ -41,48 +43,48 @@
 import javax.xml.transform.sax.SAXSource;
 import javax.xml.transform.stream.StreamResult;
 import javax.xml.transform.stream.StreamSource;
-
-import org.w3c.dom.Document;
-import org.w3c.dom.Element;
-import org.w3c.dom.Node;
-
-import org.xml.sax.InputSource;
-import org.xml.sax.SAXException;
-import org.xml.sax.XMLReader;
-
-import org.apache.camel.Converter;
-import org.apache.camel.util.ObjectHelper;
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.lang.reflect.Constructor;
+import java.nio.ByteBuffer;
 
 /**
- * A helper class to transform to and from various JAXB types such as
- * {@link Source} and {@link Document}
- * 
+ * A helper class to transform to and from various JAXB types such as {@link Source} and {@link Document}
+ *
  * @version $Revision$
  */
 @Converter
 public class XmlConverter {
-    
     public static final String DEFAULT_CHARSET_PROPERTY = "org.apache.camel.default.charset";
-    public static final String DEFAULT_CHARSET = ObjectHelper.getSystemProperty(DEFAULT_CHARSET_PROPERTY, "UTF-8");    
-    
+
+    public static String defaultCharset = ObjectHelper.getSystemProperty(DEFAULT_CHARSET_PROPERTY, "UTF-8");
+
+    private DocumentBuilderFactory documentBuilderFactory;
+    private TransformerFactory transformerFactory;
+
     /*
-     * When converting a DOM tree to a SAXSource, we try to use Xalan internal
-     * DOM parser if available. Else, transform the DOM tree to a String and
-     * build a SAXSource on top of it.
+     * When converting a DOM tree to a SAXSource,
+     * we try to use Xalan internal DOM parser if
+     * available.  Else, transform the DOM tree
+     * to a String and build a SAXSource on top of
+     * it.
      */
-    private static final Class DOM2SAX_CLASS;
+    private static final Class dom2SaxClass;
 
     static {
         Class cl = null;
         try {
             cl = Class.forName("org.apache.xalan.xsltc.trax.DOM2SAX");
-        } catch (Throwable ignore) {
-        }
-        DOM2SAX_CLASS = cl;
+        } catch (Throwable t) {}
+        dom2SaxClass = cl;
     }
 
-    private DocumentBuilderFactory documentBuilderFactory;
-    private TransformerFactory transformerFactory;
 
     public XmlConverter() {
     }
@@ -91,6 +93,7 @@
         this.documentBuilderFactory = documentBuilderFactory;
     }
 
+
     /**
      * Converts the given input Source into the required result
      */
@@ -102,7 +105,7 @@
         if (transformer == null) {
             throw new TransformerException("Could not create a transformer - JAXP is misconfigured!");
         }
-        transformer.setOutputProperty(OutputKeys.ENCODING, DEFAULT_CHARSET);
+        transformer.setOutputProperty(OutputKeys.ENCODING, defaultCharset);
         transformer.transform(source, result);
     }
 
@@ -114,6 +117,7 @@
         return new BytesSource(data);
     }
 
+
     /**
      * Converts the given String to a Source
      */
@@ -138,9 +142,9 @@
         if (source == null) {
             return null;
         } else if (source instanceof StringSource) {
-            return ((StringSource)source).getText();
+            return ((StringSource) source).getText();
         } else if (source instanceof BytesSource) {
-            return new String(((BytesSource)source).getData());
+            return new String(((BytesSource) source).getData());
         } else {
             StringWriter buffer = new StringWriter();
             toResult(source, new StreamResult(buffer));
@@ -157,54 +161,74 @@
     }
 
     /**
-     * Converts the source instance to a {@link DOMSource} or returns null if
-     * the conversion is not supported (making it easy to derive from this class
-     * to add new kinds of conversion).
+     * Converts the source instance to a {@link DOMSource} or returns null if the conversion is not
+     * supported (making it easy to derive from this class to add new kinds of conversion).
      */
     @Converter
-    public DOMSource toDOMSource(Source source) throws ParserConfigurationException, IOException,
-        SAXException, TransformerException {
+    public DOMSource toDOMSource(Source source) throws ParserConfigurationException, IOException, SAXException, TransformerException {
         if (source instanceof DOMSource) {
-            return (DOMSource)source;
-        } else if (source instanceof SAXSource) {
-            return toDOMSourceFromSAX((SAXSource)source);
-        } else if (source instanceof StreamSource) {
-            return toDOMSourceFromStream((StreamSource)source);
-        } else {
+            return (DOMSource) source;
+        }
+        else if (source instanceof SAXSource) {
+            return toDOMSourceFromSAX((SAXSource) source);
+        }
+        else if (source instanceof StreamSource) {
+            return toDOMSourceFromStream((StreamSource) source);
+        }
+        else {
             return null;
         }
     }
 
     /**
-     * Converts the source instance to a {@link DOMSource} or returns null if
-     * the conversion is not supported (making it easy to derive from this class
-     * to add new kinds of conversion).
+     * Converts the source instance to a {@link DOMSource} or returns null if the conversion is not
+     * supported (making it easy to derive from this class to add new kinds of conversion).
      */
     @Converter
-    public DOMSource toDOMSource(String text) throws ParserConfigurationException, IOException, SAXException,
-        TransformerException {
+    public DOMSource toDOMSource(String text) throws ParserConfigurationException, IOException, SAXException, TransformerException {
         Source source = toSource(text);
         if (source != null) {
-            return toDOMSourceFromStream((StreamSource)source);
-        } else {
+            return toDOMSourceFromStream((StreamSource) source);
+        }
+        else {
             return null;
         }
     }
 
     /**
-     * Converts the source instance to a {@link SAXSource} or returns null if
-     * the conversion is not supported (making it easy to derive from this class
-     * to add new kinds of conversion).
+     * Converts the source instance to a {@link SAXSource} or returns null if the conversion is not
+     * supported (making it easy to derive from this class to add new kinds of conversion).
+     */
+    @Converter
+    public SAXSource toSAXSource(String source) throws IOException, SAXException, TransformerException {
+        return toSAXSource(toSource(source));
+    }
+
+    /**
+     * Converts the source instance to a {@link SAXSource} or returns null if the conversion is not
+     * supported (making it easy to derive from this class to add new kinds of conversion).
+     */
+    @Converter
+    public SAXSource toSAXSource(InputStream source) throws IOException, SAXException, TransformerException {
+        return toSAXSource(toStreamSource(source));
+    }
+
+    /**
+     * Converts the source instance to a {@link SAXSource} or returns null if the conversion is not
+     * supported (making it easy to derive from this class to add new kinds of conversion).
      */
     @Converter
     public SAXSource toSAXSource(Source source) throws IOException, SAXException, TransformerException {
         if (source instanceof SAXSource) {
-            return (SAXSource)source;
-        } else if (source instanceof DOMSource) {
-            return toSAXSourceFromDOM((DOMSource)source);
-        } else if (source instanceof StreamSource) {
-            return toSAXSourceFromStream((StreamSource)source);
-        } else {
+            return (SAXSource) source;
+        }
+        else if (source instanceof DOMSource) {
+            return toSAXSourceFromDOM((DOMSource) source);
+        }
+        else if (source instanceof StreamSource) {
+            return toSAXSourceFromStream((StreamSource) source);
+        }
+        else {
             return null;
         }
     }
@@ -212,17 +236,57 @@
     @Converter
     public StreamSource toStreamSource(Source source) throws TransformerException {
         if (source instanceof StreamSource) {
-            return (StreamSource)source;
+            return (StreamSource) source;
         } else if (source instanceof DOMSource) {
-            return toStreamSourceFromDOM((DOMSource)source);
+            return toStreamSourceFromDOM((DOMSource) source);
         } else if (source instanceof SAXSource) {
-            return toStreamSourceFromSAX((SAXSource)source);
+            return toStreamSourceFromSAX((SAXSource) source);
         } else {
             return null;
         }
     }
 
     @Converter
+    public StreamSource toStreamSource(InputStream in) throws TransformerException {
+        if (in != null) {
+            return new StreamSource(in);
+        }
+        return null;
+    }
+
+    @Converter
+    public StreamSource toStreamSource(Reader in) throws TransformerException {
+        if (in != null) {
+            return new StreamSource(in);
+        }
+        return null;
+    }
+
+    @Converter
+    public StreamSource toStreamSource(File in) throws TransformerException {
+        if (in != null) {
+            return new StreamSource(in);
+        }
+        return null;
+    }
+
+    @Converter
+    public StreamSource toStreamSource(byte[] in) throws TransformerException {
+        if (in != null) {
+            return new StreamSource(IOConverter.toInputStream(in));
+        }
+        return null;
+    }
+
+    @Converter
+    public StreamSource toStreamSource(ByteBuffer in) throws TransformerException {
+        if (in != null) {
+            return new StreamSource(NIOConverter.toInputStream(in));
+        }
+        return null;
+    }
+
+    @Converter
     public StreamSource toStreamSourceFromSAX(SAXSource source) throws TransformerException {
         InputSource inputSource = source.getInputSource();
         if (inputSource != null) {
@@ -267,8 +331,7 @@
     }
 
     @Converter
-    public DOMSource toDOMSourceFromStream(StreamSource source) throws ParserConfigurationException,
-        IOException, SAXException {
+    public DOMSource toDOMSourceFromStream(StreamSource source) throws ParserConfigurationException, IOException, SAXException {
         DocumentBuilder builder = createDocumentBuilder();
         String systemId = source.getSystemId();
         Document document = null;
@@ -281,7 +344,8 @@
                 InputSource inputsource = new InputSource(inputStream);
                 inputsource.setSystemId(systemId);
                 document = builder.parse(inputsource);
-            } else {
+            }
+            else {
                 throw new IOException("No input stream or reader available");
             }
         }
@@ -290,10 +354,10 @@
 
     @Converter
     public SAXSource toSAXSourceFromDOM(DOMSource source) throws TransformerException {
-        if (DOM2SAX_CLASS != null) {
+        if (dom2SaxClass != null) {
             try {
-                Constructor cns = DOM2SAX_CLASS.getConstructor(new Class[] {Node.class});
-                XMLReader converter = (XMLReader)cns.newInstance(new Object[] {source.getNode()});
+                Constructor cns = dom2SaxClass.getConstructor(new Class[] { Node.class });
+                XMLReader converter = (XMLReader) cns.newInstance(new Object[] { source.getNode() });
                 return new SAXSource(converter, new InputSource());
             } catch (Exception e) {
                 throw new TransformerException(e);
@@ -306,14 +370,12 @@
     }
 
     @Converter
-    public DOMSource toDOMSourceFromSAX(SAXSource source) throws IOException, SAXException,
-        ParserConfigurationException, TransformerException {
+    public DOMSource toDOMSourceFromSAX(SAXSource source) throws IOException, SAXException, ParserConfigurationException, TransformerException {
         return new DOMSource(toDOMNodeFromSAX(source));
     }
 
     @Converter
-    public Node toDOMNodeFromSAX(SAXSource source) throws ParserConfigurationException, IOException,
-        SAXException, TransformerException {
+    public Node toDOMNodeFromSAX(SAXSource source) throws ParserConfigurationException, IOException, SAXException, TransformerException {
         DOMResult result = new DOMResult();
         toResult(source, result);
         return result.getNode();
@@ -321,21 +383,19 @@
 
     /**
      * Converts the given TRaX Source into a W3C DOM node
-     * 
      * @throws SAXException
      * @throws IOException
      * @throws ParserConfigurationException
      */
     @Converter
-    public Node toDOMNode(Source source) throws TransformerException, ParserConfigurationException,
-        IOException, SAXException {
+    public Node toDOMNode(Source source) throws TransformerException, ParserConfigurationException, IOException, SAXException {
         DOMSource domSrc = toDOMSource(source);
-        return domSrc != null ? domSrc.getNode() : null;
+        return domSrc != null ? domSrc.getNode() :  null;
     }
 
     /**
      * Create a DOM element from the given source.
-     * 
+     *
      * @param source
      * @return
      * @throws TransformerException
@@ -344,16 +404,16 @@
      * @throws SAXException
      */
     @Converter
-    public Element toDOMElement(Source source) throws TransformerException, ParserConfigurationException,
-        IOException, SAXException {
+    public Element toDOMElement(Source source) throws TransformerException, ParserConfigurationException, IOException, SAXException {
         Node node = toDOMNode(source);
         return toDOMElement(node);
     }
 
     /**
-     * Create a DOM element from the DOM node. Simply cast if the node is an
-     * Element, or return the root element if it is a Document.
-     * 
+     * Create a DOM element from the DOM node.
+     * Simply cast if the node is an Element, or
+     * return the root element if it is a Document.
+     *
      * @param node
      * @return
      * @throws TransformerException
@@ -362,11 +422,11 @@
     public Element toDOMElement(Node node) throws TransformerException {
         // If the node is an document, return the root element
         if (node instanceof Document) {
-            return ((Document)node).getDocumentElement();
-            // If the node is an element, just cast it
+            return ((Document) node).getDocumentElement();
+        // If the node is an element, just cast it
         } else if (node instanceof Element) {
-            return (Element)node;
-            // Other node types are not handled
+            return (Element) node;
+        // Other node types are not handled
         } else {
             throw new TransformerException("Unable to convert DOM node to an Element");
         }
@@ -374,7 +434,7 @@
 
     /**
      * Converts the given data to a DOM document
-     * 
+     *
      * @param data is the data to be parsed
      * @return the parsed document
      */
@@ -386,33 +446,31 @@
 
     /**
      * Converts the given {@link InputStream} to a DOM document
-     * 
+     *
      * @param in is the data to be parsed
      * @return the parsed document
      */
     @Converter
-    public Document toDOMDocument(InputStream in) throws IOException, SAXException,
-        ParserConfigurationException {
+    public Document toDOMDocument(InputStream in) throws IOException, SAXException, ParserConfigurationException {
         DocumentBuilder documentBuilder = getDocumentBuilderFactory().newDocumentBuilder();
         return documentBuilder.parse(in);
     }
 
     /**
      * Converts the given {@link InputSource} to a DOM document
-     * 
+     *
      * @param in is the data to be parsed
      * @return the parsed document
      */
     @Converter
-    public Document toDOMDocument(InputSource in) throws IOException, SAXException,
-        ParserConfigurationException {
+    public Document toDOMDocument(InputSource in) throws IOException, SAXException, ParserConfigurationException {
         DocumentBuilder documentBuilder = getDocumentBuilderFactory().newDocumentBuilder();
         return documentBuilder.parse(in);
     }
 
     /**
      * Converts the given {@link String} to a DOM document
-     * 
+     *
      * @param text is the data to be parsed
      * @return the parsed document
      */
@@ -423,7 +481,7 @@
 
     /**
      * Converts the given {@link File} to a DOM document
-     * 
+     *
      * @param file is the data to be parsed
      * @return the parsed document
      */
@@ -433,9 +491,10 @@
         return documentBuilder.parse(file);
     }
 
+
     /**
      * Create a DOM document from the given source.
-     * 
+     *
      * @param source
      * @return
      * @throws TransformerException
@@ -444,17 +503,18 @@
      * @throws SAXException
      */
     @Converter
-    public Document toDOMDocument(Source source) throws TransformerException, ParserConfigurationException,
-        IOException, SAXException {
+    public Document toDOMDocument(Source source) throws TransformerException, ParserConfigurationException, IOException, SAXException {
         Node node = toDOMNode(source);
         return toDOMDocument(node);
     }
 
     /**
-     * Create a DOM document from the given Node. If the node is an document,
-     * just cast it, if the node is an root element, retrieve its owner element
-     * or create a new document and import the node.
-     * 
+     * Create a DOM document from the given Node.
+     * If the node is an document, just cast it,
+     * if the node is an root element, retrieve its
+     * owner element or create a new document and import
+     * the node.
+     *
      * @param node
      * @return
      * @throws ParserConfigurationException
@@ -464,27 +524,27 @@
     public Document toDOMDocument(Node node) throws ParserConfigurationException, TransformerException {
         // If the node is the document, just cast it
         if (node instanceof Document) {
-            return (Document)node;
-            // If the node is an element
+            return (Document) node;
+        // If the node is an element
         } else if (node instanceof Element) {
-            Element elem = (Element)node;
+            Element elem = (Element) node;
             // If this is the root element, return its owner document
             if (elem.getOwnerDocument().getDocumentElement() == elem) {
                 return elem.getOwnerDocument();
-                // else, create a new doc and copy the element inside it
+            // else, create a new doc and copy the element inside it
             } else {
                 Document doc = createDocument();
                 doc.appendChild(doc.importNode(node, true));
                 return doc;
             }
-            // other element types are not handled
+        // other element types are not handled
         } else {
             throw new TransformerException("Unable to convert DOM node to a Document");
         }
     }
 
     // Properties
-    // -------------------------------------------------------------------------
+    //-------------------------------------------------------------------------
     public DocumentBuilderFactory getDocumentBuilderFactory() {
         if (documentBuilderFactory == null) {
             documentBuilderFactory = createDocumentBuilderFactory();
@@ -496,8 +556,9 @@
         this.documentBuilderFactory = documentBuilderFactory;
     }
 
+
     // Helper methods
-    // -------------------------------------------------------------------------
+    //-------------------------------------------------------------------------
     public DocumentBuilderFactory createDocumentBuilderFactory() {
         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
         factory.setNamespaceAware(true);
@@ -506,6 +567,7 @@
         return factory;
     }
 
+
     public DocumentBuilder createDocumentBuilder() throws ParserConfigurationException {
         DocumentBuilderFactory factory = getDocumentBuilderFactory();
         return factory.newDocumentBuilder();
@@ -537,4 +599,4 @@
         return answer;
     }
 
-}
+}
\ No newline at end of file

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/validation/DefaultValidationErrorHandler.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/validation/DefaultValidationErrorHandler.java?view=diff&rev=563618&r1=563617&r2=563618
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/validation/DefaultValidationErrorHandler.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/validation/DefaultValidationErrorHandler.java Tue Aug  7 11:46:58 2007
@@ -22,6 +22,8 @@
 import javax.xml.transform.dom.DOMResult;
 import javax.xml.validation.Schema;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.xml.sax.SAXException;
 import org.xml.sax.SAXParseException;
 
@@ -29,23 +31,34 @@
 import org.apache.camel.ValidationException;
 
 /**
+ * A default error handler which just stores all the errors so they can be reported or transformed.
+ * 
  * @version $Revision: $
  */
 public class DefaultValidationErrorHandler implements ValidatorErrorHandler {
+    private static final transient Log log = LogFactory.getLog(DefaultValidationErrorHandler.class);
     private List<SAXParseException> warnings = new ArrayList<SAXParseException>();
     private List<SAXParseException> errors = new ArrayList<SAXParseException>();
     private List<SAXParseException> fatalErrors = new ArrayList<SAXParseException>();
 
-
     public void warning(SAXParseException e) throws SAXException {
+        if (log.isDebugEnabled()) {
+            log.debug("warning: " + e, e);
+        }
         warnings.add(e);
     }
 
     public void error(SAXParseException e) throws SAXException {
+        if (log.isDebugEnabled()) {
+            log.debug("error: " + e, e);
+        }
         errors.add(e);
     }
 
     public void fatalError(SAXParseException e) throws SAXException {
+        if (log.isDebugEnabled()) {
+            log.debug("fatalError: " + e, e);
+        }
         fatalErrors.add(e);
     }