View Javadoc

1   /*
2    * Copyright 2005 Jukka Zitting <jz@yukatan.fi>
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sf.exorcist.xml;
17  
18  import java.io.IOException;
19  import java.io.InputStream;
20  
21  import org.apache.commons.io.IOUtils;
22  
23  import net.sf.exorcist.api.ContentException;
24  import net.sf.exorcist.api.ContentExporter;
25  import net.sf.exorcist.api.ContentState;
26  
27  /***
28   * Content exporter that exports the content XML document from
29   * a given input stream. 
30   */
31  public class XmlExporter implements ContentExporter {
32  
33      /*** The XML document stream. */
34      private InputStream content;
35  
36      /***
37       * Sets the XML document stream to be exported.
38       *
39       * @param content XML document stream
40       */
41      public void setContent(InputStream content) {
42          this.content = content;
43      }
44  
45      /***
46       * Copies the XML document from the configured content stream into
47       * the given content state.
48       * <p>
49       * Note that at the moment this method <em>does not</em> parse or
50       * validate the XML document stream.
51       *
52       * @param state the content state
53       * @throws ContentException if the content stream can not be read
54       * @see ContentExporter#exportContent(ContentState)
55       */
56      public void exportContent(ContentState state) throws ContentException {
57          try {
58              state.setContent(content);
59          } catch (IOException e) {
60              throw new ContentException(e);
61          } finally {
62              IOUtils.closeQuietly(content);
63          }
64      }
65  
66  }