1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.jelly.tags.regexp;
17
18 import org.apache.commons.jelly.TagSupport;
19 import org.apache.commons.jelly.XMLOutput;
20 import org.apache.commons.jelly.MissingAttributeException;
21 import org.apache.commons.jelly.JellyTagException;
22 import org.apache.oro.text.regex.Perl5Matcher;
23 import org.apache.oro.text.regex.Perl5Compiler;
24 import org.apache.oro.text.regex.Pattern;
25 import org.apache.oro.text.regex.MalformedPatternException;
26
27 /***
28 * Base class for tags using the Oro Regexp library.
29 *
30 * @author <a href="mailto:christian@inx-soft.com">Christian Amor Kvalheim</a>
31 * @version $Revision: 155420 $
32 */
33 public abstract class RegexpTag extends TagSupport {
34 private Perl5Matcher patternMatcher = new Perl5Matcher();
35 private Pattern pattern;
36 private String var;
37 private String text;
38 private String scope;
39
40 protected final String getText() {
41 return text;
42 }
43
44 protected final Pattern getPattern() {
45 return pattern;
46 }
47
48 protected final Perl5Matcher getPatternMatcher() {
49 return patternMatcher;
50 }
51
52 public final void setExpr(String expr) throws MalformedPatternException {
53 Perl5Compiler patternCompiler = new Perl5Compiler();
54 pattern = patternCompiler.compile(expr);
55 }
56
57 public final void setText(String text) {
58 this.text = text;
59 }
60
61
62 public final void setVar(String var) {
63 this.var = var;
64 }
65
66 /***
67 * Sets the variable scope for this variable. For example setting this value to 'parent' will
68 * set this value in the parent scope. When Jelly is run from inside a Servlet environment
69 * then other scopes will be available such as 'request', 'session' or 'application'.
70 *
71 * Other applications may implement their own custom scopes.
72 */
73 public final void setScope(String scope) {
74 this.scope = scope;
75 }
76
77 public void doTag(XMLOutput output) throws MissingAttributeException, JellyTagException {
78
79 if (getText() == null || getText().length() == 0)
80 throw new MissingAttributeException("text must be provided");
81
82 if (pattern == null)
83 throw new MissingAttributeException("expr must be provided");
84
85 if (var == null || var.length() == 0)
86 throw new MissingAttributeException("var must be provided");
87
88
89 boolean result = getResult();
90 String resultString = result ? "true" : "false";
91
92 if (var != null) {
93 if (scope != null) {
94 context.setVariable(var, scope, resultString);
95 } else {
96 context.setVariable(var, resultString);
97 }
98 }
99 }
100
101 protected abstract boolean getResult();
102 }