1 16 package org.apache.cocoon.bean.helpers; 17 18 import java.io.File ; 19 import java.io.FileWriter ; 20 import java.io.IOException ; 21 import java.io.OutputStream ; 22 import java.io.PrintWriter ; 23 import java.text.DecimalFormat ; 24 import java.util.ArrayList ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 28 import org.apache.cocoon.bean.BeanListener; 29 30 37 public class OutputStreamListener implements BeanListener { 38 39 private final PrintWriter writer; 40 private final List brokenLinks = new ArrayList (); 41 private final long startTimeMillis; 42 private String reportFile = null; 43 private String reportType = "text"; 44 private long siteSize = 0L; 45 private int sitePages = 0; 46 47 public OutputStreamListener(OutputStream os) { 48 writer = new PrintWriter (os); 49 startTimeMillis = System.currentTimeMillis(); 50 } 51 52 public void setReportFile(String filename) { 53 reportFile = filename; 54 } 55 56 public void setReportType(String type) { 57 reportType = type; 58 } 59 60 public void pageGenerated(String sourceURI, 61 String destinationURI, 62 int pageSize, 63 int linksInPage, 64 int newLinksInPage, 65 int pagesRemaining, 66 int pagesComplete, 67 long timeTaken) { 68 this.siteSize += pageSize; 69 this.sitePages++; 70 71 double time = (((double)timeTaken)/1000); 72 73 String size; 74 if (pageSize < 1024) { 75 size = pageSize + "b"; 76 } else { 77 size = ((float)((int)(pageSize/102.4)))/10 + "Kb"; 78 } 79 80 if (linksInPage == -1) { 81 this.print("* " + sourceURI); 82 } else { 83 this.print(pad(12, "* [" + pagesComplete + "/" + pagesRemaining + "] ") + 84 pad(10, "[" + newLinksInPage + "/" + linksInPage + "] ") + 85 pad(7,time + "s ") + 86 pad(7, size) + " " + 87 sourceURI); 88 } 89 90 } 91 public void messageGenerated(String msg) { 92 this.print(msg); 93 } 94 95 public void warningGenerated(String uri, String warning) { 96 this.print("Warning: "+warning + " when generating " + uri); 97 } 98 99 public void brokenLinkFound(String uri, String parentURI, String message, Throwable t) { 100 this.print(pad(42,"X [0] ")+uri+"\tBROKEN: "+message); 101 brokenLinks.add(uri + "\t" + message); 102 103 107 } 108 109 public void pageSkipped(String uri, String message) { 110 this.print(pad(37, "^ ") + uri); 111 } 112 113 public void complete() { 114 outputBrokenLinks(); 115 116 long duration = System.currentTimeMillis() - startTimeMillis; 117 DecimalFormat df = new DecimalFormat ("###,###,##0"); 118 119 this.print("Total time: " + 120 (duration / 60000) + " minutes " + 121 (duration % 60000)/1000 + " seconds, " + 122 " Site size: " + df.format(this.siteSize) + 123 " Site pages: " + this.sitePages); 124 this.close(); 125 } 126 127 public boolean isSuccessful() { 128 return brokenLinks.size() == 0; 129 } 130 131 private void outputBrokenLinks() { 132 if (reportFile == null) { 133 return; 134 } else if ("text".equalsIgnoreCase(reportType)) { 135 outputBrokenLinksAsText(); 136 } else if ("xml".equalsIgnoreCase(reportType)) { 137 outputBrokenLinksAsXML(); 138 } 139 } 140 141 private void outputBrokenLinksAsText() { 142 PrintWriter writer; 143 try { 144 writer = 145 new PrintWriter ( 146 new FileWriter (new File (reportFile)), 147 true); 148 for (Iterator i = brokenLinks.iterator(); i.hasNext();) { 149 writer.println((String ) i.next()); 150 } 151 writer.close(); 152 } catch (IOException ioe) { 153 this.print("Broken link file does not exist: " + reportFile); 154 } 155 } 156 private void outputBrokenLinksAsXML() { 157 PrintWriter writer; 158 try { 159 writer = 160 new PrintWriter ( 161 new FileWriter (new File (reportFile)), 162 true); 163 writer.println("<broken-links>"); 164 for (Iterator i = brokenLinks.iterator(); i.hasNext();) { 165 String linkMsg = (String ) i.next(); 166 String uri = linkMsg.substring(0,linkMsg.indexOf('\t')); 167 String msg = linkMsg.substring(linkMsg.indexOf('\t')+1); 168 writer.println(" <link message=\"" + msg + "\">" + uri + "</link>"); 169 } 170 writer.println("</broken-links>"); 171 writer.close(); 172 } catch (IOException ioe) { 173 this.print("Could not create broken link file: " + reportFile); 174 } 175 } 176 177 private String pad(int chars, String str) { 178 int len = str.length(); 179 if (len < chars) { 180 StringBuffer sb = new StringBuffer (chars > len ? chars+1 : len+1); 181 sb.append(str); 182 for (int i=len; i<chars; i++) { 183 sb.append(" "); 184 } 185 return sb.toString(); 186 } 187 return str; 188 } 189 190 private void print(String message) { 191 writer.println(message); 192 writer.flush(); 193 } 194 195 private void close() { 196 writer.flush(); 198 } 199 } 200 | Popular Tags |