1 16 17 package org.apache.taglibs.regexp; 18 19 import java.util.ArrayList ; 20 import java.util.Iterator ; 21 import org.apache.oro.text.*; 22 import org.apache.oro.text.perl.*; 23 import org.apache.oro.text.regex.*; 24 import javax.servlet.*; 25 import javax.servlet.http.*; 26 import javax.servlet.jsp.*; 27 import javax.servlet.jsp.tagext.*; 28 29 84 85 public class SplitTag extends BodyTagSupport 86 { 87 private String regexpid = null; 88 private String textid = null; 89 private int limit = -1; 90 private Iterator values = null; 91 private String value = null; 92 93 98 public final int doStartTag() throws JspException 99 { 100 TextData td = (TextData)pageContext.getAttribute(textid,PageContext.PAGE_SCOPE); 101 if( td == null ) 102 throw new JspException( 103 "regexp tag split could not find text tag with id: " + 104 textid); 105 106 Perl5Util perl = new Perl5Util(RegexpData.getPatternCache()); 107 String spliton = "/\\s+/m"; 108 if( regexpid != null ) { 109 RegexpData rd = (RegexpData)pageContext.getAttribute(regexpid,PageContext.PAGE_SCOPE); 110 if( rd == null ) 111 throw new JspException( 112 "regexp tag split could not find regexp tag with id: " + 113 regexpid); 114 spliton = rd.getRegexp(); 115 } 116 ArrayList split = new ArrayList (); 117 if( limit > -1 ) { 118 perl.split(split,spliton,td.getText(),limit); 119 } else { 120 perl.split(split,spliton,td.getText()); 121 } 122 values = split.iterator(); 123 if( !values.hasNext() ) { 124 return SKIP_BODY; 125 } 126 127 value = (String )values.next(); 128 pageContext.setAttribute(id,this,PageContext.PAGE_SCOPE); 129 130 return EVAL_BODY_TAG; 131 } 132 133 138 public final int doAfterBody() throws JspException 139 { 140 if( !values.hasNext() ) { 141 return SKIP_BODY; 142 } 143 value = (String )values.next(); 144 return EVAL_BODY_TAG; 145 } 146 147 152 public final int doEndTag() throws JspException 153 { 154 if( id != null && id.length() > 0 ) 155 pageContext.removeAttribute(id,PageContext.PAGE_SCOPE); 156 try { 157 if(bodyContent != null) 158 bodyContent.writeOut(bodyContent.getEnclosingWriter()); 159 } catch(java.io.IOException e) { 160 throw new JspException("IO Error: " + e.getMessage()); 161 } 162 return EVAL_PAGE; 163 } 164 165 170 public final void setRegexp(String str) 171 { 172 regexpid = str; 173 } 174 175 180 public final void setText(String str) 181 { 182 textid = str; 183 } 184 185 191 public final void setLimit(int limit) 192 { 193 this.limit = limit; 194 } 195 196 202 public final String getSplit() 203 { 204 return value; 205 } 206 207 } 208 | Popular Tags |