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 org.jdom2.Document;
22 import org.jdom2.Element;
23 import org.jdom2.transform.JDOMSource;
24 import org.mycore.common.MCRException;
25 import org.mycore.common.xml.MCRURIResolver;
26 import org.w3c.dom.Node;
27
28 import javax.xml.transform.Source;
29 import javax.xml.transform.Transformer;
30 import javax.xml.transform.TransformerException;
31 import javax.xml.transform.TransformerFactory;
32 import javax.xml.transform.dom.DOMSource;
33 import javax.xml.transform.sax.SAXSource;
34 import javax.xml.transform.stream.StreamResult;
35 import javax.xml.transform.stream.StreamSource;
36 import java.io.ByteArrayOutputStream;
37 import java.io.IOException;
38 import java.io.InputStream;
39 import java.net.MalformedURLException;
40 import java.net.URL;
41
42 import jakarta.xml.bind.util.JAXBSource;
43
44
45
46
47 public class MCRSourceContent extends MCRWrappedContent {
48 private static final MCRURIResolver URI_RESOLVER = MCRURIResolver.instance();
49
50 private Source source;
51
52 public MCRSourceContent(Source source) {
53 if (source == null) {
54 throw new NullPointerException("Source cannot be null");
55 }
56 this.source = source;
57 MCRContent baseContent = null;
58 if (source instanceof JDOMSource) {
59 JDOMSource src = (JDOMSource) source;
60 Document xml = src.getDocument();
61 if (xml != null) {
62 baseContent = new MCRJDOMContent(xml);
63 } else {
64 for (Object node : src.getNodes()) {
65 if (node instanceof Element) {
66 Element element = (Element) node;
67 Document doc = element.getDocument();
68 if (doc == null) {
69 baseContent = new MCRJDOMContent(element);
70 } else {
71 if (doc.getRootElement() == element) {
72 baseContent = new MCRJDOMContent(doc);
73 } else {
74 baseContent = new MCRJDOMContent(element.clone());
75 }
76 }
77 break;
78 } else if (node instanceof Document) {
79 baseContent = new MCRJDOMContent((Document) node);
80 break;
81 }
82 }
83 }
84 } else if (source instanceof JAXBSource) {
85 TransformerFactory transformerFactory = TransformerFactory.newDefaultInstance();
86 try {
87 Transformer transformer = transformerFactory.newTransformer();
88 ByteArrayOutputStream bout = new ByteArrayOutputStream();
89 transformer.transform(source, new StreamResult(bout));
90 baseContent = new MCRByteContent(bout.toByteArray());
91 } catch (TransformerException e) {
92 throw new MCRException("Error while resolving JAXBSource", e);
93 }
94 } else if (source instanceof SAXSource) {
95 SAXSource src = (SAXSource) source;
96 baseContent = new MCRSAXContent(src.getXMLReader(), src.getInputSource());
97 } else if (source instanceof DOMSource) {
98 Node node = ((DOMSource) source).getNode();
99 baseContent = new MCRDOMContent(node.getOwnerDocument());
100 } else if (source instanceof StreamSource) {
101 InputStream inputStream = ((StreamSource) source).getInputStream();
102 if (inputStream != null) {
103 baseContent = new MCRStreamContent(inputStream);
104 } else {
105 try {
106 URL url = new URL(source.getSystemId());
107 baseContent = new MCRURLContent(url);
108 } catch (MalformedURLException e) {
109 throw new MCRException("Could not create instance of MCRURLContent for SYSTEMID: "
110 + source.getSystemId(), e);
111 }
112 }
113 }
114 if (baseContent == null) {
115 throw new MCRException("Could not get MCRContent from " + source.getClass().getCanonicalName()
116 + ", systemId:" + source.getSystemId());
117 }
118 baseContent.setSystemId(getSystemId());
119 this.setBaseContent(baseContent);
120 }
121
122
123
124
125
126
127
128 public static MCRSourceContent getInstance(String uri) throws TransformerException {
129 Source source = URI_RESOLVER.resolve(uri, null);
130 if (source == null) {
131 return null;
132 }
133 return new MCRSourceContent(source);
134 }
135
136 @Override
137 public String getSystemId() {
138 return source.getSystemId();
139 }
140
141 @Override
142 public Source getSource() throws IOException {
143 return source;
144 }
145 }