Exemple #1
0
  /**
   * Find an element by ID, including or under this element.
   *
   * <p>Note that this finds the first matching ID, starting with this element. If you search down
   * from a different starting point, it is possible to find a different element by ID. For unique
   * element by ID within a Document, use {@link Document#getElementById(String)}
   *
   * @param id The ID to search for.
   * @return The first matching element by ID, starting with this element, or null if none found.
   */
  public Element getElementById(String id) {
    Validate.notEmpty(id);

    Elements elements = Collector.collect(new Evaluator.Id(id), this);
    if (elements.size() > 0) return elements.get(0);
    else return null;
  }
  public void testNegativeScores() throws Exception {

    // The Top*Collectors previously filtered out documents with <= scores. This
    // behavior has changed. This test checks that if PositiveOnlyScoresFilter
    // wraps one of these collectors, documents with <= 0 scores are indeed
    // filtered.

    int numPositiveScores = 0;
    for (int i = 0; i < scores.length; i++) {
      if (scores[i] > 0) {
        ++numPositiveScores;
      }
    }

    Scorer s = new SimpleScorer();
    TopDocsCollector tdc = TopScoreDocCollector.create(scores.length, true);
    Collector c = new PositiveScoresOnlyCollector(tdc);
    c.setScorer(s);
    while (s.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) {
      c.collect(0);
    }
    TopDocs td = tdc.topDocs();
    ScoreDoc[] sd = td.scoreDocs;
    assertEquals(numPositiveScores, td.totalHits);
    for (int i = 0; i < sd.length; i++) {
      assertTrue("only positive scores should return: " + sd[i].score, sd[i].score > 0);
    }
  }
