1 16 17 package org.apache.taglibs.xtags.servlet; 18 19 import java.io.IOException ; 20 import java.net.MalformedURLException ; 21 import java.net.URL ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 25 import javax.servlet.ServletException ; 26 import javax.servlet.http.HttpServlet ; 27 import javax.servlet.http.HttpServletRequest ; 28 import javax.servlet.http.HttpServletResponse ; 29 30 import org.dom4j.Document; 31 import org.dom4j.DocumentException; 32 import org.dom4j.DocumentHelper; 33 import org.dom4j.Element; 34 import org.dom4j.Node; 35 import org.dom4j.XPath; 36 import org.dom4j.DocumentHelper; 37 import org.dom4j.io.OutputFormat; 38 import org.dom4j.io.SAXReader; 39 import org.dom4j.io.XMLWriter; 40 41 45 46 public class XPathServlet extends HttpServlet { 47 48 private SAXReader reader = new SAXReader(); 49 private OutputFormat outputFormat = new OutputFormat( " ", true ); 50 private XMLWriter writer; 51 52 protected static final String XML_MIME_TYPE = "application/xml"; 53 55 public XPathServlet() { 57 } 58 59 public void init() throws ServletException { 60 } 61 62 public void destroy() { 63 log( "Destroyed" ); 64 } 65 66 public void doGet( HttpServletRequest request, HttpServletResponse response ) throws IOException , ServletException { 67 response.setContentType( XML_MIME_TYPE ); 68 try { 69 if ( writer == null ) { 70 writer = new XMLWriter( outputFormat ); 71 } 72 writer.setOutputStream( response.getOutputStream() ); 73 writer.write( createDocument(request) ); 74 writer.flush(); 75 } 76 catch (ServletException e) { 77 throw e; 78 } 79 catch (IOException e) { 80 throw e; 81 } 82 catch (Exception e) { 83 throw new ServletException (e); 84 } 85 } 86 87 88 protected Document createDocument( HttpServletRequest request ) throws ServletException { 91 Document document = DocumentHelper.createDocument(); 92 Element element = document.addElement( "results" ); 93 94 try { 95 URL url = getDocumentURL(request); 96 if ( url != null ) { 97 String path = request.getParameter("path"); 98 if ( path != null && path.length() > 0 ) { 99 Document source = reader.read( url ); 100 XPath xpath = source.createXPath( path ); 101 102 String contextPath = request.getParameter("contextPath"); 103 if ( contextPath == null ) { 104 contextPath = "."; 105 } 106 List context = source.selectNodes( contextPath ); 107 108 List results = null; 109 if ( ! getBoolean( request, "sort" ) ) { 110 results = xpath.selectNodes( context ); 111 } 112 else { 113 String sortPath = request.getParameter("sortPath"); 114 if ( sortPath == null ) { 115 sortPath = "."; 116 } 117 boolean distinct = getBoolean( request, "distinct" ); 118 XPath sortXPath = source.createXPath( sortPath ); 119 results = xpath.selectNodes( context, sortXPath, distinct ); 120 } 121 appendResults( element, results ); 122 } 123 } 124 } 125 catch (DocumentException e) { 126 e.printStackTrace(); 127 throw new ServletException ( "Error parsing document: " + e, e ); 128 } 129 return document; 130 } 131 132 protected void appendResults( Element element, List results ) { 133 for ( int i = 0, size = results.size(); i < size; i++ ) { 134 Object result = results.get(i); 135 if ( result instanceof String ) { 136 element.addText( (String ) result ); 137 } 138 else if ( result instanceof Node ) { 139 Node node = (Node) result; 140 node.detach(); 141 element.add( node ); 142 } 143 else if ( result != null ) { 144 element.addText( result.toString() ); 145 } 146 } 147 } 148 149 protected boolean getBoolean( HttpServletRequest request, String parameterName ) { 150 String text = request.getParameter( parameterName ); 151 boolean answer = false; 152 if ( text != null && text.equalsIgnoreCase( "true" ) ) { 153 answer = true; 154 } 155 return answer; 156 } 157 158 protected URL getDocumentURL( HttpServletRequest request ) throws ServletException { 159 String uri = request.getParameter("uri"); 160 if ( uri != null && uri.length() > 0 ) { 161 try { 162 return new URL ( uri ); 163 } 164 catch (MalformedURLException e) { 165 try { 166 return getServletContext().getResource( uri ); 167 } 168 catch (MalformedURLException e2) { 169 throw new ServletException ( "Cannot resolve URI: " + uri, e ); 170 } 171 } 172 } 173 return null; 174 } 175 } 176 | Popular Tags |