Ejemplo n.º 1
0
  /** Compile the jsp file into equivalent servlet in java source */
  private void generateJava() throws Exception {

    long t1, t2, t3, t4;
    t1 = t2 = t3 = t4 = 0;

    if (log.isLoggable(Level.FINE)) {
      t1 = System.currentTimeMillis();
    }

    // Setup page info area
    pageInfo =
        new PageInfo(new BeanRepository(ctxt.getClassLoader(), errDispatcher), ctxt.getJspFile());

    JspConfig jspConfig = options.getJspConfig();
    JspProperty jspProperty = jspConfig.findJspProperty(ctxt.getJspFile());

    /*
     * If the current uri is matched by a pattern specified in
     * a jsp-property-group in web.xml, initialize pageInfo with
     * those properties.
     */
    pageInfo.setELIgnored(JspUtil.booleanValue(jspProperty.isELIgnored()));
    pageInfo.setScriptingInvalid(JspUtil.booleanValue(jspProperty.isScriptingInvalid()));
    pageInfo.setTrimDirectiveWhitespaces(JspUtil.booleanValue(jspProperty.getTrimSpaces()));
    pageInfo.setDeferredSyntaxAllowedAsLiteral(JspUtil.booleanValue(jspProperty.getPoundAllowed()));
    pageInfo.setErrorOnUndeclaredNamespace(
        JspUtil.booleanValue(jspProperty.errorOnUndeclaredNamespace()));

    if (jspProperty.getIncludePrelude() != null) {
      pageInfo.setIncludePrelude(jspProperty.getIncludePrelude());
    }
    if (jspProperty.getIncludeCoda() != null) {
      pageInfo.setIncludeCoda(jspProperty.getIncludeCoda());
    }
    if (options.isDefaultBufferNone() && pageInfo.getBufferValue() == null) {
      // Set to unbuffered if not specified explicitly
      pageInfo.setBuffer(0);
    }

    String javaFileName = ctxt.getServletJavaFileName();
    ServletWriter writer = null;

    try {
      // Setup the ServletWriter
      Writer javaWriter =
          javaCompiler.getJavaWriter(javaFileName, ctxt.getOptions().getJavaEncoding());
      writer = new ServletWriter(new PrintWriter(javaWriter));
      ctxt.setWriter(writer);

      // Reset the temporary variable counter for the generator.
      JspUtil.resetTemporaryVariableName();

      // Parse the file
      ParserController parserCtl = new ParserController(ctxt, this);
      pageNodes = parserCtl.parse(ctxt.getJspFile());

      if (ctxt.isPrototypeMode()) {
        // generate prototype .java file for the tag file
        Generator.generate(writer, this, pageNodes);
        writer.close();
        writer = null;
        return;
      }

      // Validate and process attributes
      Validator.validate(this, pageNodes);

      if (log.isLoggable(Level.FINE)) {
        t2 = System.currentTimeMillis();
      }

      // Collect page info
      Collector.collect(this, pageNodes);

      // Compile (if necessary) and load the tag files referenced in
      // this compilation unit.
      tfp = new TagFileProcessor();
      tfp.loadTagFiles(this, pageNodes);

      if (log.isLoggable(Level.FINE)) {
        t3 = System.currentTimeMillis();
      }

      // Determine which custom tag needs to declare which scripting vars
      ScriptingVariabler.set(pageNodes, errDispatcher);

      // Optimizations by Tag Plugins
      TagPluginManager tagPluginManager = options.getTagPluginManager();
      tagPluginManager.apply(pageNodes, errDispatcher, pageInfo);

      // Optimization: concatenate contiguous template texts.
      TextOptimizer.concatenate(this, pageNodes);

      // Generate static function mapper codes.
      ELFunctionMapper.map(this, pageNodes);

      // generate servlet .java file
      Generator.generate(writer, this, pageNodes);
      writer.close();
      writer = null;

      // The writer is only used during the compile, dereference
      // it in the JspCompilationContext when done to allow it
      // to be GC'd and save memory.
      ctxt.setWriter(null);

      if (log.isLoggable(Level.FINE)) {
        t4 = System.currentTimeMillis();
        log.fine(
            "Generated "
                + javaFileName
                + " total="
                + (t4 - t1)
                + " generate="
                + (t4 - t3)
                + " validate="
                + (t2 - t1));
      }

    } catch (Exception e) {
      if (writer != null) {
        try {
          writer.close();
          writer = null;
        } catch (Exception e1) {
          // do nothing
        }
      }
      // Remove the generated .java file
      javaCompiler.doJavaFile(false);
      throw e;
    } finally {
      if (writer != null) {
        try {
          writer.close();
        } catch (Exception e2) {
          // do nothing
        }
      }
    }

    // JSR45 Support
    if (!options.isSmapSuppressed()) {
      smapUtil.generateSmap(pageNodes);
    }

    // If any proto type .java and .class files was generated,
    // the prototype .java may have been replaced by the current
    // compilation (if the tag file is self referencing), but the
    // .class file need to be removed, to make sure that javac would
    // generate .class again from the new .java file just generated.
    tfp.removeProtoTypeFiles(ctxt.getClassFileName());
  }
