1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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.schematron.saxon.SchematronTransformerFactory;
34 import com.helger.xml.serialize.read.DOMReader;
35 import com.helger.xml.transform.DefaultTransformURIResolver;
36 import com.helger.xml.transform.TransformSourceFactory;
37
38
39
40
41
42
43
44 @NotThreadSafe
45 public 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
58 m_aSchematronXSLTDoc = DOMReader.readXMLDOM (aXSLTResource);
59
60
61 final TransformerFactory aTF = SchematronTransformerFactory.createTransformerFactorySaxonFirst ((ClassLoader) null,
62 aCustomErrorListener,
63 new DefaultTransformURIResolver (aCustomURIResolver));
64 m_aSchematronXSLTTemplates = aTF.newTemplates (TransformSourceFactory.create (m_aSchematronXSLTDoc));
65 }
66 catch (final Exception ex)
67 {
68 s_aLogger.error ("XSLT read/compilation error for " + aXSLTResource, ex);
69 }
70 }
71
72 public boolean isValidSchematron ()
73 {
74 return m_aSchematronXSLTTemplates != null;
75 }
76
77 @Nullable
78 public Document getXSLTDocument ()
79 {
80 return m_aSchematronXSLTDoc;
81 }
82
83 @Nullable
84 public Transformer getXSLTTransformer () throws TransformerConfigurationException
85 {
86 return m_aSchematronXSLTTemplates == null ? null : m_aSchematronXSLTTemplates.newTransformer ();
87 }
88 }