<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">

  <xsl:template match="/jsp-script">

    <xsl:text>// Generated by PolyJsp&#13;&#10;</xsl:text>

    <!-- Package declaration -->
    <xsl:script><![CDATA[
      var Util = Packages.org.plenix.jsp.Util;

      var scriptName = this.getAttribute("name");

      var file = new java.io.File(scriptName);
      var fileName = file.getCanonicalPath();
      var baseName = Util.getNormalizedName(fileName);

      var path = baseName.split(java.io.File.separator);

      var className = path[path.length - 1];

      var packageDirective = "package ";
      for (var i = 0; i < path.length - 1; i++)
      {
        if (i > 0)
        {
          packageDirective += ".";
        }

        packageDirective += path[i];
      }

      packageDirective += ";";

      packageDirective;
]]></xsl:script>

    <xsl:text>

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

</xsl:text>

    <xsl:apply-templates select="import"/>

    <xsl:text>&#13;&#10;public class </xsl:text>
    <xsl:script>className</xsl:script>
    <xsl:text> extends HttpServlet&#13;&#10;{</xsl:text>

    <xsl:apply-templates select="declaration"/>
    <!-- <xsl:apply-templates select="errorpage"/> -->

    <xsl:text>
  public void service(HttpServletRequest servletrequest, HttpServletResponse servletresponse)
    throws IOException, ServletException
  {
    // 0.91 compatibility
    HttpServletRequest request = servletrequest;
    HttpServletResponse response = servletresponse;

    PrintWriter out = servletresponse.getWriter();
    BufferedReader in = servletrequest.getReader();

</xsl:text>

    <xsl:apply-templates select="contenttype"/>

    <xsl:apply-templates select="body/*"/>

    <xsl:text>  }
}
</xsl:text>

  </xsl:template>

  <xsl:template match="*" priority="-1"/>

  <xsl:template match="import">
    <xsl:script><![CDATA[
      var result = "// User imports\r\n";
      var csImport = this.getText();
      var importList = csImport.split(",");
      for (var i = 0; i < importList.length; i++) {
        result += "import " + importList[i] + ";\r\n"
      }
      return result;
    ]]></xsl:script>
  </xsl:template>

  <xsl:template match="declaration">
    <xsl:text>&#13;&#10;  // Declaration&#13;&#10;  </xsl:text>
    <xsl:value-of select="."/>
    <xsl:text>&#13;&#10;&#13;&#10;</xsl:text>
  </xsl:template>

  <xsl:template match="errorpage">
    <xsl:text>  String errorPage = "</xsl:text>
    <xsl:value-of select='.'/>
    <xsl:text>";&#13;&#10;</xsl:text>
  </xsl:template>

  <xsl:template match="contenttype">
    <xsl:text>    servletresponse.setContentType("</xsl:text>
    <xsl:value-of select='.'/>
    <xsl:text>");&#13;&#10;</xsl:text>
  </xsl:template>

  <xsl:template match="header">
    <xsl:text>    servletresponse.setHeader("</xsl:text>
    <xsl:value-of select='@name'/>
    <xsl:text>", "</xsl:text>
    <xsl:value-of select='@value'/>
    <xsl:text>");&#13;&#10;</xsl:text>
  </xsl:template>

  <xsl:template match="boilerplate">
    <xsl:text>    out.print("</xsl:text>
    <xsl:value-of select='.'/>
    <xsl:text>");&#13;&#10;</xsl:text>
  </xsl:template>

  <xsl:template match="scriptlet">
    <xsl:text>    </xsl:text>
    <xsl:value-of select="."/>
    <xsl:text>&#13;&#10;</xsl:text>
  </xsl:template>

  <xsl:template match="expression">
    <xsl:text>    out.print(</xsl:text>
    <xsl:value-of select="."/>
    <xsl:text>);&#13;&#10;</xsl:text>
  </xsl:template>
</xsl:stylesheet>
