Hi there,
I've been struggling with this task for days and still I'm not able to get it validated.
Any help, please?
package com.codegym.task.task19.task1918;
/*
Introducing tags
*/
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Solution {
public static void main(String[] args) {
args = new String[]{"tag"};
// If there's no args the return
if(args[0]==null){
return;
}
// Getting the file name
String fileName = null;
try(BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))){
while(fileName==null || fileName.isEmpty()) {
fileName = reader.readLine();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
// Reading the info from the file
StringBuilder stringBuilder = new StringBuilder();
try(BufferedReader reader = new BufferedReader(new FileReader(fileName))){
// While there's info to be read, read it
while(reader.ready()){
stringBuilder.append(reader.readLine());
}
// If the file is empty then return
if (stringBuilder.toString().isEmpty()) {
return;
}
// Write to the console the result
for(String str : Tags(stringBuilder.toString(), args[0])){
if(str!=null && !str.isEmpty()) {
System.out.println(str);
}
}
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static List<String> Tags(String string, String tag){
final String openingTag = "<" + tag;
final String closingTag = "</".concat(tag).concat(">");
int pos = string.indexOf(openingTag);
pos += openingTag.length();
List<String> tags = new ArrayList<>();
// Splitting the file content by the REGEX
Collections.addAll(tags, string.substring(pos).split(openingTag));
int index = 0;
// Adding the REGEX to the start of every tag
while (index < tags.size()){
tags.set(index, openingTag.concat(tags.get(index)));
index++;
}
// Searching for strings with non ending tags
// When a string is found, the next element of the Tags list is added
// Until an ending tag is found and there're the same number of opening and closing tags
index = 0;
while(index < tags.size()){
int addIndex = 1;
while(!tags.get(index).endsWith(closingTag) ||
Collections.frequency(Collections.singleton(tags.get(index)),openingTag)!= Collections.frequency(Collections.singleton(tags.get(index)), closingTag)){
tags.set(index, tags.get(index).concat(tags.get(index + addIndex)));
tags.remove(index + addIndex);
}
index++;
}
// Getting the inner tags
index = 0;
while(index < tags.size()){
int n = 1;
if(CountTags(tags.get(index),openingTag)>1){
int posOpeningTag = tags.get(index).indexOf(openingTag);
int posClosingTag = tags.get(index).lastIndexOf(closingTag);
while(true){
if(posOpeningTag>0){
posOpeningTag = tags.get(index).indexOf(openingTag, posOpeningTag);
if(posOpeningTag==-1){
break;
}
posClosingTag = tags.get(index).lastIndexOf(closingTag,posClosingTag);
String innerStr = tags.get(index).substring(posOpeningTag, posClosingTag + closingTag.length());
tags.add((index + n), innerStr);
n++;
}
posOpeningTag++;
posClosingTag--;
}
}
index+=n;
}
return tags;
}
//Counts how many tags are in a string
private static int CountTags(String string, String tag){
int countTags = 0;
String[] buffer = string.split(tag);
for(String str : buffer){
countTags = (str!=null && !str.isEmpty())?countTags+=1: countTags;
}
return countTags;
}
}
String startTag = "<" + search + ".*?>"
. The substring approach needs a fixed length