View Javadoc

1   /*
2    * Copyright 2007 - 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: Servlet3.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 v3, rendering all formulas with JEuclid to
36   * SVG.
37   * 
38   * @version $Revision: 92549dfb4024 $
39   */
40  public class Servlet3 extends HttpServlet {
41  
42      private static final int BLOCK_SIZE = 4096;
43  
44      /**
45       * 
46       */
47      private static final long serialVersionUID = 1L;
48  
49      private static final TestSuiteProcessor TSP = TestSuiteProcessor
50              .getInstance();
51  
52      /**
53       * Default Constructor.
54       */
55      public Servlet3() {
56      }
57  
58      /** {@inheritDoc} */
59      @Override
60      protected void doGet(final HttpServletRequest req,
61              final HttpServletResponse resp) throws ServletException,
62              IOException {
63          final String file = req.getPathInfo();
64          final InputStream stream = Thread.currentThread()
65                  .getContextClassLoader().getResourceAsStream(
66                          "mml3-testsuite/" + file);
67          if (stream == null) {
68              resp.sendError(HttpServletResponse.SC_NOT_FOUND, file);
69          } else {
70              final OutputStream out = resp.getOutputStream();
71  
72              boolean processed = false;
73              if (file.endsWith(".xhtml")) {
74                  final Source inputSource = new StreamSource(stream);
75                  final Result result = new StreamResult(out);
76                  processed = Servlet3.TSP.process(inputSource, result, true);
77              }
78              if (!processed) {
79                  final byte[] buf = new byte[Servlet3.BLOCK_SIZE];
80                  int count = stream.read(buf);
81                  while (count > -1) {
82                      out.write(buf, 0, count);
83                      count = stream.read(buf);
84                  }
85              }
86          }
87      }
88  
89  }