KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dlog4j > util > tags > CompareTag


1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU Library General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  */

16 package dlog4j.util.tags;
17
18 import javax.servlet.jsp.JspException JavaDoc;
19 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
20
21 /**
22  * @author Liudong
23  * 比较两个变量大小的标签库
24  */

25 public class CompareTag extends TagSupport JavaDoc {
26
27     public final static int EQUAL = 0; //等于
28
public final static int NOT_EQUAL = 1; //不等于
29
public final static int GREATER_THAN = 2; //大于
30
public final static int NOT_GREATER_THAN = 3; //不大于
31
public final static int LESS_THAN = 4; //小于
32
public final static int NOT_LESS_THAN = 5; //不小于
33
public final static int QUOTIENT = 100; //求商
34
public final static int COMPLIMENT_ZERO = 101;//余数为零
35
public final static int COMPLIMENT_NOT_ZERO = 102;//余数不为零
36

37     String JavaDoc num1;
38     String JavaDoc num2;
39     int method = 0;
40     
41     public int doEndTag() throws JspException JavaDoc {
42         //release();
43
return EVAL_PAGE;
44     }
45     /*
46     public void release() {
47         method = 0;
48         num1 = null;
49         num2 = null;
50     }*/

51     /* (non-Javadoc)
52      * @see javax.servlet.jsp.tagext.Tag#doStartTag()
53      */

54     public int doStartTag() throws JspException JavaDoc {
55         Object JavaDoc obj1 = pageContext.findAttribute(num1);
56         Object JavaDoc obj2 = pageContext.findAttribute(num2);
57         String JavaDoc s1 = (obj1!=null)?obj1.toString():num1;
58         String JavaDoc s2 = (obj2!=null)?obj2.toString():num2;
59         return compare(s1,s2,method)?EVAL_BODY_INCLUDE:SKIP_BODY;
60     }
61     /**
62      * 两数字比较,通过重载该类来实现更高级的比较操作
63      * @param s1
64      * @param s2
65      * @param extend
66      * @return
67      */

68     public boolean compare(String JavaDoc s1, String JavaDoc s2, int extend){
69         if(s1==null||s2==null)
70             return false;
71         int i1 = Integer.parseInt(s1);
72         int i2 = Integer.parseInt(s2);
73         if(extend==EQUAL && i1==i2)
74             return true;
75         if(extend==NOT_EQUAL && i1!=i2)
76             return true;
77         if(extend==GREATER_THAN && i1>i2)
78             return true;
79         if(extend==LESS_THAN && i1<i2)
80             return true;
81         if(extend==NOT_LESS_THAN && i1>=i2)
82             return true;
83         if(extend==NOT_GREATER_THAN && i1<=i2)
84             return true;
85         if(extend==COMPLIMENT_ZERO)
86             return (i1 % i2) == 0;
87         if(extend==COMPLIMENT_NOT_ZERO)
88             return (i1 % i2) > 0;
89             
90         return false;
91     }
92
93     /**
94      * @return
95      */

96     public String JavaDoc getNum1() {
97         return num1;
98     }
99
100     /**
101      * @return
102      */

103     public String JavaDoc getNum2() {
104         return num2;
105     }
106
107     /**
108      * @param string
109      */

110     public void setNum1(String JavaDoc string) {
111         num1 = string;
112     }
113     /**
114      * @param string
115      */

116     public void setNum2(String JavaDoc string) {
117         num2 = string;
118     }
119
120     /**
121      * @return
122      */

123     public int getMethod() {
124         return method;
125     }
126
127     /**
128      * @param i
129      */

130     public void setMethod(int i) {
131         method = i;
132     }
133
134 }
135
Popular Tags