1 16 17 package org.apache.taglibs.xtags.xpath; 18 19 import java.io.IOException ; 20 import java.util.Collections ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 24 import javax.servlet.ServletContext ; 25 import javax.servlet.jsp.JspException ; 26 import javax.servlet.jsp.PageContext ; 27 import javax.servlet.jsp.tagext.Tag ; 28 import javax.servlet.jsp.tagext.TagSupport ; 29 30 import org.dom4j.Document; 31 import org.dom4j.Node; 32 import org.dom4j.NodeFilter; 33 import org.dom4j.DocumentFactory; 34 import org.dom4j.DocumentHelper; 35 import org.dom4j.XPath; 36 import org.dom4j.rule.Stylesheet; 37 38 import org.jaxen.VariableContext; 39 40 import org.apache.taglibs.xtags.util.JspNestedException; 41 import org.apache.taglibs.xtags.util.JspVariableContext; 42 43 44 45 49 public abstract class AbstractTag extends TagSupport { 50 51 protected static final Document EMPTY_DOCUMENT = DocumentHelper.createDocument(); 52 53 protected static final boolean ALLOW_FLUSH = false; 54 55 protected Object context; 56 57 58 public AbstractTag() { 59 } 60 61 public void release() { 62 context = null; 63 } 64 65 public void flush() throws JspException { 66 if ( ALLOW_FLUSH ) { 67 try { 68 pageContext.getOut().flush(); 69 } 70 catch (IOException e) { 71 handleException(e); 72 } 73 } 74 } 75 76 public void setContext(Object context) { 79 this.context = context; 80 } 81 82 83 88 public boolean matches(NodeFilter filter) { 89 Object input = getInputNodes( false ); 90 if ( input == null ) { 91 input = EMPTY_DOCUMENT; 95 } 96 if ( input instanceof List ) { 97 List list = (List ) input; 98 for ( Iterator iter = list.iterator(); iter.hasNext(); ) { 99 Object object = iter.next(); 100 if ( object instanceof Node ) { 101 Node node = (Node) object; 102 if ( filter.matches( node ) ) { 103 return true; 104 } 105 } 106 } 107 } 108 else if ( input instanceof Node ) { 109 Node node = (Node) input; 110 if ( filter.matches( node ) ) { 111 return true; 112 } 113 } 114 return false; 115 } 116 117 119 public Object getInputNodes() { 120 return getInputNodes( true ); 121 } 122 123 public Object getInputNodes( boolean warn ) { 124 if ( context == null ) { 125 return TagHelper.getInputNodes( pageContext, this, warn ); 126 } 127 return context; 128 } 129 130 public void setInputNodes( Object inputNodes ) { 131 TagHelper.setInputNodes( pageContext, inputNodes ); 132 } 133 134 public Stylesheet getStylesheet() { 135 StylesheetTag tag = (StylesheetTag) findAncestorWithClass( 136 this, StylesheetTag.class 137 ); 138 if ( tag != null ) { 139 return tag.getStylesheet(); 140 } 141 else { 142 return TagHelper.getStylesheet( pageContext ); 143 } 144 } 145 146 147 150 151 protected XPath createXPath(String xpathExpression) { 152 VariableContext variableContext = JspVariableContext.getInstance( pageContext ); 153 return getDocumentFactory().createXPath( xpathExpression, variableContext ); 154 } 155 156 157 protected NodeFilter createXPathFilter(String xpathExpression) { 158 VariableContext variableContext = JspVariableContext.getInstance( pageContext ); 159 return getDocumentFactory().createXPathFilter( xpathExpression, variableContext ); 160 } 161 162 163 protected DocumentFactory getDocumentFactory() { 164 return DocumentFactory.getInstance(); 165 } 166 167 169 protected void handleException( Exception e ) throws JspException { 170 if ( e instanceof JspException ) { 171 throw (JspException ) e; 172 } 173 else { 174 pageContext.getServletContext().log( e.getMessage(), e ); 175 throw new JspNestedException( e ); 176 } 177 } 178 } 179 | Popular Tags |