KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > contrib > dbroggisch > display > filters > LookupFilter


1 /*
2  * Copyright (C) 2003 Diez B. Roggisch [deets@web.de]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: LookupFilter.java,v 1.3 2004/02/01 05:16:27 christianc Exp $
19  */

20 package org.enhydra.barracuda.contrib.dbroggisch.display.filters;
21
22 import java.util.*;
23 import java.io.*;
24 import org.enhydra.barracuda.contrib.dbroggisch.display.filters.dtd.Lookup;
25 import org.enhydra.barracuda.contrib.dbroggisch.display.filters.dtd.Entry;
26 import org.apache.log4j.Logger;
27
28 /* This class uses a given Map to lookup the
29  * objects meaning.
30  *
31  * @author <a HREF="mailto:deets@web.de">Diez B. Roggisch</a>
32  * @version 1.0
33  */

34 public class LookupFilter implements Filter {
35     protected static final Logger logger = Logger.getLogger(LookupFilter.class.getName());
36
37     private Map lookupMap;
38
39     public LookupFilter() {
40         this(null);
41     }
42
43     public LookupFilter(Map lookupMap) {
44         this.lookupMap = lookupMap;
45     }
46
47     /** Looks up the given key. If the key doesn't exist, <code>null</code>
48      * returned. If the lookupMap is <code>null</code>, the key itself will be returned.
49      *
50      * @param key The key to lookup.
51      * @return the mapped objet.
52      */

53     public Object JavaDoc filter(Object JavaDoc key, FilterContext context)
54         throws FilterException
55     {
56         if(key != null && lookupMap != null) {
57             return lookupMap.get(key);
58         }
59         return key;
60     }
61
62     public Filter configure(Object JavaDoc obj)
63         throws FilterException
64     {
65         lookupMap = new HashMap();
66         try {
67             Lookup xmlObj = (Lookup)obj;
68             List entries = xmlObj.getEntryList();
69             for(Iterator it = entries.iterator(); it.hasNext();) {
70                 Entry entry = (Entry)it.next();
71                 Object JavaDoc key;
72                 if(entry.getType().equals("Integer")) {
73                     key = new Integer JavaDoc(Integer.parseInt(entry.getKey()));
74                 } else if(entry.getType().equals("Boolean")) {
75                     key = Boolean.valueOf(entry.getKey());
76                 } else if(entry.getType().equals("Class")) {
77                     try {
78                         key = Class.forName(entry.getKey());
79                     } catch(ClassNotFoundException JavaDoc ex) {
80                         StringWriter sw = new StringWriter();
81                         ex.printStackTrace(new PrintWriter(sw));
82                         logger.error(sw.toString());
83                         throw new FilterException(ex.getMessage());
84                     }
85                 } else {
86                     key = entry.getKey();
87                 }
88                 lookupMap.put(key, entry.getValue());
89             }
90         } catch(ClassCastException JavaDoc ex) {
91             ex.printStackTrace();
92         }
93         return this;
94     }
95 }
96
Popular Tags