View Javadoc
1   /**
2    * Copyright (C) 2014-2017 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 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.ext.CommonsLinkedHashMap;
30  import com.helger.commons.collection.ext.ICommonsOrderedMap;
31  import com.helger.xml.transform.LoggingTransformErrorListener;
32  
33  /**
34   * A wrapper for easier customization of the SCH to XSLT transformation.
35   *
36   * @author Philip Helger
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, ?> 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     // Ensure an error listener is present
135     if (m_aCustomErrorListener != null)
136       aTransformer.setErrorListener (m_aCustomErrorListener);
137     else
138       aTransformer.setErrorListener (new LoggingTransformErrorListener (Locale.US));
139 
140     // Set the optional URI Resolver
141     if (m_aCustomURIResolver != null)
142       aTransformer.setURIResolver (m_aCustomURIResolver);
143 
144     // Set all custom parameters
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       // On the last step, set the respective Schematron parameters as the
152       // last action to avoid they are overwritten by a custom parameter.
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 }