1 | /*
|
---|
2 | * Copyright 2001-2005 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 | */
|
---|
17 | package org.apache.tools.ant.taskdefs.optional.junit;
|
---|
18 |
|
---|
19 | import java.io.BufferedOutputStream;
|
---|
20 | import java.io.ByteArrayOutputStream;
|
---|
21 | import java.io.File;
|
---|
22 | import java.io.FileOutputStream;
|
---|
23 | import java.io.IOException;
|
---|
24 | import java.io.OutputStream;
|
---|
25 | import java.lang.reflect.Field;
|
---|
26 | import org.apache.tools.ant.BuildException;
|
---|
27 | import org.apache.tools.ant.Project;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Command class that encapsulate specific behavior for each
|
---|
31 | * Xalan version. The right executor will be instantiated at
|
---|
32 | * runtime via class lookup. For instance, it will check first
|
---|
33 | * for Xalan2/XSLTC, then for Xalan1.
|
---|
34 | */
|
---|
35 | abstract class XalanExecutor {
|
---|
36 | private static final String pack =
|
---|
37 | "org.apache.tools.ant.taskdefs.optional.junit.";
|
---|
38 |
|
---|
39 | /** the transformer caller */
|
---|
40 | protected AggregateTransformer caller;
|
---|
41 |
|
---|
42 | /** set the caller for this object. */
|
---|
43 | private final void setCaller(AggregateTransformer caller) {
|
---|
44 | this.caller = caller;
|
---|
45 | }
|
---|
46 |
|
---|
47 | /** get the appropriate stream based on the format (frames/noframes) */
|
---|
48 | protected final OutputStream getOutputStream() throws IOException {
|
---|
49 | if (AggregateTransformer.FRAMES.equals(caller.format)) {
|
---|
50 | // dummy output for the framed report
|
---|
51 | // it's all done by extension...
|
---|
52 | return new ByteArrayOutputStream();
|
---|
53 | } else {
|
---|
54 | return new BufferedOutputStream(new FileOutputStream(new File(caller.toDir, "junit-noframes.html")));
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | /** override to perform transformation */
|
---|
59 | abstract void execute() throws Exception;
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Create a valid Xalan executor. It checks first if Xalan2 is
|
---|
63 | * present, if not it checks for xalan1. If none is available, it
|
---|
64 | * fails.
|
---|
65 | * @param caller object containing the transformation information.
|
---|
66 | * @throws BuildException thrown if it could not find a valid xalan
|
---|
67 | * executor.
|
---|
68 | */
|
---|
69 | static XalanExecutor newInstance(AggregateTransformer caller)
|
---|
70 | throws BuildException {
|
---|
71 | XalanExecutor executor = null;
|
---|
72 | try {
|
---|
73 | Class clazz = Class.forName(pack + "Xalan2Executor");
|
---|
74 | executor = (XalanExecutor)clazz.newInstance();
|
---|
75 | } catch (Exception xsltcApacheMissing){
|
---|
76 | caller.task.log(xsltcApacheMissing.toString());
|
---|
77 | try {
|
---|
78 | Class clazz = Class.forName(pack + "Xalan1Executor");
|
---|
79 | executor = (XalanExecutor) clazz.newInstance();
|
---|
80 | } catch (Exception xalan1Missing){
|
---|
81 | caller.task.log(xalan1Missing.toString());
|
---|
82 | throw new BuildException("Could not find xstlc nor xalan2 nor "
|
---|
83 | + "xalan1 in the classpath. Check "
|
---|
84 | + "http://xml.apache.org/xalan-j");
|
---|
85 | }
|
---|
86 | }
|
---|
87 | String classNameImpl = executor.getImplementation();
|
---|
88 | String version = executor.getProcVersion(classNameImpl);
|
---|
89 | caller.task.log("Using " + version, Project.MSG_VERBOSE);
|
---|
90 | executor.setCaller(caller);
|
---|
91 | return executor;
|
---|
92 | }
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * This methods should return the classname implementation of the
|
---|
96 | * underlying xslt processor
|
---|
97 | * @return the classname of the implementation, for example:
|
---|
98 | * org.apache.xalan.processor.TransformerFactoryImpl
|
---|
99 | * @see #getProcVersion(String)
|
---|
100 | */
|
---|
101 | protected abstract String getImplementation();
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Try to discover the xslt processor version based on the
|
---|
105 | * className. There is nothing carved in stone and it can change
|
---|
106 | * anytime, so this is just for the sake of giving additional
|
---|
107 | * information if we can find it.
|
---|
108 | * @param classNameImpl the classname of the underlying xslt processor
|
---|
109 | * @return a string representing the implementation version.
|
---|
110 | * @throws BuildException
|
---|
111 | */
|
---|
112 | protected abstract String getProcVersion(String classNameImpl)
|
---|
113 | throws BuildException;
|
---|
114 |
|
---|
115 | /** a bit simplistic but xsltc data are conveniently private non final */
|
---|
116 | protected final String getXSLTCVersion(String procVersionClassName)
|
---|
117 | throws ClassNotFoundException {
|
---|
118 | // there's a convenient xsltc class version but data are
|
---|
119 | // private so use package information
|
---|
120 | Class procVersion = Class.forName(procVersionClassName);
|
---|
121 | Package pkg = procVersion.getPackage();
|
---|
122 | return pkg.getName() + " " + pkg.getImplementationTitle()
|
---|
123 | + " " + pkg.getImplementationVersion();
|
---|
124 | }
|
---|
125 |
|
---|
126 | /** pretty useful data (Xalan version information) to display. */
|
---|
127 | protected final String getXalanVersion(String procVersionClassName)
|
---|
128 | throws ClassNotFoundException {
|
---|
129 | Class procVersion = Class.forName(procVersionClassName);
|
---|
130 | String pkg = procVersion.getPackage().getName();
|
---|
131 | try {
|
---|
132 | Field f = procVersion.getField("S_VERSION");
|
---|
133 | return pkg + " " + f.get(null).toString();
|
---|
134 | } catch (Exception e) {
|
---|
135 | return pkg + " ?.?";
|
---|
136 | }
|
---|
137 | }
|
---|
138 | }
|
---|