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.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.5 $, $Date: 2004/08/30 15:40:21 $, $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.
55       *
56       * @param document the document to process
57       * @param context a map of objects that are made available for processing
58       * @throws IOException if an I/O error occurs
59       * @throws ProcessingException if an error occurs during processing
60       */
61      public abstract void process(final Document document,
62              final ContextMap context) throws IOException, ProcessingException;
63  
64      /***
65       * Processes an XML document, writing a modified version to a writer.
66       * This implementation delegates to {@link #process(Document, ContextMap)}.
67       *
68       * @param document the document to process
69       * @param writer the writer to write the processed text to; flushed
70       * but not closed by this method
71       * @param context a map of objects that are made available for processing;
72       * <strong>must</strong> contain a mapping from
73       * {@link IOUtils#KEY_LOCAL_CHARSET} to the character set of the
74       * <code>writer</code>
75       * @throws IOException if an I/O error occurs
76       * @throws ProcessingException if an error occurs during processing
77       */
78      public final void process(final Document document, final Writer writer,
79              final ContextMap context) throws IOException, ProcessingException {
80          // delegate to abstract method + serialize result
81          process(document, context);
82          DOMUtils.writeDocument(document, writer,
83              (String) context.get(IOUtils.KEY_LOCAL_CHARSET));
84      }
85  
86  }