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  import java.io.OutputStream;
21  
22  import org.apache.commons.io.CopyUtils;
23  import org.apache.commons.io.IOUtils;
24  
25  import net.sf.exorcist.api.ContentException;
26  import net.sf.exorcist.api.ContentImporter;
27  import net.sf.exorcist.api.ContentState;
28  
29  /***
30   * Content imports that imports the content XML document to
31   * a given output stream. 
32   */
33  public class XmlImporter implements ContentImporter {
34  
35      /*** The XML document stream. */
36      private OutputStream content;
37  
38      /***
39       * Sets the XML document stream to be imported.
40       *
41       * @param content XML document stream
42       */
43      public void setContent(OutputStream content) {
44          this.content = content;
45      }
46  
47      /***
48       * Copies the XML document from the given content state to the
49       * configured content stream.
50       * <p>
51       * Note that at the moment this method <em>does not</em> parse or
52       * validate the XML document stream.
53       *
54       * @param state the content state
55       * @throws ContentException if the content stream can not be written
56       * @see ContentImporter#importContent(ContentState)
57       */
58      public void importContent(ContentState state) throws ContentException {
59          try {
60              InputStream xml = state.getContent();
61              try {
62                  CopyUtils.copy(xml, content);
63              } finally {
64                  IOUtils.closeQuietly(xml);
65              }
66          } catch (IOException e) {
67              throw new ContentException(e);
68          } finally {
69              IOUtils.closeQuietly(content);
70          }
71      }
72  
73  }