public void testUrlModificationWithVisitor() throws Exception {
   Parser parser = Parser.createParser(HTML_WITH_LINK, null);
   UrlModifyingVisitor visitor = new UrlModifyingVisitor("localhost://");
   parser.visitAllNodesWith(visitor);
   String result = visitor.getModifiedResult();
   assertStringEquals("Expected HTML", MODIFIED_HTML, result);
 }
Exemple #2
0
  /**
   * 获取新闻的内容
   *
   * @param newsContentFilter
   * @param parser
   * @return content 新闻内容
   */
  public String getNewsContent(NodeFilter newsContentFilter, Parser parser) {
    String content = null;
    StringBuilder builder = new StringBuilder();

    try {
      NodeList newsContentList = parser.parse(newsContentFilter);
      for (int i = 0; i < newsContentList.size(); i++) {
        Div newsContenTag = (Div) newsContentList.elementAt(i);
        builder = builder.append(newsContenTag.getStringText());
      }
      content = builder.toString(); // 转换为String 类型。
      if (content != null) {
        parser.reset();
        parser = Parser.createParser(content, "utf8");
        StringBean sb = new StringBean();
        sb.setCollapse(true);
        parser.visitAllNodesWith(sb);
        content = sb.getStrings();
        // String s = "\";} else{ document.getElementById('TurnAD444').innerHTML = \"\";} }
        // showTurnAD444(intTurnAD444); }catch(e){}";

        content = content.replaceAll("\\\".*[a-z].*\\}", "");

        content = content.replace("[我来说两句]", "");

      } else {
        System.out.println("没有得到新闻内容!");
      }

    } catch (ParserException ex) {
      Logger.getLogger(AreaTest.class.getName()).log(Level.SEVERE, null, ex);
    }

    return content;
  }
  public static void setInnerHTML(Element root, String html) {

    // remove old root childs
    OverrideNodeList<Node> list = (OverrideNodeList<Node>) root.getChildNodes();
    list.getList().clear();

    if (html != null) {
      Parser parser = Parser.createParser(html, "UTF-8");
      try {
        parser.visitAllNodesWith(new GwtNodeVisitor(root));
      } catch (ParserException e) {
        throw new RuntimeException(
            "error while parsing <" + root.getTagName() + "> element's innerHTML : " + html, e);
      }
    }
  }