Source Code Cross Referenced for XSLTRenderer.java in  » Code-Analyzer » pmd-4.2rc1 » net » sourceforge » pmd » renderers » 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 » Code Analyzer » pmd 4.2rc1 » net.sourceforge.pmd.renderers 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
003:         */package net.sourceforge.pmd.renderers;
004:
005:        import java.io.ByteArrayInputStream;
006:        import java.io.File;
007:        import java.io.FileInputStream;
008:        import java.io.FileNotFoundException;
009:        import java.io.IOException;
010:        import java.io.InputStream;
011:        import java.io.StringWriter;
012:        import java.io.Writer;
013:
014:        import javax.xml.parsers.DocumentBuilder;
015:        import javax.xml.parsers.DocumentBuilderFactory;
016:        import javax.xml.parsers.ParserConfigurationException;
017:        import javax.xml.transform.Transformer;
018:        import javax.xml.transform.TransformerConfigurationException;
019:        import javax.xml.transform.TransformerException;
020:        import javax.xml.transform.TransformerFactory;
021:        import javax.xml.transform.dom.DOMSource;
022:        import javax.xml.transform.stream.StreamResult;
023:        import javax.xml.transform.stream.StreamSource;
024:
025:        import org.w3c.dom.Document;
026:        import org.xml.sax.SAXException;
027:
028:        /**
029:         * @author Romain Pelisse, belaran@gmail.com
030:         *
031:         */
032:        public class XSLTRenderer extends XMLRenderer {
033:
034:            private Transformer transformer;
035:            private String xsltFilename = "/etc/pmd-nicerhtml.xsl";
036:            private Writer outputWriter;
037:
038:            public XSLTRenderer() {
039:
040:            }
041:
042:            /**
043:             * This constuctor provides a way to override default stylesheet
044:             * @param xsltFilename
045:             */
046:            public XSLTRenderer(String xsltFilename) {
047:                File file = new File(xsltFilename);
048:                if (xsltFilename != null && file.exists() && file.canRead()) {
049:                    this .xsltFilename = xsltFilename;
050:                }
051:            }
052:
053:            @Override
054:            public void start() throws IOException {
055:                // We keep the inital writer to put the final html output
056:                this .outputWriter = getWriter();
057:                // We use a new one to store the XML...
058:                Writer w = new StringWriter();
059:                setWriter(w);
060:                // If don't find the xsl no need to bother doing the all report,
061:                // so we check this here...
062:                InputStream xslt = null;
063:                File file = new File(this .xsltFilename);
064:                if (file.exists() && file.canRead()) {
065:                    xslt = new FileInputStream(file);
066:                } else {
067:                    xslt = this .getClass().getResourceAsStream(xsltFilename);
068:                }
069:                if (xslt == null) {
070:                    throw new FileNotFoundException("Can't file XSLT sheet :"
071:                            + xsltFilename);
072:                }
073:                this .prepareTransformer(xslt);
074:                // Now we build the XML file
075:                super .start();
076:            }
077:
078:            /**
079:             * Prepare the transformer, doing the proper "building"...
080:             *
081:             * @param xslt, the xslt provided as an InputStream
082:             */
083:            private void prepareTransformer(InputStream xslt) {
084:                if (xslt != null) {
085:                    try {
086:                        //Get a TransformerFactory object
087:                        TransformerFactory factory = TransformerFactory
088:                                .newInstance();
089:                        StreamSource src = new StreamSource(xslt);
090:                        //Get an XSL Transformer object
091:                        this .transformer = factory.newTransformer(src);
092:                    } catch (TransformerConfigurationException e) {
093:                        e.printStackTrace();
094:                    }
095:                }
096:            }
097:
098:            @Override
099:            public void end() throws IOException {
100:                // First we finish the XML report
101:                super .end();
102:                // Now we transform it using XSLT
103:                Writer writer = super .getWriter();
104:                if (writer instanceof  StringWriter) {
105:                    StringWriter w = (StringWriter) writer;
106:                    StringBuffer buffer = w.getBuffer();
107:                    // FIXME: If we change the encoding in XMLRenderer, we should change this too !
108:                    InputStream xml = new ByteArrayInputStream(buffer
109:                            .toString().getBytes(this .encoding));
110:                    Document doc = this .getDocument(xml);
111:                    this .transform(doc);
112:                } else {
113:                    // Should not happen !
114:                    new RuntimeException("Wrong writer").printStackTrace();
115:                }
116:
117:            }
118:
119:            private void transform(Document doc) {
120:                DOMSource source = new DOMSource(doc);
121:                this .setWriter(new StringWriter());
122:                StreamResult result = new StreamResult(this .outputWriter);
123:                try {
124:                    transformer.transform(source, result);
125:                } catch (TransformerException e) {
126:                    e.printStackTrace();
127:                }
128:            }
129:
130:            private Document getDocument(InputStream xml) {
131:                try {
132:                    DocumentBuilder parser = DocumentBuilderFactory
133:                            .newInstance().newDocumentBuilder();
134:                    return parser.parse(xml);
135:                } catch (ParserConfigurationException e) {
136:                    e.printStackTrace();
137:                } catch (SAXException e) {
138:                    e.printStackTrace();
139:                } catch (IOException e) {
140:                    e.printStackTrace();
141:                }
142:                return null;
143:            }
144:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.