Удалось ли кому нибудь пройти валидацию реализовав поиск через Jsoup?
Ну, и если не сложно помочь добить 5 пункт таким способом.
String printText = String.valueOf(str).replaceAll("\n","");
char [] text = printText.toCharArray();
char [] openT = ("<"+args[0]).toCharArray();
char [] closeT = ("</"+args[0]+">").toCharArray();
List<Integer> indexOpen = new ArrayList<>(); // индексы открывающего тега
List<Integer> indexClose = new ArrayList<>(); // индексы закрывающего тега
Stack<Integer> tag = new Stack<>(); // стек на индексы вложенных тегов
int countOpen = 0; // переменная счетчика на открытие тега
int countClose = 0; //переменная счетчика на закрытие тега
int tagNum =0; // переменная счетчика на вложенность тегов
for (int i =0; i<text.length;i++){
if(countClose==closeT.length-1 && tagNum==1){ // если закрывается тег и нет вложеных
countClose = 0; tagNum--;
indexClose.add(i+1);
while (!tag.empty()){
indexClose.add(tag.pop());
}
} else if(countClose==closeT.length-1){ // если закрывается и есть вложенные
tag.push(i+1); tagNum--; countClose =0;
}
if(countOpen==openT.length-1){ // если открывается тег
indexOpen.add(i-openT.length+1); countOpen = 0; tagNum++;}
if(text[i]==openT[countOpen]){ // счетчик на открытие
countOpen++; } else { countOpen = 0; }
if(text[i]==closeT[countClose]){ // счетчик на закрытие
countClose++;} else {countClose = 0;}
}
// печать по индексам
for (int i= 0;i<indexClose.size();i++ ){
System.out.println(printText.substring(indexOpen.get(i),indexClose.get(i)));
}
}
}package com.javarush.task.task19.task1918;
/*
Знакомство с тегами
*/
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import java.io.*;
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
BufferedReader in = new BufferedReader(new FileReader(reader.readLine()));
StringBuilder str= new StringBuilder();
while (in.ready()){
str.append(in.readLine());
}
reader.close();
in.close();
// String s = "<tag1><tag2><tag3></tag></tag></tag>";
// Jsoup - не валидается
// Document doc = Jsoup.parse(String.valueOf(str));
// String tag = doc.body().getElementsByTag(args[0]).outerHtml();
// System.out.println(tag);
String printText = String.valueOf(str).replaceAll("\n","");
char [] text = printText.toCharArray();
char [] openT = ("<"+args[0]).toCharArray();
char [] closeT = ("</"+args[0]+">").toCharArray();
List<Integer> indexOpen = new ArrayList<>();
List<Integer> indexClose = new ArrayList<>();
Stack<Integer> tag = new Stack<>();
int countOpen = 0;
int countClose = 0;
int tagNum =0;
for (int i =0; i<text.length;i++){
if(countClose==closeT.length-1 && tagNum==1){
countClose = 0; tagNum--;
indexClose.add(i+1);
while (!tag.empty()){
indexClose.add(tag.pop());
}
} else if(countClose==closeT.length-1){
tag.push(i+1); tagNum--; countClose =0;
}
if(countOpen==openT.length-1){
indexOpen.add(i-openT.length+1); countOpen = 0; tagNum++;}
if(text[i]==openT[countOpen]){
countOpen++; } else { countOpen = 0; }
if(text[i]==closeT[countClose]){
countClose++;} else {countClose = 0;}
}
// for (Integer i : indexOpen){
// System.out.print(i+" ");
// }
// System.out.println();
// for (Integer i : indexClose){
// System.out.print(i+" ");
// }
for (int i= 0;i<indexClose.size();i++ ){
System.out.println(printText.substring(indexOpen.get(i),indexClose.get(i)));
}
}
}