1 16 17 package org.apache.taglibs.xtags.xpath; 18 19 import java.io.IOException ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 23 import javax.servlet.ServletContext ; 24 import javax.servlet.jsp.JspException ; 25 import javax.servlet.jsp.JspWriter ; 26 import javax.servlet.jsp.PageContext ; 27 import javax.servlet.jsp.tagext.TagSupport ; 28 29 import org.dom4j.DocumentException; 30 import org.dom4j.Node; 31 import org.dom4j.XPath; 32 33 37 public class ValueOfTag extends AbstractTag { 38 39 40 private XPath xpath; 41 42 43 public ValueOfTag() { 44 } 45 46 public int doStartTag() throws JspException { 49 if ( xpath != null ) { 50 try { 51 String text = xpath.valueOf( getInputNodes() ); 52 text = encode( text ); 53 pageContext.getOut().print( text ); 54 } 55 catch (IOException e) { 56 handleException(e); 57 } 58 } 59 return SKIP_BODY; 60 } 61 62 public void release() { 63 super.release(); 64 xpath = null; 65 } 66 67 70 72 public void setSelect(String select) { 73 this.xpath = createXPath( select ); 74 } 75 76 78 public void setSelectXPath(XPath xpath) { 79 this.xpath = xpath; 80 } 81 82 83 86 88 public static String encode(String text) { 89 StringBuffer buffer = new StringBuffer (); 90 char[] block = text.toCharArray(); 91 int size = block.length; 92 int last = 0; 93 for ( int i = 0; i < size; i++) { 94 char ch = block[i]; 95 96 String entity = null; 97 switch ( ch ) { 98 case '<' : 99 entity = "<"; 100 break; 101 case '>' : 102 entity = ">"; 103 break; 104 case '&' : 105 entity = "&"; 106 break; 107 default : 108 ; 109 } 110 if (entity != null) { 111 buffer.append(block, last, i - last); 112 buffer.append(entity); 113 last = i + 1; 114 } 115 } 116 if ( last < size ) { 117 buffer.append(block, last, size - last); 118 } 119 return buffer.toString(); 120 } 121 } 122 | Popular Tags |