View Javadoc

1   /*
2    * Copyright (C) 2004 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 library is free software; you can redistribute it and/or
8    * modify it under the terms of the GNU Lesser General Public
9    * License as published by the Free Software Foundation; either
10   * version 2.1 of the License, or (at your option) any later version.
11   *
12   * This library 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 GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this library; if not, visit
19   * http://www.gnu.org/licenses/lgpl.html or write to the Free Software
20   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, 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  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.4 $, $Date: 2004/03/11 16:18:22 $, $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              // reader should contain a well-formed document
73              document = DOMUtils.readDocument(reader);
74          } catch (DocumentException de) {
75              // wrap exception
76              throw new ParsingException(de);
77          }
78  
79          // delegate to abstract method
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  }