Note that this element doesn't have any effect with a + * processor other than trax or if the Transformer is not Xalan2's + * transformer implementation.
+ * + * @since Ant 1.8.0 + */ + public TraceConfiguration createTrace() { + if (traceConfiguration != null) { + throw new BuildException("can't have more than one trace" + + " configuration"); + } + traceConfiguration = new TraceConfiguration(); + return traceConfiguration; + } + + /** + * Configuration for Xalan2 traces. + * + * @since Ant 1.8.0 + */ + public TraceConfiguration getTraceConfiguration() { + return traceConfiguration; + } + /** * Load processor here instead of in setProcessor - this will be * called from within execute, so we have access to the latest @@ -1358,4 +1392,100 @@ public String[] mapFileName(String xmlFile) { } } + /** + * Configuration for Xalan2 traces. + * + * @since Ant 1.8.0 + */ + public final class TraceConfiguration { + private boolean elements, extension, generation, selection, templates; + + /** + * Set to true if the listener is to print events that occur + * as each node is 'executed' in the stylesheet. + */ + public void setElements(boolean b) { + elements = b; + } + + /** + * True if the listener is to print events that occur as each + * node is 'executed' in the stylesheet. + */ + public boolean getElements() { + return elements; + } + + /** + * Set to true if the listener is to print information after + * each extension event. + */ + public void setExtension(boolean b) { + extension = b; + } + + /** + * True if the listener is to print information after each + * extension event. + */ + public boolean getExtension() { + return extension; + } + + /** + * Set to true if the listener is to print information after + * each result-tree generation event. + */ + public void setGeneration(boolean b) { + generation = b; + } + + /** + * True if the listener is to print information after each + * result-tree generation event. + */ + public boolean getGeneration() { + return generation; + } + + /** + * Set to true if the listener is to print information after + * each selection event. + */ + public void setSelection(boolean b) { + selection = b; + } + + /** + * True if the listener is to print information after each + * selection event. + */ + public boolean getSelection() { + return selection; + } + + /** + * Set to true if the listener is to print an event whenever a + * template is invoked. + */ + public void setTemplates(boolean b) { + templates = b; + } + + /** + * True if the listener is to print an event whenever a + * template is invoked. + */ + public boolean getTemplates() { + return templates; + } + + /** + * The stream to write traces to. + */ + public java.io.OutputStream getOutputStream() { + return new LogOutputStream(XSLTProcess.this); + } + } + } diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/TraXLiaison.java b/src/main/org/apache/tools/ant/taskdefs/optional/TraXLiaison.java index 5a5e0b3569..a22fee0830 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/TraXLiaison.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/TraXLiaison.java @@ -125,6 +125,9 @@ public class TraXLiaison implements XSLTLiaison3, ErrorListener, XSLTLoggerAware /** whether to suppress warnings */ private boolean suppressWarnings = false; + /** optional trace configuration. */ + private XSLTProcess.TraceConfiguration traceConfiguration = null; + /** * Constructor for TraXLiaison. * @throws Exception never @@ -324,6 +327,37 @@ private void createTransformer() throws Exception { final String[] pair = (String[]) outputProperties.elementAt(i); transformer.setOutputProperty(pair[0], pair[1]); } + + if (traceConfiguration != null) { + if ("org.apache.xalan.transformer.TransformerImpl" + .equals(transformer.getClass().getName())) { + try { + Class traceSupport = + Class.forName("org.apache.tools.ant.taskdefs.optional." + + "Xalan2TraceSupport", true, + Thread.currentThread() + .getContextClassLoader()); + XSLTTraceSupport ts = + (XSLTTraceSupport) traceSupport.newInstance(); + ts.configureTrace(transformer, traceConfiguration); + } catch (Exception e) { + String msg = "Failed to enable tracing because of " + e; + if (project != null) { + project.log(msg, Project.MSG_WARN); + } else { + System.err.println(msg); + } + } + } else { + String msg = "Not enabling trace support for transformer" + + " implementation" + transformer.getClass().getName(); + if (project != null) { + project.log(msg, Project.MSG_WARN); + } else { + System.err.println(msg); + } + } + } } /** @@ -583,5 +617,7 @@ public void configure(XSLTProcess xsltTask) { } suppressWarnings = xsltTask.getSuppressWarnings(); + + traceConfiguration = xsltTask.getTraceConfiguration(); } } diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/XSLTTraceSupport.java b/src/main/org/apache/tools/ant/taskdefs/optional/XSLTTraceSupport.java new file mode 100644 index 0000000000..98ca15669c --- /dev/null +++ b/src/main/org/apache/tools/ant/taskdefs/optional/XSLTTraceSupport.java @@ -0,0 +1,31 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.tools.ant.taskdefs.optional; + +import org.apache.tools.ant.taskdefs.XSLTProcess; +import javax.xml.transform.Transformer; + +/** + * Sets up trace support for a given transformer. + * + * @since Ant 1.8.0 + */ +public interface XSLTTraceSupport { + void configureTrace(Transformer t, XSLTProcess.TraceConfiguration conf); +} diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/Xalan2TraceSupport.java b/src/main/org/apache/tools/ant/taskdefs/optional/Xalan2TraceSupport.java new file mode 100644 index 0000000000..0e66cc4506 --- /dev/null +++ b/src/main/org/apache/tools/ant/taskdefs/optional/Xalan2TraceSupport.java @@ -0,0 +1,52 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.tools.ant.taskdefs.optional; + +import java.io.PrintWriter; +import java.util.TooManyListenersException; +import javax.xml.transform.Transformer; +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.taskdefs.XSLTProcess; +import org.apache.xalan.trace.PrintTraceListener; +import org.apache.xalan.transformer.TransformerImpl; + +/** + * Sets up trace support for a given transformer. + * + * @since Ant 1.8.0 + */ +public class Xalan2TraceSupport implements XSLTTraceSupport { + public void configureTrace(Transformer t, + XSLTProcess.TraceConfiguration conf) { + if (t instanceof TransformerImpl && conf != null) { + PrintWriter w = new PrintWriter(conf.getOutputStream(), false); + PrintTraceListener tl = new PrintTraceListener(w); + tl.m_traceElements = conf.getElements(); + tl.m_traceExtension = conf.getExtension(); + tl.m_traceGeneration = conf.getGeneration(); + tl.m_traceSelection = conf.getSelection(); + tl.m_traceTemplates = conf.getTemplates(); + try { + ((TransformerImpl) t).getTraceManager().addTraceListener(tl); + } catch (TooManyListenersException tml) { + throw new BuildException(tml); + } + } + } +} diff --git a/src/tests/antunit/taskdefs/xslt-test.xml b/src/tests/antunit/taskdefs/xslt-test.xml index 2bb0e9877c..17576a4d23 100644 --- a/src/tests/antunit/taskdefs/xslt-test.xml +++ b/src/tests/antunit/taskdefs/xslt-test.xml @@ -146,4 +146,28 @@ undefined='