Source Code Cross Referenced for XSLTServletWithParams.java in  » J2EE » enhydra » servlet » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » enhydra » servlet 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 1999-2004 The Apache Software Foundation.
003:         *
004:         * Licensed under the Apache License, Version 2.0 (the "License");
005:         * you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at
007:         *
008:         *     http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         * Unless required by applicable law or agreed to in writing, software
011:         * distributed under the License is distributed on an "AS IS" BASIS,
012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         * See the License for the specific language governing permissions and
014:         * limitations under the License.
015:         */
016:        /*
017:         * $Id: XSLTServletWithParams.java,v 1.1 2006-06-27 14:42:52 sinisa Exp $
018:         */
019:        package servlet;
020:
021:        import javax.servlet.*;
022:        import javax.servlet.http.*;
023:        import java.io.*;
024:        import java.util.Enumeration;
025:        import java.net.URL;
026:
027:        import org.xml.sax.*;
028:        import javax.xml.transform.TransformerFactory;
029:        import javax.xml.transform.Transformer;
030:        import javax.xml.transform.Source;
031:        import javax.xml.transform.stream.StreamSource;
032:        import javax.xml.transform.stream.StreamResult;
033:
034:        /*
035:         * This sample takes input parameters in the request URL: a URL
036:         * parameter for the XML input, an xslURL parameter for the stylesheet,
037:         * and optional stylesheet parameters.
038:         * To run the equivalent of SimplestXSLServlet (with the documents in the
039:         * servlet document root directory), the request URL is
040:         * http://<server/servletpath>servlet.SimpleXSLServlet?URL=file:todo.xml&xslURL=file:todo.xsl
041:         *
042:         * Using a stylesheet Processing Instruction:
043:         * If the XML document includes a stylesheet PI that you want to use, 
044:         * omit the xslURL parameter.
045:         *
046:         * Sending stylesheet parameters: 
047:         * If, for example, a servlet takes a stylesheet parameter named param1
048:         * param1 that you want to set to foo, include param1=foo in the URL.
049:         */
050:
051:        public class XSLTServletWithParams extends HttpServlet {
052:
053:            /**
054:             * String representing the file separator characters for the System.
055:             */
056:            public final static String FS = System
057:                    .getProperty("file.separator");
058:
059:            public void init(ServletConfig config) throws ServletException {
060:                super .init(config);
061:            }
062:
063:            public void doGet(HttpServletRequest request,
064:                    HttpServletResponse response) throws ServletException,
065:                    IOException {
066:                // The servlet returns HTML; charset is UTF8.
067:                // See ApplyXSLT.getContentType() to get output properties from <xsl:output>.
068:                response.setContentType("text/html; charset=UTF-8");
069:                PrintWriter out = response.getWriter();
070:                try {
071:                    TransformerFactory tFactory = TransformerFactory
072:                            .newInstance();
073:                    // Get params from URL.
074:                    String xml = getRequestParam(request, "URL");
075:                    String xsl = getRequestParam(request, "xslURL");
076:                    Source xmlSource = null;
077:                    Source xslSource = null;
078:                    Transformer transformer = null;
079:                    //get the real path for xml and xsl files.
080:                    String ctx = getServletContext().getRealPath("") + FS;
081:
082:                    // Get the XML input document.
083:                    if (xml != null && xml.length() > 0)
084:                        xmlSource = new StreamSource(new URL("file", "", ctx
085:                                + xml).openStream());
086:                    // Get the stylesheet.
087:                    if (xsl != null && xsl.length() > 0)
088:                        xslSource = new StreamSource(new URL("file", "", ctx
089:                                + xsl).openStream());
090:                    if (xmlSource != null) // We have an XML input document.
091:                    {
092:                        if (xslSource == null) // If no stylesheet, look for PI in XML input document.
093:                        {
094:                            String media = null, title = null, charset = null;
095:                            xslSource = tFactory.getAssociatedStylesheet(
096:                                    xmlSource, media, title, charset);
097:                        }
098:                        if (xslSource != null) // Now do we have a stylesheet?
099:                        {
100:                            transformer = tFactory.newTransformer(xslSource);
101:                            setParameters(transformer, request); // Set stylesheet params.
102:                            // Perform the transformation.
103:                            transformer.transform(xmlSource, new StreamResult(
104:                                    out));
105:                        } else
106:                            out.write("No Stylesheet!");
107:                    } else
108:                        out.write("No XML Input Document!");
109:                } catch (Exception e) {
110:                    e.printStackTrace(out);
111:                }
112:                out.close();
113:            }
114:
115:            // Get parameters from the request URL.
116:            String getRequestParam(HttpServletRequest request, String param) {
117:                if (request != null) {
118:                    String paramVal = request.getParameter(param);
119:                    return paramVal;
120:                }
121:                return null;
122:            }
123:
124:            // Set stylesheet parameters from the request URL.
125:            void setParameters(Transformer transformer,
126:                    HttpServletRequest request) {
127:                Enumeration paramNames = request.getParameterNames();
128:                while (paramNames.hasMoreElements()) {
129:                    String paramName = (String) paramNames.nextElement();
130:                    try {
131:                        String paramVal = request.getParameter(paramName);
132:                        if (paramVal != null)
133:                            transformer.setParameter(paramName, paramVal);
134:                    } catch (Exception e) {
135:                    }
136:                }
137:            }
138:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.