Exemple #3
0
    @SuppressWarnings("unchecked")
    public String getValue() {
      // restart the error list for a possibly new value.
      errors = null;

      Collector collector = collectors.get(source.getClass());

      if (collector == null) {
        throw new InvalidSourceTypeException(
            "No collector for the type " + source.getClass().getCanonicalName());
      }

      return collector.collect(source);
    }
  /**
   * Compile the jsp file into equivalent servlet in .java file
   *
   * @return a smap for the current JSP page, if one is generated, null otherwise
   */
  protected String[] generateJava() throws Exception {

    String[] smapStr = null;

    long t1, t2, t3, t4;

    t1 = t2 = t3 = t4 = 0;

    if (log.isDebugEnabled()) {
      t1 = System.currentTimeMillis();
    }

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

    JspConfig jspConfig = options.getJspConfig();
    JspConfig.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.
     */
    if (jspProperty.isELIgnored() != null) {
      pageInfo.setELIgnored(JspUtil.booleanValue(jspProperty.isELIgnored()));
    }
    if (jspProperty.isScriptingInvalid() != null) {
      pageInfo.setScriptingInvalid(JspUtil.booleanValue(jspProperty.isScriptingInvalid()));
    }
    if (jspProperty.getIncludePrelude() != null) {
      pageInfo.setIncludePrelude(jspProperty.getIncludePrelude());
    }
    if (jspProperty.getIncludeCoda() != null) {
      pageInfo.setIncludeCoda(jspProperty.getIncludeCoda());
    }
    if (jspProperty.isDeferedSyntaxAllowedAsLiteral() != null) {
      pageInfo.setDeferredSyntaxAllowedAsLiteral(
          JspUtil.booleanValue(jspProperty.isDeferedSyntaxAllowedAsLiteral()));
    }
    if (jspProperty.isTrimDirectiveWhitespaces() != null) {
      pageInfo.setTrimDirectiveWhitespaces(
          JspUtil.booleanValue(jspProperty.isTrimDirectiveWhitespaces()));
    }
    // Default ContentType processing is deferred until after the page has
    // been parsed
    if (jspProperty.getBuffer() != null) {
      pageInfo.setBufferValue(jspProperty.getBuffer(), null, errDispatcher);
    }
    if (jspProperty.isErrorOnUndeclaredNamespace() != null) {
      pageInfo.setErrorOnUndeclaredNamespace(
          JspUtil.booleanValue(jspProperty.isErrorOnUndeclaredNamespace()));
    }
    if (ctxt.isTagFile()) {
      try {
        double libraryVersion =
            Double.parseDouble(ctxt.getTagInfo().getTagLibrary().getRequiredVersion());
        if (libraryVersion < 2.0) {
          pageInfo.setIsELIgnored("true", null, errDispatcher, true);
        }
        if (libraryVersion < 2.1) {
          pageInfo.setDeferredSyntaxAllowedAsLiteral("true", null, errDispatcher, true);
        }
      } catch (NumberFormatException ex) {
        errDispatcher.jspError(ex);
      }
    }

    ctxt.checkOutputDir();
    String javaFileName = ctxt.getServletJavaFileName();

    ServletWriter writer = null;
    try {
      /*
       * The setting of isELIgnored changes the behaviour of the parser
       * in subtle ways. To add to the 'fun', isELIgnored can be set in
       * any file that forms part of the translation unit so setting it
       * in a file included towards the end of the translation unit can
       * change how the parser should have behaved when parsing content
       * up to the point where isELIgnored was set. Arghh!
       * Previous attempts to hack around this have only provided partial
       * solutions. We now use two passes to parse the translation unit.
       * The first just parses the directives and the second parses the
       * whole translation unit once we know how isELIgnored has been set.
       * TODO There are some possible optimisations of this process.
       */
      // Parse the file
      ParserController parserCtl = new ParserController(ctxt, this);

      // Pass 1 - the directives
      Node.Nodes directives = parserCtl.parseDirectives(ctxt.getJspFile());
      Validator.validateDirectives(this, directives);

      // Pass 2 - the whole translation unit
      pageNodes = parserCtl.parse(ctxt.getJspFile());

      // Leave this until now since it can only be set once - bug 49726
      if (pageInfo.getContentType() == null && jspProperty.getDefaultContentType() != null) {
        pageInfo.setContentType(jspProperty.getDefaultContentType());
      }

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

      // Validate and process attributes - don't re-validate the
      // directives we validated in pass 1
      Validator.validateExDirectives(this, pageNodes);

      if (log.isDebugEnabled()) {
        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.isDebugEnabled()) {
        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(pageNodes);

      // generate servlet .java file
      writer = setupContextWriter(javaFileName);
      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.isDebugEnabled()) {
        t4 = System.currentTimeMillis();
        log.debug(
            "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
      File file = new File(javaFileName);
      if (file.exists()) {
        if (!file.delete()) {
          log.warn(
              Localizer.getMessage(
                  "jsp.warning.compiler.javafile.delete.fail", file.getAbsolutePath()));
        }
      }
      throw e;
    } finally {
      if (writer != null) {
        try {
          writer.close();
        } catch (Exception e2) {
          // do nothing
        }
      }
    }

    // JSR45 Support
    if (!options.isSmapSuppressed()) {
      smapStr = SmapUtil.generateSmap(ctxt, 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());

    return smapStr;
  }
Exemple #5
0
 /**
  * Find all elements under this element (including self, and children of children).
  *
  * @return all elements
  */
 public Elements getAllElements() {
   return Collector.collect(new Evaluator.AllElements(), this);
 }
Exemple #6
0
 /**
  * Find elements whose own text matches the supplied regular expression.
  *
  * @param pattern regular expression to match text against
  * @return elements matching the supplied regular expression.
  * @see org.jsoup.nodes.Element#ownText()
  */
 public Elements getElementsMatchingOwnText(Pattern pattern) {
   return Collector.collect(new Evaluator.MatchesOwn(pattern), this);
 }
Exemple #7
0
 /**
  * Find elements that directly contain the specified string. The search is case insensitive. The
  * text must appear directly in the element, not in any of its descendants.
  *
  * @param searchText to look for in the element's own text
  * @return elements that contain the string, case insensitive.
  * @see org.jsoup.nodes.Element#ownText()
  */
 public Elements getElementsContainingOwnText(String searchText) {
   return Collector.collect(new Evaluator.ContainsOwnText(searchText), this);
 }
Exemple #8
0
  /**
   * Finds elements, including and recursively under this element, with the specified tag name.
   *
   * @param tagName The tag name to search for (case insensitively).
   * @return a matching unmodifiable list of elements. Will be empty if this element and none of its
   *     children match.
   */
  public Elements getElementsByTag(String tagName) {
    Validate.notEmpty(tagName);
    tagName = tagName.toLowerCase().trim();

    return Collector.collect(new Evaluator.Tag(tagName), this);
  }
 @Override
 public void select(Collector collector) throws IOException {
   collector.collect(this);
 }
Exemple #10
0
 @Override
 public void process(String path) {
   collector.collect("a", 111);
   collector.collect("b", 222);
   collector.collect("aa", 111);
 }
Exemple #11
0
 /**
  * Find elements that have attributes whose value contains the match string. Case insensitive.
  *
  * @param key name of the attribute
  * @param match substring of value to search for
  * @return elements that have attributes containing this text
  */
 public Elements getElementsByAttributeValueContaining(String key, String match) {
   return Collector.collect(new Evaluator.AttributeWithValueContaining(key, match), this);
 }
Exemple #12
0
 /**
  * Find elements that have attributes that end with the value suffix. Case insensitive.
  *
  * @param key name of the attribute
  * @param valueSuffix end of the attribute value
  * @return elements that have attributes that end with the value suffix
  */
 public Elements getElementsByAttributeValueEnding(String key, String valueSuffix) {
   return Collector.collect(new Evaluator.AttributeWithValueEnding(key, valueSuffix), this);
 }
Exemple #13
0
 /**
  * Find elements that either do not have this attribute, or have it with a different value. Case
  * insensitive.
  *
  * @param key name of the attribute
  * @param value value of the attribute
  * @return elements that do not have a matching attribute
  */
 public Elements getElementsByAttributeValueNot(String key, String value) {
   return Collector.collect(new Evaluator.AttributeWithValueNot(key, value), this);
 }
Exemple #14
0
  /**
   * Find elements that have an attribute name starting with the supplied prefix. Use {@code data-}
   * to find elements that have HTML5 datasets.
   *
   * @param keyPrefix name prefix of the attribute e.g. {@code data-}
   * @return elements that have attribute names that start with with the prefix, empty if none.
   */
  public Elements getElementsByAttributeStarting(String keyPrefix) {
    Validate.notEmpty(keyPrefix);
    keyPrefix = keyPrefix.trim().toLowerCase();

    return Collector.collect(new Evaluator.AttributeStarting(keyPrefix), this);
  }
Exemple #15
0
  /**
   * Find elements that have a named attribute set. Case insensitive.
   *
   * @param key name of the attribute, e.g. {@code href}
   * @return elements that have this attribute, empty if none
   */
  public Elements getElementsByAttribute(String key) {
    Validate.notEmpty(key);
    key = key.trim().toLowerCase();

    return Collector.collect(new Evaluator.Attribute(key), this);
  }
Exemple #16
0
  /**
   * Find elements that have this class, including or under this element. Case insensitive.
   *
   * <p>Elements can have multiple classes (e.g. {@code <div class="header round first">}. This
   * method checks each class, so you can find the above with {@code
   * el.getElementsByClass("header");}.
   *
   * @param className the name of the class to search for.
   * @return elements with the supplied class name, empty if none
   * @see #hasClass(String)
   * @see #classNames()
   */
  public Elements getElementsByClass(String className) {
    Validate.notEmpty(className);

    return Collector.collect(new Evaluator.Class(className), this);
  }
 @Override
 public void collect(int doc) throws IOException {
   if (scorer.score() > 0) {
     c.collect(doc);
   }
 }
Exemple #18
0
 /**
  * Find elements that have attributes whose values match the supplied regular expression.
  *
  * @param key name of the attribute
  * @param pattern compiled regular expression to match against attribute values
  * @return elements that have attributes matching this regular expression
  */
 public Elements getElementsByAttributeValueMatching(String key, Pattern pattern) {
   return Collector.collect(new Evaluator.AttributeWithValueMatching(key, pattern), this);
 }
 @Override
 public void collect(int doc, long owningBucketOrdinal) throws IOException {
   collector.collect(doc, owningBucketOrdinal);
 }
Exemple #20
0
 /**
  * Find elements whose sibling index is greater than the supplied index.
  *
  * @param index 0-based index
  * @return elements greater than index
  */
 public Elements getElementsByIndexGreaterThan(int index) {
   return Collector.collect(new Evaluator.IndexGreaterThan(index), this);
 }
Exemple #21
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());
  }
Exemple #22
0
 /**
  * Find elements whose sibling index is equal to the supplied index.
  *
  * @param index 0-based index
  * @return elements equal to index
  */
 public Elements getElementsByIndexEquals(int index) {
   return Collector.collect(new Evaluator.IndexEquals(index), this);
 }