⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmllogger.java

📁 Use the links below to download a source distribution of Ant from one of our mirrors. It is good pra
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* *  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;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;import java.io.OutputStreamWriter;import java.io.PrintStream;import java.io.Writer;import java.util.Hashtable;import java.util.Stack;import java.util.Enumeration;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import org.apache.tools.ant.util.DOMElementWriter;import org.apache.tools.ant.util.StringUtils;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.Text;/** * Generates a file in the current directory with * an XML description of what happened during a build. * The default filename is "log.xml", but this can be overridden * with the property <code>XmlLogger.file</code>. * * This implementation assumes in its sanity checking that only one * thread runs a particular target/task at a time. This is enforced * by the way that parallel builds and antcalls are done - and * indeed all but the simplest of tasks could run into problems * if executed in parallel. * * @see Project#addBuildListener(BuildListener) */public class XmlLogger implements BuildLogger {    private int msgOutputLevel = Project.MSG_DEBUG;    private PrintStream outStream;    /** DocumentBuilder to use when creating the document to start with. */    private static DocumentBuilder builder = getDocumentBuilder();    /**     * Returns a default DocumentBuilder instance or throws an     * ExceptionInInitializerError if it can't be created.     *     * @return a default DocumentBuilder instance.     */    private static DocumentBuilder getDocumentBuilder() {        try {            return DocumentBuilderFactory.newInstance().newDocumentBuilder();        } catch (Exception exc) {            throw new ExceptionInInitializerError(exc);        }    }    /** XML element name for a build. */    private static final String BUILD_TAG = "build";    /** XML element name for a target. */    private static final String TARGET_TAG = "target";    /** XML element name for a task. */    private static final String TASK_TAG = "task";    /** XML element name for a message. */    private static final String MESSAGE_TAG = "message";    /** XML attribute name for a name. */    private static final String NAME_ATTR = "name";    /** XML attribute name for a time. */    private static final String TIME_ATTR = "time";    /** XML attribute name for a message priority. */    private static final String PRIORITY_ATTR = "priority";    /** XML attribute name for a file location. */    private static final String LOCATION_ATTR = "location";    /** XML attribute name for an error description. */    private static final String ERROR_ATTR = "error";    /** XML element name for a stack trace. */    private static final String STACKTRACE_TAG = "stacktrace";    /** The complete log document for this build. */    private Document doc = builder.newDocument();    /** Mapping for when tasks started (Task to TimedElement). */    private Hashtable tasks = new Hashtable();    /** Mapping for when targets started (Task to TimedElement). */    private Hashtable targets = new Hashtable();    /**     * Mapping of threads to stacks of elements     * (Thread to Stack of TimedElement).     */    private Hashtable threadStacks = new Hashtable();    /**     * When the build started.     */    private TimedElement buildElement = null;    /** Utility class representing the time an element started. */    private static class TimedElement {        /**         * Start time in milliseconds         * (as returned by <code>System.currentTimeMillis()</code>).         */        private long startTime;        /** Element created at the start time. */        private Element element;        public String toString() {            return element.getTagName() + ":" + element.getAttribute("name");        }    }    /**     *  Constructs a new BuildListener that logs build events to an XML file.     */    public XmlLogger() {    }    /**     * Fired when the build starts, this builds the top-level element for the     * document and remembers the time of the start of the build.     *     * @param event Ignored.     */    public void buildStarted(BuildEvent event) {        buildElement = new TimedElement();        buildElement.startTime = System.currentTimeMillis();        buildElement.element = doc.createElement(BUILD_TAG);    }    /**     * Fired when the build finishes, this adds the time taken and any     * error stacktrace to the build element and writes the document to disk.     *     * @param event An event with any relevant extra information.     *              Will not be <code>null</code>.     */    public void buildFinished(BuildEvent event) {        long totalTime = System.currentTimeMillis() - buildElement.startTime;        buildElement.element.setAttribute(TIME_ATTR,                DefaultLogger.formatTime(totalTime));        if (event.getException() != null) {            buildElement.element.setAttribute(ERROR_ATTR,                    event.getException().toString());            // print the stacktrace in the build file it is always useful...            // better have too much info than not enough.            Throwable t = event.getException();            Text errText = doc.createCDATASection(StringUtils.getStackTrace(t));            Element stacktrace = doc.createElement(STACKTRACE_TAG);            stacktrace.appendChild(errText);            buildElement.element.appendChild(stacktrace);        }        String outFilename = event.getProject().getProperty("XmlLogger.file");        if (outFilename == null) {            outFilename = "log.xml";        }        String xslUri                = event.getProject().getProperty("ant.XmlLogger.stylesheet.uri");        if (xslUri == null) {            xslUri = "log.xsl";        }        Writer out = null;        try {            // specify output in UTF8 otherwise accented characters will blow            // up everything            OutputStream stream = outStream;            if (stream == null) {                stream = new FileOutputStream(outFilename);            }            out = new OutputStreamWriter(stream, "UTF8");            out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");            if (xslUri.length() > 0) {                out.write("<?xml-stylesheet type=\"text/xsl\" href=\""                        + xslUri + "\"?>\n\n");            }            (new DOMElementWriter()).write(buildElement.element, out, 0, "\t");            out.flush();        } catch (IOException exc) {            throw new BuildException("Unable to write log file", exc);        } finally {            if (out != null) {                try {                    out.close();                } catch (IOException e) {                    // ignore                }            }        }        buildElement = null;    }    /**     * Returns the stack of timed elements for the current thread.     * @return the stack of timed elements for the current thread     */    private Stack getStack() {        Stack threadStack = (Stack) threadStacks.get(Thread.currentThread());        if (threadStack == null) {            threadStack = new Stack();            threadStacks.put(Thread.currentThread(), threadStack);        }        /* For debugging purposes uncomment:        org.w3c.dom.Comment s = doc.createComment("stack=" + threadStack);        buildElement.element.appendChild(s);         */        return threadStack;    }    /**     * Fired when a target starts building, this pushes a timed element     * for the target onto the stack of elements for the current thread,     * remembering the current time and the name of the target.     *     * @param event An event with any relevant extra information.     *              Will not be <code>null</code>.     */    public void targetStarted(BuildEvent event) {        Target target = event.getTarget();        TimedElement targetElement = new TimedElement();        targetElement.startTime = System.currentTimeMillis();        targetElement.element = doc.createElement(TARGET_TAG);        targetElement.element.setAttribute(NAME_ATTR, target.getName());        targets.put(target, targetElement);        getStack().push(targetElement);    }    /**     * Fired when a target finishes building, this adds the time taken     * and any error stacktrace to the appropriate target element in the log.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -