package org.apache.maven.j2ee;

/* ====================================================================
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 2002 The Apache Software Foundation.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 4. The names "Apache" and "Apache Software Foundation" and
 *    "Apache Maven" must not be used to endorse or promote products
 *    derived from this software without prior written permission. For
 *    written permission, please contact apache@apache.org.
 *
 * 5. Products derived from this software may not be called "Apache",
 *    "Apache Maven", nor may "Apache" appear in their name, without
 *    prior written permission of the Apache Software Foundation.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 *
 * ====================================================================
 */

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;

/**
 * Base class for formatters of validation events.
 * Handles plain and xml formats.
 * 
 * @version $Id: ValidationFormatter.java,v 1.2 2003/02/07 11:47:14 evenisse Exp $
 * @author  dion
 */
public class ValidationFormatter implements ValidationListener
{

    /** Whether the formatter should write it's results to a file */
    private boolean usefile;
    /** name of the file (if usefile is true) to send output to */
    private String file;
    /** type of output: plain/xml */
    private String type = ValidationFormatter.PLAIN;
    /** Plain format output */
    public static final String PLAIN = "plain";
    /** xml format output */
    public static final String XML = "xml";
    /** output destination */
    private PrintStream stream = null;
    
    // --- Constructors --------------------------------------------------------
    /**
     * Creates a new instance of ValidationFormatter
     */
    public ValidationFormatter()
    {
        stream = System.out;
    }

    // --- Methods -------------------------------------------------------------
    
    /**
     * Test whether the formatter is in plain mode.
     * 
     * @return true if {@link #type} is set to {@link PLAIN}
     */
    private boolean isPlain()
    {
        return ValidationFormatter.PLAIN.equals(getType());
    }

    /**
     * Print an event in xml format
     * 
     * @param event the event to print
     * @param eventName started/ended/error etc
     */
    private void printEventAsXML(ValidationEvent event, String eventName)
    {
        getStream().println("\t<" + eventName + ">");
        getStream().println("\t\t<source>");
        getStream().println("\t\t\t" + event.getSource());
        getStream().println("\t\t</source>");
        getStream().println("\t\t<subject>");
        getStream().println("\t\t\t" + event.getSubject());
        getStream().println("\t\t</subject>");
        getStream().println("\t\t<message>");
        getStream().println("\t\t\t" + event.getMessage());
        getStream().println("\t\t</message>");
        getStream().println("\t</" + eventName + ">");
    }
    
    /**
     * Print an event in plain format
     * 
     * @param event the {@link ValidationEvent event} to print
     * @param type started, ended etc
     */
    private void printEvent(String type, ValidationEvent event)
    {
        getStream().println(event.getSubject() + " " + type + ": "
            + event.getMessage());
    }
    
    /**
     * Called when validation ends
     * 
     * @param event a {@link ValidationEvent}
     */
    public void validationEnded(ValidationEvent event) 
    {
        if (isPlain())
        {
            printEvent("ended", event);
        }
        else
        {
            printEventAsXML(event, "ended");
            getStream().println("</validation-report>");
        }
    }
    
    /**
     * Called when a validation error occurs. That is, the subject being
     * validated has a serious (fatal) problem
     * 
     * @param event a {@link ValidationEvent}
     */
    public void validationError(ValidationEvent event) 
    {
        if (isPlain())
        {
            printEvent("error", event);
        }
        else
        {
            printEventAsXML(event, "error");
        }
    }
    
    /**
     * Called when validation starts
     * 
     * @param event a {@link ValidationEvent}
     */
    public void validationStarted(ValidationEvent event) 
    {
        if (isPlain())
        {
            printEvent("started", event);
        }
        else
        {
            getStream().println("<?xml version=\"1.0\" ?>");
            getStream().println("<validation-report>");
            printEventAsXML(event, "started");
        }
    }
    
    /**
     * Called when a validation warning occurs. That is, the subject being
     * validated has a problem which may not be fatal
     * 
     * @param event a {@link ValidationEvent}
     */
    public void validationWarning(ValidationEvent event) 
    {
        if (isPlain())
        {
            printEvent("warning", event);
        }
        else
        {
            printEventAsXML(event, "warning");
        }
    }
    
    /**
     * Called when validation information events are fired.
     * 
     * @param event a {@link ValidationEvent}
     */
    public void validationInformation(ValidationEvent event) 
    {
        if (isPlain())
        {
            printEvent("info", event);
        }
        else
        {
            printEventAsXML(event, "info");
        }
    }
    
    /**
     * Getter for property file.
     * 
     * @return Value of property file.
     */
    public String getFile() 
    {
        return file;
    }
    
    /**
     * Setter for property file.
     * 
     * @param file New value of property file.
     * @throws FileNotFoundException if the named file doesn't exist
     */
    public void setFile(String file) throws FileNotFoundException
    {
        this.file = file;
        File theFile = new File(file);
        File dir = theFile.getParentFile();
        if (dir != null) {
            dir.mkdirs();
        }
        FileOutputStream fos = new FileOutputStream(file);
        stream = new PrintStream(fos);
    }
    
    /**
     * Getter for property usefile.
     * 
     * @return Value of property usefile.
     */
    public boolean isUsefile() 
    {
        return usefile;
    }
    
    /**
     * Setter for property usefile.
     * 
     * @param usefile New value of property usefile.
     */
    public void setUsefile(boolean usefile) 
    {
        this.usefile = usefile;
    }
    
    /**
     * Getter for property stream.
     * 
     * @return Value of property stream.
     */
    public PrintStream getStream()
    {
        return stream;
    }
    
    /**
     * Setter for property stream.
     * 
     * @param stream New value of property stream.
     */
    public void setStream(PrintStream stream)
    {
        this.stream = stream;
    }
    
    /**
     * Getter for property type.
     * 
     * @return Value of property type.
     */
    public String getType()
    {
        return type;
    }
    
    /**
     * Setter for property type.
     * 
     * @param type New value of property type.
     */
    public void setType(String type)
    {
        this.type = type;
    }
}
