Средство проверки HTML-тегов с использованием стека Java - PullRequest
0 голосов
/ 10 марта 2019

Я просто пишу приложение JAVA, которое проверяет тег проверки файла HTML (правильный тег формы <>, <<> недействителен, тег закрылся правильно), но почему-то это не работает и выдает исключение в моем методе parseHTML , Мой образец кода прилагается ниже

Это мой метод проверки

public class CheckingTag {


private final Stack<String> tagstack; 
public CheckingTag(String[] tag) {
    tagstack = new Stack<String>();     
}
public CheckingTag() {
    tagstack = new Stack<String>();
}


public static String stripEnds(String t) {
    if (t.length() <= 2) {
        return null; // this is a degenerate tag
    }
    return t.substring(1, t.length() - 1);
}

public static boolean isOpeningTag(String tag) {
    return (tag.length() == 0) || (tag.charAt(0) != '/');
}
public static boolean areMatchingTags(String tag1, String tag2) {
    return tag1.equals(tag2.substring(1)); // test against name after '/'
}

public boolean isHTMLMatched(String[] tag) {

    for (int i = 0; (i < tag.length) && (tag[i] != null); i++) {
        if (isOpeningTag(tag[i])) {
            tagstack.push(tag[i]); 
        } else {
            if (tagstack.isEmpty()) {
                return false; 
            }
            if (!areMatchingTags(tagstack.peek(), tag[i])) {
                return false; 
            }
        }
    }
    if (tagstack.isEmpty()) {
        return true; 
    }
    return false; 
}
public final static int CAPACITY = 1000;
public static String[] parseHTML(Scanner s) {
    String[] tag = new String[CAPACITY];
    int count = 0; 
    String token; 
    while (s.hasNextLine()) {
        while ((token = s.findInLine("<[^>]*>")) != null) 
        {
            tag[count++] = stripEnds(token); 
        }
        s.nextLine(); 
    }
    return tag; 
}

}

а это главное

public class CheckingHTML {

public boolean isHtml(String s) {

    String typeFile = s.substring((s.indexOf('.')) + 1);
    if (!typeFile.equalsIgnoreCase("html")) {
        return false;
    }
    return true;
}

public static void main(String[] args) throws FileNotFoundException, 

    Scanner sc = new Scanner(System.in);
    System.out.println("Enter the path of HTML file");
    String pathName = sc.nextLine();
    CheckingHTML checkingHtml = new CheckingHTML();

    CheckingTag checkingTag = new CheckingTag();
    if (pathName.isEmpty()) {
        System.out.println("The path is empty");
    } else if (!(checkingHtml.isHtml(pathName))) {
        System.out.println("This is not a HTML file directory");
    } else {

        File file = new File(pathName);
        sc = new Scanner(file);
        if (checkingTag.isHTMLMatched(checkingTag.parseHTML(sc))) {
            System.out.println("The input file is a matched HTML document.");
        } else {
            System.out.println("The input file is not a matched HTML document.");

        }``
    }
}
}

И, как я знаю, это ошибка метода чтения файла, возможно, я использую сканер, но я не уверен и не могу понять, как это исправить. Пожалуйста, помогите мне.

...