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.Document; 30 import org.dom4j.Element; 31 import org.dom4j.Node; 32 import org.dom4j.XPath; 33 import org.dom4j.io.XMLWriter; 34 35 39 public class CopyTag extends AbstractTag { 40 41 42 private Node node; 43 44 45 private XMLWriter writer; 46 47 48 private XPath xpath; 49 50 51 public CopyTag() { 52 } 53 54 public int doStartTag() throws JspException { 57 node = getNode(); 58 if ( node != null ) { 59 try { 60 writer = TagHelper.getXMLWriter( pageContext, this ); 61 if ( node instanceof Element ) { 62 writer.writeOpen( (Element) node ); 63 } 64 else { 65 writer.write( node ); 66 } 67 } 68 catch (IOException e) { 69 handleException(e); 70 } 71 } 72 return EVAL_BODY_INCLUDE; 73 } 74 75 public int doEndTag() throws JspException { 76 if ( node instanceof Element && writer != null ) { 77 try { 78 writer.writeClose( (Element) node ); 79 } 80 catch (IOException e) { 81 handleException(e); 82 } 83 finally { 84 node = null; 85 writer = null; 86 } 87 } 88 return EVAL_PAGE; 89 } 90 91 public void release() { 92 super.release(); 93 node = null; 94 writer = null; 95 xpath = null; 96 } 97 98 101 104 public void setSelect(String select) { 105 if ( select != null && ! select.equals( "." ) ) { 106 xpath = createXPath( select ); 107 } 108 else { 109 xpath = null; 110 } 111 } 112 113 115 protected Node getNode() { 116 Object input = getInputNodes(); 117 if ( xpath != null ) { 118 input = xpath.selectNodes( input ); 119 } 120 return getNode( input ); 121 } 122 123 protected Node getNode(Object input) { 124 if ( input instanceof Element ) { 125 return (Element) input; 126 } 127 else if ( input instanceof Node ) { 128 if ( !( input instanceof Document ) ) { 130 return (Node) input; 131 } 132 } 133 else if ( input instanceof List ) { 134 List list = (List ) input; 135 if ( list.size() > 0 ) { 136 return getNode( list.get(0) ); 137 } 138 } 139 return null; 140 } 141 142 } 143 | Popular Tags |