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.
41 private static final Log LOG = LogFactory
42 .getLog(Report.class);
45 * A map of category name to a set of program. Useful for displaying the
46 * output of possibly interesting programs on a per category basis.
48 private Map<String, Set<Program>> _interestingShows;
51 * Map or recording result to a set of programs.
53 private EnumMap<RecordingResult, Set<Program>> _recordings;
56 * Messages generated while doing all the work.
58 private List<String> _messages;
61 * Constructs the report.
65 _interestingShows = new TreeMap<String, Set<Program>>();
66 _recordings = new EnumMap<RecordingResult, Set<Program>>(
67 RecordingResult.class);
68 for (RecordingResult result : RecordingResult.values()) {
69 _recordings.put(result, new TreeSet<Program>(
70 new Program.TimeComparator()));
72 _messages = new ArrayList<String>();
81 public void addMessage(String aMessage) {
82 _messages.add(aMessage);
90 * @param aException Exception that caused the problem.
92 public void addMessage(String aMessage, Exception aException) {
93 String msg = aMessage;
94 for (Throwable e = aException; e != null; e = e.getCause()) {
95 msg += ": " + e.getMessage();
101 * Called to indicate that a program is interesting.
104 * Category of the program.
108 public void interestingProgram(String aCategory, Program aProgram) {
109 LOG.info("category = '" + aCategory + "', program: " + aProgram);
110 Set<Program> programs = _interestingShows.get(aCategory);
111 if (programs == null) {
112 programs = new TreeSet<Program>(new Program.TimeComparator());
113 _interestingShows.put(aCategory, programs);
115 programs.add(aProgram);
119 * Called to specify the result of recording a program.
120 * @param aResult Result.
121 * @param aProgram Program.
123 public void setRecordingResult(RecordingResult aResult, Program aProgram) {
124 _recordings.get(aResult).add(aProgram);
133 public Element asXml() {
134 DocumentFactory factory = DocumentFactory.getInstance();
135 Element report = factory.createElement("report");
137 if (_messages.size() > 0) {
138 Element messages = report.addElement("messages");
139 for (String message : _messages) {
140 messages.addElement("message").setText(message);
144 Set<Program> reportedPrograms = new HashSet<Program>();
146 for (RecordingResult result : RecordingResult.values()) {
147 if (_recordings.get(result).size() > 0) {
148 Element recordingResult = report.addElement("recorded")
149 .addAttribute("result", result.toString());
151 for (Program program : _recordings.get(result)) {
152 recordingResult.add(program.asXml());
153 reportedPrograms.add(program);
158 if (_interestingShows.size() > 0) {
159 Element interesting = report.addElement("interesting");
160 for (String category : _interestingShows.keySet()) {
161 Element categoryElem = interesting;
162 if (category.length() > 0) {
163 categoryElem = interesting.addElement("category");
164 categoryElem.addAttribute("name", category);
166 for (Program program : _interestingShows.get(category)) {
167 if (!reportedPrograms.contains(program)) {
168 categoryElem.add(program.asXml());
170 LOG.info("Category '" + category + "', program "
171 + program + " already reported");
174 if (categoryElem.elements().size() == 0) {
175 // Remove empty category element.
177 .info("Removing element for category '" + category
179 interesting.remove(categoryElem);