KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > transformers > simple > MapLookup


1 /*
2  * $Id: MapLookup.java 3798 2006-11-04 04:07:14Z aperepel $
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.simple;
12
13 import java.util.Map JavaDoc;
14
15 import org.mule.config.i18n.Message;
16 import org.mule.transformers.AbstractTransformer;
17 import org.mule.umo.transformer.TransformerException;
18
19 /**
20  * <code>MapLookup</code> looks up and returns an object from a Map based on a key.
21  *
22  * @author <a HREF="mailto:carlson@hotpop.com">Travis Carlson</a>
23  */

24
25 public class MapLookup extends AbstractTransformer
26 {
27
28     private static final long serialVersionUID = -9033005899991305309L;
29
30     protected Object JavaDoc key;
31
32     public MapLookup()
33     {
34         registerSourceType(Map JavaDoc.class);
35         setReturnClass(Object JavaDoc.class);
36     }
37
38     public Object JavaDoc doTransform(Object JavaDoc src, String JavaDoc encoding) throws TransformerException
39     {
40         if (src instanceof Map JavaDoc)
41         {
42             if (key != null)
43             {
44                 return ((Map JavaDoc)src).get(key);
45             }
46             else
47             {
48                 throw new TransformerException(
49                     Message.createStaticMessage("Property 'key' must be set in order to use this transformer."));
50             }
51         }
52         else
53             throw new TransformerException(
54                 Message.createStaticMessage("Message to transform must be of type java.util.Map"));
55     }
56
57     public Object JavaDoc getKey()
58     {
59         return key;
60     }
61
62     public void setKey(Object JavaDoc key)
63     {
64         this.key = key;
65     }
66 }
67
Popular Tags