View Javadoc
1   /**
2    * Copyright (C) 2014-2015 Philip Helger (www.helger.com)
3    * philip[at]helger[dot]com
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    *         http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package com.helger.schematron.xslt;
18  
19  import javax.annotation.Nullable;
20  import javax.annotation.concurrent.NotThreadSafe;
21  import javax.xml.transform.ErrorListener;
22  import javax.xml.transform.Templates;
23  import javax.xml.transform.Transformer;
24  import javax.xml.transform.TransformerConfigurationException;
25  import javax.xml.transform.TransformerFactory;
26  import javax.xml.transform.URIResolver;
27  
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  import org.w3c.dom.Document;
31  
32  import com.helger.commons.io.resource.IReadableResource;
33  import com.helger.commons.xml.serialize.read.DOMReader;
34  import com.helger.commons.xml.transform.DefaultTransformURIResolver;
35  import com.helger.commons.xml.transform.TransformSourceFactory;
36  import com.helger.commons.xml.transform.XMLTransformerFactory;
37  
38  /**
39   * This Schematron validator factory uses an existing, precompiled Schematron
40   * XSLT for validation.
41   *
42   * @author Philip Helger
43   */
44  @NotThreadSafe
45  final class SchematronProviderXSLTPrebuild implements ISchematronXSLTBasedProvider
46  {
47    private static final Logger s_aLogger = LoggerFactory.getLogger (SchematronProviderXSLTPrebuild.class);
48    private Document m_aSchematronXSLTDoc;
49    private Templates m_aSchematronXSLTTemplates;
50  
51    public SchematronProviderXSLTPrebuild (@Nullable final IReadableResource aXSLTResource,
52                                           @Nullable final ErrorListener aCustomErrorListener,
53                                           @Nullable final URIResolver aCustomURIResolver)
54    {
55      try
56      {
57        // Read XSLT file as XML
58        m_aSchematronXSLTDoc = DOMReader.readXMLDOM (aXSLTResource);
59  
60        // compile result of read file
61        final TransformerFactory aTF = XMLTransformerFactory.createTransformerFactory (aCustomErrorListener,
62                                                                                       new DefaultTransformURIResolver (aCustomURIResolver));
63        m_aSchematronXSLTTemplates = aTF.newTemplates (TransformSourceFactory.create (m_aSchematronXSLTDoc));
64      }
65      catch (final Exception ex)
66      {
67        s_aLogger.error ("XSLT read/compilation error for " + aXSLTResource, ex);
68      }
69    }
70  
71    public boolean isValidSchematron ()
72    {
73      return m_aSchematronXSLTTemplates != null;
74    }
75  
76    @Nullable
77    public Document getXSLTDocument ()
78    {
79      return m_aSchematronXSLTDoc;
80    }
81  
82    @Nullable
83    public Transformer getXSLTTransformer () throws TransformerConfigurationException
84    {
85      return m_aSchematronXSLTTemplates == null ? null : m_aSchematronXSLTTemplates.newTransformer ();
86    }
87  }