KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > bridge > jsp > taglib > pageflow > CompareTag


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.bridge.jsp.taglib.pageflow;
11
12 import org.mmbase.bridge.jsp.taglib.util.Attribute;
13 import org.mmbase.bridge.jsp.taglib.Condition;
14 import org.mmbase.bridge.jsp.taglib.WriterReferrer;
15
16 import org.mmbase.bridge.Node;
17 import org.mmbase.util.Casting;
18 import javax.servlet.jsp.JspTagException JavaDoc;
19
20 import org.mmbase.util.logging.Logger;
21 import org.mmbase.util.logging.Logging;
22
23 import java.util.*;
24 import java.util.regex.Pattern JavaDoc;
25 import java.math.BigDecimal JavaDoc;
26
27 /**
28  * A very simple tag to check if the value of a certain context
29  * variable equals a certain String value.
30  *
31  * @author Michiel Meeuwissen
32  * @version $Id: CompareTag.java,v 1.43 2005/12/05 14:19:25 michiel Exp $
33  */

34
35 public class CompareTag extends PresentTag implements Condition, WriterReferrer {
36
37     protected static final Logger log = Logging.getLoggerInstance(CompareTag.class);
38
39     private Attribute value = Attribute.NULL;
40     private Attribute valueSet = Attribute.NULL;
41     private Attribute regexp = Attribute.NULL;
42     public void setValue(String JavaDoc v) throws JspTagException JavaDoc {
43         value = getAttribute(v);
44     }
45
46     public void setValueset(String JavaDoc vs) throws JspTagException JavaDoc {
47         valueSet = getAttribute(vs);
48     }
49     public void setRegexp(String JavaDoc r) throws JspTagException JavaDoc {
50         regexp = getAttribute(r);
51     }
52
53     private Attribute referid2 = Attribute.NULL;
54     public void setReferid2(String JavaDoc r) throws JspTagException JavaDoc {
55         referid2 = getAttribute(r);
56     }
57
58     protected boolean doCompare(Comparable JavaDoc v1, Comparable JavaDoc v2) {
59         if (log.isDebugEnabled()) {
60             log.debug("comparing " + (v1 != null ? v1.getClass().getName() : "") + "'" + v1 + "' to " + (v2 != null ? v2.getClass().getName() : "")+ "'" + v2 + "'");
61         }
62
63         // TODO this is a bit oddly implemented, perhaps using org.mmbase.util.Casting, or 'equals' after all.
64
try {
65             return v1.compareTo(v2) == 0; // (cannot use 'equals' because BigDecimal then also compares scale, which doesn't interest us too much).
66
} catch (Throwable JavaDoc e) {
67             // for example if v1 is a Node and v2 is not, then it throws a ClassCastException, which we of course don't want here.
68
return false;
69         }
70     }
71
72     protected Object JavaDoc getCompare2() throws JspTagException JavaDoc {
73         if (referid2 == Attribute.NULL) {
74             throw new JspTagException JavaDoc("Attribute 'value' or 'referid2' must be indicated");
75         }
76         Object JavaDoc o = getObject(referid2.getString(this));
77         if (o instanceof Node) {
78             return "" + ((Node)o).getNumber();
79         } else {
80             return o;
81         }
82         
83     }
84
85
86     public int doStartTag() throws JspTagException JavaDoc {
87         Object JavaDoc compare1;
88         
89         // find compare1
90
if (getReferid() == null) {
91             compare1 = findWriter().getWriterValue();
92             if (compare1 == null) compare1 = "";
93         } else {
94             compare1 = getObject(getReferid());
95         }
96         if (compare1 instanceof Boolean JavaDoc) {
97             compare1 = compare1.toString();
98         } else if (compare1 instanceof Date) {
99             compare1 = Casting.toInteger(compare1);
100         } else if (compare1 instanceof List) {
101             compare1 = Casting.toString(compare1);
102         } else if (compare1 instanceof Node) {
103             compare1 = Casting.toString(compare1);
104         } else if (compare1 instanceof byte[]) {
105             compare1 = Casting.toString(compare1);
106         } else if (compare1 instanceof org.apache.commons.fileupload.FileItem) {
107             compare1 = Casting.toString(compare1);
108         }
109
110         if (! (compare1 instanceof Comparable JavaDoc)) {
111             throw new JspTagException JavaDoc("Cannot compare variable '" + getReferid() + "' of type " + compare1.getClass().getName());
112         }
113
114         boolean result = false;
115         if (regexp != Attribute.NULL) {
116             if (value != Attribute.NULL || valueSet != Attribute.NULL) {
117                 throw new JspTagException JavaDoc("Cannot use 'regexp' attribute in combination with 'value' or 'valueSet' attributes");
118             }
119             Pattern JavaDoc pattern = Pattern.compile (regexp.getString(this));
120             result = pattern.matcher("" + compare1).matches();
121         } else {
122             // find compare2-set.
123
Set compareToSet = new HashSet();
124             if (value != Attribute.NULL) {
125                 if (valueSet != Attribute.NULL) {
126                     throw new JspTagException JavaDoc("Can specify both 'value' and 'valueset' attributes");
127                 }
128                 if (referid2 != Attribute.NULL) {
129                     throw new JspTagException JavaDoc("Cannot indicate 'referid2' and 'value' attributes both");
130                 }
131                 compareToSet.add(value.getValue(this));
132             } else if (valueSet != Attribute.NULL) {
133                 if (referid2 != Attribute.NULL) {
134                     throw new JspTagException JavaDoc("Cannot indicate 'referid2' and 'valueSet' attributes both");
135                 }
136                 compareToSet.addAll(valueSet.getList(this));
137             } else {
138                 compareToSet.add(getCompare2());
139             }
140             
141             Iterator i = compareToSet.iterator();
142             
143             
144             if (compare1 instanceof Number JavaDoc) {
145                 compare1 = new BigDecimal JavaDoc(compare1.toString());
146                 while (i.hasNext()) {
147                     Object JavaDoc compare2 = i.next();
148                     if (compare2 instanceof Date) {
149                         compare2 = Casting.toInteger(compare2);
150                     }
151
152                     if (compare2 instanceof String JavaDoc) {
153                         if ("".equals(compare2)) { // do something reasonable in IsEmpty
154
compare2 = new BigDecimal JavaDoc("0");
155                         } else {
156                             compare2 = new BigDecimal JavaDoc((String JavaDoc)compare2);
157                         }
158                     } else if (compare2 instanceof Number JavaDoc) {
159                         compare2 = new BigDecimal JavaDoc(compare2.toString());
160                     } else if (compare2 instanceof Node) {
161                         compare2 = new BigDecimal JavaDoc(((Node)compare2).getNumber());
162                     }
163                     
164                     if (doCompare((Comparable JavaDoc)compare1, (Comparable JavaDoc)compare2)) {
165                         result = true;
166                         break;
167                         
168                     }
169                     
170                 }
171             } else {
172                 while (i.hasNext()) {
173                     Object JavaDoc compare2 = i.next();
174                     if (compare2 instanceof Date) {
175                         compare2 = Casting.toInteger(compare2);
176                     }
177                     if (compare2 instanceof Number JavaDoc) {
178                         compare2 = new BigDecimal JavaDoc(compare2.toString());
179                         Number JavaDoc compare1n;
180                         if ("".equals(compare1)) { // do something reasonable in IsEmpty
181
compare1n = new BigDecimal JavaDoc("0");
182                         } else {
183                             compare1n = new BigDecimal JavaDoc((String JavaDoc)compare1);
184                         }
185                         if (doCompare((Comparable JavaDoc)compare1n, (Comparable JavaDoc)compare2)) {
186                             result = true;
187                             break;
188                         }
189                     } else { // both compare1 and compare2 are not Number, simply compare then
190
if (! (compare2 instanceof Comparable JavaDoc)) compare2 = Casting.toString(compare2);
191                         if (doCompare((Comparable JavaDoc)compare1, (Comparable JavaDoc)compare2)) {
192                             result = true;
193                             break;
194                         }
195                     }
196                 }
197             }
198         }
199         if (result != getInverse() ) {
200             return EVAL_BODY;
201         } else {
202             return SKIP_BODY;
203         }
204     }
205 }
206
Popular Tags