1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 }