diff --git a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/metamata/AbstractMetamataTask.java b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/metamata/AbstractMetamataTask.java index f7e19df11..9d74e018f 100644 --- a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/metamata/AbstractMetamataTask.java +++ b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/metamata/AbstractMetamataTask.java @@ -15,10 +15,10 @@ import java.util.ArrayList; import java.util.Hashtable; import java.util.Iterator; import java.util.Random; +import org.apache.myrmidon.api.AbstractTask; import org.apache.myrmidon.api.TaskException; import org.apache.tools.ant.DirectoryScanner; -import org.apache.tools.ant.Task; -import org.apache.tools.ant.taskdefs.exec.Execute; +import org.apache.tools.ant.taskdefs.exec.Execute2; import org.apache.tools.ant.types.Argument; import org.apache.tools.ant.types.CommandlineJava; import org.apache.tools.ant.types.FileSet; @@ -31,48 +31,46 @@ import org.apache.tools.ant.types.Path; * * @author Stephane Bailliez */ -public abstract class AbstractMetamataTask extends Task +public abstract class AbstractMetamataTask + extends AbstractTask { - - //--------------------------- ATTRIBUTES ----------------------------------- - /** * The user classpath to be provided. It matches the -classpath of the * command line. The classpath must includes both the .class and * the .java files for accurate audit. */ - protected Path classPath = null; + private Path m_classPath; /** * the path to the source file */ - protected Path sourcePath = null; + private Path m_sourcePath; /** * Metamata home directory. It will be passed as a metamata.home * property and should normally matches the environment property * META_HOME set by the Metamata installer. */ - protected File metamataHome = null; + private File m_metamataHome; /** * the command line used to run MAudit */ - protected CommandlineJava cmdl = new CommandlineJava(); + private CommandlineJava m_cmdl = new CommandlineJava(); /** * the set of files to be audited */ - protected ArrayList fileSets = new ArrayList(); + private ArrayList m_fileSets = new ArrayList(); /** * the options file where are stored the command line options */ - protected File optionsFile = null; + private File m_optionsFile; // this is used to keep track of which files were included. It will // be set when calling scanFileSets(); - protected Hashtable includedFiles = null; + private Hashtable m_includedFiles; public AbstractMetamataTask() { @@ -85,8 +83,8 @@ public abstract class AbstractMetamataTask extends Task */ protected AbstractMetamataTask( String className ) { - cmdl.setVm( "java" ); - cmdl.setClassname( className ); + m_cmdl.setVm( "java" ); + m_cmdl.setClassname( className ); } /** @@ -123,60 +121,50 @@ public abstract class AbstractMetamataTask extends Task /** * the metamata.home property to run all tasks. - * - * @param metamataHome The new Metamatahome value */ public void setMetamatahome( final File metamataHome ) { - this.metamataHome = metamataHome; + this.m_metamataHome = metamataHome; } /** * The java files or directory to be audited - * - * @param fs The feature to be added to the FileSet attribute */ - public void addFileSet( FileSet fs ) + public void addFileSet( final FileSet fileSet ) { - fileSets.add( fs ); + m_fileSets.add( fileSet ); } /** * user classpath - * - * @return Description of the Returned Value */ public Path createClasspath() { - if( classPath == null ) + if( m_classPath == null ) { - classPath = new Path(); + m_classPath = new Path(); } - return classPath; + return m_classPath; } /** * Creates a nested jvmarg element. - * - * @return Description of the Returned Value */ public Argument createJvmarg() { - return cmdl.createVmArgument(); + return m_cmdl.createVmArgument(); } /** * create the source path for this task - * - * @return Description of the Returned Value */ public Path createSourcepath() { - if( sourcePath == null ) + if( m_sourcePath == null ) { - sourcePath = new Path(); + m_sourcePath = new Path(); } - return sourcePath; + return m_sourcePath; } /** @@ -198,44 +186,37 @@ public abstract class AbstractMetamataTask extends Task } } - //--------------------- PRIVATE/PROTECTED METHODS -------------------------- - /** * check the options and build the command line - * - * @exception TaskException Description of Exception */ protected void setUp() throws TaskException { - checkOptions(); + validate(); // set the classpath as the jar file - File jar = getMetamataJar( metamataHome ); - final Path classPath = cmdl.createClasspath( getProject() ); + File jar = getMetamataJar( m_metamataHome ); + final Path classPath = m_cmdl.createClasspath(); classPath.createPathElement().setLocation( jar ); // set the metamata.home property - final Argument vmArgs = cmdl.createVmArgument(); - vmArgs.setValue( "-Dmetamata.home=" + metamataHome.getAbsolutePath() ); + final Argument vmArgs = m_cmdl.createVmArgument(); + vmArgs.setValue( "-Dmetamata.home=" + m_metamataHome.getAbsolutePath() ); // retrieve all the files we want to scan - includedFiles = scanFileSets(); - getLogger().debug( includedFiles.size() + " files added for audit" ); + m_includedFiles = scanFileSets(); + getLogger().debug( m_includedFiles.size() + " files added for audit" ); // write all the options to a temp file and use it ro run the process ArrayList options = getOptions(); - optionsFile = createTmpFile(); - generateOptionsFile( optionsFile, options ); - Argument args = cmdl.createArgument(); - args.setLine( "-arguments " + optionsFile.getAbsolutePath() ); + m_optionsFile = createTmpFile(); + generateOptionsFile( m_optionsFile, options ); + Argument args = m_cmdl.createArgument(); + args.setLine( "-arguments " + m_optionsFile.getAbsolutePath() ); } /** * return the location of the jar file used to run - * - * @param home Description of Parameter - * @return The MetamataJar value */ protected final File getMetamataJar( File home ) { @@ -244,31 +225,30 @@ public abstract class AbstractMetamataTask extends Task protected Hashtable getFileMapping() { - return includedFiles; + return m_includedFiles; } /** * return all options of the command line as string elements - * - * @return The Options value */ - protected abstract ArrayList getOptions(); + protected abstract ArrayList getOptions() + throws TaskException; /** * validate options set * * @exception TaskException Description of Exception */ - protected void checkOptions() + protected void validate() throws TaskException { // do some validation first - if( metamataHome == null || !metamataHome.exists() ) + if( m_metamataHome == null || !m_metamataHome.exists() ) { throw new TaskException( "'metamatahome' must point to Metamata home directory." ); } - metamataHome = resolveFile( metamataHome.getPath() ); - File jar = getMetamataJar( metamataHome ); + m_metamataHome = resolveFile( m_metamataHome.getPath() ); + File jar = getMetamataJar( m_metamataHome ); if( !jar.exists() ) { throw new TaskException( jar + " does not exist. Check your metamata installation." ); @@ -279,22 +259,15 @@ public abstract class AbstractMetamataTask extends Task * clean up all the mess that we did with temporary objects */ protected void cleanUp() + throws TaskException { - if( optionsFile != null ) + if( m_optionsFile != null ) { - optionsFile.delete(); - optionsFile = null; + m_optionsFile.delete(); + m_optionsFile = null; } } - /** - * create a stream handler that will be used to get the output since - * metamata tools do not report with convenient files such as XML. - * - * @return Description of the Returned Value - */ - protected abstract void setupStreamHandler( Execute exe ); - /** * execute the process with a specific handler * @@ -304,13 +277,13 @@ public abstract class AbstractMetamataTask extends Task protected void execute0() throws TaskException { - final Execute exe = new Execute(); - setupStreamHandler( exe ); - getLogger().debug( cmdl.toString() ); - exe.setCommandline( cmdl.getCommandline() ); + final Execute2 exe = new Execute2(); + setupLogger( exe ); + getLogger().debug( m_cmdl.toString() ); + exe.setCommandline( m_cmdl.getCommandline() ); try { - if( exe.execute() != 0 ) + if( 0 != exe.execute() ) { throw new TaskException( "Metamata task failed." ); } @@ -360,11 +333,12 @@ public abstract class AbstractMetamataTask extends Task * audited. */ protected Hashtable scanFileSets() + throws TaskException { Hashtable files = new Hashtable(); - for( int i = 0; i < fileSets.size(); i++ ) + for( int i = 0; i < m_fileSets.size(); i++ ) { - FileSet fs = (FileSet)fileSets.get( i ); + FileSet fs = (FileSet)m_fileSets.get( i ); DirectoryScanner ds = fs.getDirectoryScanner(); ds.scan(); String[] f = ds.getIncludedFiles(); @@ -385,4 +359,34 @@ public abstract class AbstractMetamataTask extends Task return files; } + protected ArrayList getFileSets() + { + return m_fileSets; + } + + protected Hashtable getIncludedFiles() + { + return m_includedFiles; + } + + protected Path getClassPath() + { + return m_classPath; + } + + protected void setClassPath( Path classPath ) + { + m_classPath = classPath; + } + + protected Path getSourcePath() + { + return m_sourcePath; + } + + protected void setSourcePath( Path sourcePath ) + { + m_sourcePath = sourcePath; + } + } diff --git a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/metamata/MAudit.java b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/metamata/MAudit.java index f52976eaa..7f8384c8b 100644 --- a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/metamata/MAudit.java +++ b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/metamata/MAudit.java @@ -8,13 +8,8 @@ package org.apache.tools.ant.taskdefs.optional.metamata; import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.OutputStream; import java.util.ArrayList; import org.apache.myrmidon.api.TaskException; -import org.apache.tools.ant.taskdefs.exec.Execute; -import org.apache.tools.ant.taskdefs.exec.LogOutputStream; import org.apache.tools.ant.types.Path; /** @@ -32,9 +27,9 @@ import org.apache.tools.ant.types.Path; * * @author Stephane Bailliez */ -public class MAudit extends AbstractMetamataTask +public class MAudit + extends AbstractMetamataTask { - /* * As of Metamata 2.0, the command line of MAudit is as follows: * Usage @@ -72,15 +67,11 @@ public class MAudit extends AbstractMetamataTask // (?:file:)?((?#filepath).+):((?#line)\\d+)\\s*:\\s+((?#message).*) final static String AUDIT_PATTERN = "(?:file:)?(.+):(\\d+)\\s*:\\s+(.*)"; - protected File outFile = null; - - protected Path searchPath = null; - - protected boolean fix = false; - - protected boolean list = false; - - protected boolean unused = false; + private File m_outFile; + private Path m_searchPath; + private boolean m_fix; + private boolean m_list; + private boolean m_unused; /** * default constructor @@ -90,78 +81,62 @@ public class MAudit extends AbstractMetamataTask super( "com.metamata.gui.rc.MAudit" ); } - /** - * handy factory to create a violation - * - * @param line Description of Parameter - * @param msg Description of Parameter - * @return Description of the Returned Value - */ - final static Violation createViolation( int line, String msg ) + public void setFix( final boolean fix ) { - Violation violation = new Violation(); - violation.line = line; - violation.error = msg; - return violation; + m_fix = fix; } - public void setFix( boolean flag ) + public void setList( final boolean list ) { - this.fix = flag; - } - - public void setList( boolean flag ) - { - this.list = flag; + m_list = list; } /** * set the destination file which should be an xml file - * - * @param outFile The new Tofile value */ - public void setTofile( File outFile ) + public void setTofile( final File outFile ) { - this.outFile = outFile; + m_outFile = outFile; } - public void setUnused( boolean flag ) + public void setUnused( final boolean unused ) { - this.unused = flag; + m_unused = unused; } public Path createSearchpath() { - if( searchPath == null ) + if( m_searchPath == null ) { - searchPath = new Path(); + m_searchPath = new Path(); } - return searchPath; + return m_searchPath; } protected ArrayList getOptions() + throws TaskException { ArrayList options = new ArrayList( 512 ); // there is a bug in Metamata 2.0 build 37. The sourcepath argument does // not work. So we will use the sourcepath prepended to classpath. (order // is important since Metamata looks at .class and .java) - if( sourcePath != null ) + if( getSourcePath() != null ) { - sourcePath.append( classPath );// srcpath is prepended - classPath = sourcePath; - sourcePath = null;// prevent from using -sourcepath + getSourcePath().append( getClassPath() );// srcpath is prepended + setClassPath( getSourcePath() ); + setSourcePath( null );// prevent from using -sourcepath } // don't forget to modify the pattern if you change the options reporting - if( classPath != null ) + if( getClassPath() != null ) { options.add( "-classpath" ); - options.add( classPath.toString() ); + options.add( getClassPath().toString() ); } // suppress copyright msg when running, we will let it so that this // will be the only output to the console if in xml mode // options.add("-quiet"); - if( fix ) + if( m_fix ) { options.add( "-fix" ); } @@ -170,34 +145,34 @@ public class MAudit extends AbstractMetamataTask // generate .maudit files much more detailed than the report // I don't like it very much, I think it could be interesting // to get all .maudit files and include them in the XML. - if( list ) + if( m_list ) { options.add( "-list" ); } - if( sourcePath != null ) + if( getSourcePath() != null ) { options.add( "-sourcepath" ); - options.add( sourcePath.toString() ); + options.add( getSourcePath().toString() ); } - if( unused ) + if( m_unused ) { options.add( "-unused" ); - options.add( searchPath.toString() ); + options.add( m_searchPath.toString() ); } - addAllArrayList( options, includedFiles.keySet().iterator() ); + addAllArrayList( options, getIncludedFiles().keySet().iterator() ); return options; } - protected void checkOptions() + protected void validate() throws TaskException { - super.checkOptions(); - if( unused && searchPath == null ) + super.validate(); + if( m_unused && m_searchPath == null ) { throw new TaskException( "'searchpath' element must be set when looking for 'unused' declarations." ); } - if( !unused && searchPath != null ) + if( !m_unused && m_searchPath != null ) { getLogger().warn( "'searchpath' element ignored. 'unused' attribute is disabled." ); } @@ -218,44 +193,5 @@ public class MAudit extends AbstractMetamataTask * } */ } - - protected void setupStreamHandler( final Execute exe ) - throws TaskException - { - // if we didn't specify a file, then use a screen report - if( outFile == null ) - { - exe.setOutput( new LogOutputStream( getLogger(), false ) ); - exe.setError( new LogOutputStream( getLogger(), true ) ); - } - else - { - try - { - //XXX - OutputStream out = new FileOutputStream( outFile ); - //handler = new MAuditStreamHandler( this, out ); - //FIXME: should behave like in Ant1.x - exe.setOutput( out ); - exe.setError( out ); - } - catch( IOException e ) - { - throw new TaskException( "Error", e ); - } - } - } - - /** - * the inner class used to report violation information - * - * @author RT - */ - final static class Violation - { - String error; - int line; - } - } diff --git a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/metamata/MAuditStreamHandler.java b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/metamata/MAuditStreamHandler.java index 18f1b0b2a..c166a44fe 100644 --- a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/metamata/MAuditStreamHandler.java +++ b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/metamata/MAuditStreamHandler.java @@ -7,10 +7,8 @@ */ package org.apache.tools.ant.taskdefs.optional.metamata; -import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; -import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.Writer; @@ -19,6 +17,10 @@ import java.util.Hashtable; import java.util.Iterator; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import org.apache.avalon.framework.logger.AbstractLogEnabled; +import org.apache.myrmidon.api.TaskException; +import org.apache.myrmidon.framework.exec.ExecOutputHandler; import org.apache.tools.ant.taskdefs.exec.ExecuteStreamHandler; import org.apache.tools.ant.util.DOMElementWriter; import org.apache.tools.ant.util.regexp.RegexpMatcher; @@ -43,97 +45,73 @@ import org.w3c.dom.Element; * * @author Stephane Bailliez */ -class MAuditStreamHandler implements ExecuteStreamHandler +class MAuditStreamHandler + extends AbstractLogEnabled + implements ExecuteStreamHandler, ExecOutputHandler { + public void setProcessInputStream( OutputStream os ) + throws IOException + { + } + + public void setProcessErrorStream( InputStream is ) + throws IOException + { + } + + public void setProcessOutputStream( InputStream is ) + throws TaskException, IOException + { + } + + public void start() + throws IOException + { + } /** * this is where the XML output will go, should mostly be a file the caller * is responsible for flushing and closing this stream */ - protected OutputStream xmlOut = null; + private OutputStream m_xmlOut; /** * the multimap. The key in the map is the filepath that caused the audit * error and the value is a vector of MAudit.Violation entries. */ - protected Hashtable auditedFiles = new Hashtable(); - - /** - * reader for stdout - */ - protected BufferedReader br; + private Hashtable m_auditedFiles = new Hashtable(); /** * matcher that will be used to extract the info from the line */ - protected RegexpMatcher matcher; + private RegexpMatcher m_matcher; - protected MAudit task; + private Hashtable m_fileMapping; - MAuditStreamHandler( MAudit task, OutputStream xmlOut ) + MAuditStreamHandler( Hashtable fileMapping, OutputStream xmlOut ) + throws TaskException { - this.task = task; - this.xmlOut = xmlOut; + m_fileMapping = fileMapping; + m_xmlOut = xmlOut; /** * the matcher should be the Oro one. I don't know about the other one */ - matcher = ( new RegexpMatcherFactory() ).newRegexpMatcher(); - matcher.setPattern( MAudit.AUDIT_PATTERN ); + m_matcher = ( new RegexpMatcherFactory() ).newRegexpMatcher(); + m_matcher.setPattern( MAudit.AUDIT_PATTERN ); } - protected static DocumentBuilder getDocumentBuilder() + private static final DocumentBuilder getDocumentBuilder() { try { return DocumentBuilderFactory.newInstance().newDocumentBuilder(); } - catch( Exception exc ) + catch( ParserConfigurationException pce ) { - throw new ExceptionInInitializerError( exc ); + throw new ExceptionInInitializerError( pce ); } } - /** - * Ignore. - * - * @param is The new ProcessErrorStream value - */ - public void setProcessErrorStream( InputStream is ) - { - } - - /** - * Ignore. - * - * @param os The new ProcessInputStream value - */ - public void setProcessInputStream( OutputStream os ) - { - } - - /** - * Set the inputstream - * - * @param is The new ProcessOutputStream value - * @exception IOException Description of Exception - */ - public void setProcessOutputStream( InputStream is ) - throws IOException - { - br = new BufferedReader( new InputStreamReader( is ) ); - } - - /** - * Invokes parseOutput. This will block until the end :-( - * - * @exception IOException Description of Exception - */ - public void start() - throws IOException - { - parseOutput( br ); - } - /** * Pretty dangerous business here. It serializes what was extracted from the * MAudit output and write it to the output. @@ -144,19 +122,18 @@ class MAuditStreamHandler implements ExecuteStreamHandler // this is the only code that could be needed to be overrided Document doc = getDocumentBuilder().newDocument(); Element rootElement = doc.createElement( "classes" ); - Iterator keys = auditedFiles.keys(); - Hashtable filemapping = task.getFileMapping(); - rootElement.setAttribute( "audited", String.valueOf( filemapping.size() ) ); - rootElement.setAttribute( "reported", String.valueOf( auditedFiles.size() ) ); + final Iterator keys = m_auditedFiles.keySet().iterator(); + rootElement.setAttribute( "audited", String.valueOf( m_fileMapping.size() ) ); + rootElement.setAttribute( "reported", String.valueOf( m_auditedFiles.size() ) ); int errors = 0; while( keys.hasNext() ) { String filepath = (String)keys.next(); - ArrayList v = (ArrayList)auditedFiles.get( filepath ); - String fullclassname = (String)filemapping.get( filepath ); + ArrayList v = (ArrayList)m_auditedFiles.get( filepath ); + String fullclassname = (String)m_fileMapping.get( filepath ); if( fullclassname == null ) { - task.getLogger().warn( "Could not find class mapping for " + filepath ); + getLogger().warn( "Could not find class mapping for " + filepath ); continue; } int pos = fullclassname.lastIndexOf( '.' ); @@ -169,10 +146,10 @@ class MAuditStreamHandler implements ExecuteStreamHandler errors += v.size(); for( int i = 0; i < v.size(); i++ ) { - MAudit.Violation violation = (MAudit.Violation)v.get( i ); + Violation violation = (Violation)v.get( i ); Element error = doc.createElement( "violation" ); - error.setAttribute( "line", String.valueOf( violation.line ) ); - error.setAttribute( "message", violation.error ); + error.setAttribute( "line", String.valueOf( violation.getLine() ) ); + error.setAttribute( "message", violation.getError() ); clazz.appendChild( error ); } rootElement.appendChild( clazz ); @@ -180,23 +157,23 @@ class MAuditStreamHandler implements ExecuteStreamHandler rootElement.setAttribute( "violations", String.valueOf( errors ) ); // now write it to the outputstream, not very nice code - if( xmlOut != null ) + if( m_xmlOut != null ) { Writer wri = null; try { - wri = new OutputStreamWriter( xmlOut, "UTF-8" ); + wri = new OutputStreamWriter( m_xmlOut, "UTF-8" ); wri.write( "\n" ); ( new DOMElementWriter() ).write( rootElement, wri, 0, " " ); wri.flush(); } catch( IOException exc ) { - task.getLogger().error( "Unable to write log file" ); + getLogger().error( "Unable to write log file" ); } finally { - if( xmlOut != System.out && xmlOut != System.err ) + if( m_xmlOut != System.out && m_xmlOut != System.err ) { if( wri != null ) { @@ -216,56 +193,63 @@ class MAuditStreamHandler implements ExecuteStreamHandler /** * add a violation entry for the file - * - * @param file The feature to be added to the ViolationEntry attribute - * @param entry The feature to be added to the ViolationEntry attribute */ - protected void addViolationEntry( String file, MAudit.Violation entry ) + protected void addViolationEntry( String file, Violation entry ) { - ArrayList violations = (ArrayList)auditedFiles.get( file ); - // if there is no decl for this file yet, create it. + ArrayList violations = (ArrayList)m_auditedFiles.get( file ); if( violations == null ) { + // if there is no decl for this file yet, create it. violations = new ArrayList(); - auditedFiles.put( file, violations ); + m_auditedFiles.put( file, violations ); } violations.add( entry ); } /** - * read each line and process it - * - * @param br Description of Parameter - * @exception IOException Description of Exception + * Receive notification about the process writing + * to standard error. */ - protected void parseOutput( BufferedReader br ) - throws IOException + public void stderr( String line ) { - String line = null; - while( ( line = br.readLine() ) != null ) - { - processLine( line ); - } } - // we suppose here that there is only one report / line. - // There will obviouslly be a problem if the message is on several lines... - protected void processLine( String line ) + /** + * Receive notification about the process writing + * to standard output. + */ + public void stdout( final String line ) { - ArrayList matches = matcher.getGroups( line ); + // we suppose here that there is only one report / line. + // There will obviouslly be a problem if the message is on several lines... + + final ArrayList matches = getGroups( line ); if( matches != null ) { - String file = (String)matches.get( 1 ); - int lineNum = Integer.parseInt( (String)matches.get( 2 ) ); - String msg = (String)matches.get( 3 ); - addViolationEntry( file, MAudit.createViolation( lineNum, msg ) ); + final String file = (String)matches.get( 1 ); + final int lineNum = Integer.parseInt( (String)matches.get( 2 ) ); + final String msg = (String)matches.get( 3 ); + final Violation violation = new Violation( msg, lineNum ); + addViolationEntry( file, violation ); } else { // this doesn't match..report it as info, it could be // either the copyright, summary or a multiline message (damn !) - task.getLogger().info( line ); + getLogger().info( line ); } } + private ArrayList getGroups( final String line ) + { + try + { + return m_matcher.getGroups( line ); + } + catch( final TaskException te ) + { + getLogger().error( "Failed to process matcher", te ); + return new ArrayList(); + } + } } diff --git a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/metamata/MMetrics.java b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/metamata/MMetrics.java index c12593785..ef8d4bc4a 100644 --- a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/metamata/MMetrics.java +++ b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/metamata/MMetrics.java @@ -13,9 +13,7 @@ import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import org.apache.myrmidon.api.TaskException; -import org.apache.tools.ant.taskdefs.exec.Execute; import org.apache.tools.ant.taskdefs.exec.ExecuteStreamHandler; -import org.apache.tools.ant.taskdefs.exec.LogOutputStream; import org.apache.tools.ant.types.Path; /** @@ -119,23 +117,24 @@ public class MMetrics extends AbstractMetamataTask } protected ArrayList getOptions() + throws TaskException { ArrayList options = new ArrayList( 512 ); // there is a bug in Metamata 2.0 build 37. The sourcepath argument does // not work. So we will use the sourcepath prepended to classpath. (order // is important since Metamata looks at .class and .java) - if( sourcePath != null ) + if( getSourcePath() != null ) { - sourcePath.append( classPath );// srcpath is prepended - classPath = sourcePath; - sourcePath = null;// prevent from using -sourcepath + getSourcePath().append( getClassPath() );// srcpath is prepended + setClassPath( getSourcePath() ); + setSourcePath( null );// prevent from using -sourcepath } // don't forget to modify the pattern if you change the options reporting - if( classPath != null ) + if( getClassPath() != null ) { options.add( "-classpath" ); - options.add( classPath ); + options.add( getClassPath() ); } options.add( "-output" ); options.add( tmpFile.toString() ); @@ -155,13 +154,13 @@ public class MMetrics extends AbstractMetamataTask options.add( "/" ); // directories - String[] dirs = path.list(); + final String[] dirs = path.list(); for( int i = 0; i < dirs.length; i++ ) { options.add( dirs[ i ] ); } // files next. - addAllArrayList( options, includedFiles.keySet().iterator() ); + addAllArrayList( options, getIncludedFiles().keySet().iterator() ); return options; } @@ -169,10 +168,10 @@ public class MMetrics extends AbstractMetamataTask // check for existing options and outfile, all other are optional - protected void checkOptions() + protected void validate() throws TaskException { - super.checkOptions(); + super.validate(); if( !"files".equals( granularity ) && !"methods".equals( granularity ) && !"types".equals( granularity ) ) @@ -183,12 +182,12 @@ public class MMetrics extends AbstractMetamataTask { throw new TaskException( "Output XML file must be set via 'tofile' attribute." ); } - if( path == null && fileSets.size() == 0 ) + if( path == null && getFileSets().size() == 0 ) { throw new TaskException( "Must set either paths (path element) or files (fileset element)" ); } // I don't accept dirs and files at the same time, I cannot recognize the semantic in the result - if( path != null && fileSets.size() > 0 ) + if( path != null && getFileSets().size() > 0 ) { throw new TaskException( "Cannot set paths (path element) and files (fileset element) at the same time" ); } @@ -216,23 +215,10 @@ public class MMetrics extends AbstractMetamataTask } } - /** - * if the report is transform via a temporary txt file we should use a a - * normal logger here, otherwise we could use the metrics handler directly - * to capture and transform the output on stdout to XML. - * - * @return Description of the Returned Value - */ - protected void setupStreamHandler( final Execute exe ) - { - exe.setOutput( new LogOutputStream( getLogger(), false ) ); - exe.setError( new LogOutputStream( getLogger(), false ) ); - } - - protected void execute0( ExecuteStreamHandler handler ) + protected void execute0() throws TaskException { - super.execute0( handler ); + super.execute0(); transformFile(); } @@ -260,7 +246,8 @@ public class MMetrics extends AbstractMetamataTask try { xmlStream = new FileOutputStream( outFile ); - ExecuteStreamHandler xmlHandler = new MMetricsStreamHandler( this, xmlStream ); + ExecuteStreamHandler xmlHandler = new MMetricsStreamHandler( xmlStream ); + setupLogger( xmlHandler ); xmlHandler.setProcessOutputStream( tmpStream ); xmlHandler.start(); xmlHandler.stop(); @@ -293,5 +280,4 @@ public class MMetrics extends AbstractMetamataTask } } } - } diff --git a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/metamata/MMetricsStreamHandler.java b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/metamata/MMetricsStreamHandler.java index ffaa776d5..1aa97b47a 100644 --- a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/metamata/MMetricsStreamHandler.java +++ b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/metamata/MMetricsStreamHandler.java @@ -26,7 +26,7 @@ import javax.xml.transform.TransformerFactory; import javax.xml.transform.sax.SAXTransformerFactory; import javax.xml.transform.sax.TransformerHandler; import javax.xml.transform.stream.StreamResult; -import org.apache.tools.ant.Task; +import org.apache.avalon.framework.logger.AbstractLogEnabled; import org.apache.tools.ant.taskdefs.exec.ExecuteStreamHandler; import org.xml.sax.Attributes; import org.xml.sax.SAXException; @@ -42,9 +42,10 @@ import org.xml.sax.helpers.AttributesImpl; * * @author Stephane Bailliez */ -public class MMetricsStreamHandler implements ExecuteStreamHandler +public class MMetricsStreamHandler + extends AbstractLogEnabled + implements ExecuteStreamHandler { - /** * CLASS construct, it should be named something like 'MyClass' */ @@ -87,26 +88,14 @@ public class MMetricsStreamHandler implements ExecuteStreamHandler */ protected InputStream metricsOutput; - /** - * the task - */ - protected Task task; - /** * this is where the XML output will go, should mostly be a file the caller * is responsible for flushing and closing this stream */ protected OutputStream xmlOutputStream; - /** - * initialize this handler - * - * @param task Description of Parameter - * @param xmlOut Description of Parameter - */ - MMetricsStreamHandler( Task task, OutputStream xmlOut ) + MMetricsStreamHandler( OutputStream xmlOut ) { - this.task = task; this.xmlOutputStream = xmlOut; } @@ -322,7 +311,7 @@ public class MMetricsStreamHandler implements ExecuteStreamHandler { e.printStackTrace(); // invalid lines are sent to the output as information, it might be anything, - task.getLogger().info( line ); + getLogger().info( line ); } } diff --git a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/metamata/MParse.java b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/metamata/MParse.java index a93b21433..77e6e00ba 100644 --- a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/metamata/MParse.java +++ b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/metamata/MParse.java @@ -15,10 +15,7 @@ import java.util.ArrayList; import java.util.Random; import org.apache.myrmidon.api.TaskException; import org.apache.tools.ant.Task; -import org.apache.tools.ant.taskdefs.exec.Execute; -import org.apache.tools.ant.taskdefs.exec.ExecuteStreamHandler; -import org.apache.tools.ant.taskdefs.exec.LogOutputStream; -import org.apache.tools.ant.taskdefs.exec.LogStreamHandler; +import org.apache.tools.ant.taskdefs.exec.Execute2; import org.apache.tools.ant.types.Argument; import org.apache.tools.ant.types.CommandlineJava; import org.apache.tools.ant.types.Path; @@ -31,24 +28,24 @@ import org.apache.tools.ant.types.Path; * * @author Stephane Bailliez */ -public class MParse extends Task +public class MParse + extends Task { - - private Path classpath = null; - private Path sourcepath = null; - private File metahome = null; - private File target = null; - private boolean verbose = false; - private boolean debugparser = false; - private boolean debugscanner = false; - private boolean cleanup = false; - private CommandlineJava cmdl = new CommandlineJava(); - private File optionsFile = null; + private Path m_classpath; + private Path m_sourcepath; + private File m_metahome; + private File m_target; + private boolean m_verbose; + private boolean m_debugparser; + private boolean m_debugscanner; + private boolean m_cleanup; + private CommandlineJava m_cmdl = new CommandlineJava(); + private File m_optionsFile; public MParse() { - cmdl.setVm( "java" ); - cmdl.setClassname( "com.metamata.jj.MParse" ); + m_cmdl.setVm( "java" ); + m_cmdl.setClassname( "com.metamata.jj.MParse" ); } /** @@ -71,7 +68,7 @@ public class MParse extends Task */ public void setCleanup( boolean value ) { - cleanup = value; + m_cleanup = value; } /** @@ -81,7 +78,7 @@ public class MParse extends Task */ public void setDebugparser( boolean flag ) { - debugparser = flag; + m_debugparser = flag; } /** @@ -91,7 +88,7 @@ public class MParse extends Task */ public void setDebugscanner( boolean flag ) { - debugscanner = flag; + m_debugscanner = flag; } /** @@ -111,7 +108,7 @@ public class MParse extends Task */ public void setMetamatahome( File metamatahome ) { - this.metahome = metamatahome; + this.m_metahome = metamatahome; } /** @@ -121,7 +118,7 @@ public class MParse extends Task */ public void setTarget( File target ) { - this.target = target; + this.m_target = target; } /** @@ -131,7 +128,7 @@ public class MParse extends Task */ public void setVerbose( boolean flag ) { - verbose = flag; + m_verbose = flag; } /** @@ -141,11 +138,11 @@ public class MParse extends Task */ public Path createClasspath() { - if( classpath == null ) + if( m_classpath == null ) { - classpath = new Path(); + m_classpath = new Path(); } - return classpath; + return m_classpath; } /** @@ -155,7 +152,7 @@ public class MParse extends Task */ public Argument createJvmarg() { - return cmdl.createVmArgument(); + return m_cmdl.createVmArgument(); } /** @@ -165,11 +162,11 @@ public class MParse extends Task */ public Path createSourcepath() { - if( sourcepath == null ) + if( m_sourcepath == null ) { - sourcepath = new Path(); + m_sourcepath = new Path(); } - return sourcepath; + return m_sourcepath; } /** @@ -183,8 +180,7 @@ public class MParse extends Task try { setUp(); - ExecuteStreamHandler handler = createStreamHandler(); - _execute( handler ); + doExecute(); } finally { @@ -204,22 +200,22 @@ public class MParse extends Task // set the classpath as the jar files File[] jars = getMetamataLibs(); - final Path classPath = cmdl.createClasspath( getProject() ); + final Path classPath = m_cmdl.createClasspath(); for( int i = 0; i < jars.length; i++ ) { classPath.createPathElement().setLocation( jars[ i ] ); } // set the metamata.home property - final Argument vmArgs = cmdl.createVmArgument(); - vmArgs.setValue( "-Dmetamata.home=" + metahome.getAbsolutePath() ); + final Argument vmArgs = m_cmdl.createVmArgument(); + vmArgs.setValue( "-Dmetamata.home=" + m_metahome.getAbsolutePath() ); // write all the options to a temp file and use it ro run the process String[] options = getOptions(); - optionsFile = createTmpFile(); - generateOptionsFile( optionsFile, options ); - Argument args = cmdl.createArgument(); - args.setLine( "-arguments " + optionsFile.getAbsolutePath() ); + m_optionsFile = createTmpFile(); + generateOptionsFile( m_optionsFile, options ); + Argument args = m_cmdl.createArgument(); + args.setLine( "-arguments " + m_optionsFile.getAbsolutePath() ); } /** @@ -231,13 +227,11 @@ public class MParse extends Task */ protected File[] getMetamataLibs() { - ArrayList files = new ArrayList(); - files.add( new File( metahome, "lib/metamata.jar" ) ); - files.add( new File( metahome, "bin/lib/JavaCC.zip" ) ); + final ArrayList files = new ArrayList(); + files.add( new File( m_metahome, "lib/metamata.jar" ) ); + files.add( new File( m_metahome, "bin/lib/JavaCC.zip" ) ); - File[] array = new File[ files.size() ]; - files.copyInto( array ); - return array; + return (File[])files.toArray( new File[ files.size() ] ); } /** @@ -248,62 +242,58 @@ public class MParse extends Task protected String[] getOptions() { ArrayList options = new ArrayList(); - if( verbose ) + if( m_verbose ) { options.add( "-verbose" ); } - if( debugscanner ) + if( m_debugscanner ) { options.add( "-ds" ); } - if( debugparser ) + if( m_debugparser ) { options.add( "-dp" ); } - if( classpath != null ) + if( m_classpath != null ) { options.add( "-classpath" ); - options.add( classpath.toString() ); + options.add( m_classpath.toString() ); } - if( sourcepath != null ) + if( m_sourcepath != null ) { options.add( "-sourcepath" ); - options.add( sourcepath.toString() ); + options.add( m_sourcepath.toString() ); } - options.add( target.getAbsolutePath() ); + options.add( m_target.getAbsolutePath() ); - String[] array = new String[ options.size() ]; - options.copyInto( array ); - return array; + return (String[])options.toArray( new String[ options.size() ] ); } /** * execute the process with a specific handler - * - * @param handler Description of Parameter - * @exception TaskException Description of Exception */ - protected void _execute( ExecuteStreamHandler handler ) + protected void doExecute() throws TaskException { // target has been checked as a .jj, see if there is a matching // java file and if it is needed to run to process the grammar - String pathname = target.getAbsolutePath(); + String pathname = m_target.getAbsolutePath(); int pos = pathname.length() - ".jj".length(); pathname = pathname.substring( 0, pos ) + ".java"; File javaFile = new File( pathname ); - if( javaFile.exists() && target.lastModified() < javaFile.lastModified() ) + if( javaFile.exists() && m_target.lastModified() < javaFile.lastModified() ) { - getLogger().info( "Target is already build - skipping (" + target + ")" ); + getLogger().info( "Target is already build - skipping (" + m_target + ")" ); return; } - final Execute process = new Execute( handler ); - getLogger().debug( cmdl.toString() ); - process.setCommandline( cmdl.getCommandline() ); + final Execute2 exe = new Execute2(); + setupLogger( exe ); + getLogger().debug( m_cmdl.toString() ); + exe.setCommandline( m_cmdl.getCommandline() ); try { - if( process.execute() != 0 ) + if( exe.execute() != 0 ) { throw new TaskException( "Metamata task failed." ); } @@ -323,11 +313,11 @@ public class MParse extends Task throws TaskException { // check that the home is ok. - if( metahome == null || !metahome.exists() ) + if( m_metahome == null || !m_metahome.exists() ) { throw new TaskException( "'metamatahome' must point to Metamata home directory." ); } - metahome = resolveFile( metahome.getPath() ); + m_metahome = resolveFile( m_metahome.getPath() ); // check that the needed jar exists. File[] jars = getMetamataLibs(); @@ -340,11 +330,11 @@ public class MParse extends Task } // check that the target is ok and resolve it. - if( target == null || !target.isFile() || !target.getName().endsWith( ".jj" ) ) + if( m_target == null || !m_target.isFile() || !m_target.getName().endsWith( ".jj" ) ) { - throw new TaskException( "Invalid target: " + target ); + throw new TaskException( "Invalid target: " + m_target ); } - target = resolveFile( target.getPath() ); + m_target = resolveFile( m_target.getPath() ); } /** @@ -352,17 +342,17 @@ public class MParse extends Task */ protected void cleanUp() { - if( optionsFile != null ) + if( m_optionsFile != null ) { - optionsFile.delete(); - optionsFile = null; + m_optionsFile.delete(); + m_optionsFile = null; } - if( cleanup ) + if( m_cleanup ) { - String name = target.getName(); + String name = m_target.getName(); int pos = name.length() - ".jj".length(); name = "__jj" + name.substring( 0, pos ) + ".sunjj"; - final File sunjj = new File( target.getParent(), name ); + final File sunjj = new File( m_target.getParent(), name ); if( sunjj.exists() ) { getLogger().info( "Removing stale file: " + sunjj.getName() ); @@ -371,18 +361,6 @@ public class MParse extends Task } } - /** - * return the default stream handler for this task - * - * @return Description of the Returned Value - */ - protected ExecuteStreamHandler createStreamHandler() - { - final LogOutputStream output = new LogOutputStream( getLogger(), false ); - final LogOutputStream error = new LogOutputStream( getLogger(), false ); - return new LogStreamHandler( output, error ); - } - /** * write all options to a file with one option / line * diff --git a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/metamata/Violation.java b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/metamata/Violation.java new file mode 100644 index 000000000..58fba32f3 --- /dev/null +++ b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/metamata/Violation.java @@ -0,0 +1,33 @@ +/* + * Copyright (C) The Apache Software Foundation. All rights reserved. + * + * This software is published under the terms of the Apache Software License + * version 1.1, a copy of which has been included with this distribution in + * the LICENSE.txt file. + */ +package org.apache.tools.ant.taskdefs.optional.metamata; + +/** + * the class used to report violation information + */ +final class Violation +{ + private final String m_error; + private final int m_line; + + public Violation( final String error, final int line ) + { + m_error = error; + m_line = line; + } + + protected String getError() + { + return m_error; + } + + protected int getLine() + { + return m_line; + } +} diff --git a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/optional/metamata/AbstractMetamataTask.java b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/optional/metamata/AbstractMetamataTask.java index f7e19df11..9d74e018f 100644 --- a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/optional/metamata/AbstractMetamataTask.java +++ b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/optional/metamata/AbstractMetamataTask.java @@ -15,10 +15,10 @@ import java.util.ArrayList; import java.util.Hashtable; import java.util.Iterator; import java.util.Random; +import org.apache.myrmidon.api.AbstractTask; import org.apache.myrmidon.api.TaskException; import org.apache.tools.ant.DirectoryScanner; -import org.apache.tools.ant.Task; -import org.apache.tools.ant.taskdefs.exec.Execute; +import org.apache.tools.ant.taskdefs.exec.Execute2; import org.apache.tools.ant.types.Argument; import org.apache.tools.ant.types.CommandlineJava; import org.apache.tools.ant.types.FileSet; @@ -31,48 +31,46 @@ import org.apache.tools.ant.types.Path; * * @author Stephane Bailliez */ -public abstract class AbstractMetamataTask extends Task +public abstract class AbstractMetamataTask + extends AbstractTask { - - //--------------------------- ATTRIBUTES ----------------------------------- - /** * The user classpath to be provided. It matches the -classpath of the * command line. The classpath must includes both the .class and * the .java files for accurate audit. */ - protected Path classPath = null; + private Path m_classPath; /** * the path to the source file */ - protected Path sourcePath = null; + private Path m_sourcePath; /** * Metamata home directory. It will be passed as a metamata.home * property and should normally matches the environment property * META_HOME set by the Metamata installer. */ - protected File metamataHome = null; + private File m_metamataHome; /** * the command line used to run MAudit */ - protected CommandlineJava cmdl = new CommandlineJava(); + private CommandlineJava m_cmdl = new CommandlineJava(); /** * the set of files to be audited */ - protected ArrayList fileSets = new ArrayList(); + private ArrayList m_fileSets = new ArrayList(); /** * the options file where are stored the command line options */ - protected File optionsFile = null; + private File m_optionsFile; // this is used to keep track of which files were included. It will // be set when calling scanFileSets(); - protected Hashtable includedFiles = null; + private Hashtable m_includedFiles; public AbstractMetamataTask() { @@ -85,8 +83,8 @@ public abstract class AbstractMetamataTask extends Task */ protected AbstractMetamataTask( String className ) { - cmdl.setVm( "java" ); - cmdl.setClassname( className ); + m_cmdl.setVm( "java" ); + m_cmdl.setClassname( className ); } /** @@ -123,60 +121,50 @@ public abstract class AbstractMetamataTask extends Task /** * the metamata.home property to run all tasks. - * - * @param metamataHome The new Metamatahome value */ public void setMetamatahome( final File metamataHome ) { - this.metamataHome = metamataHome; + this.m_metamataHome = metamataHome; } /** * The java files or directory to be audited - * - * @param fs The feature to be added to the FileSet attribute */ - public void addFileSet( FileSet fs ) + public void addFileSet( final FileSet fileSet ) { - fileSets.add( fs ); + m_fileSets.add( fileSet ); } /** * user classpath - * - * @return Description of the Returned Value */ public Path createClasspath() { - if( classPath == null ) + if( m_classPath == null ) { - classPath = new Path(); + m_classPath = new Path(); } - return classPath; + return m_classPath; } /** * Creates a nested jvmarg element. - * - * @return Description of the Returned Value */ public Argument createJvmarg() { - return cmdl.createVmArgument(); + return m_cmdl.createVmArgument(); } /** * create the source path for this task - * - * @return Description of the Returned Value */ public Path createSourcepath() { - if( sourcePath == null ) + if( m_sourcePath == null ) { - sourcePath = new Path(); + m_sourcePath = new Path(); } - return sourcePath; + return m_sourcePath; } /** @@ -198,44 +186,37 @@ public abstract class AbstractMetamataTask extends Task } } - //--------------------- PRIVATE/PROTECTED METHODS -------------------------- - /** * check the options and build the command line - * - * @exception TaskException Description of Exception */ protected void setUp() throws TaskException { - checkOptions(); + validate(); // set the classpath as the jar file - File jar = getMetamataJar( metamataHome ); - final Path classPath = cmdl.createClasspath( getProject() ); + File jar = getMetamataJar( m_metamataHome ); + final Path classPath = m_cmdl.createClasspath(); classPath.createPathElement().setLocation( jar ); // set the metamata.home property - final Argument vmArgs = cmdl.createVmArgument(); - vmArgs.setValue( "-Dmetamata.home=" + metamataHome.getAbsolutePath() ); + final Argument vmArgs = m_cmdl.createVmArgument(); + vmArgs.setValue( "-Dmetamata.home=" + m_metamataHome.getAbsolutePath() ); // retrieve all the files we want to scan - includedFiles = scanFileSets(); - getLogger().debug( includedFiles.size() + " files added for audit" ); + m_includedFiles = scanFileSets(); + getLogger().debug( m_includedFiles.size() + " files added for audit" ); // write all the options to a temp file and use it ro run the process ArrayList options = getOptions(); - optionsFile = createTmpFile(); - generateOptionsFile( optionsFile, options ); - Argument args = cmdl.createArgument(); - args.setLine( "-arguments " + optionsFile.getAbsolutePath() ); + m_optionsFile = createTmpFile(); + generateOptionsFile( m_optionsFile, options ); + Argument args = m_cmdl.createArgument(); + args.setLine( "-arguments " + m_optionsFile.getAbsolutePath() ); } /** * return the location of the jar file used to run - * - * @param home Description of Parameter - * @return The MetamataJar value */ protected final File getMetamataJar( File home ) { @@ -244,31 +225,30 @@ public abstract class AbstractMetamataTask extends Task protected Hashtable getFileMapping() { - return includedFiles; + return m_includedFiles; } /** * return all options of the command line as string elements - * - * @return The Options value */ - protected abstract ArrayList getOptions(); + protected abstract ArrayList getOptions() + throws TaskException; /** * validate options set * * @exception TaskException Description of Exception */ - protected void checkOptions() + protected void validate() throws TaskException { // do some validation first - if( metamataHome == null || !metamataHome.exists() ) + if( m_metamataHome == null || !m_metamataHome.exists() ) { throw new TaskException( "'metamatahome' must point to Metamata home directory." ); } - metamataHome = resolveFile( metamataHome.getPath() ); - File jar = getMetamataJar( metamataHome ); + m_metamataHome = resolveFile( m_metamataHome.getPath() ); + File jar = getMetamataJar( m_metamataHome ); if( !jar.exists() ) { throw new TaskException( jar + " does not exist. Check your metamata installation." ); @@ -279,22 +259,15 @@ public abstract class AbstractMetamataTask extends Task * clean up all the mess that we did with temporary objects */ protected void cleanUp() + throws TaskException { - if( optionsFile != null ) + if( m_optionsFile != null ) { - optionsFile.delete(); - optionsFile = null; + m_optionsFile.delete(); + m_optionsFile = null; } } - /** - * create a stream handler that will be used to get the output since - * metamata tools do not report with convenient files such as XML. - * - * @return Description of the Returned Value - */ - protected abstract void setupStreamHandler( Execute exe ); - /** * execute the process with a specific handler * @@ -304,13 +277,13 @@ public abstract class AbstractMetamataTask extends Task protected void execute0() throws TaskException { - final Execute exe = new Execute(); - setupStreamHandler( exe ); - getLogger().debug( cmdl.toString() ); - exe.setCommandline( cmdl.getCommandline() ); + final Execute2 exe = new Execute2(); + setupLogger( exe ); + getLogger().debug( m_cmdl.toString() ); + exe.setCommandline( m_cmdl.getCommandline() ); try { - if( exe.execute() != 0 ) + if( 0 != exe.execute() ) { throw new TaskException( "Metamata task failed." ); } @@ -360,11 +333,12 @@ public abstract class AbstractMetamataTask extends Task * audited. */ protected Hashtable scanFileSets() + throws TaskException { Hashtable files = new Hashtable(); - for( int i = 0; i < fileSets.size(); i++ ) + for( int i = 0; i < m_fileSets.size(); i++ ) { - FileSet fs = (FileSet)fileSets.get( i ); + FileSet fs = (FileSet)m_fileSets.get( i ); DirectoryScanner ds = fs.getDirectoryScanner(); ds.scan(); String[] f = ds.getIncludedFiles(); @@ -385,4 +359,34 @@ public abstract class AbstractMetamataTask extends Task return files; } + protected ArrayList getFileSets() + { + return m_fileSets; + } + + protected Hashtable getIncludedFiles() + { + return m_includedFiles; + } + + protected Path getClassPath() + { + return m_classPath; + } + + protected void setClassPath( Path classPath ) + { + m_classPath = classPath; + } + + protected Path getSourcePath() + { + return m_sourcePath; + } + + protected void setSourcePath( Path sourcePath ) + { + m_sourcePath = sourcePath; + } + } diff --git a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/optional/metamata/MAudit.java b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/optional/metamata/MAudit.java index f52976eaa..7f8384c8b 100644 --- a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/optional/metamata/MAudit.java +++ b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/optional/metamata/MAudit.java @@ -8,13 +8,8 @@ package org.apache.tools.ant.taskdefs.optional.metamata; import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.OutputStream; import java.util.ArrayList; import org.apache.myrmidon.api.TaskException; -import org.apache.tools.ant.taskdefs.exec.Execute; -import org.apache.tools.ant.taskdefs.exec.LogOutputStream; import org.apache.tools.ant.types.Path; /** @@ -32,9 +27,9 @@ import org.apache.tools.ant.types.Path; * * @author Stephane Bailliez */ -public class MAudit extends AbstractMetamataTask +public class MAudit + extends AbstractMetamataTask { - /* * As of Metamata 2.0, the command line of MAudit is as follows: * Usage @@ -72,15 +67,11 @@ public class MAudit extends AbstractMetamataTask // (?:file:)?((?#filepath).+):((?#line)\\d+)\\s*:\\s+((?#message).*) final static String AUDIT_PATTERN = "(?:file:)?(.+):(\\d+)\\s*:\\s+(.*)"; - protected File outFile = null; - - protected Path searchPath = null; - - protected boolean fix = false; - - protected boolean list = false; - - protected boolean unused = false; + private File m_outFile; + private Path m_searchPath; + private boolean m_fix; + private boolean m_list; + private boolean m_unused; /** * default constructor @@ -90,78 +81,62 @@ public class MAudit extends AbstractMetamataTask super( "com.metamata.gui.rc.MAudit" ); } - /** - * handy factory to create a violation - * - * @param line Description of Parameter - * @param msg Description of Parameter - * @return Description of the Returned Value - */ - final static Violation createViolation( int line, String msg ) + public void setFix( final boolean fix ) { - Violation violation = new Violation(); - violation.line = line; - violation.error = msg; - return violation; + m_fix = fix; } - public void setFix( boolean flag ) + public void setList( final boolean list ) { - this.fix = flag; - } - - public void setList( boolean flag ) - { - this.list = flag; + m_list = list; } /** * set the destination file which should be an xml file - * - * @param outFile The new Tofile value */ - public void setTofile( File outFile ) + public void setTofile( final File outFile ) { - this.outFile = outFile; + m_outFile = outFile; } - public void setUnused( boolean flag ) + public void setUnused( final boolean unused ) { - this.unused = flag; + m_unused = unused; } public Path createSearchpath() { - if( searchPath == null ) + if( m_searchPath == null ) { - searchPath = new Path(); + m_searchPath = new Path(); } - return searchPath; + return m_searchPath; } protected ArrayList getOptions() + throws TaskException { ArrayList options = new ArrayList( 512 ); // there is a bug in Metamata 2.0 build 37. The sourcepath argument does // not work. So we will use the sourcepath prepended to classpath. (order // is important since Metamata looks at .class and .java) - if( sourcePath != null ) + if( getSourcePath() != null ) { - sourcePath.append( classPath );// srcpath is prepended - classPath = sourcePath; - sourcePath = null;// prevent from using -sourcepath + getSourcePath().append( getClassPath() );// srcpath is prepended + setClassPath( getSourcePath() ); + setSourcePath( null );// prevent from using -sourcepath } // don't forget to modify the pattern if you change the options reporting - if( classPath != null ) + if( getClassPath() != null ) { options.add( "-classpath" ); - options.add( classPath.toString() ); + options.add( getClassPath().toString() ); } // suppress copyright msg when running, we will let it so that this // will be the only output to the console if in xml mode // options.add("-quiet"); - if( fix ) + if( m_fix ) { options.add( "-fix" ); } @@ -170,34 +145,34 @@ public class MAudit extends AbstractMetamataTask // generate .maudit files much more detailed than the report // I don't like it very much, I think it could be interesting // to get all .maudit files and include them in the XML. - if( list ) + if( m_list ) { options.add( "-list" ); } - if( sourcePath != null ) + if( getSourcePath() != null ) { options.add( "-sourcepath" ); - options.add( sourcePath.toString() ); + options.add( getSourcePath().toString() ); } - if( unused ) + if( m_unused ) { options.add( "-unused" ); - options.add( searchPath.toString() ); + options.add( m_searchPath.toString() ); } - addAllArrayList( options, includedFiles.keySet().iterator() ); + addAllArrayList( options, getIncludedFiles().keySet().iterator() ); return options; } - protected void checkOptions() + protected void validate() throws TaskException { - super.checkOptions(); - if( unused && searchPath == null ) + super.validate(); + if( m_unused && m_searchPath == null ) { throw new TaskException( "'searchpath' element must be set when looking for 'unused' declarations." ); } - if( !unused && searchPath != null ) + if( !m_unused && m_searchPath != null ) { getLogger().warn( "'searchpath' element ignored. 'unused' attribute is disabled." ); } @@ -218,44 +193,5 @@ public class MAudit extends AbstractMetamataTask * } */ } - - protected void setupStreamHandler( final Execute exe ) - throws TaskException - { - // if we didn't specify a file, then use a screen report - if( outFile == null ) - { - exe.setOutput( new LogOutputStream( getLogger(), false ) ); - exe.setError( new LogOutputStream( getLogger(), true ) ); - } - else - { - try - { - //XXX - OutputStream out = new FileOutputStream( outFile ); - //handler = new MAuditStreamHandler( this, out ); - //FIXME: should behave like in Ant1.x - exe.setOutput( out ); - exe.setError( out ); - } - catch( IOException e ) - { - throw new TaskException( "Error", e ); - } - } - } - - /** - * the inner class used to report violation information - * - * @author RT - */ - final static class Violation - { - String error; - int line; - } - } diff --git a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/optional/metamata/MAuditStreamHandler.java b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/optional/metamata/MAuditStreamHandler.java index 18f1b0b2a..c166a44fe 100644 --- a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/optional/metamata/MAuditStreamHandler.java +++ b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/optional/metamata/MAuditStreamHandler.java @@ -7,10 +7,8 @@ */ package org.apache.tools.ant.taskdefs.optional.metamata; -import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; -import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.Writer; @@ -19,6 +17,10 @@ import java.util.Hashtable; import java.util.Iterator; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import org.apache.avalon.framework.logger.AbstractLogEnabled; +import org.apache.myrmidon.api.TaskException; +import org.apache.myrmidon.framework.exec.ExecOutputHandler; import org.apache.tools.ant.taskdefs.exec.ExecuteStreamHandler; import org.apache.tools.ant.util.DOMElementWriter; import org.apache.tools.ant.util.regexp.RegexpMatcher; @@ -43,97 +45,73 @@ import org.w3c.dom.Element; * * @author Stephane Bailliez */ -class MAuditStreamHandler implements ExecuteStreamHandler +class MAuditStreamHandler + extends AbstractLogEnabled + implements ExecuteStreamHandler, ExecOutputHandler { + public void setProcessInputStream( OutputStream os ) + throws IOException + { + } + + public void setProcessErrorStream( InputStream is ) + throws IOException + { + } + + public void setProcessOutputStream( InputStream is ) + throws TaskException, IOException + { + } + + public void start() + throws IOException + { + } /** * this is where the XML output will go, should mostly be a file the caller * is responsible for flushing and closing this stream */ - protected OutputStream xmlOut = null; + private OutputStream m_xmlOut; /** * the multimap. The key in the map is the filepath that caused the audit * error and the value is a vector of MAudit.Violation entries. */ - protected Hashtable auditedFiles = new Hashtable(); - - /** - * reader for stdout - */ - protected BufferedReader br; + private Hashtable m_auditedFiles = new Hashtable(); /** * matcher that will be used to extract the info from the line */ - protected RegexpMatcher matcher; + private RegexpMatcher m_matcher; - protected MAudit task; + private Hashtable m_fileMapping; - MAuditStreamHandler( MAudit task, OutputStream xmlOut ) + MAuditStreamHandler( Hashtable fileMapping, OutputStream xmlOut ) + throws TaskException { - this.task = task; - this.xmlOut = xmlOut; + m_fileMapping = fileMapping; + m_xmlOut = xmlOut; /** * the matcher should be the Oro one. I don't know about the other one */ - matcher = ( new RegexpMatcherFactory() ).newRegexpMatcher(); - matcher.setPattern( MAudit.AUDIT_PATTERN ); + m_matcher = ( new RegexpMatcherFactory() ).newRegexpMatcher(); + m_matcher.setPattern( MAudit.AUDIT_PATTERN ); } - protected static DocumentBuilder getDocumentBuilder() + private static final DocumentBuilder getDocumentBuilder() { try { return DocumentBuilderFactory.newInstance().newDocumentBuilder(); } - catch( Exception exc ) + catch( ParserConfigurationException pce ) { - throw new ExceptionInInitializerError( exc ); + throw new ExceptionInInitializerError( pce ); } } - /** - * Ignore. - * - * @param is The new ProcessErrorStream value - */ - public void setProcessErrorStream( InputStream is ) - { - } - - /** - * Ignore. - * - * @param os The new ProcessInputStream value - */ - public void setProcessInputStream( OutputStream os ) - { - } - - /** - * Set the inputstream - * - * @param is The new ProcessOutputStream value - * @exception IOException Description of Exception - */ - public void setProcessOutputStream( InputStream is ) - throws IOException - { - br = new BufferedReader( new InputStreamReader( is ) ); - } - - /** - * Invokes parseOutput. This will block until the end :-( - * - * @exception IOException Description of Exception - */ - public void start() - throws IOException - { - parseOutput( br ); - } - /** * Pretty dangerous business here. It serializes what was extracted from the * MAudit output and write it to the output. @@ -144,19 +122,18 @@ class MAuditStreamHandler implements ExecuteStreamHandler // this is the only code that could be needed to be overrided Document doc = getDocumentBuilder().newDocument(); Element rootElement = doc.createElement( "classes" ); - Iterator keys = auditedFiles.keys(); - Hashtable filemapping = task.getFileMapping(); - rootElement.setAttribute( "audited", String.valueOf( filemapping.size() ) ); - rootElement.setAttribute( "reported", String.valueOf( auditedFiles.size() ) ); + final Iterator keys = m_auditedFiles.keySet().iterator(); + rootElement.setAttribute( "audited", String.valueOf( m_fileMapping.size() ) ); + rootElement.setAttribute( "reported", String.valueOf( m_auditedFiles.size() ) ); int errors = 0; while( keys.hasNext() ) { String filepath = (String)keys.next(); - ArrayList v = (ArrayList)auditedFiles.get( filepath ); - String fullclassname = (String)filemapping.get( filepath ); + ArrayList v = (ArrayList)m_auditedFiles.get( filepath ); + String fullclassname = (String)m_fileMapping.get( filepath ); if( fullclassname == null ) { - task.getLogger().warn( "Could not find class mapping for " + filepath ); + getLogger().warn( "Could not find class mapping for " + filepath ); continue; } int pos = fullclassname.lastIndexOf( '.' ); @@ -169,10 +146,10 @@ class MAuditStreamHandler implements ExecuteStreamHandler errors += v.size(); for( int i = 0; i < v.size(); i++ ) { - MAudit.Violation violation = (MAudit.Violation)v.get( i ); + Violation violation = (Violation)v.get( i ); Element error = doc.createElement( "violation" ); - error.setAttribute( "line", String.valueOf( violation.line ) ); - error.setAttribute( "message", violation.error ); + error.setAttribute( "line", String.valueOf( violation.getLine() ) ); + error.setAttribute( "message", violation.getError() ); clazz.appendChild( error ); } rootElement.appendChild( clazz ); @@ -180,23 +157,23 @@ class MAuditStreamHandler implements ExecuteStreamHandler rootElement.setAttribute( "violations", String.valueOf( errors ) ); // now write it to the outputstream, not very nice code - if( xmlOut != null ) + if( m_xmlOut != null ) { Writer wri = null; try { - wri = new OutputStreamWriter( xmlOut, "UTF-8" ); + wri = new OutputStreamWriter( m_xmlOut, "UTF-8" ); wri.write( "\n" ); ( new DOMElementWriter() ).write( rootElement, wri, 0, " " ); wri.flush(); } catch( IOException exc ) { - task.getLogger().error( "Unable to write log file" ); + getLogger().error( "Unable to write log file" ); } finally { - if( xmlOut != System.out && xmlOut != System.err ) + if( m_xmlOut != System.out && m_xmlOut != System.err ) { if( wri != null ) { @@ -216,56 +193,63 @@ class MAuditStreamHandler implements ExecuteStreamHandler /** * add a violation entry for the file - * - * @param file The feature to be added to the ViolationEntry attribute - * @param entry The feature to be added to the ViolationEntry attribute */ - protected void addViolationEntry( String file, MAudit.Violation entry ) + protected void addViolationEntry( String file, Violation entry ) { - ArrayList violations = (ArrayList)auditedFiles.get( file ); - // if there is no decl for this file yet, create it. + ArrayList violations = (ArrayList)m_auditedFiles.get( file ); if( violations == null ) { + // if there is no decl for this file yet, create it. violations = new ArrayList(); - auditedFiles.put( file, violations ); + m_auditedFiles.put( file, violations ); } violations.add( entry ); } /** - * read each line and process it - * - * @param br Description of Parameter - * @exception IOException Description of Exception + * Receive notification about the process writing + * to standard error. */ - protected void parseOutput( BufferedReader br ) - throws IOException + public void stderr( String line ) { - String line = null; - while( ( line = br.readLine() ) != null ) - { - processLine( line ); - } } - // we suppose here that there is only one report / line. - // There will obviouslly be a problem if the message is on several lines... - protected void processLine( String line ) + /** + * Receive notification about the process writing + * to standard output. + */ + public void stdout( final String line ) { - ArrayList matches = matcher.getGroups( line ); + // we suppose here that there is only one report / line. + // There will obviouslly be a problem if the message is on several lines... + + final ArrayList matches = getGroups( line ); if( matches != null ) { - String file = (String)matches.get( 1 ); - int lineNum = Integer.parseInt( (String)matches.get( 2 ) ); - String msg = (String)matches.get( 3 ); - addViolationEntry( file, MAudit.createViolation( lineNum, msg ) ); + final String file = (String)matches.get( 1 ); + final int lineNum = Integer.parseInt( (String)matches.get( 2 ) ); + final String msg = (String)matches.get( 3 ); + final Violation violation = new Violation( msg, lineNum ); + addViolationEntry( file, violation ); } else { // this doesn't match..report it as info, it could be // either the copyright, summary or a multiline message (damn !) - task.getLogger().info( line ); + getLogger().info( line ); } } + private ArrayList getGroups( final String line ) + { + try + { + return m_matcher.getGroups( line ); + } + catch( final TaskException te ) + { + getLogger().error( "Failed to process matcher", te ); + return new ArrayList(); + } + } } diff --git a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/optional/metamata/MMetrics.java b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/optional/metamata/MMetrics.java index c12593785..ef8d4bc4a 100644 --- a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/optional/metamata/MMetrics.java +++ b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/optional/metamata/MMetrics.java @@ -13,9 +13,7 @@ import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import org.apache.myrmidon.api.TaskException; -import org.apache.tools.ant.taskdefs.exec.Execute; import org.apache.tools.ant.taskdefs.exec.ExecuteStreamHandler; -import org.apache.tools.ant.taskdefs.exec.LogOutputStream; import org.apache.tools.ant.types.Path; /** @@ -119,23 +117,24 @@ public class MMetrics extends AbstractMetamataTask } protected ArrayList getOptions() + throws TaskException { ArrayList options = new ArrayList( 512 ); // there is a bug in Metamata 2.0 build 37. The sourcepath argument does // not work. So we will use the sourcepath prepended to classpath. (order // is important since Metamata looks at .class and .java) - if( sourcePath != null ) + if( getSourcePath() != null ) { - sourcePath.append( classPath );// srcpath is prepended - classPath = sourcePath; - sourcePath = null;// prevent from using -sourcepath + getSourcePath().append( getClassPath() );// srcpath is prepended + setClassPath( getSourcePath() ); + setSourcePath( null );// prevent from using -sourcepath } // don't forget to modify the pattern if you change the options reporting - if( classPath != null ) + if( getClassPath() != null ) { options.add( "-classpath" ); - options.add( classPath ); + options.add( getClassPath() ); } options.add( "-output" ); options.add( tmpFile.toString() ); @@ -155,13 +154,13 @@ public class MMetrics extends AbstractMetamataTask options.add( "/" ); // directories - String[] dirs = path.list(); + final String[] dirs = path.list(); for( int i = 0; i < dirs.length; i++ ) { options.add( dirs[ i ] ); } // files next. - addAllArrayList( options, includedFiles.keySet().iterator() ); + addAllArrayList( options, getIncludedFiles().keySet().iterator() ); return options; } @@ -169,10 +168,10 @@ public class MMetrics extends AbstractMetamataTask // check for existing options and outfile, all other are optional - protected void checkOptions() + protected void validate() throws TaskException { - super.checkOptions(); + super.validate(); if( !"files".equals( granularity ) && !"methods".equals( granularity ) && !"types".equals( granularity ) ) @@ -183,12 +182,12 @@ public class MMetrics extends AbstractMetamataTask { throw new TaskException( "Output XML file must be set via 'tofile' attribute." ); } - if( path == null && fileSets.size() == 0 ) + if( path == null && getFileSets().size() == 0 ) { throw new TaskException( "Must set either paths (path element) or files (fileset element)" ); } // I don't accept dirs and files at the same time, I cannot recognize the semantic in the result - if( path != null && fileSets.size() > 0 ) + if( path != null && getFileSets().size() > 0 ) { throw new TaskException( "Cannot set paths (path element) and files (fileset element) at the same time" ); } @@ -216,23 +215,10 @@ public class MMetrics extends AbstractMetamataTask } } - /** - * if the report is transform via a temporary txt file we should use a a - * normal logger here, otherwise we could use the metrics handler directly - * to capture and transform the output on stdout to XML. - * - * @return Description of the Returned Value - */ - protected void setupStreamHandler( final Execute exe ) - { - exe.setOutput( new LogOutputStream( getLogger(), false ) ); - exe.setError( new LogOutputStream( getLogger(), false ) ); - } - - protected void execute0( ExecuteStreamHandler handler ) + protected void execute0() throws TaskException { - super.execute0( handler ); + super.execute0(); transformFile(); } @@ -260,7 +246,8 @@ public class MMetrics extends AbstractMetamataTask try { xmlStream = new FileOutputStream( outFile ); - ExecuteStreamHandler xmlHandler = new MMetricsStreamHandler( this, xmlStream ); + ExecuteStreamHandler xmlHandler = new MMetricsStreamHandler( xmlStream ); + setupLogger( xmlHandler ); xmlHandler.setProcessOutputStream( tmpStream ); xmlHandler.start(); xmlHandler.stop(); @@ -293,5 +280,4 @@ public class MMetrics extends AbstractMetamataTask } } } - } diff --git a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/optional/metamata/MMetricsStreamHandler.java b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/optional/metamata/MMetricsStreamHandler.java index ffaa776d5..1aa97b47a 100644 --- a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/optional/metamata/MMetricsStreamHandler.java +++ b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/optional/metamata/MMetricsStreamHandler.java @@ -26,7 +26,7 @@ import javax.xml.transform.TransformerFactory; import javax.xml.transform.sax.SAXTransformerFactory; import javax.xml.transform.sax.TransformerHandler; import javax.xml.transform.stream.StreamResult; -import org.apache.tools.ant.Task; +import org.apache.avalon.framework.logger.AbstractLogEnabled; import org.apache.tools.ant.taskdefs.exec.ExecuteStreamHandler; import org.xml.sax.Attributes; import org.xml.sax.SAXException; @@ -42,9 +42,10 @@ import org.xml.sax.helpers.AttributesImpl; * * @author Stephane Bailliez */ -public class MMetricsStreamHandler implements ExecuteStreamHandler +public class MMetricsStreamHandler + extends AbstractLogEnabled + implements ExecuteStreamHandler { - /** * CLASS construct, it should be named something like 'MyClass' */ @@ -87,26 +88,14 @@ public class MMetricsStreamHandler implements ExecuteStreamHandler */ protected InputStream metricsOutput; - /** - * the task - */ - protected Task task; - /** * this is where the XML output will go, should mostly be a file the caller * is responsible for flushing and closing this stream */ protected OutputStream xmlOutputStream; - /** - * initialize this handler - * - * @param task Description of Parameter - * @param xmlOut Description of Parameter - */ - MMetricsStreamHandler( Task task, OutputStream xmlOut ) + MMetricsStreamHandler( OutputStream xmlOut ) { - this.task = task; this.xmlOutputStream = xmlOut; } @@ -322,7 +311,7 @@ public class MMetricsStreamHandler implements ExecuteStreamHandler { e.printStackTrace(); // invalid lines are sent to the output as information, it might be anything, - task.getLogger().info( line ); + getLogger().info( line ); } } diff --git a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/optional/metamata/MParse.java b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/optional/metamata/MParse.java index a93b21433..77e6e00ba 100644 --- a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/optional/metamata/MParse.java +++ b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/optional/metamata/MParse.java @@ -15,10 +15,7 @@ import java.util.ArrayList; import java.util.Random; import org.apache.myrmidon.api.TaskException; import org.apache.tools.ant.Task; -import org.apache.tools.ant.taskdefs.exec.Execute; -import org.apache.tools.ant.taskdefs.exec.ExecuteStreamHandler; -import org.apache.tools.ant.taskdefs.exec.LogOutputStream; -import org.apache.tools.ant.taskdefs.exec.LogStreamHandler; +import org.apache.tools.ant.taskdefs.exec.Execute2; import org.apache.tools.ant.types.Argument; import org.apache.tools.ant.types.CommandlineJava; import org.apache.tools.ant.types.Path; @@ -31,24 +28,24 @@ import org.apache.tools.ant.types.Path; * * @author Stephane Bailliez */ -public class MParse extends Task +public class MParse + extends Task { - - private Path classpath = null; - private Path sourcepath = null; - private File metahome = null; - private File target = null; - private boolean verbose = false; - private boolean debugparser = false; - private boolean debugscanner = false; - private boolean cleanup = false; - private CommandlineJava cmdl = new CommandlineJava(); - private File optionsFile = null; + private Path m_classpath; + private Path m_sourcepath; + private File m_metahome; + private File m_target; + private boolean m_verbose; + private boolean m_debugparser; + private boolean m_debugscanner; + private boolean m_cleanup; + private CommandlineJava m_cmdl = new CommandlineJava(); + private File m_optionsFile; public MParse() { - cmdl.setVm( "java" ); - cmdl.setClassname( "com.metamata.jj.MParse" ); + m_cmdl.setVm( "java" ); + m_cmdl.setClassname( "com.metamata.jj.MParse" ); } /** @@ -71,7 +68,7 @@ public class MParse extends Task */ public void setCleanup( boolean value ) { - cleanup = value; + m_cleanup = value; } /** @@ -81,7 +78,7 @@ public class MParse extends Task */ public void setDebugparser( boolean flag ) { - debugparser = flag; + m_debugparser = flag; } /** @@ -91,7 +88,7 @@ public class MParse extends Task */ public void setDebugscanner( boolean flag ) { - debugscanner = flag; + m_debugscanner = flag; } /** @@ -111,7 +108,7 @@ public class MParse extends Task */ public void setMetamatahome( File metamatahome ) { - this.metahome = metamatahome; + this.m_metahome = metamatahome; } /** @@ -121,7 +118,7 @@ public class MParse extends Task */ public void setTarget( File target ) { - this.target = target; + this.m_target = target; } /** @@ -131,7 +128,7 @@ public class MParse extends Task */ public void setVerbose( boolean flag ) { - verbose = flag; + m_verbose = flag; } /** @@ -141,11 +138,11 @@ public class MParse extends Task */ public Path createClasspath() { - if( classpath == null ) + if( m_classpath == null ) { - classpath = new Path(); + m_classpath = new Path(); } - return classpath; + return m_classpath; } /** @@ -155,7 +152,7 @@ public class MParse extends Task */ public Argument createJvmarg() { - return cmdl.createVmArgument(); + return m_cmdl.createVmArgument(); } /** @@ -165,11 +162,11 @@ public class MParse extends Task */ public Path createSourcepath() { - if( sourcepath == null ) + if( m_sourcepath == null ) { - sourcepath = new Path(); + m_sourcepath = new Path(); } - return sourcepath; + return m_sourcepath; } /** @@ -183,8 +180,7 @@ public class MParse extends Task try { setUp(); - ExecuteStreamHandler handler = createStreamHandler(); - _execute( handler ); + doExecute(); } finally { @@ -204,22 +200,22 @@ public class MParse extends Task // set the classpath as the jar files File[] jars = getMetamataLibs(); - final Path classPath = cmdl.createClasspath( getProject() ); + final Path classPath = m_cmdl.createClasspath(); for( int i = 0; i < jars.length; i++ ) { classPath.createPathElement().setLocation( jars[ i ] ); } // set the metamata.home property - final Argument vmArgs = cmdl.createVmArgument(); - vmArgs.setValue( "-Dmetamata.home=" + metahome.getAbsolutePath() ); + final Argument vmArgs = m_cmdl.createVmArgument(); + vmArgs.setValue( "-Dmetamata.home=" + m_metahome.getAbsolutePath() ); // write all the options to a temp file and use it ro run the process String[] options = getOptions(); - optionsFile = createTmpFile(); - generateOptionsFile( optionsFile, options ); - Argument args = cmdl.createArgument(); - args.setLine( "-arguments " + optionsFile.getAbsolutePath() ); + m_optionsFile = createTmpFile(); + generateOptionsFile( m_optionsFile, options ); + Argument args = m_cmdl.createArgument(); + args.setLine( "-arguments " + m_optionsFile.getAbsolutePath() ); } /** @@ -231,13 +227,11 @@ public class MParse extends Task */ protected File[] getMetamataLibs() { - ArrayList files = new ArrayList(); - files.add( new File( metahome, "lib/metamata.jar" ) ); - files.add( new File( metahome, "bin/lib/JavaCC.zip" ) ); + final ArrayList files = new ArrayList(); + files.add( new File( m_metahome, "lib/metamata.jar" ) ); + files.add( new File( m_metahome, "bin/lib/JavaCC.zip" ) ); - File[] array = new File[ files.size() ]; - files.copyInto( array ); - return array; + return (File[])files.toArray( new File[ files.size() ] ); } /** @@ -248,62 +242,58 @@ public class MParse extends Task protected String[] getOptions() { ArrayList options = new ArrayList(); - if( verbose ) + if( m_verbose ) { options.add( "-verbose" ); } - if( debugscanner ) + if( m_debugscanner ) { options.add( "-ds" ); } - if( debugparser ) + if( m_debugparser ) { options.add( "-dp" ); } - if( classpath != null ) + if( m_classpath != null ) { options.add( "-classpath" ); - options.add( classpath.toString() ); + options.add( m_classpath.toString() ); } - if( sourcepath != null ) + if( m_sourcepath != null ) { options.add( "-sourcepath" ); - options.add( sourcepath.toString() ); + options.add( m_sourcepath.toString() ); } - options.add( target.getAbsolutePath() ); + options.add( m_target.getAbsolutePath() ); - String[] array = new String[ options.size() ]; - options.copyInto( array ); - return array; + return (String[])options.toArray( new String[ options.size() ] ); } /** * execute the process with a specific handler - * - * @param handler Description of Parameter - * @exception TaskException Description of Exception */ - protected void _execute( ExecuteStreamHandler handler ) + protected void doExecute() throws TaskException { // target has been checked as a .jj, see if there is a matching // java file and if it is needed to run to process the grammar - String pathname = target.getAbsolutePath(); + String pathname = m_target.getAbsolutePath(); int pos = pathname.length() - ".jj".length(); pathname = pathname.substring( 0, pos ) + ".java"; File javaFile = new File( pathname ); - if( javaFile.exists() && target.lastModified() < javaFile.lastModified() ) + if( javaFile.exists() && m_target.lastModified() < javaFile.lastModified() ) { - getLogger().info( "Target is already build - skipping (" + target + ")" ); + getLogger().info( "Target is already build - skipping (" + m_target + ")" ); return; } - final Execute process = new Execute( handler ); - getLogger().debug( cmdl.toString() ); - process.setCommandline( cmdl.getCommandline() ); + final Execute2 exe = new Execute2(); + setupLogger( exe ); + getLogger().debug( m_cmdl.toString() ); + exe.setCommandline( m_cmdl.getCommandline() ); try { - if( process.execute() != 0 ) + if( exe.execute() != 0 ) { throw new TaskException( "Metamata task failed." ); } @@ -323,11 +313,11 @@ public class MParse extends Task throws TaskException { // check that the home is ok. - if( metahome == null || !metahome.exists() ) + if( m_metahome == null || !m_metahome.exists() ) { throw new TaskException( "'metamatahome' must point to Metamata home directory." ); } - metahome = resolveFile( metahome.getPath() ); + m_metahome = resolveFile( m_metahome.getPath() ); // check that the needed jar exists. File[] jars = getMetamataLibs(); @@ -340,11 +330,11 @@ public class MParse extends Task } // check that the target is ok and resolve it. - if( target == null || !target.isFile() || !target.getName().endsWith( ".jj" ) ) + if( m_target == null || !m_target.isFile() || !m_target.getName().endsWith( ".jj" ) ) { - throw new TaskException( "Invalid target: " + target ); + throw new TaskException( "Invalid target: " + m_target ); } - target = resolveFile( target.getPath() ); + m_target = resolveFile( m_target.getPath() ); } /** @@ -352,17 +342,17 @@ public class MParse extends Task */ protected void cleanUp() { - if( optionsFile != null ) + if( m_optionsFile != null ) { - optionsFile.delete(); - optionsFile = null; + m_optionsFile.delete(); + m_optionsFile = null; } - if( cleanup ) + if( m_cleanup ) { - String name = target.getName(); + String name = m_target.getName(); int pos = name.length() - ".jj".length(); name = "__jj" + name.substring( 0, pos ) + ".sunjj"; - final File sunjj = new File( target.getParent(), name ); + final File sunjj = new File( m_target.getParent(), name ); if( sunjj.exists() ) { getLogger().info( "Removing stale file: " + sunjj.getName() ); @@ -371,18 +361,6 @@ public class MParse extends Task } } - /** - * return the default stream handler for this task - * - * @return Description of the Returned Value - */ - protected ExecuteStreamHandler createStreamHandler() - { - final LogOutputStream output = new LogOutputStream( getLogger(), false ); - final LogOutputStream error = new LogOutputStream( getLogger(), false ); - return new LogStreamHandler( output, error ); - } - /** * write all options to a file with one option / line * diff --git a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/optional/metamata/Violation.java b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/optional/metamata/Violation.java new file mode 100644 index 000000000..58fba32f3 --- /dev/null +++ b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/optional/metamata/Violation.java @@ -0,0 +1,33 @@ +/* + * Copyright (C) The Apache Software Foundation. All rights reserved. + * + * This software is published under the terms of the Apache Software License + * version 1.1, a copy of which has been included with this distribution in + * the LICENSE.txt file. + */ +package org.apache.tools.ant.taskdefs.optional.metamata; + +/** + * the class used to report violation information + */ +final class Violation +{ + private final String m_error; + private final int m_line; + + public Violation( final String error, final int line ) + { + m_error = error; + m_line = line; + } + + protected String getError() + { + return m_error; + } + + protected int getLine() + { + return m_line; + } +}