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.multi;
17  
18  import java.io.File;
19  import java.io.FileInputStream;
20  import java.io.FileOutputStream;
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.io.OutputStream;
24  import java.util.Collection;
25  
26  import javax.xml.parsers.ParserConfigurationException;
27  import javax.xml.parsers.SAXParser;
28  import javax.xml.parsers.SAXParserFactory;
29  
30  import net.sf.exorcist.api.ContentState;
31  
32  import org.apache.commons.io.IOUtils;
33  import org.apache.xml.serialize.OutputFormat;
34  import org.apache.xml.serialize.XMLSerializer;
35  import org.xml.sax.ContentHandler;
36  import org.xml.sax.SAXException;
37  
38  /***
39   * TODO
40   */
41  public class MultiState implements ContentState {
42  
43      private static final String NSURI = "http://exorcist.sf.net/ns/2005/multi";
44  
45      private final ContentState state;
46  
47      private final SAXParser parser;
48  
49      private File xml;
50  
51      private OutputStream output;
52  
53      private ContentHandler handler;
54      
55      public MultiState(ContentState state)
56              throws ParserConfigurationException, SAXException, IOException {
57          this.state = state;
58          SAXParserFactory factory = SAXParserFactory.newInstance();
59          factory.setNamespaceAware(true);
60          this.parser = factory.newSAXParser();
61          this.xml = File.createTempFile("exorcist", ".xml");
62          this.xml.deleteOnExit();
63          this.output = null;
64          this.handler = null;
65      }
66  
67      public void startCollection() throws IOException, SAXException {
68          output = new FileOutputStream(xml);
69          
70          OutputFormat format = new OutputFormat();
71          format.setIndenting(true);
72          XMLSerializer serializer = new XMLSerializer(output, format); 
73          handler = serializer.asContentHandler();
74          
75          handler.startPrefixMapping(null, NSURI);
76          handler.startDocument();
77          handler.startElement(NSURI, "multi", "multi", null);
78      }
79  
80      public void endCollection() throws IOException, SAXException {
81          handler.endElement(NSURI, "multi", "multi");
82          handler.endDocument();
83          handler.endPrefixMapping(null);
84  
85          output.close();
86  
87          InputStream input = new FileInputStream(xml);
88          try {
89              state.setContent(input);
90          } finally {
91              IOUtils.closeQuietly(input);
92          }
93      }
94  
95      /*** {@inheritDoc} */
96      public void setContent(InputStream content) throws IOException {
97          try {
98              parser.parse(content, new MultiHandler(handler));
99          } catch (SAXException e) {
100             throw new IOException(e.getMessage());
101         }
102     }
103 
104     /*** {@inheritDoc} */
105     public InputStream getContent() throws IOException {
106         return state.getContent();
107     }
108 
109     /*** {@inheritDoc} */
110     public String addAttachment(InputStream data) throws IOException {
111         return state.addAttachment(data);
112     }
113 
114     /*** {@inheritDoc} */
115     public Collection getAttachmentHashes() {
116         return state.getAttachmentHashes();
117     }
118 
119     /*** {@inheritDoc} */
120     public InputStream getAttachment(String hash) throws IOException,
121             IllegalArgumentException {
122         return state.getAttachment(hash);
123     }
124 
125     /*** {@inheritDoc} */
126     public void removeAttachment(String hash) throws IllegalArgumentException {
127         // state.removeAttachment(hash);
128     }
129 
130 }