1 16 17 package org.apache.taglibs.regexp; 18 19 import java.util.*; 20 import org.apache.oro.text.*; 21 import org.apache.oro.text.perl.*; 22 import org.apache.oro.text.regex.*; 23 import javax.servlet.*; 24 import javax.servlet.http.*; 25 import javax.servlet.jsp.*; 26 import javax.servlet.jsp.tagext.*; 27 28 71 72 public class MatchTag extends BodyTagSupport 73 { 74 private String regexpid = null; 75 private String textid = null; 76 private Perl5Util perl = new Perl5Util(RegexpData.getPatternCache()); 77 private PatternMatcherInput pmi = null; 78 private RegexpData rd = null; 79 80 85 public final int doStartTag() throws JspException 86 { 87 TextData td = (TextData)pageContext.getAttribute(textid,PageContext.PAGE_SCOPE); 88 if( td == null ) 89 throw new JspException( 90 "regexp tag match could not find text tag with id: " + 91 textid); 92 pmi = new PatternMatcherInput(td.getText()); 93 94 rd = (RegexpData)pageContext.getAttribute(regexpid,PageContext.PAGE_SCOPE); 95 if( rd == null ) 96 throw new JspException( 97 "regexp tag match could not find regexp tag with id: " + 98 regexpid); 99 if( !perl.match(rd.getRegexp(),pmi) ) 100 return SKIP_BODY; 101 102 pageContext.setAttribute(id,this,PageContext.PAGE_SCOPE); 103 104 return EVAL_BODY_TAG; 105 } 106 107 112 public final int doAfterBody() throws JspException 113 { 114 if( !perl.match(rd.getRegexp(),pmi) ) 115 return SKIP_BODY; 116 return EVAL_BODY_TAG; 117 } 118 119 124 public final int doEndTag() throws JspException 125 { 126 if( id != null && id.length() > 0 ) 127 pageContext.removeAttribute(id,PageContext.PAGE_SCOPE); 128 try { 129 if(bodyContent != null) 130 bodyContent.writeOut(bodyContent.getEnclosingWriter()); 131 } catch(java.io.IOException e) { 132 throw new JspException("IO Error: " + e.getMessage()); 133 } 134 return EVAL_PAGE; 135 } 136 137 142 public final void setRegexp(String str) 143 { 144 regexpid = str; 145 } 146 147 152 public final void setText(String str) 153 { 154 textid = str; 155 } 156 157 163 public final String getMatch() 164 { 165 return perl.toString(); 166 } 167 168 174 public final String getPreMatch() 175 { 176 return perl.preMatch(); 177 } 178 179 185 public final String getPostMatch() 186 { 187 return perl.postMatch(); 188 } 189 190 195 public final String getGroup(int i) 196 { 197 String group = perl.group(i); 198 if( group == null ) 199 return ""; 200 return group; 201 } 202 203 } 204 | Popular Tags |