Code clean up. Generated source directory is everywhere set in consistent way now

git-svn-id: https://svn.apache.org/repos/asf/maven/maven-1/plugins/trunk@115141 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
michal 2004-05-01 14:34:09 +00:00
parent eaa8e84536
commit 262803fb6d
6 changed files with 304 additions and 231 deletions

View File

@ -16,43 +16,32 @@ package org.apache.maven.javacc;
* limitations under the License.
* ====================================================================
*/
import org.codehaus.plexus.util.FileUtils;
import java.io.File;
import org.codehaus.plexus.util.FileUtils;
/**
* @author <a href="michal.maczka@dimatics.com">Michal Maczka</a>
* @version $Id: BaseBean.java,v 1.2 2004/03/02 15:05:55 evenisse Exp $
*
* @author <a href="michal.maczka@dimatics.com">Michal Maczka</a>
* @version $Id: BaseBean.java,v 1.3 2004/05/01 14:34:09 michal Exp $
*/
public class BaseBean
{
/**
* jjtree or javacc grammar file
*/
/** jjtree or javacc grammar file*/
private String grammar;
/**
* the name of the java package which will contain generated files
*/
/** the name of the java package which will contain generated files */
private String javaccPackageName;
private String jjtreePackageName;
private String header;
private String header ;
private String generatedSourceDirectory;
public void setGeneratedSourceDirectory( String generatedSourceDirectory )
{
this.generatedSourceDirectory = generatedSourceDirectory;
}
/**
* Returns grammar file (javacc of jjtree grammar)
*
* Returns grammar file (javacc of jjtree grammar)
* @return grammar file
*/
public String getGrammar()
@ -62,8 +51,7 @@ public class BaseBean
/**
* Sets grammar file
*
* @param grammar
* @param grammar
*/
public void setGrammar( final String grammar )
{
@ -73,13 +61,13 @@ public class BaseBean
/**
* Sets the name of the java package which will contain generated files
*
* @param javaccPackageName the name of java package e.g com.wombat.parser.ast
* @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.
*/
@ -87,7 +75,6 @@ public class BaseBean
{
return header;
}
/**
* @param header The header to set.
*/
@ -95,68 +82,118 @@ public class BaseBean
{
this.header = header;
}
/**
* @return
*
* @return
*/
public File getJavaccOutputDir()
{
final String packagePath = javaccPackageName
.replace( '.', File.separatorChar );
final File retValue = new File( getGeneratedSourceDirectory(), packagePath );
return retValue;
final String packagePath = javaccPackageName.replace( '.', File.separatorChar);
return new File( generatedSourceDirectory, packagePath );
}
public String getGeneratedSourceDirectory()
{
return generatedSourceDirectory;
}
/**
* @return
*
* @return
*/
public File getJJTreeOutputDir()
{
final String packagePath = jjtreePackageName
.replace( '.', File.separatorChar );
return new File( getGeneratedSourceDirectory(), packagePath );
final String packagePath = jjtreePackageName.replace( '.', File.separatorChar);
return new File( generatedSourceDirectory, packagePath );
}
/**
*
* @param timestamp
* @param directory
*
* @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" );
final File timestampFile = new File( directory, ".timestamp" );
FileUtils.fileWrite( timestampFile.getAbsolutePath(), "" + timestamp );
}
public long readTimestampFile( final File directory )
{
return 0;
}
/**
* Returns the flag indicating if generator should run
* @return
* @throws Exception
*/
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;
@ -164,8 +201,8 @@ public class BaseBean
// }
return true;
}
/**
* @return Returns the jjtreePackageName.
*/
@ -173,12 +210,22 @@ public class BaseBean
{
return jjtreePackageName;
}
/**
* @param jjtreePackageName The jjtreePackageName to set.
*/
public void setJjtreePackageName( String jjtreePackageName )
public void setJjtreePackageName( final String jjtreePackageName )
{
this.jjtreePackageName = jjtreePackageName;
}
public String getGeneratedSourceDirectory()
{
return generatedSourceDirectory;
}
public void setGeneratedSourceDirectory( final String generatedSourceDirectory )
{
this.generatedSourceDirectory = generatedSourceDirectory;
}
}

