1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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
53
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
81 }
82
83 @Override
84 public String getEncoding() {
85 return inputSource.getEncoding();
86 }
87
88 }