1 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 ; 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 ; 25 import java.math.BigDecimal ; 26 27 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 v) throws JspTagException { 43 value = getAttribute(v); 44 } 45 46 public void setValueset(String vs) throws JspTagException { 47 valueSet = getAttribute(vs); 48 } 49 public void setRegexp(String r) throws JspTagException { 50 regexp = getAttribute(r); 51 } 52 53 private Attribute referid2 = Attribute.NULL; 54 public void setReferid2(String r) throws JspTagException { 55 referid2 = getAttribute(r); 56 } 57 58 protected boolean doCompare(Comparable v1, Comparable 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 try { 65 return v1.compareTo(v2) == 0; } catch (Throwable e) { 67 return false; 69 } 70 } 71 72 protected Object getCompare2() throws JspTagException { 73 if (referid2 == Attribute.NULL) { 74 throw new JspTagException ("Attribute 'value' or 'referid2' must be indicated"); 75 } 76 Object 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 { 87 Object compare1; 88 89 if (getReferid() == null) { 91 compare1 = findWriter().getWriterValue(); 92 if (compare1 == null) compare1 = ""; 93 } else { 94 compare1 = getObject(getReferid()); 95 } 96 if (compare1 instanceof Boolean ) { 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 )) { 111 throw new JspTagException ("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 ("Cannot use 'regexp' attribute in combination with 'value' or 'valueSet' attributes"); 118 } 119 Pattern pattern = Pattern.compile (regexp.getString(this)); 120 result = pattern.matcher("" + compare1).matches(); 121 } else { 122 Set compareToSet = new HashSet(); 124 if (value != Attribute.NULL) { 125 if (valueSet != Attribute.NULL) { 126 throw new JspTagException ("Can specify both 'value' and 'valueset' attributes"); 127 } 128 if (referid2 != Attribute.NULL) { 129 throw new JspTagException ("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 ("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 ) { 145 compare1 = new BigDecimal (compare1.toString()); 146 while (i.hasNext()) { 147 Object compare2 = i.next(); 148 if (compare2 instanceof Date) { 149 compare2 = Casting.toInteger(compare2); 150 } 151 152 if (compare2 instanceof String ) { 153 if ("".equals(compare2)) { compare2 = new BigDecimal ("0"); 155 } else { 156 compare2 = new BigDecimal ((String )compare2); 157 } 158 } else if (compare2 instanceof Number ) { 159 compare2 = new BigDecimal (compare2.toString()); 160 } else if (compare2 instanceof Node) { 161 compare2 = new BigDecimal (((Node)compare2).getNumber()); 162 } 163 164 if (doCompare((Comparable )compare1, (Comparable )compare2)) { 165 result = true; 166 break; 167 168 } 169 170 } 171 } else { 172 while (i.hasNext()) { 173 Object compare2 = i.next(); 174 if (compare2 instanceof Date) { 175 compare2 = Casting.toInteger(compare2); 176 } 177 if (compare2 instanceof Number ) { 178 compare2 = new BigDecimal (compare2.toString()); 179 Number compare1n; 180 if ("".equals(compare1)) { compare1n = new BigDecimal ("0"); 182 } else { 183 compare1n = new BigDecimal ((String )compare1); 184 } 185 if (doCompare((Comparable )compare1n, (Comparable )compare2)) { 186 result = true; 187 break; 188 } 189 } else { if (! (compare2 instanceof Comparable )) compare2 = Casting.toString(compare2); 191 if (doCompare((Comparable )compare1, (Comparable )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 |