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
30 import de.fu_berlin.ties.io.IOUtils;
31 import de.fu_berlin.ties.xml.dom.DOMUtils;
32
33 /***
34 * Abstract base class for a {@link de.fu_berlin.ties.Processor} that writes
35 * {@linkplain org.dom4j.Document XML documents}.
36 *
37 * @author Christian Siefkes
38 * @version $Revision: 1.3 $, $Date: 2006/10/21 16:03:52 $, $Author: siefkes $
39 */
40 public abstract class DocumentWriter 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 DocumentWriter(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(Reader, ContextMap)}.
57 *
58 * @param reader reader containing the text to process; not closed
59 * by this method
60 * @param writer the writer to write the processed text to; flushed
61 * but not closed by this method
62 * @param context a map of objects that are made available for processing
63 * @throws IOException if an I/O error occurs
64 * @throws ProcessingException if an error occurs during processing
65 */
66 protected final void doProcess(final Reader reader, final Writer writer,
67 final ContextMap context) throws IOException, ProcessingException {
68
69 final Document document = process(reader, context);
70 final String charset = (String) context.get(IOUtils.KEY_LOCAL_CHARSET);
71 DOMUtils.writeDocument(document, writer, charset);
72 }
73
74 /***
75 * Processes the contents of a reader and stores the results of processing
76 * in an XML document.
77 *
78 * @param reader reader containing the text to process; not closed
79 * by this method
80 * @param context a map of objects that are made available for processing
81 * @return the created document
82 * @throws IOException if an I/O error occurs
83 * @throws ProcessingException if an error occurs during processing
84 */
85 public abstract Document process(final Reader reader,
86 final ContextMap context) throws IOException, ProcessingException;
87
88 }