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