Ejemplo n.º 2
0
  /**
   * Find a property that best matches the supplied resource.
   *
   * @param uri the resource supplied.
   * @return a JspProperty indicating the best match, or some default.
   */
  public JspProperty findJspProperty(String uri) {

    init();

    // JSP Configuration settings do not apply to tag files
    if (jspProperties == null || uri.endsWith(".tag") || uri.endsWith(".tagx")) {
      return defaultJspProperty;
    }

    String uriPath = null;
    int index = uri.lastIndexOf('/');
    if (index >= 0) {
      uriPath = uri.substring(0, index + 1);
    }
    String uriExtension = null;
    index = uri.lastIndexOf('.');
    if (index >= 0) {
      uriExtension = uri.substring(index + 1);
    }

    Collection<String> includePreludes = new ArrayList<>();
    Collection<String> includeCodas = new ArrayList<>();

    JspPropertyGroup isXmlMatch = null;
    JspPropertyGroup elIgnoredMatch = null;
    JspPropertyGroup scriptingInvalidMatch = null;
    JspPropertyGroup pageEncodingMatch = null;
    JspPropertyGroup deferedSyntaxAllowedAsLiteralMatch = null;
    JspPropertyGroup trimDirectiveWhitespacesMatch = null;
    JspPropertyGroup defaultContentTypeMatch = null;
    JspPropertyGroup bufferMatch = null;
    JspPropertyGroup errorOnUndeclaredNamespaceMatch = null;

    Iterator<JspPropertyGroup> iter = jspProperties.iterator();
    while (iter.hasNext()) {

      JspPropertyGroup jpg = iter.next();
      JspProperty jp = jpg.getJspProperty();

      // (arrays will be the same length)
      String extension = jpg.getExtension();
      String path = jpg.getPath();

      if (extension == null) {
        // exact match pattern: /a/foo.jsp
        if (!uri.equals(path)) {
          // not matched;
          continue;
        }
      } else {
        // Matching patterns *.ext or /p/*
        if (path != null && uriPath != null && !uriPath.startsWith(path)) {
          // not matched
          continue;
        }
        if (!extension.equals("*") && !extension.equals(uriExtension)) {
          // not matched
          continue;
        }
      }
      // We have a match
      // Add include-preludes and include-codas
      if (jp.getIncludePrelude() != null) {
        includePreludes.addAll(jp.getIncludePrelude());
      }
      if (jp.getIncludeCoda() != null) {
        includeCodas.addAll(jp.getIncludeCoda());
      }

      // If there is a previous match for the same property, remember
      // the one that is more restrictive.
      if (jp.isXml() != null) {
        isXmlMatch = selectProperty(isXmlMatch, jpg);
      }
      if (jp.isELIgnored() != null) {
        elIgnoredMatch = selectProperty(elIgnoredMatch, jpg);
      }
      if (jp.isScriptingInvalid() != null) {
        scriptingInvalidMatch = selectProperty(scriptingInvalidMatch, jpg);
      }
      if (jp.getPageEncoding() != null) {
        pageEncodingMatch = selectProperty(pageEncodingMatch, jpg);
      }
      if (jp.isDeferedSyntaxAllowedAsLiteral() != null) {
        deferedSyntaxAllowedAsLiteralMatch =
            selectProperty(deferedSyntaxAllowedAsLiteralMatch, jpg);
      }
      if (jp.isTrimDirectiveWhitespaces() != null) {
        trimDirectiveWhitespacesMatch = selectProperty(trimDirectiveWhitespacesMatch, jpg);
      }
      if (jp.getDefaultContentType() != null) {
        defaultContentTypeMatch = selectProperty(defaultContentTypeMatch, jpg);
      }
      if (jp.getBuffer() != null) {
        bufferMatch = selectProperty(bufferMatch, jpg);
      }
      if (jp.isErrorOnUndeclaredNamespace() != null) {
        errorOnUndeclaredNamespaceMatch = selectProperty(errorOnUndeclaredNamespaceMatch, jpg);
      }
    }

    String isXml = defaultIsXml;
    String isELIgnored = defaultIsELIgnored;
    String isScriptingInvalid = defaultIsScriptingInvalid;
    String pageEncoding = null;
    String isDeferedSyntaxAllowedAsLiteral = defaultDeferedSyntaxAllowedAsLiteral;
    String isTrimDirectiveWhitespaces = defaultTrimDirectiveWhitespaces;
    String defaultContentType = defaultDefaultContentType;
    String buffer = defaultBuffer;
    String errorOnUndelcaredNamespace = defaultErrorOnUndeclaredNamespace;

    if (isXmlMatch != null) {
      isXml = isXmlMatch.getJspProperty().isXml();
    }
    if (elIgnoredMatch != null) {
      isELIgnored = elIgnoredMatch.getJspProperty().isELIgnored();
    }
    if (scriptingInvalidMatch != null) {
      isScriptingInvalid = scriptingInvalidMatch.getJspProperty().isScriptingInvalid();
    }
    if (pageEncodingMatch != null) {
      pageEncoding = pageEncodingMatch.getJspProperty().getPageEncoding();
    }
    if (deferedSyntaxAllowedAsLiteralMatch != null) {
      isDeferedSyntaxAllowedAsLiteral =
          deferedSyntaxAllowedAsLiteralMatch.getJspProperty().isDeferedSyntaxAllowedAsLiteral();
    }
    if (trimDirectiveWhitespacesMatch != null) {
      isTrimDirectiveWhitespaces =
          trimDirectiveWhitespacesMatch.getJspProperty().isTrimDirectiveWhitespaces();
    }
    if (defaultContentTypeMatch != null) {
      defaultContentType = defaultContentTypeMatch.getJspProperty().getDefaultContentType();
    }
    if (bufferMatch != null) {
      buffer = bufferMatch.getJspProperty().getBuffer();
    }
    if (errorOnUndeclaredNamespaceMatch != null) {
      errorOnUndelcaredNamespace =
          errorOnUndeclaredNamespaceMatch.getJspProperty().isErrorOnUndeclaredNamespace();
    }

    return new JspProperty(
        isXml,
        isELIgnored,
        isScriptingInvalid,
        pageEncoding,
        includePreludes,
        includeCodas,
        isDeferedSyntaxAllowedAsLiteral,
        isTrimDirectiveWhitespaces,
        defaultContentType,
        buffer,
        errorOnUndelcaredNamespace);
  }