KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > transformers > codec > XmlEntityEncoder


1 /*
2  * $Id: XmlEntityEncoder.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.codec;
12
13 import org.mule.config.i18n.Message;
14 import org.mule.config.i18n.Messages;
15 import org.mule.transformers.AbstractTransformer;
16 import org.mule.umo.transformer.TransformerException;
17 import org.mule.util.XMLEntityCodec;
18
19 /**
20  * Encodes a string with XML entities
21  */

22 public class XmlEntityEncoder extends AbstractTransformer
23 {
24     /**
25      * Serial version
26      */

27     private static final long serialVersionUID = -6652088350713495578L;
28
29     public XmlEntityEncoder()
30     {
31         registerSourceType(String JavaDoc.class);
32         registerSourceType(byte[].class);
33         setReturnClass(String JavaDoc.class);
34     }
35
36     public Object JavaDoc doTransform(Object JavaDoc src, String JavaDoc encoding) throws TransformerException
37     {
38         try
39         {
40             String JavaDoc data;
41
42             if (src instanceof byte[])
43             {
44                 data = new String JavaDoc((byte[])src, encoding);
45             }
46             else
47             {
48                 data = (String JavaDoc)src;
49             }
50
51             return XMLEntityCodec.encodeString(data);
52         }
53         catch (Exception JavaDoc ex)
54         {
55             throw new TransformerException(new Message(Messages.TRANSFORM_FAILED_FROM_X_TO_X, src.getClass()
56                 .getName(), "XML"), this, ex);
57
58         }
59     }
60 }
61
Popular Tags