sbailliez    02/02/08 16:47:43

  Modified:    src/main/org/apache/tools/ant/taskdefs/optional/metamata
                        MAudit.java MAuditStreamHandler.java
  Log:
  Refactored handler by extracting specialized parser class.
  
  Revision  Changes    Path
  1.5       +15 -15    
jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/metamata/MAudit.java
  
  Index: MAudit.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/metamata/MAudit.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- MAudit.java       8 Feb 2002 22:06:56 -0000       1.4
  +++ MAudit.java       9 Feb 2002 00:47:43 -0000       1.5
  @@ -273,17 +273,23 @@
       }
   
       protected ExecuteStreamHandler createStreamHandler() throws 
BuildException {
  -        ExecuteStreamHandler handler = null;
           // if we didn't specify a file, then use a screen report
           if (outFile == null) {
  -            handler = new LogStreamHandler(this, Project.MSG_INFO, 
Project.MSG_INFO);
  -        } else {
  -            try {
  -                //XXX
  -                OutputStream out = new FileOutputStream(outFile);
  -                handler = new MAuditStreamHandler(this, out);
  -            } catch (IOException e) {
  -                throw new BuildException(e);
  +            return new LogStreamHandler(this, Project.MSG_INFO, 
Project.MSG_ERR);
  +        }
  +        ExecuteStreamHandler handler = null;
  +        OutputStream out = null;
  +        try {
  +            out = new FileOutputStream(outFile);
  +            handler = new MAuditStreamHandler(this, out);
  +        } catch (IOException e) {
  +            throw new BuildException(e);
  +        } finally {
  +            if (out == null){
  +                try {
  +                    out.close();
  +                } catch (IOException e){
  +                }
               }
           }
           return handler;
  @@ -299,12 +305,6 @@
           /*if (out != null){
               // close it if not closed by the handler...
           }*/
  -    }
  -
  -    /** the inner class used to report violation information */
  -    final static class Violation {
  -        String line;
  -        String error;
       }
   
   }
  
  
  
  1.5       +15 -46    
jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/metamata/MAuditStreamHandler.java
  
  Index: MAuditStreamHandler.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/metamata/MAuditStreamHandler.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- MAuditStreamHandler.java  8 Feb 2002 22:06:56 -0000       1.4
  +++ MAuditStreamHandler.java  9 Feb 2002 00:47:43 -0000       1.5
  @@ -72,6 +72,7 @@
   import org.w3c.dom.Element;
   
   import org.apache.tools.ant.Project;
  +import org.apache.tools.ant.BuildException;
   import org.apache.tools.ant.taskdefs.ExecuteStreamHandler;
   import org.apache.tools.ant.taskdefs.LogOutputStream;
   import org.apache.tools.ant.taskdefs.StreamPumper;
  @@ -106,9 +107,6 @@
       /** reader for stdout */
       private BufferedReader br;
   
  -    /** matcher that will be used to extract the info from the line */
  -    private RegexpMatcher matcher;
  -
       /**
        * this is where the XML output will go, should mostly be a file
        * the caller is responsible for flushing and closing this stream
  @@ -133,9 +131,6 @@
       MAuditStreamHandler(MAudit task, OutputStream xmlOut) {
           this.task = task;
           this.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);
       }
   
       /** Ignore. */
  @@ -205,7 +200,7 @@
               clazz.setAttribute("violations", String.valueOf(violationCount));
               errors += violationCount;
               for (int i = 0; i < violationCount; i++) {
  -                MAudit.Violation violation = (MAudit.Violation) 
v.elementAt(i);
  +                MAuditParser.Violation violation = (MAuditParser.Violation) 
v.elementAt(i);
                   Element error = doc.createElement("violation");
                   error.setAttribute("line", violation.line);
                   error.setAttribute("message", violation.error);
  @@ -216,21 +211,11 @@
           rootElement.setAttribute("violations", String.valueOf(errors));
   
           // now write it to the outputstream, not very nice code
  -        Writer wri = null;
  +        DOMElementWriter domWriter = new DOMElementWriter();
           try {
  -            wri = new OutputStreamWriter(xmlOut, "UTF-8");
  -            wri.write("<?xml version=\"1.0\"?>\n");
  -            (new DOMElementWriter()).write(rootElement, wri, 0, "  ");
  -            wri.flush();
  -        } catch (IOException exc) {
  -            task.log("Unable to write log file", Project.MSG_ERR);
  -        } finally {
  -            if (wri != null) {
  -                try {
  -                    wri.close();
  -                } catch (IOException e) {
  -                }
  -            }
  +            domWriter.write(rootElement, xmlOut);
  +        } catch (IOException e){
  +            throw new BuildException(e);
           }
       }
   
  @@ -255,37 +240,21 @@
       /** read each line and process it */
       protected void parseOutput(BufferedReader br) throws IOException {
           String line = null;
  +        final MAuditParser parser = new MAuditParser();
           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) {
  -        Vector matches = matcher.getGroups(line);
  -        if (matches != null) {
  -            String file = (String) matches.elementAt(1);
  -            MAudit.Violation violation = new MAudit.Violation();
  -            violation.line = (String) matches.elementAt(2);
  -            violation.error = (String) matches.elementAt(3);
  -            // remove the pathname from any messages and let the classname 
only.
  -            final int pos = file.lastIndexOf(File.separatorChar);
  -            if ((pos != -1) && (pos != file.length() - 1)) {
  -                String filename = file.substring(pos + 1);
  -                violation.error = StringUtils.replace(violation.error,
  -                        "file:" + file, filename);
  +            final MAuditParser.Violation violation = parser.parseLine(line);
  +            if (violation != null) {
  +                addViolation(violation.file, violation);
  +            } else {
  +                // this doesn't match..report it as info, it could be
  +                // either the copyright, summary or a multiline message 
(damn !)
  +                task.log(line, Project.MSG_INFO);
               }
  -            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.log(line, Project.MSG_INFO);
           }
       }
   
       /** add a violation entry for the file */
  -    protected void addViolationEntry(String file, MAudit.Violation entry) {
  +    private void addViolation(String file, MAuditParser.Violation entry) {
           Vector violations = (Vector) auditedFiles.get(file);
           // if there is no decl for this file yet, create it.
           if (violations == null) {
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to