1 17 package org.apache.tools.ant.taskdefs.optional.metamata; 18 19 import java.io.BufferedReader ; 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.io.InputStreamReader ; 23 import java.io.OutputStream ; 24 import java.util.Date ; 25 import java.util.Enumeration ; 26 import java.util.Hashtable ; 27 import java.util.Vector ; 28 import javax.xml.parsers.DocumentBuilder ; 29 import javax.xml.parsers.DocumentBuilderFactory ; 30 import org.apache.tools.ant.BuildException; 31 import org.apache.tools.ant.Project; 32 import org.apache.tools.ant.taskdefs.ExecuteStreamHandler; 33 import org.apache.tools.ant.taskdefs.LogOutputStream; 34 import org.apache.tools.ant.taskdefs.StreamPumper; 35 import org.apache.tools.ant.util.DOMElementWriter; 36 import org.apache.tools.ant.util.DateUtils; 37 import org.w3c.dom.Document ; 38 import org.w3c.dom.Element ; 39 40 55 class MAuditStreamHandler implements ExecuteStreamHandler { 56 57 58 private MAudit task; 59 60 61 private BufferedReader br; 62 63 67 private OutputStream xmlOut = null; 68 69 70 private OutputStream errStream; 71 72 73 private Thread errThread; 74 75 79 private Hashtable auditedFiles = new Hashtable (); 80 81 82 private Date program_start; 83 84 MAuditStreamHandler(MAudit task, OutputStream xmlOut) { 85 this.task = task; 86 this.xmlOut = xmlOut; 87 } 88 89 90 public void setProcessInputStream(OutputStream os) { 91 } 92 93 94 public void setProcessErrorStream(InputStream is) { 95 errStream = new LogOutputStream(task, Project.MSG_ERR); 96 errThread = createPump(is, errStream); 97 } 98 99 100 public void setProcessOutputStream(InputStream is) throws IOException { 101 br = new BufferedReader (new InputStreamReader (is)); 102 } 103 104 105 public void start() throws IOException { 106 program_start = new Date (); 107 errThread.start(); 108 parseOutput(br); 109 } 110 111 115 public void stop() { 116 try { 118 errThread.join(); 119 } catch (InterruptedException e) { 120 } 121 try { 122 errStream.flush(); 123 } catch (IOException e) { 124 } 125 Document doc = getDocumentBuilder().newDocument(); 128 Element rootElement = doc.createElement("classes"); 129 Enumeration keys = auditedFiles.keys(); 130 Hashtable filemapping = task.getFileMapping(); 131 final Date now = new Date (); 132 rootElement.setAttribute("snapshot_created", 133 DateUtils.format(now, DateUtils.ISO8601_DATETIME_PATTERN)); 134 rootElement.setAttribute("elapsed_time", 135 String.valueOf(now.getTime() - program_start.getTime())); 136 rootElement.setAttribute("program_start", 137 DateUtils.format(now, DateUtils.ISO8601_DATETIME_PATTERN)); 138 rootElement.setAttribute("audited", 139 String.valueOf(filemapping.size())); 140 rootElement.setAttribute("reported", 141 String.valueOf(auditedFiles.size())); 142 int errors = 0; 143 while (keys.hasMoreElements()) { 144 String filepath = (String ) keys.nextElement(); 145 Vector v = (Vector ) auditedFiles.get(filepath); 146 String fullclassname = (String ) filemapping.get(filepath); 147 if (fullclassname == null) { 148 task.getProject().log("Could not find class mapping for " 149 + filepath, Project.MSG_WARN); 150 continue; 151 } 152 int pos = fullclassname.lastIndexOf('.'); 153 String pkg = (pos == -1) ? "" : fullclassname.substring(0, pos); 154 String clazzname = (pos == -1) ? fullclassname : fullclassname.substring(pos + 1); 155 Element clazz = doc.createElement("class"); 156 clazz.setAttribute("package", pkg); 157 clazz.setAttribute("name", clazzname); 158 final int violationCount = v.size(); 159 clazz.setAttribute("violations", String.valueOf(violationCount)); 160 errors += violationCount; 161 for (int i = 0; i < violationCount; i++) { 162 MAuditParser.Violation violation = (MAuditParser.Violation) v.elementAt(i); 163 Element error = doc.createElement("violation"); 164 error.setAttribute("line", violation.line); 165 error.setAttribute("message", violation.error); 166 clazz.appendChild(error); 167 } 168 rootElement.appendChild(clazz); 169 } 170 rootElement.setAttribute("violations", String.valueOf(errors)); 171 172 DOMElementWriter domWriter = new DOMElementWriter(); 174 try { 175 domWriter.write(rootElement, xmlOut); 176 } catch (IOException e) { 177 throw new BuildException(e); 178 } 179 } 180 181 protected static DocumentBuilder getDocumentBuilder() { 182 try { 183 return DocumentBuilderFactory.newInstance().newDocumentBuilder(); 184 } catch (Exception exc) { 185 throw new ExceptionInInitializerError (exc); 186 } 187 } 188 189 192 protected Thread createPump(InputStream is, OutputStream os) { 193 final Thread result = new Thread (new StreamPumper(is, os)); 194 result.setDaemon(true); 195 return result; 196 } 197 198 199 200 protected void parseOutput(BufferedReader br) throws IOException { 201 String line = null; 202 final MAuditParser parser = new MAuditParser(); 203 while ((line = br.readLine()) != null) { 204 final MAuditParser.Violation violation = parser.parseLine(line); 205 if (violation != null) { 206 addViolation(violation.file, violation); 207 } else { 208 task.log(line, Project.MSG_INFO); 211 } 212 } 213 } 214 215 216 private void addViolation(String file, MAuditParser.Violation entry) { 217 Vector violations = (Vector ) auditedFiles.get(file); 218 if (violations == null) { 220 violations = new Vector (); 221 auditedFiles.put(file, violations); 222 } 223 violations.addElement(entry); 224 } 225 226 } 227 | Popular Tags |