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 java.util.Locale;
20 import java.util.Map;
21
22 import javax.annotation.Nonnull;
23 import javax.annotation.Nullable;
24 import javax.xml.transform.ErrorListener;
25 import javax.xml.transform.Transformer;
26 import javax.xml.transform.URIResolver;
27
28 import com.helger.commons.annotation.ReturnsMutableCopy;
29 import com.helger.commons.collection.impl.CommonsLinkedHashMap;
30 import com.helger.commons.collection.impl.ICommonsOrderedMap;
31 import com.helger.xml.transform.LoggingTransformErrorListener;
32
33
34
35
36
37
38 public class SCHTransformerCustomizer
39 {
40 public static enum EStep
41 {
42 SCH2XSLT_1,
43 SCH2XSLT_2,
44 SCH2XSLT_3;
45 }
46
47 private ErrorListener m_aCustomErrorListener;
48 private URIResolver m_aCustomURIResolver;
49 private ICommonsOrderedMap <String, Object> m_aCustomParameters;
50 private String m_sPhase;
51 private String m_sLanguageCode;
52
53 public SCHTransformerCustomizer ()
54 {}
55
56 @Nullable
57 public ErrorListener getErrorListener ()
58 {
59 return m_aCustomErrorListener;
60 }
61
62 @Nonnull
63 public SCHTransformerCustomizer setErrorListener (@Nullable final ErrorListener aCustomErrorListener)
64 {
65 m_aCustomErrorListener = aCustomErrorListener;
66 return this;
67 }
68
69 @Nullable
70 public URIResolver getURIResolver ()
71 {
72 return m_aCustomURIResolver;
73 }
74
75 @Nonnull
76 public SCHTransformerCustomizer setURIResolver (@Nullable final URIResolver aCustomURIResolver)
77 {
78 m_aCustomURIResolver = aCustomURIResolver;
79 return this;
80 }
81
82 public boolean hasParameters ()
83 {
84 return m_aCustomParameters != null && m_aCustomParameters.isNotEmpty ();
85 }
86
87 @Nonnull
88 @ReturnsMutableCopy
89 public ICommonsOrderedMap <String, ?> getParameters ()
90 {
91 return new CommonsLinkedHashMap <> (m_aCustomParameters);
92 }
93
94 @Nonnull
95 public SCHTransformerCustomizer setParameters (@Nullable final Map <String, ?> aCustomParameters)
96 {
97 m_aCustomParameters = new CommonsLinkedHashMap <> (aCustomParameters);
98 return this;
99 }
100
101 @Nullable
102 public String getPhase ()
103 {
104 return m_sPhase;
105 }
106
107 @Nonnull
108 public SCHTransformerCustomizer setPhase (@Nullable final String sPhase)
109 {
110 m_sPhase = sPhase;
111 return this;
112 }
113
114 @Nullable
115 public String getLanguageCode ()
116 {
117 return m_sLanguageCode;
118 }
119
120 @Nonnull
121 public SCHTransformerCustomizer setLanguageCode (@Nullable final String sLanguageCode)
122 {
123 m_sLanguageCode = sLanguageCode;
124 return this;
125 }
126
127 public boolean canCacheResult ()
128 {
129 return !hasParameters ();
130 }
131
132 public void customize (@Nonnull final EStep eStep, @Nonnull final Transformer aTransformer)
133 {
134
135 if (m_aCustomErrorListener != null)
136 aTransformer.setErrorListener (m_aCustomErrorListener);
137 else
138 aTransformer.setErrorListener (new LoggingTransformErrorListener (Locale.US));
139
140
141 if (m_aCustomURIResolver != null)
142 aTransformer.setURIResolver (m_aCustomURIResolver);
143
144
145 if (m_aCustomParameters != null)
146 for (final Map.Entry <String, ?> aEntry : m_aCustomParameters.entrySet ())
147 aTransformer.setParameter (aEntry.getKey (), aEntry.getValue ());
148
149 if (eStep == EStep.SCH2XSLT_3)
150 {
151
152
153 if (m_sPhase != null)
154 aTransformer.setParameter ("phase", m_sPhase);
155
156 if (m_sLanguageCode != null)
157 aTransformer.setParameter ("langCode", m_sLanguageCode);
158 }
159 }
160 }