示例#1
0
 public boolean satisfy(TagNode tagNode) {
   if (tagNode == null || attName == null || attValue == null) {
     return false;
   } else {
     return isCaseSensitive
         ? attValue.equals(tagNode.getAttributeByName(attName))
         : attValue.equalsIgnoreCase(tagNode.getAttributeByName(attName));
   }
 }
示例#2
0
 public void addChild(Object child) {
   if (child == null) {
     return;
   }
   if (child instanceof List) {
     addChildren((List) child);
   } else {
     children.add(child);
     if (child instanceof TagNode) {
       TagNode childTagNode = (TagNode) child;
       childTagNode.parent = this;
     }
   }
 }
示例#3
0
  /**
   * Finds first element in the tree that satisfy specified condition.
   *
   * @param condition
   * @param isRecursive
   * @return First TagNode found, or null if no such elements.
   */
  private TagNode findElement(ITagNodeCondition condition, boolean isRecursive) {
    if (condition == null) {
      return null;
    }

    for (int i = 0; i < children.size(); i++) {
      Object item = children.get(i);
      if (item instanceof TagNode) {
        TagNode currNode = (TagNode) item;
        if (condition.satisfy(currNode)) {
          return currNode;
        } else if (isRecursive) {
          TagNode inner = currNode.findElement(condition, isRecursive);
          if (inner != null) {
            return inner;
          }
        }
      }
    }

    return null;
  }
示例#4
0
  /**
   * Get all elements in the tree that satisfy specified condition.
   *
   * @param condition
   * @param isRecursive
   * @return List of TagNode instances with specified name.
   */
  private List getElementList(ITagNodeCondition condition, boolean isRecursive) {
    List result = new LinkedList();
    if (condition == null) {
      return result;
    }

    for (int i = 0; i < children.size(); i++) {
      Object item = children.get(i);
      if (item instanceof TagNode) {
        TagNode currNode = (TagNode) item;
        if (condition.satisfy(currNode)) {
          result.add(currNode);
        }
        if (isRecursive) {
          List innerList = currNode.getElementList(condition, isRecursive);
          if (innerList != null && innerList.size() > 0) {
            result.addAll(innerList);
          }
        }
      }
    }

    return result;
  }
示例#5
0
  protected void serializePrettyHtml(
      final TagNode tagNode,
      final Writer writer,
      final int level,
      final boolean isPreserveWhitespaces,
      final boolean isLastNewLine)
      throws IOException {
    final List tagChildren = tagNode.getChildren();
    final String tagName = tagNode.getName();
    final boolean isHeadlessNode = Utils.isEmptyString(tagName);
    final String indent = isHeadlessNode ? "" : getIndent(level);

    if (!isPreserveWhitespaces) {
      if (!isLastNewLine) {
        writer.write("\n");
      }
      writer.write(indent);
    }
    serializeOpenTag(tagNode, writer, true);

    final boolean preserveWhitespaces = isPreserveWhitespaces || "pre".equalsIgnoreCase(tagName);

    boolean lastWasNewLine = false;

    if (!isMinimizedTagSyntax(tagNode)) {
      final String singleLine = getSingleLineOfChildren(tagChildren);
      final boolean dontEscape = dontEscape(tagNode);
      if (!preserveWhitespaces && singleLine != null) {
        writer.write(!dontEscape(tagNode) ? escapeText(singleLine) : singleLine);
      } else {
        final Iterator childIterator = tagChildren.iterator();
        while (childIterator.hasNext()) {
          final Object child = childIterator.next();
          if (child instanceof TagNode) {
            serializePrettyHtml(
                (TagNode) child,
                writer,
                isHeadlessNode ? level : level + 1,
                preserveWhitespaces,
                lastWasNewLine);
            lastWasNewLine = false;
          } else if (child instanceof ContentNode) {
            final String content = dontEscape ? child.toString() : escapeText(child.toString());
            if (content.length() > 0) {
              if (dontEscape || preserveWhitespaces) {
                writer.write(content);
              } else if (Character.isWhitespace(content.charAt(0))) {
                if (!lastWasNewLine) {
                  writer.write("\n");
                  lastWasNewLine = false;
                }
                if (content.trim().length() > 0) {
                  writer.write(
                      getIndentedText(Utils.rtrim(content), isHeadlessNode ? level : level + 1));
                } else {
                  lastWasNewLine = true;
                }
              } else {
                if (content.trim().length() > 0) {
                  writer.write(Utils.rtrim(content));
                }
                if (!childIterator.hasNext()) {
                  writer.write("\n");
                  lastWasNewLine = true;
                }
              }
            }
          } else if (child instanceof CommentNode) {
            if (!lastWasNewLine && !preserveWhitespaces) {
              writer.write("\n");
              lastWasNewLine = false;
            }
            final CommentNode commentNode = (CommentNode) child;
            final String content = commentNode.getCommentedContent();
            writer.write(
                dontEscape
                    ? content
                    : getIndentedText(content, isHeadlessNode ? level : level + 1));
          }
        }
      }

      if (singleLine == null && !preserveWhitespaces) {
        if (!lastWasNewLine) {
          writer.write("\n");
        }
        writer.write(indent);
      }

      serializeEndTag(tagNode, writer, false);
    }
  }
