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.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   * Represents an XSL file that will be used in XSL transformation and which is loaded
41   * as a resource. The object provides helper methods to support caching of the compiled 
42   * templates file.
43   * 
44   * @author Thomas Scheffler (yagee) 
45   * @author Frank L\u00FCtzenkirchen
46   */
47  public class MCRTemplatesSource {
48  
49      private static final Logger LOGGER = LogManager.getLogger(MCRTemplatesSource.class);
50  
51      /** The path to the XSL resource */
52      private String resource;
53  
54      /**
55       * @param resource the path to the XSL file, which will be loaded as a resource
56       */
57      public MCRTemplatesSource(String resource) {
58          this.resource = resource;
59      }
60  
61      /** Have to use SAX here to resolve entities */
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      /** Returns the path to the XSL file, for use as a caching key */
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      /** Returns the timestamp the XSL file was last modified on the filesystem. */
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 }