View Javadoc

1   package net.trajano.twiff.renderer;
2   
3   import java.io.IOException;
4   import java.io.InputStream;
5   import java.util.Enumeration;
6   import java.util.Locale;
7   import javax.servlet.ServletException;
8   import javax.servlet.http.HttpServlet;
9   import javax.servlet.http.HttpServletRequest;
10  import javax.servlet.http.HttpServletResponse;
11  import javax.xml.transform.Transformer;
12  import javax.xml.transform.TransformerException;
13  import javax.xml.transform.sax.SAXResult;
14  import javax.xml.transform.sax.SAXTransformerFactory;
15  import javax.xml.transform.stream.StreamSource;
16  import org.apache.commons.lang.StringUtils;
17  
18  /***
19   * @author Archimedes Trajano
20   */
21  public class XmlRenderer extends HttpServlet {
22      /***
23       * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
24       *           javax.servlet.http.HttpServletResponse)
25       */
26      protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
27          InputStream inputStream = getInputStream(request);
28          if (inputStream == null) {
29              response.sendError(HttpServletResponse.SC_NOT_FOUND, request.getServletPath());
30          }
31          try {
32              final XmlRendererHandler handler = new XmlRendererHandler(response.getWriter());
33              final Transformer transformer = SAXTransformerFactory.newInstance().newTransformer();
34              final SAXResult result = new SAXResult();
35              result.setHandler(handler);
36              result.setLexicalHandler(handler);
37              final StreamSource streamSource = new StreamSource(inputStream);
38              transformer.transform(streamSource, result);
39          } catch (TransformerException e) {
40              throw new ServletException(e);
41          }
42      }
43  
44      /***
45       * @param request
46       * @return input stream
47       */
48      private InputStream getInputStream(final HttpServletRequest request) {
49          HttpServletRequest httpServletRequest = (HttpServletRequest) request;
50          String servletPath = httpServletRequest.getServletPath();
51          String resource = servletPath.substring(0, servletPath.lastIndexOf('.'));
52          String extension = servletPath.substring(servletPath.lastIndexOf('.'));
53          for (Enumeration<Locale> locales = (Enumeration<Locale>) request.getLocales(); locales.hasMoreElements();) {
54              Locale locale = locales.nextElement();
55              StringBuffer buf = new StringBuffer(resource);
56              buf.append('_');
57              buf.append(locale.toString());
58              buf.append(extension);
59              final String path = buf.toString();
60              final InputStream resourceAsStream = getServletContext().getResourceAsStream(path);
61              if (resourceAsStream != null) {
62                  return resourceAsStream;
63              }
64          }
65          for (Enumeration<Locale> locales = (Enumeration<Locale>) request.getLocales(); locales.hasMoreElements();) {
66              Locale locale = locales.nextElement();
67              if (StringUtils.isEmpty(locale.getCountry())) {
68                  continue;
69              }
70              locale = new Locale(locale.getLanguage());
71              StringBuffer buf = new StringBuffer(resource);
72              buf.append('_');
73              buf.append(locale.toString());
74              buf.append(extension);
75              final String path = buf.toString();
76              final InputStream resourceAsStream = getServletContext().getResourceAsStream(path);
77              if (resourceAsStream != null) {
78                  return resourceAsStream;
79              }
80          }
81          return getServletContext().getResourceAsStream(request.getServletPath());
82      }
83  }