1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.devyant.decorutils.tags.xmldecorator;
17
18 import org.devyant.decorutils.Utils;
19 import org.devyant.decorutils.exceptions.InvalidRuleFormatException;
20 import org.devyant.decorutils.exceptions.InvalidTagPlacementException;
21 import org.devyant.decorutils.xml.DynaNode;
22
23 import javax.servlet.jsp.JspException;
24 import javax.servlet.jsp.JspTagException;
25 import javax.servlet.jsp.tagext.BodyContent;
26 import java.io.IOException;
27 import java.util.Collection;
28
29 /***
30 * This tag enables you to specify rules for the evaluation
31 * or not of it's body.
32 * <p>
33 * These rules should be separated by semi-colons (';')
34 * which will equivelent to an OR operation. These rules should be
35 * written in the form: [attribute]=[value] or [attribute]!=[value].
36 * </p>
37 *
38 * @author Filipe Tavares
39 * @version $Revision: 1.0$ ($Author: ftavares$)
40 * @since 3/Fev/2005 18:54:29
41 */
42 public class XRuleTag extends RuledTag {
43 /***
44 * The rules.
45 */
46 private Collection rules = null;
47
48 /***
49 * @param rules A set of rules seperated by semi-colons
50 */
51 public final void setRules(final String rules) {
52 this.rules = Utils.splitToCollectionIgnoreSpace(rules, ';');
53 }
54
55 /***
56 * @see javax.servlet.jsp.tagext.Tag#doStartTag()
57 */
58 public int doStartTag() throws JspException {
59 BaseXTag parent = (BaseXTag) findAncestorWithClass(this, BaseXTag.class);
60
61 if (parent == null) {
62 throw new InvalidTagPlacementException(getClass().getName());
63 }
64
65 final DynaNode node = parent.getCurrent();
66 boolean evaluateRules = true;
67
68 /***
69 * case: "*"
70 */
71 if (rules.size() == 1) {
72 String rule = String.valueOf(rules.iterator().next());
73 if (rule.equals("*")) {
74 if (parent.isGoRuleElse()) {
75 evaluateRules = false;
76 } else {
77 return SKIP_BODY;
78 }
79 }
80 }
81
82 /***
83 * case: plain normal
84 */
85 if (evaluateRules) {
86
87 boolean verifies = false;
88 try {
89 verifies = node.verifiesRules(rules);
90 } catch (InvalidRuleFormatException e) {
91 throw new JspException("XRuleTag: " + e.getMessage());
92 }
93
94 if (verifies) {
95 parent.setGoRuleElse(false);
96 } else {
97 return SKIP_BODY;
98 }
99 }
100
101 return EVAL_BODY_BUFFERED;
102 }
103
104 /***
105 * @see javax.servlet.jsp.tagext.IterationTag#doAfterBody()
106 */
107 public int doAfterBody() throws JspTagException {
108 BodyContent body = getBodyContent();
109 try {
110 body.writeOut(getPreviousOut());
111 } catch (IOException e) {
112 throw new JspTagException("XRuleTag: " + e.getMessage());
113 }
114
115
116 body.clearBody();
117
118 return SKIP_BODY;
119 }
120
121 /***
122 * @see javax.servlet.jsp.tagext.Tag#doEndTag()
123 */
124 public int doEndTag() {
125 return EVAL_PAGE;
126 }
127 }