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.Writer;
26
27 import org.dom4j.Document;
28
29 import de.fu_berlin.ties.io.IOUtils;
30 import de.fu_berlin.ties.xml.dom.DOMUtils;
31
32 /***
33 * Abstract base class for a {@link de.fu_berlin.ties.Processor} that operates
34 * on {@linkplain org.dom4j.Document XML documents}.
35 *
36 * @author Christian Siefkes
37 * @version $Revision: 1.9 $, $Date: 2006/10/21 16:03:52 $, $Author: siefkes $
38 */
39 public abstract class DocumentProcessor extends DocumentReader {
40
41 /***
42 * Creates a new instance.
43 *
44 * @param outExt the extension to use for output files.
45 * @param conf used to configure this instance; if <code>null</code>,
46 * the {@linkplain TiesConfiguration#CONF standard configuration} is used
47 */
48 public DocumentProcessor(final String outExt,
49 final TiesConfiguration conf) {
50 super(outExt, conf);
51 }
52
53 /***
54 * Processes an XML document. Callers <strong>must</strong> always continue
55 * working on the <strong>returned document</strong> instance instead of the
56 * passed-in instance -- document processors are allowed to modify the
57 * document in-place, but this is not required.
58 *
59 * @param document the document to process
60 * @param context a map of objects that are made available for processing
61 * @return the processed document; this object may or may not be identical
62 * to the <code>document</code> passed it.
63 * @throws IOException if an I/O error occurs
64 * @throws ProcessingException if an error occurs during processing
65 */
66 public abstract Document process(final Document document,
67 final ContextMap context) throws IOException, ProcessingException;
68
69 /***
70 * Processes an XML document, writing a modified version to a writer.
71 * This implementation delegates to {@link #process(Document, ContextMap)}.
72 *
73 * @param document the document to process
74 * @param writer the writer to write the processed text to; flushed
75 * but not closed by this method
76 * @param context a map of objects that are made available for processing;
77 * <strong>must</strong> contain a mapping from
78 * {@link IOUtils#KEY_LOCAL_CHARSET} to the character set of the
79 * <code>writer</code>
80 * @throws IOException if an I/O error occurs
81 * @throws ProcessingException if an error occurs during processing
82 */
83 public final void process(final Document document, final Writer writer,
84 final ContextMap context) throws IOException, ProcessingException {
85
86 final Document processedDoc = process(document, context);
87 DOMUtils.writeDocument(processedDoc, writer,
88 (String) context.get(IOUtils.KEY_LOCAL_CHARSET));
89 }
90
91 }