Coverage Report - org.apache.camel.converter.jaxp.XmlConverter
 
Classes in this File Line Coverage Branch Coverage Complexity
XmlConverter
25% 
23% 
0
 
 1  
 /**
 2  
  *
 3  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 4  
  * contributor license agreements.  See the NOTICE file distributed with
 5  
  * this work for additional information regarding copyright ownership.
 6  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 7  
  * (the "License"); you may not use this file except in compliance with
 8  
  * the License.  You may obtain a copy of the License at
 9  
  *
 10  
  * http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 package org.apache.camel.converter.jaxp;
 19  
 
 20  
 
 21  
 import java.io.IOException;
 22  
 import java.io.InputStream;
 23  
 import java.io.InputStreamReader;
 24  
 import java.io.Reader;
 25  
 import java.io.StringReader;
 26  
 import java.io.StringWriter;
 27  
 import java.io.ByteArrayInputStream;
 28  
 import java.io.File;
 29  
 import java.lang.reflect.Constructor;
 30  
 
 31  
 import javax.xml.parsers.DocumentBuilder;
 32  
 import javax.xml.parsers.DocumentBuilderFactory;
 33  
 import javax.xml.parsers.ParserConfigurationException;
 34  
 import javax.xml.transform.OutputKeys;
 35  
 import javax.xml.transform.Result;
 36  
 import javax.xml.transform.Source;
 37  
 import javax.xml.transform.Transformer;
 38  
 import javax.xml.transform.TransformerConfigurationException;
 39  
 import javax.xml.transform.TransformerException;
 40  
 import javax.xml.transform.TransformerFactory;
 41  
 import javax.xml.transform.dom.DOMResult;
 42  
 import javax.xml.transform.dom.DOMSource;
 43  
 import javax.xml.transform.sax.SAXSource;
 44  
 import javax.xml.transform.stream.StreamResult;
 45  
 import javax.xml.transform.stream.StreamSource;
 46  
 
 47  
 import org.w3c.dom.Document;
 48  
 import org.w3c.dom.Element;
 49  
 import org.w3c.dom.Node;
 50  
 import org.xml.sax.InputSource;
 51  
 import org.xml.sax.SAXException;
 52  
 import org.xml.sax.XMLReader;
 53  
 import org.apache.camel.util.ObjectHelper;
 54  
 import org.apache.camel.Converter;
 55  
 
 56  
 /**
 57  
  * A helper class to transform to and from various JAXB types such as {@link Source} and {@link Document}
 58  
  *
 59  
  * @version $Revision: 525890 $
 60  
  */
 61  
 @Converter
 62  
 public class XmlConverter {
 63  
     public static final String DEFAULT_CHARSET_PROPERTY = "org.apache.camel.default.charset";
 64  
 
 65  1
     public static String defaultCharset = ObjectHelper.getSystemProperty(DEFAULT_CHARSET_PROPERTY, "UTF-8");
 66  
 
 67  
     private DocumentBuilderFactory documentBuilderFactory;
 68  
     private TransformerFactory transformerFactory;
 69  
 
 70  
     /*
 71  
      * When converting a DOM tree to a SAXSource,
 72  
      * we try to use Xalan internal DOM parser if
 73  
      * available.  Else, transform the DOM tree
 74  
      * to a String and build a SAXSource on top of
 75  
      * it.
 76  
      */
 77  
     private static final Class dom2SaxClass;
 78  
 
 79  
     static {
 80  1
         Class cl = null;
 81  
         try {
 82  1
             cl = Class.forName("org.apache.xalan.xsltc.trax.DOM2SAX");
 83  0
         } catch (Throwable t) {}
 84  1
         dom2SaxClass = cl;
 85  1
     }
 86  
 
 87  
 
 88  21
     public XmlConverter() {
 89  21
     }
 90  
 
 91  0
     public XmlConverter(DocumentBuilderFactory documentBuilderFactory) {
 92  0
         this.documentBuilderFactory = documentBuilderFactory;
 93  0
     }
 94  
 
 95  
 
 96  
     /**
 97  
      * Converts the given input Source into the required result
 98  
      */
 99  
     public void toResult(Source source, Result result) throws TransformerException {
 100  1
         if (source == null) {
 101  0
             return;
 102  
         }
 103  1
         Transformer transformer = createTransfomer();
 104  1
         if (transformer == null) {
 105  0
             throw new TransformerException("Could not create a transformer - JAXP is misconfigured!");
 106  
         }
 107  1
         transformer.setOutputProperty(OutputKeys.ENCODING, defaultCharset);
 108  1
         transformer.transform(source, result);
 109  1
     }
 110  
 
 111  
     /**
 112  
      * Converts the given byte[] to a Source
 113  
      */
 114  
     @Converter
 115  
     public BytesSource toSource(byte[] data) {
 116  0
         return new BytesSource(data);
 117  
     }
 118  
 
 119  
 
 120  
     /**
 121  
      * Converts the given String to a Source
 122  
      */
 123  
     @Converter
 124  
     public StringSource toSource(String data) {
 125  2
         return new StringSource(data);
 126  
     }
 127  
 
 128  
     /**
 129  
      * Converts the given Document to a Source
 130  
      */
 131  
     @Converter
 132  
     public DOMSource toSource(Document document) {
 133  0
         return new DOMSource(document);
 134  
     }
 135  
 
 136  
     /**
 137  
      * Converts the given input Source into text
 138  
      */
 139  
     @Converter
 140  
     public String toString(Source source) throws TransformerException {
 141  1
         if (source == null) {
 142  0
             return null;
 143  1
         } else if (source instanceof StringSource) {
 144  0
             return ((StringSource) source).getText();
 145  1
         } else if (source instanceof BytesSource) {
 146  0
             return new String(((BytesSource) source).getData());
 147  
         } else {
 148  1
             StringWriter buffer = new StringWriter();
 149  1
             toResult(source, new StreamResult(buffer));
 150  1
             return buffer.toString();
 151  
         }
 152  
     }
 153  
 
 154  
     /**
 155  
      * Converts the given input Node into text
 156  
      */
 157  
     @Converter
 158  
     public String toString(Node node) throws TransformerException {
 159  1
         return toString(new DOMSource(node));
 160  
     }
 161  
 
 162  
     /**
 163  
      * Converts the source instance to a {@link DOMSource} or returns null if the conversion is not
 164  
      * supported (making it easy to derive from this class to add new kinds of conversion).
 165  
      */
 166  
     @Converter
 167  
     public DOMSource toDOMSource(Source source) throws ParserConfigurationException, IOException, SAXException, TransformerException {
 168  0
         if (source instanceof DOMSource) {
 169  0
             return (DOMSource) source;
 170  
         }
 171  0
         else if (source instanceof SAXSource) {
 172  0
             return toDOMSourceFromSAX((SAXSource) source);
 173  
         }
 174  0
         else if (source instanceof StreamSource) {
 175  0
             return toDOMSourceFromStream((StreamSource) source);
 176  
         }
 177  
         else {
 178  0
             return null;
 179  
         }
 180  
     }
 181  
 
 182  
     /**
 183  
      * Converts the source instance to a {@link SAXSource} or returns null if the conversion is not
 184  
      * supported (making it easy to derive from this class to add new kinds of conversion).
 185  
      */
 186  
     @Converter
 187  
     public SAXSource toSAXSource(Source source) throws IOException, SAXException, TransformerException {
 188  0
         if (source instanceof SAXSource) {
 189  0
             return (SAXSource) source;
 190  
         }
 191  0
         else if (source instanceof DOMSource) {
 192  0
             return toSAXSourceFromDOM((DOMSource) source);
 193  
         }
 194  0
         else if (source instanceof StreamSource) {
 195  0
             return toSAXSourceFromStream((StreamSource) source);
 196  
         }
 197  
         else {
 198  0
             return null;
 199  
         }
 200  
     }
 201  
 
 202  
     @Converter
 203  
     public StreamSource toStreamSource(Source source) throws TransformerException {
 204  0
         if (source instanceof StreamSource) {
 205  0
             return (StreamSource) source;
 206  0
         } else if (source instanceof DOMSource) {
 207  0
             return toStreamSourceFromDOM((DOMSource) source);
 208  0
         } else if (source instanceof SAXSource) {
 209  0
             return toStreamSourceFromSAX((SAXSource) source);
 210  
         } else {
 211  0
             return null;
 212  
         }
 213  
     }
 214  
 
 215  
     @Converter
 216  
     public StreamSource toStreamSourceFromSAX(SAXSource source) throws TransformerException {
 217  0
         InputSource inputSource = source.getInputSource();
 218  0
         if (inputSource != null) {
 219  0
             if (inputSource.getCharacterStream() != null) {
 220  0
                 return new StreamSource(inputSource.getCharacterStream());
 221  
             }
 222  0
             if (inputSource.getByteStream() != null) {
 223  0
                 return new StreamSource(inputSource.getByteStream());
 224  
             }
 225  
         }
 226  0
         String result = toString(source);
 227  0
         return new StringSource(result);
 228  
     }
 229  
 
 230  
     @Converter
 231  
     public StreamSource toStreamSourceFromDOM(DOMSource source) throws TransformerException {
 232  0
         String result = toString(source);
 233  0
         return new StringSource(result);
 234  
     }
 235  
 
 236  
     @Converter
 237  
     public SAXSource toSAXSourceFromStream(StreamSource source) {
 238  
         InputSource inputSource;
 239  0
         if (source.getReader() != null) {
 240  0
             inputSource = new InputSource(source.getReader());
 241  0
         } else {
 242  0
             inputSource = new InputSource(source.getInputStream());
 243  
         }
 244  0
         inputSource.setSystemId(source.getSystemId());
 245  0
         inputSource.setPublicId(source.getPublicId());
 246  0
         return new SAXSource(inputSource);
 247  
     }
 248  
 
 249  
     @Converter
 250  
     public Reader toReaderFromSource(Source src) throws TransformerException {
 251  0
         StreamSource stSrc = toStreamSource(src);
 252  0
         Reader r = stSrc.getReader();
 253  0
         if (r == null) {
 254  0
             r = new InputStreamReader(stSrc.getInputStream());
 255  
         }
 256  0
         return r;
 257  
     }
 258  
 
 259  
     @Converter
 260  
     public DOMSource toDOMSourceFromStream(StreamSource source) throws ParserConfigurationException, IOException, SAXException {
 261  0
         DocumentBuilder builder = createDocumentBuilder();
 262  0
         String systemId = source.getSystemId();
 263  0
         Document document = null;
 264  0
         Reader reader = source.getReader();
 265  0
         if (reader != null) {
 266  0
             document = builder.parse(new InputSource(reader));
 267  0
         } else {
 268  0
             InputStream inputStream = source.getInputStream();
 269  0
             if (inputStream != null) {
 270  0
                 InputSource inputsource = new InputSource(inputStream);
 271  0
                 inputsource.setSystemId(systemId);
 272  0
                 document = builder.parse(inputsource);
 273  0
             }
 274  
             else {
 275  0
                 throw new IOException("No input stream or reader available");
 276  
             }
 277  
         }
 278  0
         return new DOMSource(document, systemId);
 279  
     }
 280  
 
 281  
     @Converter
 282  
     public SAXSource toSAXSourceFromDOM(DOMSource source) throws TransformerException {
 283  0
         if (dom2SaxClass != null) {
 284  
             try {
 285  0
                 Constructor cns = dom2SaxClass.getConstructor(new Class[] { Node.class });
 286  0
                 XMLReader converter = (XMLReader) cns.newInstance(new Object[] { source.getNode() });
 287  0
                 return new SAXSource(converter, new InputSource());
 288  0
             } catch (Exception e) {
 289  0
                 throw new TransformerException(e);
 290  
             }
 291  
         } else {
 292  0
             String str = toString(source);
 293  0
             StringReader reader = new StringReader(str);
 294  0
             return new SAXSource(new InputSource(reader));
 295  
         }
 296  
     }
 297  
 
 298  
     @Converter
 299  
     public DOMSource toDOMSourceFromSAX(SAXSource source) throws IOException, SAXException, ParserConfigurationException, TransformerException {
 300  0
         return new DOMSource(toDOMNodeFromSAX(source));
 301  
     }
 302  
 
 303  
     @Converter
 304  
     public Node toDOMNodeFromSAX(SAXSource source) throws ParserConfigurationException, IOException, SAXException, TransformerException {
 305  0
         DOMResult result = new DOMResult();
 306  0
         toResult(source, result);
 307  0
         return result.getNode();
 308  
     }
 309  
 
 310  
     /**
 311  
      * Converts the given TRaX Source into a W3C DOM node
 312  
      * @throws SAXException
 313  
      * @throws IOException
 314  
      * @throws ParserConfigurationException
 315  
      */
 316  
     @Converter
 317  
     public Node toDOMNode(Source source) throws TransformerException, ParserConfigurationException, IOException, SAXException {
 318  0
         DOMSource domSrc = toDOMSource(source);
 319  0
         return domSrc != null ? domSrc.getNode() :  null;
 320  
     }
 321  
 
 322  
     /**
 323  
      * Create a DOM element from the given source.
 324  
      *
 325  
      * @param source
 326  
      * @return
 327  
      * @throws TransformerException
 328  
      * @throws ParserConfigurationException
 329  
      * @throws IOException
 330  
      * @throws SAXException
 331  
      */
 332  
     @Converter
 333  
     public Element toDOMElement(Source source) throws TransformerException, ParserConfigurationException, IOException, SAXException {
 334  0
         Node node = toDOMNode(source);
 335  0
         return toDOMElement(node);
 336  
     }
 337  
 
 338  
     /**
 339  
      * Create a DOM element from the DOM node.
 340  
      * Simply cast if the node is an Element, or
 341  
      * return the root element if it is a Document.
 342  
      *
 343  
      * @param node
 344  
      * @return
 345  
      * @throws TransformerException
 346  
      */
 347  
     @Converter
 348  
     public Element toDOMElement(Node node) throws TransformerException {
 349  
         // If the node is an document, return the root element
 350  0
         if (node instanceof Document) {
 351  0
             return ((Document) node).getDocumentElement();
 352  
         // If the node is an element, just cast it
 353  0
         } else if (node instanceof Element) {
 354  0
             return (Element) node;
 355  
         // Other node types are not handled
 356  
         } else {
 357  0
             throw new TransformerException("Unable to convert DOM node to an Element");
 358  
         }
 359  
     }
 360  
 
 361  
     /**
 362  
      * Converts the given data to a DOM document
 363  
      *
 364  
      * @param data is the data to be parsed
 365  
      * @return the parsed document
 366  
      */
 367  
     @Converter
 368  
     public Document toDOMDocument(byte[] data) throws IOException, SAXException, ParserConfigurationException {
 369  24
         DocumentBuilder documentBuilder = getDocumentBuilderFactory().newDocumentBuilder();
 370  24
         return documentBuilder.parse(new ByteArrayInputStream(data));
 371  
     }
 372  
 
 373  
     /**
 374  
      * Converts the given {@link InputStream} to a DOM document
 375  
      *
 376  
      * @param in is the data to be parsed
 377  
      * @return the parsed document
 378  
      */
 379  
     @Converter
 380  
     public Document toDOMDocument(InputStream in) throws IOException, SAXException, ParserConfigurationException {
 381  0
         DocumentBuilder documentBuilder = getDocumentBuilderFactory().newDocumentBuilder();
 382  0
         return documentBuilder.parse(in);
 383  
     }
 384  
 
 385  
     /**
 386  
      * Converts the given {@link InputSource} to a DOM document
 387  
      *
 388  
      * @param in is the data to be parsed
 389  
      * @return the parsed document
 390  
      */
 391  
     @Converter
 392  
     public Document toDOMDocument(InputSource in) throws IOException, SAXException, ParserConfigurationException {
 393  0
         DocumentBuilder documentBuilder = getDocumentBuilderFactory().newDocumentBuilder();
 394  0
         return documentBuilder.parse(in);
 395  
     }
 396  
 
 397  
     /**
 398  
      * Converts the given {@link String} to a DOM document
 399  
      *
 400  
      * @param text is the data to be parsed
 401  
      * @return the parsed document
 402  
      */
 403  
     @Converter
 404  
     public Document toDOMDocument(String text) throws IOException, SAXException, ParserConfigurationException {
 405  24
         return toDOMDocument(text.getBytes());
 406  
     }
 407  
 
 408  
     /**
 409  
      * Converts the given {@link File} to a DOM document
 410  
      *
 411  
      * @param file is the data to be parsed
 412  
      * @return the parsed document
 413  
      */
 414  
     @Converter
 415  
     public Document toDOMDocument(File file) throws IOException, SAXException, ParserConfigurationException {
 416  0
         DocumentBuilder documentBuilder = getDocumentBuilderFactory().newDocumentBuilder();
 417  0
         return documentBuilder.parse(file);
 418  
     }
 419  
 
 420  
 
 421  
     /**
 422  
      * Create a DOM document from the given source.
 423  
      *
 424  
      * @param source
 425  
      * @return
 426  
      * @throws TransformerException
 427  
      * @throws ParserConfigurationException
 428  
      * @throws IOException
 429  
      * @throws SAXException
 430  
      */
 431  
     @Converter
 432  
     public Document toDOMDocument(Source source) throws TransformerException, ParserConfigurationException, IOException, SAXException {
 433  0
         Node node = toDOMNode(source);
 434  0
         return toDOMDocument(node);
 435  
     }
 436  
 
 437  
     /**
 438  
      * Create a DOM document from the given Node.
 439  
      * If the node is an document, just cast it,
 440  
      * if the node is an root element, retrieve its
 441  
      * owner element or create a new document and import
 442  
      * the node.
 443  
      *
 444  
      * @param node
 445  
      * @return
 446  
      * @throws ParserConfigurationException
 447  
      * @throws TransformerException
 448  
      */
 449  
     @Converter
 450  
     public Document toDOMDocument(Node node) throws ParserConfigurationException, TransformerException {
 451  
         // If the node is the document, just cast it
 452  0
         if (node instanceof Document) {
 453  0
             return (Document) node;
 454  
         // If the node is an element
 455  0
         } else if (node instanceof Element) {
 456  0
             Element elem = (Element) node;
 457  
             // If this is the root element, return its owner document
 458  0
             if (elem.getOwnerDocument().getDocumentElement() == elem) {
 459  0
                 return elem.getOwnerDocument();
 460  
             // else, create a new doc and copy the element inside it
 461  
             } else {
 462  0
                 Document doc = createDocument();
 463  0
                 doc.appendChild(doc.importNode(node, true));
 464  0
                 return doc;
 465  
             }
 466  
         // other element types are not handled
 467  
         } else {
 468  0
             throw new TransformerException("Unable to convert DOM node to a Document");
 469  
         }
 470  
     }
 471  
 
 472  
     // Properties
 473  
     //-------------------------------------------------------------------------
 474  
     public DocumentBuilderFactory getDocumentBuilderFactory() {
 475  24
         if (documentBuilderFactory == null) {
 476  18
             documentBuilderFactory = createDocumentBuilderFactory();
 477  
         }
 478  24
         return documentBuilderFactory;
 479  
     }
 480  
 
 481  
     public void setDocumentBuilderFactory(DocumentBuilderFactory documentBuilderFactory) {
 482  0
         this.documentBuilderFactory = documentBuilderFactory;
 483  0
     }
 484  
 
 485  
 
 486  
     // Helper methods
 487  
     //-------------------------------------------------------------------------
 488  
     public DocumentBuilderFactory createDocumentBuilderFactory() {
 489  18
         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
 490  18
         factory.setNamespaceAware(true);
 491  18
         factory.setIgnoringElementContentWhitespace(true);
 492  18
         factory.setIgnoringComments(true);
 493  18
         return factory;
 494  
     }
 495  
 
 496  
 
 497  
     public DocumentBuilder createDocumentBuilder() throws ParserConfigurationException {
 498  0
         DocumentBuilderFactory factory = getDocumentBuilderFactory();
 499  0
         return factory.newDocumentBuilder();
 500  
     }
 501  
 
 502  
     public Document createDocument() throws ParserConfigurationException {
 503  0
         DocumentBuilder builder = createDocumentBuilder();
 504  0
         return builder.newDocument();
 505  
     }
 506  
 
 507  
     public TransformerFactory getTransformerFactory() {
 508  2
         if (transformerFactory == null) {
 509  2
             transformerFactory = createTransformerFactory();
 510  
         }
 511  2
         return transformerFactory;
 512  
     }
 513  
 
 514  
     public void setTransformerFactory(TransformerFactory transformerFactory) {
 515  0
         this.transformerFactory = transformerFactory;
 516  0
     }
 517  
 
 518  
     public Transformer createTransfomer() throws TransformerConfigurationException {
 519  1
         TransformerFactory factory = getTransformerFactory();
 520  1
         return factory.newTransformer();
 521  
     }
 522  
 
 523  
     public TransformerFactory createTransformerFactory() {
 524  2
         TransformerFactory answer = TransformerFactory.newInstance();
 525  2
         return answer;
 526  
     }
 527  
 
 528  
 }