示例#6
0
 /**
  * Remove this node from the tree.
  *
  * @return True if element is removed (if it is not root node).
  */
 public boolean removeFromTree() {
   return parent != null ? parent.removeChild(this) : false;
 }
示例#7
0
 private static boolean handleURL(String address) {
   Main.status(String.format("Processing page \"%s\".", address));
   try {
     NodeList posts = getPosts(address);
     if (posts.toNodeArray().length == 0) {
       return false;
     }
     for (Node post_node : posts.toNodeArray()) {
       if (post_node instanceof TagNode) {
         TagNode post = (TagNode) post_node;
         Post new_post = new Post(Long.parseLong(post.getAttribute("id").substring(5)));
         if (!Main.post_post_hash.containsKey(new_post)) {
           NodeList photo_posts = getPhotoPosts(post.getChildren());
           NodeList remarks = getRemarks(photo_posts);
           for (Node node : remarks.toNodeArray()) {
             Matcher matcher = lores.matcher(node.getText());
             String media_url = "";
             if (matcher.find()) {
               media_url = matcher.group();
               media_url = media_url.substring(17, media_url.length() - 1);
             }
             String thumb =
                 media_url.replace(
                     media_url.substring(media_url.lastIndexOf("_"), media_url.lastIndexOf(".")),
                     "_75sq");
             URL thumb_url = new URL(thumb);
             new_post.pictures.add(new Picture(new URL(media_url), thumb_url));
           }
           NodeList photoset_posts = getPhotosetPosts(post.getChildren());
           NodeList iframes = getIFrames(photoset_posts);
           for (Node node : iframes.toNodeArray()) {
             if (node instanceof TagNode) {
               String iframe_url = ((TagNode) node).getAttribute("src");
               Parser parser2 = new Parser(iframe_url);
               NodeList a_list = parser2.extractAllNodesThatMatch(new TagNameFilter("a"));
               Node[] a_array = a_list.toNodeArray();
               Node[] img_array =
                   a_list.extractAllNodesThatMatch(new TagNameFilter("img"), true).toNodeArray();
               String media_url;
               for (int i = 0; i < a_array.length; i++) {
                 media_url = ((TagNode) img_array[i]).getAttribute("src");
                 String thumb =
                     media_url.replace(
                         media_url.substring(
                             media_url.lastIndexOf("_"), media_url.lastIndexOf(".")),
                         "_75sq");
                 URL thumb_url = new URL(thumb);
                 new_post.pictures.add(new Picture(new URL(media_url), thumb_url));
               }
             }
           }
           Main.handlePost(new_post);
         } else {
           new_post = post_post_hash.get(new_post);
           handleNonDownloadPost(new_post);
         }
       }
     }
   } catch (Exception ex) {
     ex.printStackTrace();
     Main.status("Error handling post.");
   }
   return true;
 }