package org.apache.maven.javacc;

/* ====================================================================
 *   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 org.codehaus.plexus.util.FileUtils;


/**
 * 
 * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a> 
 * @version $Id$
 */
public class BaseBean
{
    /** jjtree or javacc grammar file*/
    private String grammar;

    /** the name of the java package which will contain generated files */
    private String javaccPackageName;
    
    private String jjtreePackageName;
    
    private String header ;
    
    private String generatedSourceDirectory;

    /**
     * Returns grammar file (javacc of jjtree grammar) 
     * @return grammar file
     */
    public String getGrammar()
    {
        return grammar;
    }

    /**
     * Sets grammar file
     * @param grammar
     */
    public void setGrammar( final String grammar )
    {
        this.grammar = grammar;
    }

    /**
     * Sets the name of the java package which will contain generated files
     * 
     * @param packageName the name of java package e.g com.wombat.parser.ast
     */
    public void setJavaccPackageName( final String javaccPackageName )
    {
        this.javaccPackageName = javaccPackageName;
    }
    
    /**
     * @return Returns the header.
     */
    public String getHeader()
    {
        return header;
    }
    /**
     * @param header The header to set.
     */
    public void setHeader( final String header )
    {
        this.header = header;
    }
    
    
    
    /**
     * 
     * @return
     */
    public File getJavaccOutputDir()
    {
        final String packagePath = javaccPackageName.replace( '.', File.separatorChar);
        
        return new File( generatedSourceDirectory, packagePath );
    }
    
    /**
     * 
     * @return
     */
    public File getJJTreeOutputDir()
    {
        
        final String packagePath = jjtreePackageName.replace( '.', File.separatorChar);
        
        return new File( generatedSourceDirectory, packagePath );
    }
    
    
    /**
     * 
     * @throws Exception
     */
    protected void addHeader( final File directory ) throws Exception
    {
        
        final File headerFile = new File( getHeader() );
        
        if ( headerFile.exists() )
        {
            final String headerContent = getHeaderContent( );
            
            final File[] generatedFiles = directory.listFiles( );
            
            for ( int i = 0; i < generatedFiles.length; i++ )
            {
                final File generatedFile = generatedFiles[i];
            
                addHeader( generatedFile, headerContent );
            }
        }
    }
    /**
     * 
     * @param headerFile
     * @return
     */
    protected String getHeaderContent(  )
    {
        try
        {
            return FileUtils.fileRead( header );
        }
        catch ( Exception e )
        {
            return null;
        }
    }
    
    /**
     * 
     * @param file
     * @param header
     * @throws Exception
     */
    protected void addHeader( final File file, final String headerContent )
            throws Exception
    {
        if (! file.getName().endsWith(".java") )
        {
           return;
        }    
        String content = FileUtils.fileRead( file.getCanonicalPath() );
        
        content = headerContent + content;
        
        FileUtils.fileWrite( file.getCanonicalPath(), content );
    }
    
    public void createTimestampFile( final long timestamp, final File directory ) throws Exception
    {
        final File timestampFile = new File( directory, ".timestamp" ); 
        
        FileUtils.fileWrite( timestampFile.getAbsolutePath(), "" + timestamp );
    }
    
    public long readTimestampFile( final File directory )
    {
        return 0;
    }
    
    public boolean checkTimestamp() throws Exception
    {
//        final File outputDir = getOutputDir();

//        final File grammarFile = new File ( getGrammar() ).getCanonicalFile();

//        final File timestampFile = new File( outputDir, ".timestamp" );   

//        if ( timestampFile.exists() )
//        {

//            long oldTimestamp = readTimestampFile( outputDir );            

//            if ( oldTimestamp < grammarFile.lastModified() )
//            {
//                return false;
//            }    
//        }        
        return true;
    }
    
    
    /**
     * @return Returns the jjtreePackageName.
     */
    public String getJjtreePackageName()
    {
        return jjtreePackageName;
    }
    /**
     * @param jjtreePackageName The jjtreePackageName to set.
     */
    public void setJjtreePackageName( final String jjtreePackageName )
    {
        this.jjtreePackageName = jjtreePackageName;
    }
    
    
    public String getGeneratedSourceDirectory()
    {
        return generatedSourceDirectory;	
    }
    
    public void setGeneratedSourceDirectory( final String generatedSourceDirectory )
    {
        this.generatedSourceDirectory = generatedSourceDirectory;	
    }
}
