Skip to content

Commit 4841df5

Browse files
Issue #701 fixed. - The TH HTML tag is not being handled (text styling SyntaxKind.Html) (#702)
* Issue #701 fixed. - The TH HTML tag is not being handled (text styling SyntaxKind.Html) * Replacing identation TABs with spaces.
1 parent 83a1262 commit 4841df5

3 files changed

Lines changed: 95 additions & 0 deletions

File tree

document/fr.opensagres.xdocreport.document/src/main/java/fr/opensagres/xdocreport/document/textstyling/html/HTMLTextStylingContentHandler.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ public class HTMLTextStylingContentHandler
117117

118118
private static final String TD_ELT = "td";
119119

120+
private static final String TH_ELT = "th";
121+
120122
private final IDocumentHandler documentHandler;
121123

122124
// current a href + content parsing
@@ -297,7 +299,15 @@ else if ( TD_ELT.equals( name ) )
297299
// <td>
298300
TableCellProperties properties = StylesHelper.createTableCellProperties( attributes );
299301
documentHandler.startTableCell( properties );
302+
}
303+
else if (TH_ELT.equals(name))
304+
{
305+
// <th>
306+
TableCellProperties properties = StylesHelper.createTableCellProperties(attributes);
307+
documentHandler.startTableCell(properties);
308+
documentHandler.startBold();
300309
}
310+
301311
}
302312
catch ( IOException e )
303313
{
@@ -424,6 +434,12 @@ else if ( TR_ELT.equals( name ) )
424434
else if ( TD_ELT.equals( name ) )
425435
{
426436
// </td>
437+
documentHandler.endTableCell();
438+
}
439+
else if (TH_ELT.equals(name))
440+
{
441+
// </th>
442+
documentHandler.endBold();
427443
documentHandler.endTableCell();
428444
}
429445
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package fr.opensagres.xdocreport.document.textstyling.html;
2+
3+
import java.io.ByteArrayOutputStream;
4+
import java.io.FileInputStream;
5+
import java.nio.file.Files;
6+
import java.nio.file.Path;
7+
import java.nio.file.StandardOpenOption;
8+
import java.util.logging.Logger;
9+
10+
import fr.opensagres.xdocreport.core.document.SyntaxKind;
11+
import fr.opensagres.xdocreport.core.logging.LogUtils;
12+
import fr.opensagres.xdocreport.document.IXDocReport;
13+
import fr.opensagres.xdocreport.document.registry.XDocReportRegistry;
14+
import fr.opensagres.xdocreport.template.FieldExtractor;
15+
import fr.opensagres.xdocreport.template.FieldsExtractor;
16+
import fr.opensagres.xdocreport.template.IContext;
17+
import fr.opensagres.xdocreport.template.TemplateEngineKind;
18+
import fr.opensagres.xdocreport.template.formatter.FieldsMetadata;
19+
import junit.framework.TestCase;
20+
21+
public class HTMLTextStylingContentHandlerTestCase extends TestCase {
22+
private static final Logger LOGGER = LogUtils.getLogger(HTMLTextStylingContentHandlerTestCase.class.getName());
23+
24+
public static final String HTML_WITH_TH = "<h2>Análise de Sentimento das Respostas:</h2>\n"
25+
+ "<p><strong>1. Identificação de Palavras Mais Recorrentes:</strong></p>\n" + "<p>As palavras mais recorrentes nas respostas são:</p>\n"
26+
+ "<ul>\n"
27+
+ "<li><strong>&quot;Nada a declarar&quot;</strong>: Apresenta-se como um padrão, indicando a ausência de algo a ser dito.</li>\n"
28+
+ "<li><strong>&quot;Teste com o Welkey que...&quot;</strong>: Indica uma tentativa de iniciar ou completar a tarefa.</li>\n"
29+
+ "<li><strong>&quot;Parece que tudo está bem&quot;</strong>: Expressa uma sensação geral positiva.</li>\n" + "</ul>\n"
30+
+ "<p><strong>2. Classificação das Respostas:</strong></p>\n" + "<table>\n" + "<thead>\n" + "<tr>\n" + "<th>Classificação</th>\n"
31+
+ "<th>Número de Respostas</th>\n" + "<th>Porcentagem (%)</th>\n" + "</tr>\n" + "</thead>\n" + "<tbody>\n" + "<tr>\n"
32+
+ "<td>Positiva</td>\n" + "<td>10</td>\n" + "<td>50%</td>\n" + "</tr>\n" + "<tr>\n" + "<td>Negativa</td>\n" + "<td>6</td>\n"
33+
+ "<td>30%</td>\n" + "</tr>\n" + "<tr>\n" + "<td>Neutra</td>\n" + "<td>4</td>\n" + "<td>20%</td>\n" + "</tr>\n" + "</tbody>\n"
34+
+ "</table>\n" + "<p><strong>3. Cálculo dos Percentuais:</strong></p>\n" + "<ul>\n"
35+
+ "<li><strong>Positiva:</strong> 50% das respostas expressam uma sensação positiva, com foco na ausência de algo a declarar e na sensação geral de bem-estar.</li>\n"
36+
+ "<li><strong>Negativa:</strong> 30% das respostas apresentam um tom negativo, com foco em falhas ou limitações.</li>\n"
37+
+ "<li><strong>Neutra:</strong> 20% das respostas são neutras, sem uma predominância clara de termos positivos ou negativos.</li>\n"
38+
+ "</ul>\n" + "<p><strong>4. Apresentação dos Percentuais:</strong></p>\n" + "<ul>\n" + "<li><strong>Positiva: 50%</strong></li>\n"
39+
+ "<li><strong>Negativa: 30%</strong></li>\n" + "<li><strong>Neutra: 20%</strong></li>\n" + "</ul>\n"
40+
+ "<p><strong>Observações:</strong></p>\n"
41+
+ "<p>A análise do sentimento das respostas mostra uma tendência predominante de neutra, com a ausência de algo a declarar e a sensação geral de bem-estar. A presença de palavras como &quot;Teste com o Welkey que...&quot; sugere um contexto de tarefa ou atividade específica.</p>\n"
42+
+ "";
43+
44+
/**
45+
* Test for issue 701 - https://github.com/opensagres/xdocreport/issues/701
46+
*
47+
* @throws Exception
48+
*/
49+
public void testfieldWithHtmlTagTh() throws Exception {
50+
try (var inputStream = HTMLTextStylingContentHandlerTestCase.class.getResourceAsStream("template_th.odt")) {
51+
52+
IXDocReport report = XDocReportRegistry.getRegistry().loadReport(inputStream, TemplateEngineKind.Freemarker);
53+
54+
inputStream.close();
55+
FieldsMetadata fieldsMetadata = report.createFieldsMetadata();
56+
String fieldName = "html";
57+
fieldsMetadata.addFieldAsTextStyling(fieldName, SyntaxKind.Html, true);
58+
59+
IContext context = report.createContext();
60+
context.put(fieldName, HTML_WITH_TH);
61+
62+
ByteArrayOutputStream bos = new ByteArrayOutputStream();
63+
report.process(context, bos);
64+
Path outputPath = Path.of(System.getProperty("java.io.tmpdir"), "newTest-" + System.currentTimeMillis() + ".odt");
65+
Files.write(outputPath, bos.toByteArray(), StandardOpenOption.CREATE_NEW);
66+
67+
// Try to open generated ODT
68+
try {
69+
var result = new FileInputStream(outputPath.toFile());
70+
var resultReport = XDocReportRegistry.getRegistry().loadReport(result, TemplateEngineKind.Freemarker);
71+
resultReport.extractFields(new FieldsExtractor<FieldExtractor>());
72+
} catch (Exception ex) {
73+
fail("The generated document should be parseable: " + ex.getMessage());
74+
}
75+
LOGGER.fine("Output file saved on " + outputPath.toAbsolutePath().toString());
76+
77+
}
78+
}
79+
}

0 commit comments

Comments
 (0)