Exemplo n.º 1
0
  private boolean equalsFormat(HTMLNode node1, HTMLNode node2) {
    Name name1 = node1.getName();
    Name name2 = node2.getName();
    if (name1 != name2) return false;
    Attributes attributes1 = node1.getAttributes();
    Attributes attributes2 = node2.getAttributes();
    if (attributes1.size() != attributes2.size()) return false;
    for (int i = 0; i < attributes1.size(); i++) {
      Attribute attribute1 = attributes1.get(i);
      Attribute attribute2 = attributes2.get(i);
      if (attribute1 == null && attribute2 != null) return false;
      if (attribute1 != null && attribute2 == null) return false;
      if (attribute1 != null
          && attribute2 != null
          && !attribute1.getName().equalsIgnoreCase(attribute2.getName())) return false;
    }

    List<HTMLNode> children1 = node1.getChildren();
    List<HTMLNode> children2 = node2.getChildren();
    if (children1 == null && children2 == null) return true;
    if (children1 == null && children2 != null) return false;
    if (children1 != null && children2 == null) return false;
    if (children1.size() != children2.size()) return false;
    for (int i = 0; i < children1.size(); i++) {
      if (!equalsFormat(children1.get(i), children2.get(i))) return false;
    }
    return true;
  }
Exemplo n.º 2
0
  private static void loadCSS(String address, HTMLDocument document) throws Exception {
    CSSData cssData = new CSSData();
    document.putResource("CSS.DATA", cssData);

    NodePath nodePath = pathParser.toPath("HEAD");
    HTMLNode head = extractor.lookNode(document.getRoot(), nodePath);

    URLUtils urlUtils = new URLUtils();
    NodeIterator iterator = head.iterator();
    while (iterator.hasNext()) {
      HTMLNode node = iterator.next();
      if (!node.isNode(Name.LINK)) continue;
      Attributes attributes = node.getAttributes();
      Attribute attribute = attributes.get("type");
      if (attribute == null) continue;
      if (!"text/css".equalsIgnoreCase(attribute.getValue())) continue;

      attribute = attributes.get("href");
      if (attribute == null) continue;
      String link = attribute.getValue();
      if (link == null) continue;

      link = urlUtils.createURL(new URL(address), link);

      System.out.println(link);
      byte[] bytes = loadContent(link);

      String css = new String(bytes, "utf-8");
      cssData.addValue(css);
    }
  }