View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  package org.mycore.common.content;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.io.UnsupportedEncodingException;
24  
25  import javax.xml.transform.Source;
26  import javax.xml.transform.sax.SAXSource;
27  
28  import org.jdom2.Document;
29  import org.jdom2.JDOMException;
30  import org.jdom2.input.sax.SAXHandler;
31  import org.xml.sax.InputSource;
32  import org.xml.sax.SAXException;
33  import org.xml.sax.XMLReader;
34  
35  /**
36   * @author Thomas Scheffler (yagee)
37   *
38   */
39  public class MCRSAXContent extends MCRXMLContent {
40  
41      private XMLReader xmlReader;
42  
43      private InputSource inputSource;
44  
45      public MCRSAXContent(XMLReader xmlReader, InputSource inputSource) {
46          super();
47          this.xmlReader = xmlReader;
48          this.inputSource = inputSource;
49          setSystemId(inputSource.getSystemId());
50      }
51  
52      /* (non-Javadoc)
53       * @see org.mycore.common.content.MCRContent#getInputStream()
54       */
55      @Override
56      public InputStream getInputStream() throws IOException {
57          return inputSource.getByteStream();
58      }
59  
60      @Override
61      public Source getSource() throws IOException {
62          return new SAXSource(this.xmlReader, this.inputSource);
63      }
64  
65      @Override
66      public MCRContent ensureXML() {
67          return this;
68      }
69  
70      @Override
71      public Document asXML() throws JDOMException, IOException, SAXException {
72          SAXHandler jdomContentHandler = new SAXHandler();
73          xmlReader.setContentHandler(jdomContentHandler);
74          xmlReader.parse(inputSource);
75          return jdomContentHandler.getDocument();
76      }
77  
78      @Override
79      public void setEncoding(String encoding) throws UnsupportedEncodingException {
80          //defined by inputSource
81      }
82  
83      @Override
84      public String getEncoding() {
85          return inputSource.getEncoding();
86      }
87  
88  }