View Javadoc

1   /*
2    * Copyright (C) 2005-2006 Christian Siefkes <christian@siefkes.net>.
3    * Development of this software is supported by the German Research Society,
4    * Berlin-Brandenburg Graduate School in Distributed Information Systems
5    * (DFG grant no. GRK 316).
6    *
7    * This program is free software; you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation; either version 2 of the License, or
10   * (at your option) any later version.
11   *
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program; if not, visit
19   * http://www.gnu.org/licenses/gpl.html or write to the Free Software
20   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
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          // delegate to abstract method + write result to writer
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  }