View File

@ -17,87 +17,111 @@ package org.apache.maven.javacc;
* ====================================================================
*/
import org.javacc.jjtree.JJTree;
import java.io.File;
import java.util.ArrayList;
import org.javacc.jjtree.JJTree;
/**
* @author <a href="michal.maczka@dimatics.com">Michal Maczka</a>
* @version $Id: JJTreeBean.java,v 1.2 2004/03/02 15:05:55 evenisse Exp $
*
* @author <a href="michal.maczka@dimatics.com">Michal Maczka</a>
* @version $Id: JJTreeBean.java,v 1.3 2004/05/01 14:34:09 michal Exp $
*/
public class JJTreeBean extends BaseBean
{
/**
* Generates AST and javacc grammar
*
* @return
*
* @throws Exception
* @return
* @throws Exception
*/
public int generate() throws Exception
{
{
final ArrayList params = new ArrayList();
final File outputDir = getJJTreeOutputDir();
System.out.println( "-OUTPUT_DIRECTORY=" + outputDir.getCanonicalPath() );
System.err.println("-OUTPUT_DIRECTORY=" + outputDir.getCanonicalPath());
final long timestamp = System.currentTimeMillis();
System.err.println( "Creating directory: " + outputDir.getCanonicalPath() );
if ( !outputDir.exists() )
{
outputDir.mkdirs();
{
System.out.println( "Creating directorty: " + outputDir.getAbsolutePath() );
outputDir.mkdirs();
}
else
{
if ( !checkTimestamp() )
{
return 0;
}
}
params.add( "-OUTPUT_DIRECTORY=" + outputDir.getCanonicalPath() );
}
}
params.add( "-OUTPUT_DIRECTORY=" + outputDir.getCanonicalPath() );
params.add( getGrammar() );
String[] args = ( String[] ) params.toArray( new String[params.size()] );
String[] args = ( String[] ) params.toArray( new String[ params.size() ] );
final JJTree jjtree = new JJTree();
final int retValue = jjtree.main( args );
moveJJTJavaParserState();
final int retValue = jjtree.main( args );
moveJJTJavaParserState( );
//addHeader( outputDir );
createTimestampFile( timestamp, outputDir );
return retValue;
}
/**
*
*/
private void moveJJTJavaParserState()
{
final File jjtJavaParserStateFileOrg = new File( getJJTreeOutputDir(), "JJTJavaParserState.java" );
final File jjtJavaParserStateFileNew = new File( getJavaccOutputDir(), "JJTJavaParserState.java" );
if ( !jjtJavaParserStateFileOrg.equals( jjtJavaParserStateFileNew ) )
{
final File dir = jjtJavaParserStateFileNew.getParentFile();
if ( !dir.exists() )
{
dir.mkdirs();
}
System.out.println( "Moving: " + jjtJavaParserStateFileOrg + " to: " + jjtJavaParserStateFileNew );
jjtJavaParserStateFileOrg.renameTo( jjtJavaParserStateFileNew );
private void moveJJTJavaParserState( )
{
final File jjtJavaParserStateFileOrg = new File( getJJTreeOutputDir() , "JJTJavaParserState.java" );
final File jjtJavaParserStateFileNew = new File( getJavaccOutputDir() , "JJTJavaParserState.java" );
if ( ! jjtJavaParserStateFileOrg.equals( jjtJavaParserStateFileNew ) )
{
final File dir = jjtJavaParserStateFileNew.getParentFile();
if ( !dir.exists() )
{
dir.mkdirs();
}
System.out.println("Moving: " + jjtJavaParserStateFileOrg +" to: " + jjtJavaParserStateFileNew );
jjtJavaParserStateFileOrg.renameTo( jjtJavaParserStateFileNew );
}
}
/**
* Returns the path to generated javacc grammar
*
* @return the path to generated javacc grammar
*/
public String getJavaccGrammar()
{
final File grammarFile = new File( getGrammar() );
String grammarName = grammarFile.getName();
grammarName = grammarName.substring( 0, grammarName.length() - 1 );
final String retValue = getJJTreeOutputDir() + File.separator + grammarName;
return retValue;
{
final File grammarFile = new File ( getGrammar() );
String grammarName = grammarFile.getName();
grammarName = grammarName.substring( 0, grammarName.length()-1 );
final String retValue = getJJTreeOutputDir() + File.separator + grammarName ;
return retValue;
}
}

View File

@ -25,7 +25,7 @@ import java.util.ArrayList;
/**
*
* @author <a href="michal.maczka@dimatics.com">Michal Maczka</a>
* @version $Id: JavaccBean.java,v 1.2 2004/03/02 15:05:55 evenisse Exp $
* @version $Id: JavaccBean.java,v 1.3 2004/05/01 14:34:09 michal Exp $
*/
public class JavaccBean extends BaseBean
{
@ -34,16 +34,25 @@ public class JavaccBean extends BaseBean
public int generate() throws Exception
{
final ArrayList params = new ArrayList();
final File outputDir = getJavaccOutputDir();
if ( !outputDir.exists() )
{
outputDir.mkdirs();
}
params.add( "-OUTPUT_DIRECTORY=" + outputDir.getCanonicalPath() );
params.add( getGrammar() );
String[] args = ( String[] ) params.toArray( new String[ params.size() ] );
final int retValue = Main.mainProgram( args );
//addHeader( outputDir );
return retValue;
}

View File

@ -1,58 +1,19 @@
package org.apache.maven.javacc;
/* ====================================================================
* The Apache Software License, Version 1.1
* Copyright 2001-2004 The Apache Software Foundation.
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
* 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
*
* 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/>.
* 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.
* ====================================================================
*/
@ -61,28 +22,40 @@ import junit.framework.TestCase;
/**
* @author <a href="michal.maczka@dimatics.com">Michal Maczka</a>
* @version $Id: BaseBeanTest.java,v 1.1 2004/01/31 16:29:27 michal Exp $
* @version $Id: BaseBeanTest.java,v 1.2 2004/05/01 14:34:09 michal Exp $
*/
public class BaseBeanTest extends TestCase
{
public void testBaseBean()
{
public void testBaseBean()
{
final BaseBean bean = new BaseBean();
final String grammar = "foo/baa/grammar.txt";
bean.setGrammar( grammar);
assertEquals( "Grammar file was not set correctly ", grammar, bean
.getGrammar());
assertEquals( "Grammar file was not set correctly ", grammar, bean.getGrammar());
bean.setJavaccPackageName( "com.wombat.javacc");
bean.setGeneratedSourceDirectory( "target" + File.separator + "generated-java" );
final String javaccPath = "target" + File.separator + "generated-java"
+ File.separator + "com" + File.separator + "wombat"
+ File.separator + "javacc";
final String fs = File.separator;
final String basedir = System.getProperty( "basedir" );
final String gsd = basedir + fs + "target" + fs + "generated-src" + fs + "main" + fs + "java" ;
bean.setGeneratedSourceDirectory( gsd );
final String javaccPath = gsd + fs + "com" + fs + "wombat" + fs + "javacc";
assertEquals( "Output dir not was set correctly", javaccPath, bean.getJavaccOutputDir().getPath() );
bean.setJjtreePackageName( "com.wombat.jjtree");
final String jjtreePath = "target" + File.separator + "generated-java"
+ File.separator + "com" + File.separator + "wombat"
+ File.separator + "jjtree";
final String jjtreePath = gsd + fs + "com" + fs + "wombat" + fs + "jjtree";
assertEquals( "Output dir not was set correctly", jjtreePath, bean.getJJTreeOutputDir().getPath() );
}}
}
}

View File

@ -1,5 +1,22 @@
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 junit.framework.TestCase;
import java.io.File;
@ -11,34 +28,54 @@ import org.codehaus.plexus.util.FileUtils;
/**
*
* @author <a href="michal.maczka@dimatics.com">Michal Maczka</a>
* @version $Id: JJTreeBeanTest.java,v 1.2 2004/02/20 13:23:19 evenisse Exp $
* @version $Id: JJTreeBeanTest.java,v 1.3 2004/05/01 14:34:09 michal Exp $
*/
public class JJTreeBeanTest extends TestCase
{
public void testGenerate()
{
final File grammarFile = new File( System.getProperty( "basedir" ), "src/test-data/JJTreeSample.jjt" );
final File headerFile = new File( System.getProperty( "basedir" ), "src/test-data/JJTreeSample.jjt.header" );
final String basedir = System.getProperty( "basedir" );
final File grammarFile = new File( basedir, "src/test-data/JJTreeSample.jjt" );
final File headerFile = new File( basedir, "src/test-data/JJTreeSample.jjt.header" );
final JJTreeBean bean = new JJTreeBean();
bean.setGeneratedSourceDirectory( "target" + File.separator + "generated-java" );
bean.setGrammar( grammarFile.getAbsolutePath() );
bean.setHeader( headerFile.getAbsolutePath() );
final String packageName = "org.apache.maven.javacc.jtree";
final String fs = File.separator;
final String gsd = basedir + fs + "target" + fs + "generated-src" + fs + "main" + fs + "java" ;
bean.setGeneratedSourceDirectory( gsd );
bean.setJjtreePackageName( packageName );
bean.setJavaccPackageName( packageName );
try
{
FileUtils.deleteDirectory( bean.getJJTreeOutputDir() );
bean.generate();
}
catch ( Exception e )
{
e.printStackTrace();
fail( "Generation failed" );
}
final String[] fileNames = bean.getJJTreeOutputDir().list();
final List list = Arrays.asList( fileNames );
assertTrue( list.contains( "JJTreeSample.jj" ) );
}

View File

@ -1,58 +1,19 @@
package org.apache.maven.javacc;
/* ====================================================================
* The Apache Software License, Version 1.1
* Copyright 2001-2004 The Apache Software Foundation.
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
* 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
*
* 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/>.
* 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.
* ====================================================================
*/
@ -67,33 +28,55 @@ import org.codehaus.plexus.util.FileUtils;
/**
*
* @author <a href="michal.maczka@dimatics.com">Michal Maczka</a>
* @version $Id: JavaccBeanTest.java,v 1.2 2004/02/20 13:23:19 evenisse Exp $
* @version $Id: JavaccBeanTest.java,v 1.3 2004/05/01 14:34:09 michal Exp $
*/
public class JavaccBeanTest extends TestCase
{
public void testGenerate()
{
final File grammarFile = new File( System.getProperty( "basedir" ), "src/test-data/JavaCCSample.jj" );
final File headerFile = new File( System.getProperty( "basedir" ), "src/test-data/JavaCCSample.jj.header" );
final String basedir = System.getProperty( "basedir" );
final File grammarFile = new File( basedir, "src/test-data/JavaCCSample.jj" );
final File headerFile = new File( basedir, "src/test-data/JavaCCSample.jj.header" );
final JavaccBean bean = new JavaccBean();
bean.setGeneratedSourceDirectory( "target" + File.separator + "generated-java" );
bean.setGrammar( grammarFile.getAbsolutePath() );
bean.setHeader( headerFile.getAbsolutePath() );
final String fs = File.separator;
final String gsd = basedir + fs + "target" + fs + "generated-src" + fs + "main" + fs + "java" ;
bean.setGeneratedSourceDirectory( gsd );
final String packageName = "org.apache.maven.javacc.javacc";
bean.setJavaccPackageName( packageName );
try
{
FileUtils.deleteDirectory( bean.getJavaccOutputDir() );
bean.generate();
} catch ( Exception e )
}
catch ( Exception e )
{
e.printStackTrace();
fail( "Generation failed" );
}
final String[] fileNames = bean.getJavaccOutputDir().list();
final List list = Arrays.asList( fileNames );
assertTrue( list.contains( "Simple1.java" ));
assertTrue( list.contains( "Token.java" ));