source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/taskdefs/optional/junit/AggregateTransformer.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

File size: 6.5 KB
Line 
1/*
2 * Copyright 2001-2002,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 */
17package org.apache.tools.ant.taskdefs.optional.junit;
18
19import java.io.File;
20import java.io.FileInputStream;
21import java.io.FileNotFoundException;
22import java.io.IOException;
23import java.io.InputStream;
24import java.net.URL;
25import javax.xml.parsers.DocumentBuilder;
26import javax.xml.parsers.DocumentBuilderFactory;
27import org.apache.tools.ant.BuildException;
28import org.apache.tools.ant.Project;
29import org.apache.tools.ant.Task;
30import org.apache.tools.ant.util.JAXPUtils;
31import org.apache.tools.ant.types.EnumeratedAttribute;
32import org.w3c.dom.Document;
33
34/**
35 * Transform a JUnit xml report.
36 * The default transformation generates an html report in either framed or non-framed
37 * style. The non-framed style is convenient to have a concise report via mail, the
38 * framed report is much more convenient if you want to browse into different
39 * packages or testcases since it is a Javadoc like report.
40 *
41 */
42public class AggregateTransformer {
43
44 public static final String FRAMES = "frames";
45
46 public static final String NOFRAMES = "noframes";
47
48 public static class Format extends EnumeratedAttribute {
49 public String[] getValues() {
50 return new String[]{FRAMES, NOFRAMES};
51 }
52 }
53
54 /** Task */
55 protected Task task;
56
57 /** the xml document to process */
58 protected Document document;
59
60 /** the style directory. XSLs should be read from here if necessary */
61 protected File styleDir;
62
63 /** the destination directory, this is the root from where html should be generated */
64 protected File toDir;
65
66 /** the format to use for the report. Must be <tt>FRAMES</tt> or <tt>NOFRAMES</tt> */
67 protected String format = FRAMES;
68
69 /** XML Parser factory */
70 private static DocumentBuilderFactory privateDBFactory;
71
72 /** XML Parser factory accessible to subclasses */
73 protected static DocumentBuilderFactory dbfactory;
74
75 static {
76 privateDBFactory = DocumentBuilderFactory.newInstance();
77 dbfactory = privateDBFactory;
78 }
79
80 public AggregateTransformer(Task task) {
81 this.task = task;
82 }
83
84 /**
85 * Get the Document Builder Factory
86 *
87 * @return the DocumentBuilderFactory instance in use
88 */
89 protected static DocumentBuilderFactory getDocumentBuilderFactory() {
90 return privateDBFactory;
91 }
92
93 public void setFormat(Format format) {
94 this.format = format.getValue();
95 }
96
97 public void setXmlDocument(Document doc) {
98 this.document = doc;
99 }
100
101 /**
102 * Set the xml file to be processed. This is a helper if you want
103 * to set the file directly. Much more for testing purposes.
104 * @param xmlfile xml file to be processed
105 */
106 protected void setXmlfile(File xmlfile) throws BuildException {
107 try {
108 DocumentBuilder builder = privateDBFactory.newDocumentBuilder();
109 InputStream in = new FileInputStream(xmlfile);
110 try {
111 Document doc = builder.parse(in);
112 setXmlDocument(doc);
113 } finally {
114 in.close();
115 }
116 } catch (Exception e) {
117 throw new BuildException("Error while parsing document: " + xmlfile, e);
118 }
119 }
120
121 /**
122 * set the style directory. It is optional and will override the
123 * default xsl used.
124 * @param styledir the directory containing the xsl files if the user
125 * would like to override with its own style.
126 */
127 public void setStyledir(File styledir) {
128 this.styleDir = styledir;
129 }
130
131 /** set the destination directory */
132 public void setTodir(File todir) {
133 this.toDir = todir;
134 }
135
136 /** set the extension of the output files */
137 public void setExtension(String ext) {
138 task.log("extension is not used anymore", Project.MSG_WARN);
139 }
140
141 public void transform() throws BuildException {
142 checkOptions();
143 final long t0 = System.currentTimeMillis();
144 XalanExecutor executor = XalanExecutor.newInstance(this);
145 try {
146 executor.execute();
147 } catch (Exception e) {
148 throw new BuildException("Errors while applying transformations: "
149 + e.getMessage(), e);
150 }
151 final long dt = System.currentTimeMillis() - t0;
152 task.log("Transform time: " + dt + "ms");
153 }
154
155 /** check for invalid options */
156 protected void checkOptions() throws BuildException {
157 // set the destination directory relative from the project if needed.
158 if (toDir == null) {
159 toDir = task.getProject().resolveFile(".");
160 } else if (!toDir.isAbsolute()) {
161 toDir = task.getProject().resolveFile(toDir.getPath());
162 }
163 }
164
165 /**
166 * Get the systemid of the appropriate stylesheet based on its
167 * name and styledir. If no styledir is defined it will load
168 * it as a java resource in the xsl child package, otherwise it
169 * will get it from the given directory.
170 * @throws IOException thrown if the requested stylesheet does
171 * not exist.
172 */
173 protected String getStylesheetSystemId() throws IOException {
174 String xslname = "junit-frames.xsl";
175 if (NOFRAMES.equals(format)) {
176 xslname = "junit-noframes.xsl";
177 }
178 if (styleDir == null) {
179 URL url = getClass().getResource("xsl/" + xslname);
180 if (url == null) {
181 throw new FileNotFoundException("Could not find jar resource " + xslname);
182 }
183 return url.toExternalForm();
184 }
185 File file = new File(styleDir, xslname);
186 if (!file.exists()) {
187 throw new FileNotFoundException("Could not find file '" + file + "'");
188 }
189 return JAXPUtils.getSystemId(file);
190 }
191
192}
Note: See TracBrowser for help on using the repository browser.