KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > xtags > servlet > XPathServlet


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.taglibs.xtags.servlet;
18
19 import java.io.IOException JavaDoc;
20 import java.net.MalformedURLException JavaDoc;
21 import java.net.URL JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24
25 import javax.servlet.ServletException JavaDoc;
26 import javax.servlet.http.HttpServlet JavaDoc;
27 import javax.servlet.http.HttpServletRequest JavaDoc;
28 import javax.servlet.http.HttpServletResponse JavaDoc;
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 /** A Servlet to display the result of an XPath expression as XML
42   *
43   * @author James Strachan
44   */

45
46 public class XPathServlet extends HttpServlet JavaDoc {
47     
48     private SAXReader reader = new SAXReader();
49     private OutputFormat outputFormat = new OutputFormat( " ", true );
50     private XMLWriter writer;
51     
52     protected static final String JavaDoc XML_MIME_TYPE = "application/xml";
53     //private static final String XML_MIME_TYPE = "text/xml";
54

55     //-------------------------------------------------------------------------
56
public XPathServlet() {
57     }
58     
59     public void init() throws ServletException JavaDoc {
60     }
61         
62     public void destroy() {
63         log( "Destroyed" );
64     }
65
66     public void doGet( HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response ) throws IOException JavaDoc, ServletException JavaDoc {
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 JavaDoc e) {
77             throw e;
78         }
79         catch (IOException JavaDoc e) {
80             throw e;
81         }
82         catch (Exception JavaDoc e) {
83             throw new ServletException JavaDoc(e);
84         }
85     }
86
87         
88     // Implementation methods
89
//-------------------------------------------------------------------------
90
protected Document createDocument( HttpServletRequest JavaDoc request ) throws ServletException JavaDoc {
91         Document document = DocumentHelper.createDocument();
92         Element element = document.addElement( "results" );
93
94         try {
95             URL JavaDoc url = getDocumentURL(request);
96             if ( url != null ) {
97                 String JavaDoc 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 JavaDoc contextPath = request.getParameter("contextPath");
103                     if ( contextPath == null ) {
104                         contextPath = ".";
105                     }
106                     List JavaDoc context = source.selectNodes( contextPath );
107
108                     List JavaDoc results = null;
109                     if ( ! getBoolean( request, "sort" ) ) {
110                         results = xpath.selectNodes( context );
111                     }
112                     else {
113                         String JavaDoc 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 JavaDoc( "Error parsing document: " + e, e );
128         }
129         return document;
130     }
131         
132     protected void appendResults( Element element, List JavaDoc results ) {
133         for ( int i = 0, size = results.size(); i < size; i++ ) {
134             Object JavaDoc result = results.get(i);
135             if ( result instanceof String JavaDoc ) {
136                 element.addText( (String JavaDoc) 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 JavaDoc request, String JavaDoc parameterName ) {
150         String JavaDoc 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 JavaDoc getDocumentURL( HttpServletRequest JavaDoc request ) throws ServletException JavaDoc {
159         String JavaDoc uri = request.getParameter("uri");
160         if ( uri != null && uri.length() > 0 ) {
161             try {
162                 return new URL JavaDoc( uri );
163             }
164             catch (MalformedURLException JavaDoc e) {
165                 try {
166                     return getServletContext().getResource( uri );
167                 }
168                 catch (MalformedURLException JavaDoc e2) {
169                     throw new ServletException JavaDoc( "Cannot resolve URI: " + uri, e );
170                 }
171             }
172         }
173         return null;
174     }
175 }
176
Popular Tags