upayavira 2003/09/15 12:18:18
Modified: src/java/org/apache/cocoon Main.java src/java/org/apache/cocoon/bean BeanListener.java CocoonBean.java Target.java Added: src/java/org/apache/cocoon/bean/helpers OutputStreamListener.java Log: Improving the BeanListener interface to be able to handle better reporting Replacing CLIListener with OutputStreamListener (generic bean listener, not specific to the CLI) Starting to update CLI to use this new interface Revision Changes Path 1.14 +9 -85 cocoon-2.1/src/java/org/apache/cocoon/Main.java Index: Main.java =================================================================== RCS file: /home/cvs/cocoon-2.1/src/java/org/apache/cocoon/Main.java,v retrieving revision 1.13 retrieving revision 1.14 diff -u -r1.13 -r1.14 --- Main.java 28 Aug 2003 19:21:00 -0000 1.13 +++ Main.java 15 Sep 2003 19:18:17 -0000 1.14 @@ -77,7 +77,7 @@ import org.apache.cocoon.Constants; import org.apache.cocoon.bean.CocoonBean; -import org.apache.cocoon.bean.BeanListener; +import org.apache.cocoon.bean.helpers.OutputStreamListener; /** * Command line entry point. Parses command line, create Cocoon bean and invokes it @@ -174,9 +174,7 @@ private static final String ATTR_URI_DESTURI = "dest"; private static Options options; - private static List brokenLinks = new ArrayList(); - private static String brokenLinkReportFile = null; - private static String brokenLinkReportType = "text"; + private static OutputStreamListener listener; private static void setOptions() { options = new Options(); @@ -297,11 +295,9 @@ */ public static void main(String[] args) throws Exception { - long startTimeMillis = System.currentTimeMillis(); - Main.setOptions(); CommandLine line = new PosixParser().parse( options, args ); - CLIListener listener = new Main.CLIListener(); + listener = new OutputStreamListener(System.out); CocoonBean cocoon = new CocoonBean(); cocoon.addListener(listener); @@ -369,7 +365,7 @@ cocoon.setDefaultFilename(line.getOptionValue(DEFAULT_FILENAME_OPT)); } if (line.hasOption(BROKEN_LINK_FILE_OPT)) { - brokenLinkReportFile = line.getOptionValue(BROKEN_LINK_FILE_OPT); + listener.setReportFile(line.getOptionValue(BROKEN_LINK_FILE_OPT)); } if (line.hasOption(URI_FILE_OPT)) { cocoon.addTargets(processURIFile(line.getOptionValue(URI_FILE_OPT)), destDir); @@ -392,12 +388,10 @@ cocoon.process(); cocoon.dispose(); - listener.outputBrokenLinks(); + listener.complete(); - long duration = System.currentTimeMillis() - startTimeMillis; - System.out.println("Total time: " + (duration / 60000) + " minutes " + (duration % 60000)/1000 + " seconds"); - int exitCode = (brokenLinks.size() == 0 ? 0 : 1); + int exitCode = (listener.isSuccessful() ? 0 : 1); System.exit(exitCode); } @@ -548,10 +542,10 @@ private static void parseBrokenLinkNode(CocoonBean cocoon, Node node) throws IllegalArgumentException { if (Main.hasAttribute(node, ATTR_BROKEN_LINK_REPORT_FILE)) { - brokenLinkReportFile = Main.getAttributeValue(node, ATTR_BROKEN_LINK_REPORT_FILE); + listener.setReportFile(Main.getAttributeValue(node, ATTR_BROKEN_LINK_REPORT_FILE)); } if (Main.hasAttribute(node, ATTR_BROKEN_LINK_REPORT_TYPE)) { - brokenLinkReportType = Main.getAttributeValue(node, ATTR_BROKEN_LINK_REPORT_TYPE); + listener.setReportType(Main.getAttributeValue(node, ATTR_BROKEN_LINK_REPORT_TYPE)); } if (Main.hasAttribute(node, ATTR_BROKEN_LINK_GENERATE)) { cocoon.setBrokenLinkGenerate(Main.getBooleanAttributeValue(node, ATTR_BROKEN_LINK_GENERATE)); @@ -714,75 +708,5 @@ private static void printVersion() { System.out.println(Constants.VERSION); System.exit(0); - } - public static class CLIListener implements BeanListener { - public void pageGenerated(String uri, int linksInPage, int pagesRemaining) { - if (linksInPage == -1) { - this.print("* " + uri); - } else { - this.print("* ["+linksInPage + "] "+uri); - } - } - public void messageGenerated(String msg) { - this.print(msg); - } - - public void warningGenerated(String uri, String warning) { - this.print("Warning: "+warning + " when generating " + uri); - } - - public void brokenLinkFound(String uri, String message) { - this.print("X [0] "+uri+"\tBROKEN: "+message); - brokenLinks.add(uri + "\t" + message); - } - - public void outputBrokenLinks() { - if (brokenLinkReportFile == null) { - return; - } else if ("text".equalsIgnoreCase(brokenLinkReportType)) { - outputBrokenLinksAsText(); - } else if ("xml".equalsIgnoreCase(brokenLinkReportType)) { - outputBrokenLinksAsXML(); - } - } - private void outputBrokenLinksAsText() { - PrintWriter writer; - try { - writer = - new PrintWriter( - new FileWriter(new File(brokenLinkReportFile)), - true); - for (Iterator i = brokenLinks.iterator(); i.hasNext();) { - writer.println((String) i.next()); - } - writer.close(); - } catch (IOException ioe) { - this.print("Broken link file does not exist: " + brokenLinkReportFile); - } - } - private void outputBrokenLinksAsXML() { - PrintWriter writer; - try { - writer = - new PrintWriter( - new FileWriter(new File(brokenLinkReportFile)), - true); - writer.println("<broken-links>"); - for (Iterator i = brokenLinks.iterator(); i.hasNext();) { - String linkMsg = (String) i.next(); - String uri = linkMsg.substring(0,linkMsg.indexOf('\t')); - String msg = linkMsg.substring(linkMsg.indexOf('\t')+1); - writer.println(" <link message=\"" + msg + "\">" + uri + "</link>"); - } - writer.println("</broken-links>"); - writer.close(); - } catch (IOException ioe) { - this.print("Could not create broken link file: " + brokenLinkReportFile); - } - } - - private void print(String message) { - System.out.println(message); - } } } 1.2 +25 -4 cocoon-2.1/src/java/org/apache/cocoon/bean/BeanListener.java Index: BeanListener.java =================================================================== RCS file: /home/cvs/cocoon-2.1/src/java/org/apache/cocoon/bean/BeanListener.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- BeanListener.java 27 Jun 2003 13:50:38 -0000 1.1 +++ BeanListener.java 15 Sep 2003 19:18:17 -0000 1.2 @@ -65,8 +65,22 @@ * @param linksInPage Number of links found in this page * @param pagesRemaining Number of pages still to be generated */ - public void pageGenerated(String uri, int linksInPage, int pagesRemaining); - + public void pageGenerated(String sourceURI, + String destinationURI, + int pageSize, + int linksInPage, + int newLinksinPage, + int pagesRemaining, + int pageComplete, + long timeTaken); + + /** + * Report a that was skipped because its URI matched an + * include/exclude pattern. + * @param msg + */ + public void pageSkipped(String uri); + /** * Report a general message about operation of the bean * @param msg The message to be reported @@ -86,5 +100,12 @@ * @param uri The URI that failed to be generated * @param message A reason why the link was not generated */ - public void brokenLinkFound(String uri, String message); + public void brokenLinkFound(String uri, String parentURI, String message, Throwable t); + + /** + * Signals completion of the generation process. This method can + * be used to write out reports, display time generation duration, + * etc. + */ + public void complete(); } 1.26 +3 -3 cocoon-2.1/src/java/org/apache/cocoon/bean/CocoonBean.java Index: CocoonBean.java =================================================================== RCS file: /home/cvs/cocoon-2.1/src/java/org/apache/cocoon/bean/CocoonBean.java,v retrieving revision 1.25 retrieving revision 1.26 diff -u -r1.25 -r1.26 --- CocoonBean.java 15 Sep 2003 11:37:04 -0000 1.25 +++ CocoonBean.java 15 Sep 2003 19:18:17 -0000 1.26 @@ -231,7 +231,7 @@ Iterator i = listeners.iterator(); while (i.hasNext()) { BeanListener l = (BeanListener) i.next(); - l.pageGenerated(uri, linksInPage, pagesRemaining); + l.pageGenerated(uri, "", 0, linksInPage, 0, pagesRemaining, 0, 0L); } } @@ -255,7 +255,7 @@ Iterator i = listeners.iterator(); while (i.hasNext()) { BeanListener l = (BeanListener) i.next(); - l.brokenLinkFound(uri, warning); + l.brokenLinkFound(uri, "", warning, null); } } 1.5 +19 -0 cocoon-2.1/src/java/org/apache/cocoon/bean/Target.java Index: Target.java =================================================================== RCS file: /home/cvs/cocoon-2.1/src/java/org/apache/cocoon/bean/Target.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- Target.java 15 Sep 2003 11:26:04 -0000 1.4 +++ Target.java 15 Sep 2003 19:18:18 -0000 1.5 @@ -79,6 +79,7 @@ private final String deparameterizedSourceURI; private final TreeMap parameters; + private String parentURI = null; private String originalURI = null; private String mimeType = null; private String defaultFilename = Constants.INDEX_URI; @@ -139,6 +140,7 @@ Target target = new Target(this.type, this.root, linkURI, this.destURI); target.setOriginalURI(originalLinkURI); + target.setParentURI(this.sourceURI); return target; } @@ -153,6 +155,14 @@ } /** + * Sets the URI of the page that contained the link to this + * URI. Used for reporting purposes. + */ + public void setParentURI(String uri) { + this.parentURI = uri; + } + + /** * Sets the mime type for the resource referenced by this target. * If a mime type is specified, the file extension of the * destination URI will be checked to see that it matches the @@ -220,6 +230,15 @@ return NetUtils.getPath(this.getSourceURI()); } + /** + * Gets the parent URI (the URI of the page that contained + * a link to this URI). null is returned if this page was + * not referred to in a link. + */ + public String getParentURI() { + return this.parentURI; + } + /** * Calculates the destination URI - the URI to which the generated * page should be written. This will be a URI that, when resolved 1.1 cocoon-2.1/src/java/org/apache/cocoon/bean/helpers/OutputStreamListener.java Index: OutputStreamListener.java =================================================================== /* ============================================================================ The Apache Software License, Version 1.1 ============================================================================ Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved. Redistribution and use in source and binary forms, with or without modifica- tion, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The end-user documentation included with the redistribution, if any, must include the following acknowledgment: "This product includes software developed by the Apache Software Foundation (http://www.apache.org/)." Alternately, this acknowledgment may appear in the software itself, if and wherever such third-party acknowledgments normally appear. 4. The names "Apache Cocoon" and "Apache Software Foundation" must not be used to endorse or promote products derived from this software without prior written permission. For written permission, please contact [EMAIL PROTECTED] 5. Products derived from this software may not be called "Apache", nor may "Apache" appear in their name, without prior written permission of the Apache Software Foundation. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU- DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. This software consists of voluntary contributions made by many individuals on behalf of the Apache Software Foundation and was originally created by Stefano Mazzocchi <[EMAIL PROTECTED]>. For more information on the Apache Software Foundation, please see <http://www.apache.org/>. */ package org.apache.cocoon.bean.helpers; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.OutputStream; import java.io.PrintWriter; import org.apache.cocoon.bean.BeanListener; /** * Command line entry point. Parses command line, create Cocoon bean and invokes it * with file destination. * * @author <a href="mailto:[EMAIL PROTECTED]">Upayavira</a> * @version CVS $Id: OutputStreamListener.java,v 1.1 2003/09/15 19:18:18 upayavira Exp $ */ public class OutputStreamListener implements BeanListener { private final PrintWriter writer; private final List brokenLinks = new ArrayList(); private final long startTimeMillis; private String reportFile = null; private String reportType = "text"; public OutputStreamListener(OutputStream os) { writer = new PrintWriter(os); startTimeMillis = System.currentTimeMillis(); } public void setReportFile(String filename) { reportFile = filename; } public void setReportType(String type) { reportType = type; } public void pageGenerated(String sourceURI, String destinationURI, int pageSize, int linksInPage, int newLinksinPage, int pagesRemaining, int pageComplete, long timeTaken) { if (linksInPage == -1) { this.print("* " + sourceURI); } else { this.print("* ["+linksInPage + "] "+sourceURI); } } public void messageGenerated(String msg) { this.print(msg); } public void warningGenerated(String uri, String warning) { this.print("Warning: "+warning + " when generating " + uri); } public void brokenLinkFound(String uri, String parentURI, String message, Throwable t) { this.print("X [0] "+uri+"\tBROKEN: "+message); brokenLinks.add(uri + "\t" + message); // StringWriter sw = new StringWriter(); // t.printStackTrace(new PrintWriter(sw)); // System.out.println(sw.toString()); } public void pageSkipped(String uri) { // @TODO@ Do something } public void complete() { outputBrokenLinks(); long duration = System.currentTimeMillis() - startTimeMillis; this.print("Total time: " + (duration / 60000) + " minutes " + (duration % 60000)/1000 + " seconds"); writer.flush(); writer.close(); } public boolean isSuccessful() { return brokenLinks.size() == 0; } private void outputBrokenLinks() { if (reportFile == null) { return; } else if ("text".equalsIgnoreCase(reportType)) { outputBrokenLinksAsText(); } else if ("xml".equalsIgnoreCase(reportType)) { outputBrokenLinksAsXML(); } } private void outputBrokenLinksAsText() { PrintWriter writer; try { writer = new PrintWriter( new FileWriter(new File(reportFile)), true); for (Iterator i = brokenLinks.iterator(); i.hasNext();) { writer.println((String) i.next()); } writer.close(); } catch (IOException ioe) { this.print("Broken link file does not exist: " + reportFile); } } private void outputBrokenLinksAsXML() { PrintWriter writer; try { writer = new PrintWriter( new FileWriter(new File(reportFile)), true); writer.println("<broken-links>"); for (Iterator i = brokenLinks.iterator(); i.hasNext();) { String linkMsg = (String) i.next(); String uri = linkMsg.substring(0,linkMsg.indexOf('\t')); String msg = linkMsg.substring(linkMsg.indexOf('\t')+1); writer.println(" <link message=\"" + msg + "\">" + uri + "</link>"); } writer.println("</broken-links>"); writer.close(); } catch (IOException ioe) { this.print("Could not create broken link file: " + reportFile); } } private void print(String message) { writer.println(message); } }