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.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
40
41
42
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
58 m_aSchematronXSLTDoc = DOMReader.readXMLDOM (aXSLTResource);
59
60
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 }