Search in sources :

Example 1 with FieldExtractor

use of fr.opensagres.xdocreport.template.FieldExtractor in project SORMAS-Project by hzi-braunschweig.

the class TemplateEngine method extractTemplateVariablesDocx.

public DocumentVariables extractTemplateVariablesDocx(File templateFile) throws DocumentTemplateException {
    try {
        FileInputStream templateInputStream = new FileInputStream(templateFile);
        IXDocReport report = readXDocReport(templateInputStream);
        FieldsExtractor<FieldExtractor> extractor = FieldsExtractor.create();
        report.extractFields(extractor);
        return filterExtractedVariables(extractor);
    } catch (XDocReportException | IOException e) {
        throw new DocumentTemplateException(String.format(I18nProperties.getString(Strings.errorReadingTemplate), templateFile.getName()));
    }
}
Also used : IXDocReport(fr.opensagres.xdocreport.document.IXDocReport) DocumentTemplateException(de.symeda.sormas.api.docgeneneration.DocumentTemplateException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) FieldExtractor(fr.opensagres.xdocreport.template.FieldExtractor) XDocReportException(fr.opensagres.xdocreport.core.XDocReportException)

Example 2 with FieldExtractor

use of fr.opensagres.xdocreport.template.FieldExtractor in project SORMAS-Project by hzi-braunschweig.

the class TemplateEngine method getFieldExtractorTxt.

private FieldsExtractor<FieldExtractor> getFieldExtractorTxt(Reader templateFileReader, String templateName) throws DocumentTemplateException {
    FieldsExtractor<FieldExtractor> extractor = FieldsExtractor.create();
    ExtractVariablesVelocityVisitor visitor = new ExtractVariablesVelocityVisitor(extractor);
    try {
        Template template = new Template();
        template.setName(templateName);
        SimpleNode document = RuntimeSingleton.parse(templateFileReader, template);
        document.jjtAccept(visitor, null);
        return extractor;
    } catch (ParseException e) {
        throw new DocumentTemplateException(I18nProperties.getString(Strings.errorProcessingTemplate));
    }
}
Also used : ExtractVariablesVelocityVisitor(fr.opensagres.xdocreport.template.velocity.internal.ExtractVariablesVelocityVisitor) DocumentTemplateException(de.symeda.sormas.api.docgeneneration.DocumentTemplateException) ParseException(org.apache.velocity.runtime.parser.ParseException) FieldExtractor(fr.opensagres.xdocreport.template.FieldExtractor) Template(org.apache.velocity.Template) SimpleNode(org.apache.velocity.runtime.parser.node.SimpleNode)

Example 3 with FieldExtractor

use of fr.opensagres.xdocreport.template.FieldExtractor in project SORMAS-Project by hzi-braunschweig.

the class TemplateEngine method extractTemplateVariablesTxt.

public DocumentVariables extractTemplateVariablesTxt(File templateFile) throws DocumentTemplateException {
    try {
        FileReader templateFileReader = new FileReader(templateFile);
        String templateName = templateFile.getName();
        FieldsExtractor<FieldExtractor> extractor = getFieldExtractorTxt(templateFileReader, templateName);
        return filterExtractedVariables(extractor);
    } catch (IOException e) {
        throw new DocumentTemplateException(String.format(I18nProperties.getString(Strings.errorReadingTemplate), templateFile.getName()));
    }
}
Also used : DocumentTemplateException(de.symeda.sormas.api.docgeneneration.DocumentTemplateException) FileReader(java.io.FileReader) IOException(java.io.IOException) FieldExtractor(fr.opensagres.xdocreport.template.FieldExtractor)

Example 4 with FieldExtractor

use of fr.opensagres.xdocreport.template.FieldExtractor in project SORMAS-Project by hzi-braunschweig.

the class TemplateEngine method filterExtractedVariables.

private DocumentVariables filterExtractedVariables(FieldsExtractor<FieldExtractor> extractor) {
    Set<String> variables = new HashSet<>();
    Set<String> nullableVariables = new HashSet<>();
    for (FieldExtractor field : extractor.getFields()) {
        String fieldName = field.getName();
        Matcher matcher = VARIABLE_PATTERN.matcher(fieldName);
        if (matcher.matches()) {
            String withBrackets = matcher.group(3);
            String withoutBrackets = matcher.group(5);
            String variable = withBrackets != null ? withBrackets : withoutBrackets;
            if (variable != null) {
                variables.add(variable);
            }
            if (matcher.group(2) != null || matcher.group(4) != null) {
                nullableVariables.add(variable);
            } else {
                nullableVariables.remove(variable);
            }
        }
    }
    return new DocumentVariables(variables, nullableVariables);
}
Also used : Matcher(java.util.regex.Matcher) DocumentVariables(de.symeda.sormas.api.docgeneneration.DocumentVariables) FieldExtractor(fr.opensagres.xdocreport.template.FieldExtractor) HashSet(java.util.HashSet)

Example 5 with FieldExtractor

use of fr.opensagres.xdocreport.template.FieldExtractor in project SORMAS-Project by hzi-braunschweig.

the class TemplateEngine method validateTemplateDocx.

public void validateTemplateDocx(InputStream templateInputStream) throws DocumentTemplateException {
    try {
        IXDocReport report = readXDocReport(templateInputStream);
        FieldsExtractor<FieldExtractor> extractor = FieldsExtractor.create();
        report.extractFields(extractor);
    } catch (XDocReportException | IOException e) {
        throw new DocumentTemplateException(I18nProperties.getString(Strings.errorProcessingTemplate));
    }
}
Also used : IXDocReport(fr.opensagres.xdocreport.document.IXDocReport) DocumentTemplateException(de.symeda.sormas.api.docgeneneration.DocumentTemplateException) IOException(java.io.IOException) FieldExtractor(fr.opensagres.xdocreport.template.FieldExtractor) XDocReportException(fr.opensagres.xdocreport.core.XDocReportException)

Aggregations

FieldExtractor (fr.opensagres.xdocreport.template.FieldExtractor)5 DocumentTemplateException (de.symeda.sormas.api.docgeneneration.DocumentTemplateException)4 IOException (java.io.IOException)3 XDocReportException (fr.opensagres.xdocreport.core.XDocReportException)2 IXDocReport (fr.opensagres.xdocreport.document.IXDocReport)2 DocumentVariables (de.symeda.sormas.api.docgeneneration.DocumentVariables)1 ExtractVariablesVelocityVisitor (fr.opensagres.xdocreport.template.velocity.internal.ExtractVariablesVelocityVisitor)1 FileInputStream (java.io.FileInputStream)1 FileReader (java.io.FileReader)1 HashSet (java.util.HashSet)1 Matcher (java.util.regex.Matcher)1 Template (org.apache.velocity.Template)1 ParseException (org.apache.velocity.runtime.parser.ParseException)1 SimpleNode (org.apache.velocity.runtime.parser.node.SimpleNode)1