/* * * Copyright 2005 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.wamblee.crawler.kiss.main; import java.util.ArrayList; import java.util.EnumMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import java.util.TreeMap; import java.util.TreeSet; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.dom4j.DocumentFactory; import org.dom4j.Element; import org.wamblee.crawler.kiss.guide.Program; import org.wamblee.crawler.kiss.guide.Program.RecordingResult; /** * Represents a report on the actions of the crawler. */ public class Report { private static final Log LOG = LogFactory .getLog(Report.class); /** * A map of category name to a set of program. Useful for displaying the * output of possibly interesting programs on a per category basis. */ private Map> _interestingShows; /** * Map or recording result to a set of programs. */ private EnumMap> _recordings; /** * Messages generated while doing all the work. */ private List _messages; /** * Constructs the report. * */ public Report() { _interestingShows = new TreeMap>(); _recordings = new EnumMap>( RecordingResult.class); for (RecordingResult result : RecordingResult.values()) { _recordings.put(result, new TreeSet( new Program.TimeComparator())); } _messages = new ArrayList(); } /** * Adds a message. * * @param aMessage * Message to add. */ public void addMessage(String aMessage) { _messages.add(aMessage); } /** * Adds a message. * * @param aMessage * Message to add. * @param aException Exception that caused the problem. */ public void addMessage(String aMessage, Exception aException) { String msg = aMessage; for (Throwable e = aException; e != null; e = e.getCause()) { msg += ": " + e.getMessage(); } addMessage(msg); } /** * Called to indicate that a program is interesting. * * @param aCategory * Category of the program. * @param aProgram * Program. */ public void interestingProgram(String aCategory, Program aProgram) { LOG.info("category = '" + aCategory + "', program: " + aProgram); Set programs = _interestingShows.get(aCategory); if (programs == null) { programs = new TreeSet(new Program.TimeComparator()); _interestingShows.put(aCategory, programs); } programs.add(aProgram); } /** * Called to specify the result of recording a program. * @param aResult Result. * @param aProgram Program. */ public void setRecordingResult(RecordingResult aResult, Program aProgram) { _recordings.get(aResult).add(aProgram); } /** * Get report as XML. * * @return XML report */ public Element asXml() { DocumentFactory factory = DocumentFactory.getInstance(); Element report = factory.createElement("report"); if (_messages.size() > 0) { Element messages = report.addElement("messages"); for (String message : _messages) { messages.addElement("message").setText(message); } } Set reportedPrograms = new HashSet(); for (RecordingResult result : RecordingResult.values()) { if (_recordings.get(result).size() > 0) { Element recordingResult = report.addElement("recorded") .addAttribute("result", result.toString()); for (Program program : _recordings.get(result)) { recordingResult.add(program.asXml()); reportedPrograms.add(program); } } } if (_interestingShows.size() > 0) { Element interesting = report.addElement("interesting"); for (String category : _interestingShows.keySet()) { Element categoryElem = interesting; if (category.length() > 0) { categoryElem = interesting.addElement("category"); categoryElem.addAttribute("name", category); } for (Program program : _interestingShows.get(category)) { if (!reportedPrograms.contains(program)) { categoryElem.add(program.asXml()); } else { LOG.info("Category '" + category + "', program " + program + " already reported"); } } if (categoryElem.elements().size() == 0) { // Remove empty category element. LOG .info("Removing element for category '" + category + "'"); interesting.remove(categoryElem); } } } return report; } }