コード例 #1
0
ファイル: HtmlValidator.java プロジェクト: Emmalee/Java
 /**
  * removes any tags from the validator's queue that match the given element
  *
  * @param element to remove from the queue
  */
 public void removeAll(String element) {
   for (HtmlTag h : tags) {
     if (h.toString().equals(element)) {
       tags.remove(h);
     }
   }
 }
コード例 #2
0
ファイル: HtmlValidator.java プロジェクト: Emmalee/Java
  /**
   * examines a sequence of HTML tags and determines if it represents a valid sequence of tags
   * prints an indented text representation of the HTML tags in the queue
   */
  public void validate() {
    int indent = 0;
    StackMP2 stack = new StackMP2();

    for (HtmlTag h : tags) {

      if (stack.isEmpty() & !h.isOpenTag()) {
        System.out.println("Error unexpected tag: " + h.toString());
        continue;
      }

      if (!h.isOpenTag()) {

        if (h.matches(stack.peek())) {
          for (int j = 0; j < indent - 1; j++) {
            System.out.print("    ");
          }
          System.out.println(h.toString());
          stack.pop();
          indent--;
        } else {
          System.out.println("Error unexpected tag: " + h.toString());
        }

      } else {

        for (int i = 0; i < indent; i++) {
          System.out.print("    ");
        }

        System.out.println(h.toString());
        if (!h.isSelfClosing()) {
          stack.push(h);
          indent++;
        }
      }
    }
    while (!stack.isEmpty()) {
      System.out.println("Error unclosed tag: " + stack.pop().toString());
    }
  }