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