Source code: com/aendvari/tethys/tag/logic/CaseTag.java
1 /*
2 * CaseTag.java
3 *
4 * Copyright (c) 2001, 2002 Aendvari, Ltd. All Rights Reserved.
5 *
6 * See the file LICENSE for terms of use.
7 *
8 */
9
10 package com.aendvari.tethys.tag.logic;
11
12 import java.io.IOException;
13
14 import javax.servlet.http.*;
15 import javax.servlet.jsp.*;
16 import javax.servlet.jsp.tagext.*;
17
18 import com.aendvari.common.model.*;
19 import com.aendvari.tethys.*;
20 import com.aendvari.tethys.context.*;
21 import com.aendvari.tethys.tag.*;
22 import com.aendvari.tethys.tag.model.*;
23
24
25 /**
26 * <p>Comparison value for match tags.</p>
27 *
28 * @author Scott Milne
29 *
30 */
31
32 public class CaseTag extends ModelTreeTag
33 {
34 /* Variables */
35
36 /** string of the value to match */
37 protected String value;
38
39
40 /* Constructor */
41
42 public CaseTag()
43 {
44 super();
45 value = "";
46 }
47
48
49 /* Attributes */
50
51 public String getValue() { return value; }
52 public void setValue(String value) { this.value = value; }
53
54
55 /* Methods */
56
57 public int doStartTag() throws JspTagException
58 {
59 try
60 {
61 boolean matched = false;
62
63 // determine context of tag
64 establishModelContext();
65
66 // check to see if a name was given, if so, grab the value from the logic map
67 if (getName() != null)
68 {
69 // retrieve specified response
70 ContextMap responseMap = LogicTagData.getData(
71 pageContext, getDataScope()).getResponseMap();
72
73 // get response value
74 Response response = (Response)responseMap.getContext(
75 modelContext.extendLocation(getName()), false);
76
77 // check response
78 if (response == null)
79 {
80 // neither a name or an operator tag was found
81 throw new JspTagException("CaseTag: operator \"" + getName() + "\" could not be found.");
82 }
83
84 // get the match value
85 String matchValue = response.getValue();
86
87 // does the given match the given "value"
88 if ((getValue().compareTo(matchValue) == 0))
89 {
90 matched = true;
91 }
92 }
93 else
94 {
95 // make sure this tag is within a "MatchTag"
96 MatchTag matchTag = (MatchTag)findAncestorWithClass(this, MatchTag.class);
97 if( matchTag == null )
98 {
99 throw new JspTagException("CaseTag: without MatchTag or \"name\"");
100 }
101 else
102 {
103 // get the value to match with from the match tag
104 String matchValue = matchTag.getValue();
105
106 // does the given match the given "value"
107 if( (getValue().compareTo(matchValue) == 0) )
108 {
109 // tell the match tag that this case matched
110 matchTag.setCaseMatched(true);
111 matched = true;
112 }
113 }
114 }
115
116 // was something matched
117 if( matched )
118 {
119 // allow the body of this tag to be shown
120 return EVAL_BODY_INCLUDE;
121 }
122 }
123 catch (Exception e)
124 {
125 throw new JspTagException("CaseTag:" + e.toString());
126 }
127
128 // nothing matched, so don't display the body
129 return SKIP_BODY;
130 }
131 }
132