KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > transformers > xml > XmlToDomDocument


1 /*
2  * $Id: XmlToDomDocument.java 3937 2006-11-20 16:04:25Z lajos $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.transformers.xml;
12
13 import org.mule.umo.transformer.TransformerException;
14 import org.w3c.dom.Document JavaDoc;
15
16 import javax.xml.transform.OutputKeys JavaDoc;
17 import javax.xml.transform.Source JavaDoc;
18 import javax.xml.transform.Transformer JavaDoc;
19 import javax.xml.transform.TransformerFactory JavaDoc;
20
21 /**
22  * <code>XmlToDomDocument</code> transforms a XML String to org.w3c.dom.Document.
23  */

24 public class XmlToDomDocument extends AbstractXmlTransformer
25 {
26     /**
27      * Serial version
28      */

29     private static final long serialVersionUID = 5056464684549099908L;
30
31     public XmlToDomDocument()
32     {
33         registerSourceType(String JavaDoc.class);
34         registerSourceType(byte[].class);
35     }
36
37     public Object JavaDoc doTransform(Object JavaDoc src, String JavaDoc encoding) throws TransformerException
38     {
39         try
40         {
41             Source JavaDoc sourceDoc = getXmlSource(src);
42             if (sourceDoc == null) return null;
43
44             // If returnClass is not set, assume W3C DOM
45
// This is the original behaviour
46
ResultHolder holder = getResultHolder(returnClass);
47             if (holder == null) holder = getResultHolder(Document JavaDoc.class);
48
49             Transformer JavaDoc idTransformer = TransformerFactory.newInstance().newTransformer();
50             idTransformer.setOutputProperty(OutputKeys.ENCODING, encoding);
51             idTransformer.transform(sourceDoc, holder.getResult());
52
53             return holder.getResultObject();
54         }
55         catch (Exception JavaDoc e)
56         {
57             throw new TransformerException(this, e);
58         }
59     }
60 }
61
Popular Tags