1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.mycore.common.xsl;
20
21 import java.io.IOException;
22 import java.net.URL;
23
24 import javax.xml.parsers.ParserConfigurationException;
25 import javax.xml.transform.sax.SAXSource;
26
27 import org.apache.logging.log4j.LogManager;
28 import org.apache.logging.log4j.Logger;
29 import org.mycore.common.MCRCache;
30 import org.mycore.common.MCRClassTools;
31 import org.mycore.common.config.MCRConfigurationDir;
32 import org.mycore.common.xml.MCREntityResolver;
33 import org.mycore.common.xml.MCRXMLParserFactory;
34 import org.mycore.common.xml.MCRXMLResource;
35 import org.xml.sax.InputSource;
36 import org.xml.sax.SAXException;
37 import org.xml.sax.XMLReader;
38
39
40
41
42
43
44
45
46
47 public class MCRTemplatesSource {
48
49 private static final Logger LOGGER = LogManager.getLogger(MCRTemplatesSource.class);
50
51
52 private String resource;
53
54
55
56
57 public MCRTemplatesSource(String resource) {
58 this.resource = resource;
59 }
60
61
62 public SAXSource getSource() throws SAXException, ParserConfigurationException {
63 XMLReader reader = MCRXMLParserFactory.getNonValidatingParser().getXMLReader();
64 reader.setEntityResolver(MCREntityResolver.instance());
65 URL resourceURL = MCRConfigurationDir.getConfigResource(resource);
66 if (resourceURL == null) {
67 throw new SAXException("Could not find resource: " + resource);
68 }
69 InputSource input = new InputSource(resourceURL.toString());
70 return new SAXSource(reader, input);
71 }
72
73
74 public String getKey() {
75 return resource;
76 }
77
78 @Override
79 public String toString() {
80 return resource;
81 }
82
83 public URL getURL() {
84 try {
85 return MCRXMLResource.instance().getURL(resource, MCRClassTools.getClassLoader());
86 } catch (IOException e) {
87 LOGGER.warn("Could not determine URL of resource {}", resource, e);
88 return null;
89 }
90 }
91
92
93 public long getLastModified() {
94 try {
95 return MCRXMLResource.instance().getLastModified(resource, MCRClassTools.getClassLoader());
96 } catch (IOException e) {
97 LOGGER.warn("Could not determine last modified date of resource {}", resource);
98 return -1;
99 }
100 }
101
102 public MCRCache.ModifiedHandle getModifiedHandle(long checkPeriod) {
103 return MCRXMLResource.instance().getModifiedHandle(resource, MCRClassTools.getClassLoader(),
104 checkPeriod);
105 }
106 }