View Javadoc

1   /*
2    * Copyright 2007 - 2007 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: Servlet.java,v 92549dfb4024 2009/09/01 09:06:26 max $ */
18  
19  package net.sourceforge.jeuclid.testsuite;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.io.OutputStream;
24  
25  import javax.servlet.ServletException;
26  import javax.servlet.http.HttpServlet;
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpServletResponse;
29  import javax.xml.transform.Result;
30  import javax.xml.transform.Source;
31  import javax.xml.transform.stream.StreamResult;
32  import javax.xml.transform.stream.StreamSource;
33  
34  /**
35   * Serve the W3C MathML Testsuite, rendering all formulas with JEuclid to SVG.
36   * 
37   * @version $Revision: 92549dfb4024 $
38   */
39  public class Servlet extends HttpServlet {
40      private static final int BLOCK_SIZE = 4096;
41  
42      private static final TestSuiteProcessor TSP = TestSuiteProcessor
43              .getInstance();
44  
45      /**
46       * 
47       */
48      private static final long serialVersionUID = 1L;
49  
50      /**
51       * Default Constructor.
52       */
53      public Servlet() {
54      }
55  
56      /** {@inheritDoc} */
57      @Override
58      protected void doGet(final HttpServletRequest req,
59              final HttpServletResponse resp) throws ServletException,
60              IOException {
61          final String file = req.getPathInfo();
62          final InputStream stream = Thread.currentThread()
63                  .getContextClassLoader().getResourceAsStream(
64                          "mml2-testsuite/" + file);
65          if (stream == null) {
66              resp.sendError(HttpServletResponse.SC_NOT_FOUND, file);
67          } else {
68              final OutputStream out = resp.getOutputStream();
69  
70              boolean processed = false;
71              if (file.endsWith(".xml")) {
72                  final Source inputSource = new StreamSource(stream);
73                  final Result result = new StreamResult(out);
74                  processed = Servlet.TSP.process(inputSource, result, false);
75              }
76              if (!processed) {
77                  final byte[] buf = new byte[Servlet.BLOCK_SIZE];
78                  int count = stream.read(buf);
79                  while (count > -1) {
80                      out.write(buf, 0, count);
81                      count = stream.read(buf);
82                  }
83              }
84          }
85      }
86  }