3 * Copyright 2005 the original author or authors.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package org.wamblee.crawler.kiss.main;
20 import java.util.ArrayList;
21 import java.util.EnumMap;
22 import java.util.HashSet;
23 import java.util.List;
26 import java.util.TreeMap;
27 import java.util.TreeSet;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.dom4j.DocumentFactory;
32 import org.dom4j.Element;
33 import org.wamblee.crawler.kiss.guide.Program;
34 import org.wamblee.crawler.kiss.guide.Program.RecordingResult;
37 * Represents a report on the actions of the crawler.
39 * @author Erik Brakkee
43 private static final Log LOG = LogFactory
44 .getLog(Report.class);
47 * A map of category name to a set of program. Useful for displaying the
48 * output of possibly interesting programs on a per category basis.
50 private Map<String, Set<Program>> _interestingShows;
53 * Map or recording result to a set of programs.
55 private EnumMap<RecordingResult, Set<Program>> _recordings;
58 * Messages generated while doing all the work.
60 private List<String> _messages;
63 * Constructs the report.
67 _interestingShows = new TreeMap<String, Set<Program>>();
68 _recordings = new EnumMap<RecordingResult, Set<Program>>(
69 RecordingResult.class);
70 for (RecordingResult result : RecordingResult.values()) {
71 _recordings.put(result, new TreeSet<Program>(
72 new Program.TimeComparator()));
74 _messages = new ArrayList<String>();
83 public void addMessage(String aMessage) {
84 _messages.add(aMessage);
92 * @param aException Exception that caused the problem.
94 public void addMessage(String aMessage, Exception aException) {
95 String msg = aMessage;
96 for (Throwable e = aException; e != null; e = e.getCause()) {
97 msg += ": " + e.getMessage();
103 * Called to indicate that a program is interesting.
106 * Category of the program.
110 public void interestingProgram(String aCategory, Program aProgram) {
111 LOG.info("category = '" + aCategory + "', program: " + aProgram);
112 Set<Program> programs = _interestingShows.get(aCategory);
113 if (programs == null) {
114 programs = new TreeSet<Program>(new Program.TimeComparator());
115 _interestingShows.put(aCategory, programs);
117 programs.add(aProgram);
121 * Called to specify the result of recording a program.
122 * @param aResult Result.
123 * @param aProgram Program.
125 public void setRecordingResult(RecordingResult aResult, Program aProgram) {
126 _recordings.get(aResult).add(aProgram);
135 public Element asXml() {
136 DocumentFactory factory = DocumentFactory.getInstance();
137 Element report = factory.createElement("report");
139 if (_messages.size() > 0) {
140 Element messages = report.addElement("messages");
141 for (String message : _messages) {
142 messages.addElement("message").setText(message);
146 Set<Program> reportedPrograms = new HashSet<Program>();
148 for (RecordingResult result : RecordingResult.values()) {
149 if (_recordings.get(result).size() > 0) {
150 Element recordingResult = report.addElement("recorded")
151 .addAttribute("result", result.toString());
153 for (Program program : _recordings.get(result)) {
154 recordingResult.add(program.asXml());
155 reportedPrograms.add(program);
160 if (_interestingShows.size() > 0) {
161 Element interesting = report.addElement("interesting");
162 for (String category : _interestingShows.keySet()) {
163 Element categoryElem = interesting;
164 if (category.length() > 0) {
165 categoryElem = interesting.addElement("category");
166 categoryElem.addAttribute("name", category);
168 for (Program program : _interestingShows.get(category)) {
169 if (!reportedPrograms.contains(program)) {
170 categoryElem.add(program.asXml());
172 LOG.info("Category '" + category + "', program "
173 + program + " already reported");
176 if (categoryElem.elements().size() == 0) {
177 // Remove empty category element.
179 .info("Removing element for category '" + category
181 interesting.remove(categoryElem);