1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package de.fu_berlin.ties;
23
24 import java.io.IOException;
25 import java.io.Reader;
26 import java.io.Writer;
27
28 import org.dom4j.Document;
29 import org.dom4j.DocumentException;
30
31 import de.fu_berlin.ties.xml.dom.DOMUtils;
32
33 /***
34 * Abstract base class for a {@link de.fu_berlin.ties.Processor} that read
35 * {@linkplain org.dom4j.Document XML documents}.
36 *
37 * @author Christian Siefkes
38 * @version $Revision: 1.7 $, $Date: 2006/10/21 16:03:52 $, $Author: siefkes $
39 */
40 public abstract class DocumentReader extends TextProcessor {
41
42 /***
43 * Creates a new instance.
44 *
45 * @param outExt the extension to use for output files
46 * @param conf used to configure this instance; if <code>null</code>,
47 * the {@linkplain TiesConfiguration#CONF standard configuration} is used
48 */
49 public DocumentReader(final String outExt, final TiesConfiguration conf) {
50 super(outExt, conf);
51 }
52
53 /***
54 * Processes the contents of a reader, writing a modified version to a
55 * writer. This implementation delegates to
56 * {@link #process(Document, Writer, ContextMap)}. The reader must contain a
57 * well-formed XML document.
58 *
59 * @param reader reader containing the text to process; not closed
60 * by this method
61 * @param writer the writer to write the processed text to; flushed
62 * but not closed by this method
63 * @param context a map of objects that are made available for processing
64 * @throws IOException if an I/O error occurs
65 * @throws ProcessingException if an error occurs during processing
66 */
67 protected final void doProcess(final Reader reader, final Writer writer,
68 final ContextMap context) throws IOException, ProcessingException {
69 final Document document;
70
71 try {
72
73 document = DOMUtils.readDocument(reader);
74 } catch (DocumentException de) {
75
76 throw new ParsingException(de);
77 }
78
79
80 process(document, writer, context);
81 }
82
83 /***
84 * Reads an XML document and stores the results of processing in a writer.
85 *
86 * @param document the document to read
87 * @param writer the writer to write the processed text to; flushed
88 * but not closed by this method
89 * @param context a map of objects that are made available for processing
90 * @throws IOException if an I/O error occurs
91 * @throws ProcessingException if an error occurs during processing
92 */
93 public abstract void process(final Document document, final Writer writer,
94 final ContextMap context) throws IOException, ProcessingException;
95
96 }