KickJava   Java API By Example, From Geeks To Geeks.

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


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: SwitchFilter.java,v 1.2 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 org.apache.log4j.Logger;
24 import org.enhydra.barracuda.contrib.dbroggisch.display.filters.dtd.*;
25
26
27 /**
28  * This class executes the <code>filter</code>-method subsequently for all
29  * ist child-filters. The first one not to return <b>null</b> or throwing
30  * a <code>FilterException</code> wins.
31  *
32  * @author <a HREF="mailto:deets@web.de">Diez Roggisch</a>
33  * @version 1.0
34  */

35 public class SwitchFilter implements Filter {
36     static Logger logger = Logger.getLogger(SwitchFilter.class.getName());
37     Filter [] filters;
38
39
40     public SwitchFilter() {
41     }
42
43     public SwitchFilter(Filter [] fs) {
44         filters = fs;
45     }
46
47     public SwitchFilter(List fs) {
48         this((Filter[])fs.toArray(new Filter[1]));
49     }
50
51
52
53     /* The method invokes the filter of all given filters
54      * from first to last. The first one not returning null or throwing
55      * an exception wins.
56      *
57      * @param value to be filtered
58      * @return The first not-null value of the
59      * @exception org.enhydra.barracuda.contrib.dbroggisch.repopulation.FilterException <description>
60      */

61     public Object JavaDoc filter(Object JavaDoc value, FilterContext context)
62         throws FilterException
63     {
64         Object JavaDoc res = null;
65         for(int i = 0; i < filters.length; i++) {
66             try {
67                 res = filters[i].filter(value, context);
68                 if(logger.isDebugEnabled()) {
69                     logger.debug("Switch result: " + res);
70                 }
71             } catch(FilterException ex) {
72             }
73             if(res != null) {
74                 break;
75             }
76         }
77         return res;
78     }
79
80     public Filter configure(Object JavaDoc obj)
81         throws FilterException {
82         Switch fw = (Switch)obj;
83         List childs = FilterFactory.accumulateChilds(fw);
84         filters = new Filter[childs.size()];
85         int i = 0;
86         for(Iterator it = childs.iterator(); it.hasNext();) {
87             Object JavaDoc decl = it.next();
88             filters[i] = FilterFactory.createFilter(decl.getClass());
89             filters[i++].configure(decl);
90         }
91         return this;
92     }
93
94
95 }
96
Popular Tags