KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > junit > XalanExecutor


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package org.apache.tools.ant.taskdefs.optional.junit;
19
20 import java.io.BufferedOutputStream JavaDoc;
21 import java.io.ByteArrayOutputStream JavaDoc;
22 import java.io.File JavaDoc;
23 import java.io.FileOutputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.OutputStream JavaDoc;
26 import java.lang.reflect.Field JavaDoc;
27 import org.apache.tools.ant.BuildException;
28 import org.apache.tools.ant.Project;
29
30 /**
31  * This class is not used by the framework any more.
32  * We plan to remove it in Ant 1.8
33  * @deprecated since Ant 1.7
34  *
35  */

36 abstract class XalanExecutor {
37     private static final String JavaDoc PACKAGE =
38         "org.apache.tools.ant.taskdefs.optional.junit.";
39
40     // CheckStyle:VisibilityModifier OFF - bc
41
/** the transformer caller */
42     protected AggregateTransformer caller;
43     // CheckStyle:VisibilityModifier ON
44

45     /** set the caller for this object. */
46     private void setCaller(AggregateTransformer caller) {
47         this.caller = caller;
48     }
49
50     /** get the appropriate stream based on the format (frames/noframes) */
51     protected final OutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
52         if (AggregateTransformer.FRAMES.equals(caller.format)) {
53             // dummy output for the framed report
54
// it's all done by extension...
55
return new ByteArrayOutputStream JavaDoc();
56         } else {
57             return new BufferedOutputStream JavaDoc(
58                 new FileOutputStream JavaDoc(new File JavaDoc(caller.toDir, "junit-noframes.html")));
59         }
60     }
61
62     /** override to perform transformation */
63     abstract void execute() throws Exception JavaDoc;
64
65     /**
66      * Create a valid Xalan executor. It checks if Xalan2 is
67      * present. If none is available, it fails.
68      * @param caller object containing the transformation information.
69      * @throws BuildException thrown if it could not find a valid xalan
70      * executor.
71      */

72     static XalanExecutor newInstance(AggregateTransformer caller)
73         throws BuildException {
74         XalanExecutor executor = null;
75         try {
76             Class JavaDoc clazz = Class.forName(PACKAGE + "Xalan2Executor");
77             executor = (XalanExecutor) clazz.newInstance();
78         } catch (Exception JavaDoc xsltcApacheMissing) {
79             caller.task.log(xsltcApacheMissing.toString());
80                 throw new BuildException("Could not find xstlc nor xalan2 "
81                                          + "in the classpath. Check "
82                                          + "http://xml.apache.org/xalan-j");
83         }
84         String JavaDoc classNameImpl = executor.getImplementation();
85         String JavaDoc version = executor.getProcVersion(classNameImpl);
86         caller.task.log("Using " + version, Project.MSG_VERBOSE);
87         executor.setCaller(caller);
88         return executor;
89     }
90
91     /**
92      * This methods should return the classname implementation of the
93      * underlying xslt processor
94      * @return the classname of the implementation, for example:
95      * org.apache.xalan.processor.TransformerFactoryImpl
96      * @see #getProcVersion(String)
97      */

98     protected abstract String JavaDoc getImplementation();
99
100     /**
101      * Try to discover the xslt processor version based on the
102      * className. There is nothing carved in stone and it can change
103      * anytime, so this is just for the sake of giving additional
104      * information if we can find it.
105      * @param classNameImpl the classname of the underlying xslt processor
106      * @return a string representing the implementation version.
107      * @throws BuildException
108      */

109     protected abstract String JavaDoc getProcVersion(String JavaDoc classNameImpl)
110         throws BuildException;
111
112     /** a bit simplistic but xsltc data are conveniently private non final */
113     protected final String JavaDoc getXSLTCVersion(String JavaDoc procVersionClassName)
114         throws ClassNotFoundException JavaDoc {
115         // there's a convenient xsltc class version but data are
116
// private so use package information
117
Class JavaDoc procVersion = Class.forName(procVersionClassName);
118         Package JavaDoc pkg = procVersion.getPackage();
119         return pkg.getName() + " " + pkg.getImplementationTitle()
120             + " " + pkg.getImplementationVersion();
121     }
122
123     /** pretty useful data (Xalan version information) to display. */
124     protected final String JavaDoc getXalanVersion(String JavaDoc procVersionClassName)
125         throws ClassNotFoundException JavaDoc {
126         Class JavaDoc procVersion = Class.forName(procVersionClassName);
127         String JavaDoc pkg = procVersion.getPackage().getName();
128         try {
129             Field JavaDoc f = procVersion.getField("S_VERSION");
130             return pkg + " " + f.get(null).toString();
131         } catch (Exception JavaDoc e) {
132             return pkg + " ?.?";
133         }
134     }
135 }
136
Popular Tags