KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > micronova > jsp > tag > ELComparator


1 /*
2
3 Copyright 2003-2007 MicroNova (R)
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or
7 without modification, are permitted provided that the following
8 conditions are met:
9
10     * Redistributions of source code must retain the above copyright
11     notice, this list of conditions and the following disclaimer.
12
13     * Redistributions in binary form must reproduce the above copyright
14     notice, this list of conditions and the following disclaimer in the
15     documentation and/or other materials provided with the distribution.
16
17     * Neither the name of MicroNova nor the names of its contributors
18     may be used to endorse or promote products derived from this
19     software without specific prior written permission.
20
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 POSSIBILITY OF SUCH DAMAGE.
32
33 */

34
35 package com.micronova.jsp.tag;
36
37 import com.micronova.util.*;
38 import java.util.*;
39 import javax.servlet.jsp.*;
40 import java.lang.reflect.*;
41 import java.util.regex.*;
42
43 public class ELComparator extends NestedMap implements Comparator
44 {
45     public static final String JavaDoc EQUAL = "equal";
46     public static final String JavaDoc LESS = "less";
47     public static final String JavaDoc MORE = "more";
48     public static final String JavaDoc X = "x";
49     public static final String JavaDoc Y = "y";
50     public static final String JavaDoc CODEC = "codec";
51
52     protected PageContext _pageContext;
53
54     public ELComparator(PageContext pageContext, Object JavaDoc source) throws Exception JavaDoc
55     {
56         super(source);
57
58         _pageContext = pageContext;
59     }
60
61     public int compare(Object JavaDoc o1, Object JavaDoc o2)
62     {
63         try
64         {
65             PageContext pageContext = _pageContext;
66
67             String JavaDoc codec = getString(CODEC);
68
69             if (!TypeUtil.isEmptyString(codec))
70             {
71                 o1 = EL.applyCodec(pageContext, codec, o1);
72                 o2 = EL.applyCodec(pageContext, codec, o2);
73             }
74
75             put(X, o1);
76             put(Y, o2);
77
78             String JavaDoc equalCodec = getString(EQUAL);
79             
80             if (!TypeUtil.isEmptyString(equalCodec))
81             {
82                 Object JavaDoc equalValue = EL.applyCodec(pageContext, equalCodec, this);
83                
84                 if (TypeUtil.isTrue(equalValue))
85                 {
86                     return 0;
87                 }
88             }
89
90             int comparison = -2;
91             
92             if (o1 == null)
93             {
94                 if (o2 == null)
95                 {
96                     return 0;
97                 }
98                 else
99                 {
100                     return -1;
101                 }
102             }
103             else
104             {
105                 comparison = ((Comparable JavaDoc)o1).compareTo(o2);
106
107                 if (comparison == 0)
108                 {
109                     return 0;
110                 }
111             }
112
113             String JavaDoc lessCodec = getString(LESS);
114             
115             if (!TypeUtil.isEmptyString(lessCodec))
116             {
117                 Object JavaDoc lessValue = EL.applyCodec(pageContext, lessCodec, this);
118                 
119                 if (TypeUtil.isTrue(lessValue))
120                 {
121                     return -1;
122                 }
123                 else
124                 {
125                     return 1;
126                 }
127             }
128             
129             String JavaDoc moreCodec = getString(MORE);
130             
131             if (!TypeUtil.isEmptyString(moreCodec))
132             {
133                 Object JavaDoc moreValue = EL.applyCodec(pageContext, moreCodec, this);
134                 
135                 if (TypeUtil.isTrue(moreValue))
136                 {
137                     return 1;
138                 }
139                 else
140                 {
141                     return -1;
142                 }
143             }
144
145             return comparison;
146         }
147         catch (Exception JavaDoc e)
148         {
149             throw new RuntimeException JavaDoc(e);
150         }
151     }
152 }
153
Popular Tags