You are on page 1of 3

public boolean searchColumns(String lastLine) throws IOException {

String[] words = lastLine.split("\\s+");


int index=0;
int count=0;
String line="";
String lineAux="";
String table=extrartorTable("<div class=\"colum-container
clearfix\">",getHtml());

Pattern trPattern = Pattern.compile("(<p[^>]*>([\\s\\S]*?)</p>)");


Matcher trMatcher = trPattern.matcher(table);
while (trMatcher.find()) {
String pContent = trMatcher.group(1) ;
for (String word: words){
if (count==(words.length-1)){
getColumns(pContent, words,
extrartorTable(pContent,getHtml()), index);
return true;
}
if (pContent.contains(word)){
lineAux=line;
line+= word +" ";

if (pContent.contains(line)){
count=line.split(" ").length - 1;
index=pContent.lastIndexOf(line);
index=index+ line.length() - 1;

}else{
line=lineAux;
if (count>=(words.length-1)/2) {
count = 0;
String[] wordsFound = line.split("\\s+");
getColumns(pContent, wordsFound,
extrartorTable(pContent,getHtml()),index);;
return true;

} else {
count=0;
line="";

}
}

}else {
if (count>5) {
count = 0;
String[] wordsFound = line.split("\\s+");
getColumns(pContent, wordsFound,
extrartorTable(pContent,getHtml()), index);;
return true;

} else {
count=0;
line="";
}
}
}

}
return false;
}

private void getColumns(String pContent, String[] words, String table, int


index) throws IOException {
List<String> newColumn = new ArrayList<>();
String coincidencia = String.join(" ", words);

if (table.contains(pContent)) {
String parte1 = pContent.substring(0, index);
parte1+= "</p>";
// Cortar el texto desde el índice específico hasta el final
String parte2 = pContent.substring(index);
parte2=parte2.replaceAll("</p>","");

// Imprimir las partes


System.out.println("Parte 1: " + parte1);
System.out.println("Parte 2: " + parte2);
modifyColums(parte1,parte2,pContent,coincidencia );
} else {
System.out.println("La coincidencia no fue encontrada.");
}

}
private void modifyColums(String parte1,String parte2,String pContent, String
coincidencia ) throws IOException {
modifiedHtml.setLength(0);
setHtml(getHtml().replaceAll(pContent,parte1));
Pattern trPattern = Pattern.compile("(<p[^>]*>[\\s\\S]*?</p>)");
Matcher trMatcher = trPattern.matcher(getHtml());

int indexStart=0;
int indexEnd=0;
String last="";
while (trMatcher.find()){
last=trMatcher.group(1);
indexStart= trMatcher.start();
indexEnd=trMatcher.end();

}
if (last.contains(parte1)){
if (last.contains("class=\"right\"")){
String[] partes = dividirTextoEnPunto(parte2);
String newContent= last+"</div><div class=\"colum-container
clearfix\"><p class=\"left\">"+partes[0]+"</p><p
class=\"right\">"+partes[1]+"</p>";
modifiedHtml.append(getHtml(), 0, indexEnd)
.append(newContent)
.append(getHtml(), indexEnd,
getHtml().length());
setHtml(modifiedHtml.toString());
try {
// Crear un PDF con el contenido actual
createPdf(getHtml());
} catch (IOException e) {
e.printStackTrace();
}
}
return;

}else {
last.replaceAll("<p class=\"right\">", "");
parte2="<p class=\"right\">"+parte2;
modifiedHtml.append(getHtml(), 0, indexStart)
.append(parte2)
.append(getHtml(), indexStart, getHtml().length());
setHtml(modifiedHtml.toString());
try {
// Crear un PDF con el contenido actual
createPdf(getHtml());
} catch (IOException e) {
e.printStackTrace();
}
String lastLine=
readerLastline(readerPage(getDirectory()+"/Update-"+name));
lastLine=lastLine.replaceAll(coincidencia,"");
searchColumns(lastLine);

}
private static String[] dividirTextoEnPunto(String texto) {
int longitudTotal = texto.length();
int mitadLongitud = longitudTotal / 2;

// Buscar el punto más cercano después de la mitad del texto


int posicionPunto = texto.indexOf('.', mitadLongitud);

String parte1, parte2;

if (posicionPunto != -1) {
// Si encontramos un punto después de la mitad, dividir el texto
allí
parte1 = texto.substring(0, posicionPunto + 1);
parte2 = texto.substring(posicionPunto + 1);
} else {
// Si no encontramos un punto, dividir el texto por la mitad
parte1 = texto.substring(0, mitadLongitud);
parte2 = texto.substring(mitadLongitud);
}

// Devolver las dos partes en un array


return new String[]{parte1, parte2};
}

You might also like