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