001: /*
002: * This program is free software; you can redistribute it and/or modify
003: * it under the terms of the GNU General Public License as published by
004: * the Free Software Foundation; either version 2 of the License, or
005: * (at your option) any later version.
006: *
007: * This program is distributed in the hope that it will be useful,
008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: * GNU Library General Public License for more details.
011: *
012: * You should have received a copy of the GNU General Public License
013: * along with this program; if not, write to the Free Software
014: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
015: */
016: package dlog4j.util.tags;
017:
018: import javax.servlet.jsp.JspException;
019: import javax.servlet.jsp.tagext.TagSupport;
020:
021: /**
022: * @author Liudong
023: * 比较两个变量大小的标签库
024: */
025: public class CompareTag extends TagSupport {
026:
027: public final static int EQUAL = 0; //等于
028: public final static int NOT_EQUAL = 1; //不等于
029: public final static int GREATER_THAN = 2; //大于
030: public final static int NOT_GREATER_THAN = 3; //不大于
031: public final static int LESS_THAN = 4; //小于
032: public final static int NOT_LESS_THAN = 5; //不小于
033: public final static int QUOTIENT = 100; //求商
034: public final static int COMPLIMENT_ZERO = 101;//余数为零
035: public final static int COMPLIMENT_NOT_ZERO = 102;//余数不为零
036:
037: String num1;
038: String num2;
039: int method = 0;
040:
041: public int doEndTag() throws JspException {
042: //release();
043: return EVAL_PAGE;
044: }
045:
046: /*
047: public void release() {
048: method = 0;
049: num1 = null;
050: num2 = null;
051: }*/
052: /* (non-Javadoc)
053: * @see javax.servlet.jsp.tagext.Tag#doStartTag()
054: */
055: public int doStartTag() throws JspException {
056: Object obj1 = pageContext.findAttribute(num1);
057: Object obj2 = pageContext.findAttribute(num2);
058: String s1 = (obj1 != null) ? obj1.toString() : num1;
059: String s2 = (obj2 != null) ? obj2.toString() : num2;
060: return compare(s1, s2, method) ? EVAL_BODY_INCLUDE : SKIP_BODY;
061: }
062:
063: /**
064: * 两数字比较,通过重载该类来实现更高级的比较操作
065: * @param s1
066: * @param s2
067: * @param extend
068: * @return
069: */
070: public boolean compare(String s1, String s2, int extend) {
071: if (s1 == null || s2 == null)
072: return false;
073: int i1 = Integer.parseInt(s1);
074: int i2 = Integer.parseInt(s2);
075: if (extend == EQUAL && i1 == i2)
076: return true;
077: if (extend == NOT_EQUAL && i1 != i2)
078: return true;
079: if (extend == GREATER_THAN && i1 > i2)
080: return true;
081: if (extend == LESS_THAN && i1 < i2)
082: return true;
083: if (extend == NOT_LESS_THAN && i1 >= i2)
084: return true;
085: if (extend == NOT_GREATER_THAN && i1 <= i2)
086: return true;
087: if (extend == COMPLIMENT_ZERO)
088: return (i1 % i2) == 0;
089: if (extend == COMPLIMENT_NOT_ZERO)
090: return (i1 % i2) > 0;
091:
092: return false;
093: }
094:
095: /**
096: * @return
097: */
098: public String getNum1() {
099: return num1;
100: }
101:
102: /**
103: * @return
104: */
105: public String getNum2() {
106: return num2;
107: }
108:
109: /**
110: * @param string
111: */
112: public void setNum1(String string) {
113: num1 = string;
114: }
115:
116: /**
117: * @param string
118: */
119: public void setNum2(String string) {
120: num2 = string;
121: }
122:
123: /**
124: * @return
125: */
126: public int getMethod() {
127: return method;
128: }
129:
130: /**
131: * @param i
132: */
133: public void setMethod(int i) {
134: method = i;
135: }
136:
137: }
|