View Javadoc

1   /*
2    * Copyright (C) 2004-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.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          // delegate to abstract method + serialize result
86          final Document processedDoc = process(document, context);
87          DOMUtils.writeDocument(processedDoc, writer,
88              (String) context.get(IOUtils.KEY_LOCAL_CHARSET));
89      }
90  
91  }