001/*
002 * Copyright 2007 - 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$ */
018
019package net.sourceforge.jeuclid.testsuite;
020
021import java.io.IOException;
022import java.io.InputStream;
023import java.io.OutputStream;
024
025import javax.servlet.ServletException;
026import javax.servlet.http.HttpServlet;
027import javax.servlet.http.HttpServletRequest;
028import javax.servlet.http.HttpServletResponse;
029import javax.xml.transform.Result;
030import javax.xml.transform.Source;
031import javax.xml.transform.stream.StreamResult;
032import javax.xml.transform.stream.StreamSource;
033
034/**
035 * Serve the W3C MathML Testsuite v3, rendering all formulas with JEuclid to
036 * SVG.
037 * 
038 * @version $Revision$
039 */
040public class Servlet3 extends HttpServlet {
041
042    private static final int BLOCK_SIZE = 4096;
043
044    /**
045     * 
046     */
047    private static final long serialVersionUID = 1L;
048
049    private static final TestSuiteProcessor TSP = TestSuiteProcessor
050            .getInstance();
051
052    /**
053     * Default Constructor.
054     */
055    public Servlet3() {
056    }
057
058    /** {@inheritDoc} */
059    @Override
060    protected void doGet(final HttpServletRequest req,
061            final HttpServletResponse resp) throws ServletException,
062            IOException {
063        final String file = req.getPathInfo();
064        final InputStream stream = Thread.currentThread()
065                .getContextClassLoader().getResourceAsStream(
066                        "mml3-testsuite/" + file);
067        if (stream == null) {
068            resp.sendError(HttpServletResponse.SC_NOT_FOUND, file);
069        } else {
070            final OutputStream out = resp.getOutputStream();
071
072            boolean processed = false;
073            if (file.endsWith(".xhtml")) {
074                final Source inputSource = new StreamSource(stream);
075                final Result result = new StreamResult(out);
076                processed = Servlet3.TSP.process(inputSource, result, true);
077            }
078            if (!processed) {
079                final byte[] buf = new byte[Servlet3.BLOCK_SIZE];
080                int count = stream.read(buf);
081                while (count > -1) {
082                    out.write(buf, 0, count);
083                    count = stream.read(buf);
084                }
085            }
086        }
087    }
088
089}