KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > bean > helpers > OutputStreamListener


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.bean.helpers;
17
18 import java.io.File JavaDoc;
19 import java.io.FileWriter JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.OutputStream JavaDoc;
22 import java.io.PrintWriter JavaDoc;
23 import java.text.DecimalFormat JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27
28 import org.apache.cocoon.bean.BeanListener;
29
30 /**
31  * Command line entry point. Parses command line, create Cocoon bean and invokes it
32  * with file destination.
33  *
34  * @author <a HREF="mailto:uv@upaya.co.uk">Upayavira</a>
35  * @version CVS $Id: OutputStreamListener.java 30932 2004-07-29 17:35:38Z vgritsenko $
36  */

37 public class OutputStreamListener implements BeanListener {
38
39     private final PrintWriter JavaDoc writer;
40     private final List JavaDoc brokenLinks = new ArrayList JavaDoc();
41     private final long startTimeMillis;
42     private String JavaDoc reportFile = null;
43     private String JavaDoc reportType = "text";
44     private long siteSize = 0L;
45     private int sitePages = 0;
46
47     public OutputStreamListener(OutputStream JavaDoc os) {
48         writer = new PrintWriter JavaDoc(os);
49         startTimeMillis = System.currentTimeMillis();
50     }
51
52     public void setReportFile(String JavaDoc filename) {
53         reportFile = filename;
54     }
55
56     public void setReportType(String JavaDoc type) {
57         reportType = type;
58     }
59
60     public void pageGenerated(String JavaDoc sourceURI,
61                               String JavaDoc 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 JavaDoc 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 JavaDoc msg) {
92         this.print(msg);
93     }
94
95     public void warningGenerated(String JavaDoc uri, String JavaDoc warning) {
96         this.print("Warning: "+warning + " when generating " + uri);
97     }
98
99     public void brokenLinkFound(String JavaDoc uri, String JavaDoc parentURI, String JavaDoc message, Throwable JavaDoc t) {
100         this.print(pad(42,"X [0] ")+uri+"\tBROKEN: "+message);
101         brokenLinks.add(uri + "\t" + message);
102
103 // StringWriter sw = new StringWriter();
104
// t.printStackTrace(new PrintWriter(sw));
105
// System.out.println(sw.toString());
106

107     }
108
109     public void pageSkipped(String JavaDoc uri, String JavaDoc message) {
110         this.print(pad(37, "^ ") + uri);
111     }
112
113     public void complete() {
114         outputBrokenLinks();
115
116         long duration = System.currentTimeMillis() - startTimeMillis;
117         DecimalFormat JavaDoc df = new DecimalFormat JavaDoc("###,###,##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 JavaDoc writer;
143         try {
144             writer =
145                     new PrintWriter JavaDoc(
146                             new FileWriter JavaDoc(new File JavaDoc(reportFile)),
147                             true);
148             for (Iterator JavaDoc i = brokenLinks.iterator(); i.hasNext();) {
149                 writer.println((String JavaDoc) i.next());
150             }
151             writer.close();
152         } catch (IOException JavaDoc ioe) {
153             this.print("Broken link file does not exist: " + reportFile);
154         }
155     }
156     private void outputBrokenLinksAsXML() {
157         PrintWriter JavaDoc writer;
158         try {
159             writer =
160                     new PrintWriter JavaDoc(
161                             new FileWriter JavaDoc(new File JavaDoc(reportFile)),
162                             true);
163             writer.println("<broken-links>");
164             for (Iterator JavaDoc i = brokenLinks.iterator(); i.hasNext();) {
165                 String JavaDoc linkMsg = (String JavaDoc) i.next();
166                 String JavaDoc uri = linkMsg.substring(0,linkMsg.indexOf('\t'));
167                 String JavaDoc 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 JavaDoc ioe) {
173             this.print("Could not create broken link file: " + reportFile);
174         }
175     }
176
177     private String JavaDoc pad(int chars, String JavaDoc str) {
178         int len = str.length();
179         if (len < chars) {
180             StringBuffer JavaDoc sb = new StringBuffer JavaDoc(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 JavaDoc message) {
191         writer.println(message);
192         writer.flush();
193     }
194
195     private void close() {
196         // Flush the writer, but don't close the underlying stream.
197
writer.flush();
198     }
199 }
200
Popular Tags