package org.apache.maven.j2ee;

/* ====================================================================
 *   Copyright 2001-2004 The Apache Software Foundation.
 *
 *   Licensed 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.
 * ====================================================================
 */

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$
 * @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;
    }
}
