001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: /*
043: * TransformXMLTask.java
044: *
045: * Created on November 22, 2001, 11:41 AM
046: */
047:
048: package org.netbeans.xtest.pe;
049:
050: import org.apache.tools.ant.*;
051: import java.io.*;
052: import org.netbeans.xtest.pe.xmlbeans.*;
053: import org.netbeans.xtest.util.XMLFactoryUtil;
054: import java.util.*;
055: import org.w3c.dom.*;
056:
057: import org.netbeans.xtest.util.*;
058:
059: import javax.xml.transform.*;
060: import javax.xml.transform.stream.*;
061: import javax.xml.transform.dom.*;
062:
063: /**
064: *
065: * @author mb115822
066: * @version
067: */
068: public class TransformXMLTask extends Task {
069:
070: public final static String TESTREPORT_FAILURES_XSL = "failures.xsl";
071: public final static String TESTREPORT_XSL = "summary.xsl";
072: public final static String TESTBAG_XSL = "testbag.xsl";
073: public final static String TESTSUITES_XSL = "suite.xsl";
074: public final static String SYSTEMINFO_XSL = "systeminfo.xsl";
075: public final static String FRAMESET_XSL = "frameset.xsl";
076: public final static String MAIN_NAVIGATOR_XSL = "main-navigator.xsl";
077: public final static String PERFORMANCE_XSL = "performance.xsl";
078:
079: // debugging flag - should be set to false :-)
080: private static final boolean DEBUG = false;
081:
082: private static final void debugInfo(String message) {
083: if (DEBUG)
084: System.out.println("TransformXMLTask." + message);
085: }
086:
087: /** Creates new TransformXMLTask */
088: public TransformXMLTask() {
089: }
090:
091: private File inputDir;
092: private File outputDir;
093:
094: // is this short report ?
095: private static boolean truncated = false;
096:
097: public static void setTruncated(boolean b) {
098: truncated = b;
099: }
100:
101: public static boolean isTruncated() {
102: return truncated;
103: }
104:
105: // include exceptions
106: private static boolean includeExceptions = true;
107:
108: public static void setIncludeExceptions(boolean b) {
109: includeExceptions = b;
110: }
111:
112: public static boolean includeExceptions() {
113: return includeExceptions;
114: }
115:
116: // include ide log
117: private static boolean includeIDELog = true;
118:
119: public static void setIncludeIDELog(boolean b) {
120: includeIDELog = b;
121: }
122:
123: public static boolean includeIDELog() {
124: return includeIDELog;
125: }
126:
127: // if this report will have mapped hostname (real hostname will not be shown)
128: private static String mappedHostname = null;
129:
130: //private File suiteXSL;
131:
132: public void setInputDir(File inputDir) {
133: this .inputDir = inputDir;
134: }
135:
136: public void setOutputDir(File outputDir) {
137: this .outputDir = outputDir;
138: }
139:
140: public static void setMappedHostname(String hostname) {
141: mappedHostname = hostname;
142: }
143:
144: public static String getMappedHostname() {
145: return mappedHostname;
146: }
147:
148: private static String xtestHome = System.getProperty("xtest.home");
149:
150: /*
151: public void setSuiteXSL(File suiteXSL) {
152: this.suiteXSL = suiteXSL;
153: }
154: */
155:
156: //private static final XSL_PACKAGE = "org.netbeans.xtest.pe.xsls";
157: private static File getXSLFile(String name) throws IOException {
158: if (xtestHome == null) {
159: debugInfo("getXSLFile(): xtest.home not set !!!!, have to be set to xtest home");
160: throw new IOException(
161: "xtest.home not set !!!!, have to be set to xtest home");
162: }
163: File xtestHomeDir = new File(xtestHome);
164: File requestedXSL = new File(xtestHomeDir, "lib"
165: + File.separator + "xsl" + File.separator + name);
166: if (!requestedXSL.exists()) {
167: debugInfo("getXSLFile(): requested XSL file does not exist:"
168: + requestedXSL);
169: new IOException("requested XSL file does not exist:"
170: + requestedXSL);
171: }
172: return requestedXSL;
173: }
174:
175: public void execute() throws BuildException {
176: try {
177: log("Transforming report's XMLs to HTMLs");
178: if (xtestHome == null) {
179: xtestHome = getProject().getProperty("xtest.home");
180: }
181: transformResults(inputDir, outputDir);
182: } catch (Exception te) {
183: debugInfo("execute(): Exception !!! : " + te);
184: te.printStackTrace();
185: throw new BuildException(te);
186: }
187: }
188:
189: public static Transformer getTransformer(File xsl)
190: throws TransformerConfigurationException {
191:
192: // when pes.config system property is specified, we're running PES and it does not run inside Ant
193: boolean runningPES = (System.getProperty("pes.config") == null) ? false
194: : true;
195:
196: if (runningPES) {
197: // PES does not use Ant class loader - use classical way to create transformer
198: TransformerFactory tFactory = javax.xml.transform.TransformerFactory
199: .newInstance();
200: StreamSource xslSource = new StreamSource(xsl);
201: return tFactory.newTransformer(xslSource);
202: } else {
203: StreamSource xslSource = new StreamSource(xsl);
204: Transformer transformer = XMLFactoryUtil
205: .newTransformer(xslSource);
206: return transformer;
207: }
208:
209: }
210:
211: public static Document transform(Document xml,
212: Transformer transformer) throws TransformerException {
213: DOMSource domSource = new DOMSource(xml);
214: DOMResult domResult = new DOMResult();
215: transform(transformer, domSource, domResult);
216: Node aNode = domResult.getNode();
217: Document resultDoc = SerializeDOM.getDocumentBuilder()
218: .newDocument();
219: resultDoc.appendChild(aNode);
220: return resultDoc;
221: }
222:
223: public static void transform(Document xml, File outputXML,
224: Transformer transformer) throws TransformerException,
225: IOException {
226: DOMSource domSource = new DOMSource(xml);
227: FileOutputStream outputFileStream = new FileOutputStream(
228: outputXML);
229: StreamResult streamResult = new StreamResult(outputFileStream);
230: transform(transformer, domSource, streamResult);
231: outputFileStream.close();
232: }
233:
234: public static void transform(Transformer transformer,
235: Source xmlSource, Result outputTarget)
236: throws TransformerException {
237: debugInfo("transform(Transformer,Source,Result) set XTest required XML parser");
238:
239: // this is not the ideal way ...
240: if (isTruncated())
241: transformer.setParameter("truncated", "true");
242: if (getMappedHostname() != null) {
243: transformer.setParameter("mappedHostname",
244: getMappedHostname());
245: }
246: if (includeExceptions()) {
247: transformer.setParameter("includeExceptions", "true");
248: }
249: if (includeIDELog()) {
250: transformer.setParameter("includeIDELog", "true");
251: }
252: transformer.transform(xmlSource, outputTarget);
253: transformer.clearParameters();
254: }
255:
256: public static void transform(File inputXML, File outputXML,
257: Transformer transformer) throws TransformerException,
258: IOException {
259: StreamSource xmlSource = new StreamSource(inputXML);
260: FileOutputStream outputFileStream = new FileOutputStream(
261: outputXML);
262: StreamResult xmlResult = new StreamResult(outputFileStream);
263: transform(transformer, xmlSource, xmlResult);
264: outputFileStream.close();
265: }
266:
267: public static void transform(File inputXML, File outputXML, File xsl)
268: throws TransformerException {
269: Transformer transformer = getTransformer(xsl);
270: transform(inputXML, outputXML, xsl);
271: }
272:
273: /* tranform results */
274: public static void transformResults(File inputDir, File outputDir)
275: throws TransformerException, IOException {
276: int inputDirType = ResultsUtils.resolveResultsDir(inputDir);
277: switch (inputDirType) {
278: case ResultsUtils.TESTBAG_DIR:
279: transformTestBag(inputDir, outputDir);
280: transformUnitSuites(inputDir, outputDir);
281: break;
282: case ResultsUtils.TESTREPORT_DIR:
283: transformReport(inputDir, outputDir);
284: transformSystemInfo(inputDir, outputDir);
285: transformFailuresReport(inputDir, outputDir);
286: transformFrameSet(inputDir, outputDir);
287: transformMainNavigator(inputDir, outputDir);
288: transformPerformance(inputDir, outputDir);
289: }
290: }
291:
292: public static void transformUnitSuites(File inputDir, File outputDir)
293: throws TransformerException, IOException {
294: debugInfo("transformUnitSuites(): inputDir=" + inputDir
295: + " outputDir=" + outputDir);
296: File suiteInputDir = new File(inputDir,
297: PEConstants.XMLRESULTS_DIR + File.separatorChar
298: + PEConstants.TESTSUITES_SUBDIR);
299: File suiteOutputDir = new File(outputDir,
300: PEConstants.HTMLRESULTS_DIR + File.separatorChar
301: + PEConstants.TESTSUITES_SUBDIR);
302: if (!suiteOutputDir.exists() && !suiteOutputDir.mkdirs()) {
303: throw new IOException(
304: "TransformXML, cannot create output dir: "
305: + suiteOutputDir);
306: }
307: String[] suites = ResultsUtils.listSuites(suiteInputDir);
308: if (suites == null)
309: return;
310: debugInfo("transformUnitSuites(): got suites: " + suites.length);
311: Transformer transformer = getTransformer(getXSLFile(TESTSUITES_XSL));
312: for (int i = 0; i < suites.length; i++) {
313: debugInfo("transformUnitSuites(): processing suite: "
314: + suites[i]);
315: File inputSuite = new File(suiteInputDir, "TEST-"
316: + suites[i] + ".xml");
317: File outputSuite = new File(suiteOutputDir, "TEST-"
318: + suites[i] + ".html");
319: outputSuite.createNewFile();
320: try {
321: transform(inputSuite, outputSuite, transformer);
322: } catch (TransformerException te) {
323: // we want other suites to be transformed as well in
324: // the case of any problem - so catch the exception
325: // print out some info and get back to work
326: System.out
327: .println("TransformXMLTask:transformUnitSuites: cannot transform this suite:"
328: + inputSuite);
329: te.printStackTrace();
330: }
331: }
332: }
333:
334: // this will create a page full of failures/errors over whole report
335: public static void transformFailuresReport(File inputRoot,
336: File outputRoot) throws TransformerException, IOException {
337: debugInfo("transformFailuresReport(): inputRoot=" + inputRoot
338: + " outputRoot=" + outputRoot);
339:
340: File failuresInputDir = ResultsUtils.getXMLResultDir(inputRoot);
341: File failuresInputFile = new File(failuresInputDir,
342: PEConstants.TESTREPORT_FAILURES_XML_FILE);
343:
344: File failuresOutputDir = ResultsUtils
345: .getHTMLResultDir(outputRoot);
346: File failuresOutputFile = new File(failuresOutputDir,
347: PEConstants.TESTREPORT_FAILURES_HTML_FILE);
348:
349: Transformer transformer = getTransformer(getXSLFile(TESTREPORT_FAILURES_XSL));
350: try {
351: transform(failuresInputFile, failuresOutputFile,
352: transformer);
353: } catch (TransformerException te) {
354: // we want other suites to be transformed as well in
355: // the case of any problem - so catch the exception
356: // print out some info and get back to work
357: System.out
358: .println("TransformXMLTask:transformUnitSuites: cannot transform this suite:"
359: + failuresInputFile);
360: }
361: }
362:
363: // this will create a page full of failures/errors over whole report
364: public static void transformReport(File inputRoot, File outputRoot)
365: throws TransformerException, IOException {
366: debugInfo("transformReport(): inputRoot=" + inputRoot
367: + " outputRoot=" + outputRoot);
368:
369: File reportInputDir = ResultsUtils.getXMLResultDir(inputRoot);
370: File reportInputFile = new File(reportInputDir,
371: PEConstants.TESTREPORT_XML_FILE);
372:
373: File reportOutputDir = ResultsUtils
374: .getHTMLResultDir(outputRoot);
375: File reportOutputFile = new File(reportOutputDir,
376: PEConstants.TESTREPORT_HTML_FILE);
377:
378: Transformer transformer = getTransformer(getXSLFile(TESTREPORT_XSL));
379: try {
380: transform(reportInputFile, reportOutputFile, transformer);
381: } catch (TransformerException te) {
382: // we want other suites to be transformed as well in
383: // the case of any problem - so catch the exception
384: // print out some info and get back to work
385: System.out
386: .println("TransformXMLTask:transformReport: cannot transform this report:"
387: + reportInputFile);
388: }
389: }
390:
391: // this will create a page with performance data
392: public static void transformPerformance(File inputRoot,
393: File outputRoot) throws TransformerException, IOException {
394: debugInfo("transformPerformance(): inputRoot=" + inputRoot
395: + " outputRoot=" + outputRoot);
396:
397: File reportInputDir = ResultsUtils.getXMLResultDir(inputRoot);
398: File reportInputFile = new File(reportInputDir,
399: PEConstants.TESTREPORT_PERFORMANCE_XML_FILE);
400:
401: File reportOutputDir = ResultsUtils
402: .getHTMLResultDir(outputRoot);
403: File reportOutputFile = new File(reportOutputDir,
404: PEConstants.TESTREPORT_PERFORMANCE_HTML_FILE);
405:
406: Transformer transformer = getTransformer(getXSLFile(PERFORMANCE_XSL));
407: try {
408: if (reportInputFile.exists()) {
409: transform(reportInputFile, reportOutputFile,
410: transformer);
411: }
412: } catch (TransformerException te) {
413: // we want other suites to be transformed as well in
414: // the case of any problem - so catch the exception
415: // print out some info and get back to work
416: System.out
417: .println("TransformXMLTask:transformPerformance: cannot transform performance:"
418: + reportInputFile);
419: }
420: }
421:
422: public static void transformTestBag(File inputRoot, File outputRoot)
423: throws TransformerException, IOException {
424: debugInfo("transformTestBag(): inputRoot=" + inputRoot
425: + " outputRoot=" + outputRoot);
426:
427: File testBagInputDir = ResultsUtils.getXMLResultDir(inputRoot);
428: File testBagInputFile = new File(testBagInputDir,
429: PEConstants.TESTBAG_XML_FILE);
430:
431: File testBagOutputDir = ResultsUtils
432: .getHTMLResultDir(outputRoot);
433: File testBagOutputFile = new File(testBagOutputDir,
434: PEConstants.TESTBAG_HTML_FILE);
435:
436: Transformer transformer = getTransformer(getXSLFile(TESTBAG_XSL));
437: try {
438: transform(testBagInputFile, testBagOutputFile, transformer);
439: } catch (TransformerException te) {
440: // we want other suites to be transformed as well in
441: // the case of any problem - so catch the exception
442: // print out some info and get back to work
443: System.out
444: .println("TransformXMLTask:transformUnitSuites: cannot transform this suite:"
445: + testBagInputFile);
446: }
447: }
448:
449: public static void transformSystemInfo(File inputRoot,
450: File outputRoot) throws TransformerException, IOException {
451: debugInfo("transformSystemInfo(): inputRoot=" + inputRoot
452: + " outputRoot=" + outputRoot);
453:
454: File siInputDir = ResultsUtils.getXMLResultDir(inputRoot);
455: File siInputFile = new File(siInputDir,
456: PEConstants.TESTREPORT_XML_FILE);
457:
458: File siOutputDir = ResultsUtils.getHTMLResultDir(outputRoot);
459: File siOutputFile = new File(siOutputDir,
460: PEConstants.SYSTEMINFO_HTML_FILE);
461:
462: Transformer transformer = getTransformer(getXSLFile(SYSTEMINFO_XSL));
463: try {
464: transform(siInputFile, siOutputFile, transformer);
465: } catch (TransformerException te) {
466: // we want other suites to be transformed as well in
467: // the case of any problem - so catch the exception
468: // print out some info and get back to work
469: System.out
470: .println("TransformXMLTask:transformUnitSuites: cannot transform this suite:"
471: + siInputFile);
472: }
473: }
474:
475: public static void transformFrameSet(File inputRoot, File outputRoot)
476: throws TransformerException, IOException {
477: debugInfo("transformFrameSet(): inputRoot=" + inputRoot
478: + " outputRoot=" + outputRoot);
479:
480: File fsInputDir = ResultsUtils.getXMLResultDir(inputRoot);
481: File fsInputFile = new File(fsInputDir,
482: PEConstants.TESTREPORT_XML_FILE);
483:
484: if (!outputRoot.exists()) {
485: outputRoot.mkdirs();
486: if (!outputRoot.isDirectory()) {
487: throw new IOException("Cannot create directory:"
488: + outputRoot);
489: }
490: }
491: File fsOutputFile = new File(outputRoot,
492: PEConstants.INDEX_HTML_FILE);
493:
494: Transformer transformer = getTransformer(getXSLFile(FRAMESET_XSL));
495: try {
496: transform(fsInputFile, fsOutputFile, transformer);
497: } catch (TransformerException te) {
498: // we want other suites to be transformed as well in
499: // the case of any problem - so catch the exception
500: // print out some info and get back to work
501: System.out
502: .println("TransformXMLTask:transformUnitSuites: cannot transform this suite:"
503: + fsInputFile);
504: }
505: }
506:
507: public static void transformMainNavigator(File inputRoot,
508: File outputRoot) throws TransformerException, IOException {
509: debugInfo("transformMainNavigator(): inputRoot=" + inputRoot
510: + " outputRoot=" + outputRoot);
511:
512: File mnInputDir = ResultsUtils.getXMLResultDir(inputRoot);
513: File mnInputFile = new File(mnInputDir,
514: PEConstants.TESTREPORT_XML_FILE);
515:
516: File mnOutputDir = ResultsUtils.getHTMLResultDir(outputRoot);
517: File mnOutputFile = new File(mnOutputDir,
518: PEConstants.MAIN_NAVIGATOR_HTML_FILE);
519:
520: Transformer transformer = getTransformer(getXSLFile(MAIN_NAVIGATOR_XSL));
521: try {
522: transform(mnInputFile, mnOutputFile, transformer);
523: } catch (TransformerException te) {
524: // we want other suites to be transformed as well in
525: // the case of any problem - so catch the exception
526: // print out some info and get back to work
527: System.out
528: .println("TransformXMLTask:transformUnitSuites: cannot transform this suite:"
529: + mnInputFile);
530: }
531: }
532:
533: }
|