From 3b349325f1e1f10b3c4616a1c7a3e9b4ec24c40b Mon Sep 17 00:00:00 2001 From: Stephane Bailliez Date: Sat, 9 Feb 2002 00:47:43 +0000 Subject: [PATCH] Refactored handler by extracting specialized parser class. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@271228 13f79535-47bb-0310-9956-ffa450edef68 --- .../taskdefs/optional/metamata/MAudit.java | 30 ++++----- .../metamata/MAuditStreamHandler.java | 61 +++++-------------- 2 files changed, 30 insertions(+), 61 deletions(-) diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/metamata/MAudit.java b/src/main/org/apache/tools/ant/taskdefs/optional/metamata/MAudit.java index 1e2caac38..f057f96dc 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/metamata/MAudit.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/metamata/MAudit.java @@ -273,17 +273,23 @@ public class MAudit extends AbstractMetamataTask { } 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; @@ -301,11 +307,5 @@ public class MAudit extends AbstractMetamataTask { }*/ } - /** the inner class used to report violation information */ - final static class Violation { - String line; - String error; - } - } diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/metamata/MAuditStreamHandler.java b/src/main/org/apache/tools/ant/taskdefs/optional/metamata/MAuditStreamHandler.java index f6fdc367c..fdda40fb7 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/metamata/MAuditStreamHandler.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/metamata/MAuditStreamHandler.java @@ -72,6 +72,7 @@ import org.w3c.dom.Document; 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 @@ class MAuditStreamHandler implements ExecuteStreamHandler { /** 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 @@ class MAuditStreamHandler implements ExecuteStreamHandler { 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 @@ class MAuditStreamHandler implements ExecuteStreamHandler { 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 @@ class MAuditStreamHandler implements ExecuteStreamHandler { 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("\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 @@ class MAuditStreamHandler implements ExecuteStreamHandler { /** 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) {