View Javadoc

1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   
5   package net.sourceforge.pmd.renderers;
6   
7   import java.io.ByteArrayInputStream;
8   import java.io.File;
9   import java.io.FileInputStream;
10  import java.io.FileNotFoundException;
11  import java.io.IOException;
12  import java.io.InputStream;
13  import java.io.StringWriter;
14  import java.io.Writer;
15  
16  import javax.xml.parsers.DocumentBuilder;
17  import javax.xml.parsers.DocumentBuilderFactory;
18  import javax.xml.parsers.ParserConfigurationException;
19  import javax.xml.transform.Transformer;
20  import javax.xml.transform.TransformerConfigurationException;
21  import javax.xml.transform.TransformerException;
22  import javax.xml.transform.TransformerFactory;
23  import javax.xml.transform.dom.DOMSource;
24  import javax.xml.transform.stream.StreamResult;
25  import javax.xml.transform.stream.StreamSource;
26  
27  import org.w3c.dom.Document;
28  import org.xml.sax.SAXException;
29  
30  /**
31   * @author Romain Pelisse, belaran@gmail.com
32   *
33   */
34  public class XSLTRenderer extends XMLRenderer {
35  
36  	private Transformer transformer;
37  	private String xsltFilename = "/etc/pmd-nicerhtml.xsl";
38  	private Writer outputWriter;
39  
40  	public XSLTRenderer() {
41  
42  	}
43  	/**
44  	 * This constuctor provides a way to override default stylesheet
45  	 * @param xsltFilename
46  	 */
47  	public XSLTRenderer(String xsltFilename) {
48  		if ( xsltFilename != null ) {
49  			File file = new File(xsltFilename);
50  			if ( file.exists() && file.canRead() ) {
51  				this.xsltFilename = xsltFilename;
52  			}
53  		}
54  	}
55  
56  	@Override
57  	public void start() throws IOException {
58  		// We keep the inital writer to put the final html output
59  		this.outputWriter = getWriter();
60  		// We use a new one to store the XML...
61  		Writer w = new StringWriter();
62  		setWriter(w);
63  		// If don't find the xsl no need to bother doing the all report,
64  		// so we check this here...
65  		InputStream xslt = null;
66  		File file = new File(this.xsltFilename);
67  		if ( file.exists() && file.canRead() ) {
68  			xslt = new FileInputStream(file);
69  		}
70  		else {
71  			xslt = this.getClass().getResourceAsStream(xsltFilename);
72  		}
73  		if ( xslt == null ) {
74  			throw new FileNotFoundException("Can't file XSLT sheet :" + xsltFilename);
75  		}
76  		this.prepareTransformer(xslt);
77  		// Now we build the XML file
78  		super.start();
79  	}
80  
81  	/**
82  	 * Prepare the transformer, doing the proper "building"...
83  	 *
84  	 * @param xslt, the xslt provided as an InputStream
85  	 */
86  	private void prepareTransformer(InputStream xslt) {
87  		if ( xslt != null ) {
88  			try {
89  		    //Get a TransformerFactory object
90  		    TransformerFactory factory = TransformerFactory.newInstance();
91  		    StreamSource src = new StreamSource(xslt);
92  		    //Get an XSL Transformer object
93  		    this.transformer = factory.newTransformer(src);
94  			} catch (TransformerConfigurationException e) {
95  				e.printStackTrace();
96  			}
97  		}
98  	}
99  
100 	@Override
101 	public void end() throws IOException {
102 		// First we finish the XML report
103 		super.end();
104 		// Now we transform it using XSLT
105 		Writer writer = super.getWriter();
106 		if ( writer instanceof StringWriter ) {
107 			StringWriter w = (StringWriter)writer;
108 			StringBuffer buffer = w.getBuffer();
109 			// FIXME: If we change the encoding in XMLRenderer, we should change this too !
110 			InputStream xml =  new ByteArrayInputStream(buffer.toString().getBytes(this.encoding));
111 			Document doc = this.getDocument(xml);
112 			this.transform(doc);
113 		}
114 		else {
115 			// Should not happen !
116 			new RuntimeException("Wrong writer").printStackTrace();
117 		}
118 
119 	}
120 
121 	private void transform(Document doc) {
122 		DOMSource source = new DOMSource(doc);
123 		this.setWriter(new StringWriter());
124 		StreamResult result = new StreamResult(this.outputWriter);
125 		try {
126 			transformer.transform(source, result);
127 		} catch (TransformerException e) {
128 			e.printStackTrace();
129 		}
130 	}
131 
132 	private Document getDocument(InputStream xml) {
133 		try {
134 			DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
135 			return parser.parse(xml);
136 		} catch (ParserConfigurationException e) {
137 			e.printStackTrace();
138 		} catch (SAXException e) {
139 			e.printStackTrace();
140 		} catch (IOException e) {
141 			e.printStackTrace();
142 		}
143 		return null;
144 	}
145 }