| 1 | /* |
| 2 | * JTiger Unit Testing Framework for J2SE 1.5 |
| 3 | * Copyright (C) 2005 Tony Morris |
| 4 | * |
| 5 | * This software is licenced under the |
| 6 | * Common Public Licence version 1.0 |
| 7 | * http://www.opensource.org/licenses/cpl1.0.php |
| 8 | * |
| 9 | * You received a copy of this licence with this software. |
| 10 | */ |
| 11 | package org.jtiger.report.html; |
| 12 | |
| 13 | import java.beans.XMLEncoder; |
| 14 | import java.io.ByteArrayInputStream; |
| 15 | import java.io.ByteArrayOutputStream; |
| 16 | import java.io.File; |
| 17 | import java.io.FileNotFoundException; |
| 18 | import java.io.FileOutputStream; |
| 19 | import java.io.IOException; |
| 20 | import java.io.InputStream; |
| 21 | import java.io.OutputStream; |
| 22 | import javax.xml.transform.Transformer; |
| 23 | import javax.xml.transform.TransformerConfigurationException; |
| 24 | import javax.xml.transform.TransformerException; |
| 25 | import javax.xml.transform.TransformerFactory; |
| 26 | import javax.xml.transform.stream.StreamResult; |
| 27 | import javax.xml.transform.stream.StreamSource; |
| 28 | import org.jtiger.framework.FixtureResultsHandlerException; |
| 29 | import org.jtiger.framework.ReadOnlyArray; |
| 30 | import static org.jtiger.report.html.FixturesFilenamePolicyFactory.newFixturesFilenamePolicy; |
| 31 | |
| 32 | final class FixturesHtmlFileWriterFactory |
| 33 | { |
| 34 | private static final String XSL_RESOURCE = "/org/jtiger/report/xsl/fixtures.xsl"; |
| 35 | private static final String FILENAME = "fixtures.html"; |
| 36 | |
| 37 | private FixturesHtmlFileWriterFactory() |
| 38 | { |
| 39 | |
| 40 | } |
| 41 | |
| 42 | static FixturesHtmlFileWriter newFixturesHtmlFileWriter() |
| 43 | { |
| 44 | return new FixturesHtmlFileWriterImpl(); |
| 45 | } |
| 46 | |
| 47 | private static final class FixturesHtmlFileWriterImpl implements FixturesHtmlFileWriter |
| 48 | { |
| 49 | FixturesHtmlFileWriterImpl() |
| 50 | { |
| 51 | |
| 52 | } |
| 53 | |
| 54 | public void writeFixturesHtml(final ReadOnlyArray<FixtureResults> results, final File destination) throws FixtureResultsHandlerException |
| 55 | { |
| 56 | final ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 57 | final XMLEncoder encoder = new XMLEncoder(out); |
| 58 | |
| 59 | final FilenamePolicy policy = newFixturesFilenamePolicy(); |
| 60 | |
| 61 | for(FixtureResults result : results) |
| 62 | { |
| 63 | final Object bean = new FixtureResultBeanImpl(result.getName(), policy.nextFilename()); |
| 64 | encoder.writeObject(bean); |
| 65 | } |
| 66 | |
| 67 | encoder.close(); |
| 68 | |
| 69 | InputStream in = null; |
| 70 | |
| 71 | try |
| 72 | { |
| 73 | in = getClass().getResourceAsStream(XSL_RESOURCE); |
| 74 | final Transformer t = TransformerFactory.newInstance().newTransformer(new StreamSource(in)); |
| 75 | OutputStream o = null; |
| 76 | |
| 77 | try |
| 78 | { |
| 79 | final File f = new File(destination, FILENAME); |
| 80 | o = new FileOutputStream(f); |
| 81 | final StreamResult r = new StreamResult(o); |
| 82 | t.transform(new StreamSource(new ByteArrayInputStream(out.toByteArray())), r); |
| 83 | } |
| 84 | catch(FileNotFoundException e) |
| 85 | { |
| 86 | throw new FixtureResultsHandlerException(e.getMessage(), e); |
| 87 | } |
| 88 | finally |
| 89 | { |
| 90 | if(o != null) |
| 91 | { |
| 92 | try |
| 93 | { |
| 94 | o.close(); |
| 95 | } |
| 96 | catch(IOException e) |
| 97 | { |
| 98 | throw new FixtureResultsHandlerException(e.getMessage(), e); |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | catch(TransformerConfigurationException e) |
| 104 | { |
| 105 | throw new FixtureResultsHandlerException(e.getMessage(), e); |
| 106 | } |
| 107 | catch(TransformerException e) |
| 108 | { |
| 109 | throw new FixtureResultsHandlerException(e.getMessage(), e); |
| 110 | } |
| 111 | finally |
| 112 | { |
| 113 | if(in != null) |
| 114 | { |
| 115 | try |
| 116 | { |
| 117 | in.close(); |
| 118 | } |
| 119 | catch(IOException e) |
| 120 | { |
| 121 | throw new FixtureResultsHandlerException(e.getMessage(), e); |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | } |