01: package net.sourceforge.cruisecontrol.dashboard.saxhandler;
02:
03: import java.util.Map;
04: import java.util.List;
05:
06: import net.sourceforge.cruisecontrol.dashboard.exception.ShouldStopParsingException;
07:
08: import org.xml.sax.Attributes;
09: import org.xml.sax.SAXException;
10:
11: /**
12: * TODO Dynamic Proxy/Functor
13: */
14: public class CompositeExtractor extends SAXBasedExtractor {
15:
16: private static final SAXException SHOULD_STOP_EXCEPTION = new ShouldStopParsingException(
17: "");
18:
19: private final List handlers;
20:
21: public CompositeExtractor(List handlers) {
22: this .handlers = handlers;
23: }
24:
25: public void report(Map resultSet) {
26: for (int i = 0; i < handlers.size(); i++) {
27: extractor(i).report(resultSet);
28: }
29: }
30:
31: public void characters(char[] ch, int start, int length)
32: throws SAXException {
33: for (int i = 0; i < handlers.size(); i++) {
34: extractor(i).characters(ch, start, length);
35: }
36: throwExceptionIfCanStop();
37: }
38:
39: public void endElement(String uri, String localName, String qName)
40: throws SAXException {
41: for (int i = 0; i < handlers.size(); i++) {
42: extractor(i).endElement(uri, localName, qName);
43: }
44: throwExceptionIfCanStop();
45: }
46:
47: public void startElement(String uri, String localName,
48: String qName, Attributes attributes) throws SAXException {
49: for (int i = 0; i < handlers.size(); i++) {
50: extractor(i)
51: .startElement(uri, localName, qName, attributes);
52: }
53: throwExceptionIfCanStop();
54: }
55:
56: private void throwExceptionIfCanStop() throws SAXException {
57: for (int i = 0; i < handlers.size(); i++) {
58: if (!extractor(i).canStop()) {
59: return;
60: }
61: }
62: throw SHOULD_STOP_EXCEPTION;
63: }
64:
65: private SAXBasedExtractor extractor(final int i) {
66: return ((SAXBasedExtractor) handlers.get(i));
67: }
68: }
|