001    /*
002     * Copyright 2009 - 2009 JEuclid, http://jeuclid.sf.net
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    /* $Id: TestSuiteProcessor.java,v c85f3d42bf55 2009/09/01 10:34:30 max $ */
018    
019    package net.sourceforge.jeuclid.testsuite;
020    
021    import javax.xml.transform.Result;
022    import javax.xml.transform.Source;
023    import javax.xml.transform.Transformer;
024    import javax.xml.transform.TransformerConfigurationException;
025    import javax.xml.transform.TransformerFactory;
026    import javax.xml.transform.dom.DOMResult;
027    import javax.xml.transform.dom.DOMSource;
028    import javax.xml.transform.stream.StreamSource;
029    
030    import net.sourceforge.jeuclid.DOMBuilder;
031    import net.sourceforge.jeuclid.MutableLayoutContext;
032    import net.sourceforge.jeuclid.context.LayoutContextImpl;
033    import net.sourceforge.jeuclid.context.Parameter;
034    import net.sourceforge.jeuclid.converter.Processor;
035    
036    import org.apache.commons.logging.Log;
037    import org.apache.commons.logging.LogFactory;
038    import org.w3c.dom.Node;
039    
040    /**
041     * Process the actual TestSuite.
042     * 
043     * @version $Revision: c85f3d42bf55 $
044     */
045    public final class TestSuiteProcessor {
046    
047        private static final float DISPLAY_SIZE = 16.0f;
048    
049        private static final Processor MML2SVGPROCESSOR = Processor.getInstance();
050        /**
051         * Logger for this class
052         */
053        private static final Log LOGGER = LogFactory.getLog(Processor.class);
054    
055        private final Transformer modificationTransformer;
056    
057        private final MutableLayoutContext context;
058    
059        private static final class SingletonHolder {
060            private static final TestSuiteProcessor INSTANCE = new TestSuiteProcessor();
061    
062            private SingletonHolder() {
063            }
064        }
065    
066        private TestSuiteProcessor() {
067            this.context = new LayoutContextImpl(LayoutContextImpl
068                    .getDefaultLayoutContext());
069            this.context.setParameter(Parameter.MATHSIZE,
070                    TestSuiteProcessor.DISPLAY_SIZE);
071            Transformer t;
072            try {
073                t = TransformerFactory.newInstance().newTemplates(
074                        new StreamSource(DOMBuilder.class
075                                .getResourceAsStream("/support/ModifySuite3.xsl")))
076                        .newTransformer();
077            } catch (final TransformerConfigurationException e) {
078                t = null;
079            }
080            this.modificationTransformer = t;
081        }
082    
083        /**
084         * Retrieve the TestSuiteProcessor singleton object.
085         * 
086         * @return the TestSuiteProcessor.
087         */
088        public static TestSuiteProcessor getInstance() {
089            return TestSuiteProcessor.SingletonHolder.INSTANCE;
090        }
091    
092        /**
093         * Process Source testsuite xhtml file to xhtml.
094         * 
095         * @param inputSource
096         *            InputSource
097         * @param result
098         *            Result
099         * @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    }