View Javadoc

1   /*
2    * Copyright 2009 - 2009 JEuclid, http://jeuclid.sf.net
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  /* $Id: TestSuiteProcessor.java,v c85f3d42bf55 2009/09/01 10:34:30 max $ */
18  
19  package net.sourceforge.jeuclid.testsuite;
20  
21  import javax.xml.transform.Result;
22  import javax.xml.transform.Source;
23  import javax.xml.transform.Transformer;
24  import javax.xml.transform.TransformerConfigurationException;
25  import javax.xml.transform.TransformerFactory;
26  import javax.xml.transform.dom.DOMResult;
27  import javax.xml.transform.dom.DOMSource;
28  import javax.xml.transform.stream.StreamSource;
29  
30  import net.sourceforge.jeuclid.DOMBuilder;
31  import net.sourceforge.jeuclid.MutableLayoutContext;
32  import net.sourceforge.jeuclid.context.LayoutContextImpl;
33  import net.sourceforge.jeuclid.context.Parameter;
34  import net.sourceforge.jeuclid.converter.Processor;
35  
36  import org.apache.commons.logging.Log;
37  import org.apache.commons.logging.LogFactory;
38  import org.w3c.dom.Node;
39  
40  /**
41   * Process the actual TestSuite.
42   * 
43   * @version $Revision: c85f3d42bf55 $
44   */
45  public final class TestSuiteProcessor {
46  
47      private static final float DISPLAY_SIZE = 16.0f;
48  
49      private static final Processor MML2SVGPROCESSOR = Processor.getInstance();
50      /**
51       * Logger for this class
52       */
53      private static final Log LOGGER = LogFactory.getLog(Processor.class);
54  
55      private final Transformer modificationTransformer;
56  
57      private final MutableLayoutContext context;
58  
59      private static final class SingletonHolder {
60          private static final TestSuiteProcessor INSTANCE = new TestSuiteProcessor();
61  
62          private SingletonHolder() {
63          }
64      }
65  
66      private TestSuiteProcessor() {
67          this.context = new LayoutContextImpl(LayoutContextImpl
68                  .getDefaultLayoutContext());
69          this.context.setParameter(Parameter.MATHSIZE,
70                  TestSuiteProcessor.DISPLAY_SIZE);
71          Transformer t;
72          try {
73              t = TransformerFactory.newInstance().newTemplates(
74                      new StreamSource(DOMBuilder.class
75                              .getResourceAsStream("/support/ModifySuite3.xsl")))
76                      .newTransformer();
77          } catch (final TransformerConfigurationException e) {
78              t = null;
79          }
80          this.modificationTransformer = t;
81      }
82  
83      /**
84       * Retrieve the TestSuiteProcessor singleton object.
85       * 
86       * @return the TestSuiteProcessor.
87       */
88      public static TestSuiteProcessor getInstance() {
89          return TestSuiteProcessor.SingletonHolder.INSTANCE;
90      }
91  
92      /**
93       * Process Source testsuite xhtml file to xhtml.
94       * 
95       * @param inputSource
96       *            InputSource
97       * @param result
98       *            Result
99       * @param apply3Mod
100      *            if true apply modifications specific to testsuite3
101      * @return true if something was written to result.
102      */
103     public boolean process(final Source inputSource, final Result result,
104             final boolean apply3Mod) {
105         boolean processed;
106         final DOMResult intermediate = new javax.xml.transform.dom.DOMResult();
107         try {
108             if (apply3Mod && (this.modificationTransformer != null)) {
109                 TestSuiteProcessor.MML2SVGPROCESSOR.process(inputSource,
110                         intermediate);
111                 final Node head = intermediate.getNode();
112                 this.modificationTransformer.transform(new DOMSource(head),
113                         result);
114             } else {
115                 TestSuiteProcessor.MML2SVGPROCESSOR
116                         .process(inputSource, result);
117             }
118             processed = true;
119             // CHECKSTYLE:OFF Here, catching all exceptions is intentional.
120         } catch (final Exception e) {
121             // CHECKSTYLE:ON
122             TestSuiteProcessor.LOGGER.warn(e.getMessage(), e);
123             processed = false;
124         }
125         return processed;
126     }
127 
128 }