Skip to content

Commit

Permalink
Templater v6.2.0
Browse files Browse the repository at this point in the history
Update to latest dependencies.
Rename Java signatures. While old signatures still work, they are deprecated.

Upgrade DepartmentReport to show how to implement filter(expression) plugin.
  • Loading branch information
zapov committed Dec 28, 2021
1 parent c3b9873 commit c1d193c
Show file tree
Hide file tree
Showing 169 changed files with 286 additions and 200 deletions.
2 changes: 1 addition & 1 deletion Advanced/CsvStreaming/CsvStreaming.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
<Private>True</Private>
</Reference>
<Reference Include="NGS.Templater">
<HintPath>..\..\packages\Templater.6.1.0\lib\Net40\NGS.Templater.dll</HintPath>
<HintPath>..\..\packages\Templater.6.2.0\lib\Net40\NGS.Templater.dll</HintPath>
<SpecificVersion>False</SpecificVersion>
</Reference>
<Reference Include="System" />
Expand Down
2 changes: 1 addition & 1 deletion Advanced/CsvStreaming/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="DotNetZip" version="1.13.0" targetFramework="net40-Client" />
<package id="Templater" version="6.1.0" targetFramework="net40-Client" />
<package id="Templater" version="6.2.0" targetFramework="net40-Client" />
</packages>
2 changes: 1 addition & 1 deletion Advanced/CsvStreaming/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>hr.ngs.templater.example</groupId>
<artifactId>csv-streaming-example</artifactId>
<packaging>jar</packaging>
<version>6.1.0</version>
<version>6.2.0</version>
<name>CSV streaming</name>
<url>https://github.com/ngs-doo/TemplaterExamples</url>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

public class CsvStreamingExample {

static class Quoter implements IDocumentFactoryBuilder.ILowLevelReplacer {
static class Quoter implements IDocumentFactoryBuilder.LowLevelReplacer {

@Override
public Object replace(Object value, String tag, String[] metadata) {
Expand All @@ -32,7 +32,7 @@ public Object replace(Object value, String tag, String[] metadata) {
return value;
}
}
static class NumberAsComma implements IDocumentFactoryBuilder.ILowLevelReplacer {
static class NumberAsComma implements IDocumentFactoryBuilder.LowLevelReplacer {

@Override
public Object replace(Object value, String tag, String[] metadata) {
Expand Down
5 changes: 4 additions & 1 deletion Advanced/DepartmentReport/DepartmentReport.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="NGS.Templater">
<HintPath>..\..\packages\Templater.6.1.0\lib\Net40\NGS.Templater.dll</HintPath>
<HintPath>..\..\packages\Templater.6.2.0\lib\Net40\NGS.Templater.dll</HintPath>
</Reference>
<Reference Include="NReco.LambdaParser">
<HintPath>..\..\packages\NReco.LambdaParser.1.0.10\lib\portable-net40+sl5+win8+wp8+wpa81\NReco.LambdaParser.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand Down
4 changes: 4 additions & 0 deletions Advanced/DepartmentReport/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,7 @@ This can be used for various purposes:
* calling methods with arguments (unlike only being able to use zero method navigation)
* various other complex logic

There are two navigation plugin implementations in the project:

* sort - using simplistic reflection implementation
* filter - using expression parsing libraries
3 changes: 2 additions & 1 deletion Advanced/DepartmentReport/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Templater" version="6.1.0" targetFramework="net40-Client" />
<package id="Templater" version="6.2.0" targetFramework="net40-Client" />
<package id="NReco.LambdaParser" version="1.0.10" targetFramework="net40" />
</packages>
2 changes: 1 addition & 1 deletion Advanced/DepartmentReport/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>hr.ngs.templater.example</groupId>
<artifactId>department-report-example</artifactId>
<packaging>jar</packaging>
<version>6.1.0</version>
<version>6.2.0</version>
<name>DepartmentReportExample</name>
<url>https://github.com/ngs-doo/TemplaterExamples</url>

Expand Down
Binary file modified Advanced/DepartmentReport/result.xlsx
Binary file not shown.
42 changes: 38 additions & 4 deletions Advanced/DepartmentReport/src/Program.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using System.Collections;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using NGS.Templater;
using NReco.Linq;

namespace DepartmentReport
{
Expand Down Expand Up @@ -64,16 +67,28 @@ public Task(string id, decimal estimated, decimal spent)

public static void Main(string[] args)
{
var company = GetCompany();
using (var fis = File.OpenRead("template/departments.xlsx"))
using (var fos = File.OpenWrite("departments.xlsx"))
using (var doc = Configuration
.Builder
.NavigateSeparator(':')
.WithMatcher(@"[\w\s \.,:?!+\*-<>=()]+")
.NavigateSeparator(':', null)
.Include(SortExpression)
.Include(FilterExpression)
.Build()
.Open(fis, "xlsx", fos))
doc.Process(GetCompany());

{
doc.Process(company);
var flattened =
from d in company.department
from t in d.team
from p in t.project
from e in p.epic
from k in e.task
select new { department = d, team = t, project = p, epic = e, task = k };
doc.Process(new { flatten = flattened });
}
Process.Start(new ProcessStartInfo("departments.xlsx") { UseShellExecute = true });
}

Expand All @@ -86,6 +101,25 @@ static object SortExpression(object parent, object value, string member, string
return col.OfType<object>().OrderBy(it => f.GetValue(it)).ToList();
}

private static readonly LambdaParser ExpressionParser = new LambdaParser { UseCache = true };
private static Func<string, object> GetValue(object instance)
{
var type = instance.GetType();
return name =>
{
var property = type.GetProperty(name);
return property.GetValue(instance, new object[0]);
};
}

static object FilterExpression(object parent, object value, string member, string metadata)
{
var col = value as IEnumerable<object>;
if (!metadata.StartsWith("filter(") || col == null) return value;
var expression = metadata.Substring(7, metadata.Length - 8);
return col.Where(it => (bool)ExpressionParser.Eval(expression, GetValue(it)));
}

private static Company GetCompany()
{
var company = new Company
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import hr.ngs.templater.IDocumentFactoryBuilder;
import hr.ngs.templater.ITemplateDocument;

import javax.script.*;
import java.awt.Desktop;
import java.io.*;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.util.*;
import java.util.stream.Collectors;
Expand All @@ -29,15 +31,10 @@ public Collection summary() {
public final String epic = e.name;
public final String task = it.id;
public final double days = it.spent;
}
)
)
)
)
).collect(Collectors.toList());
}))))).collect(Collectors.toList());
}
}
static class Department {
public static class Department {
public String name;
public String code;
public String head;
Expand All @@ -49,7 +46,7 @@ public Department(String name, String code, String head, Team... teams) {
this.team = teams;
}
}
static class Team {
public static class Team {
public String name;
public String lead;
public Project[] project;
Expand All @@ -59,23 +56,23 @@ public Team(String name, String lead, Project... projects) {
this.project = projects;
}
}
static class Project {
public static class Project {
public String name;
public Epic[] epic;
public Project(String name, Epic... epics) {
this.name = name;
this.epic = epics;
}
}
static class Epic {
public static class Epic {
public String name;
public Task[] task;
public Epic(String name, Task... tasks) {
this.name = name;
this.task = tasks;
}
}
static class Task {
public static class Task {
public String id;
public double estimated;
public double spent;
Expand All @@ -90,22 +87,39 @@ public static void main(final String[] args) throws Exception {
InputStream templateStream = DepartmentReportExample.class.getResourceAsStream("/departments.xlsx");
File tmp = File.createTempFile("department", ".xlsx");

Company company = getCompany();
FileOutputStream fos = new FileOutputStream(tmp);
ITemplateDocument tpl = Configuration
.builder()
.navigateSeparator(':')
.withMatcher("[\\w\\s \\.,:?!+\\*-<>=()]+")
.navigateSeparator(':', null)
.include(new SortExpression())
.include(new FilterExpression())
.build()
.open(templateStream, "xlsx", fos);
tpl.process(getCompany());
tpl.process(company);
List flattened =
Arrays.stream(company.department).flatMap(d ->
Arrays.stream(d.team).flatMap(t ->
Arrays.stream(t.project).flatMap(p ->
Arrays.stream(p.epic).flatMap(e ->
Arrays.stream(e.task).map(it ->
new Object() {
public final Department department = d;
public final Team team = t;
public final Project project = p;
public final Epic epic = e;
public final Task task = it;
}))))).collect(Collectors.toList());
tpl.process(new Object() { public final List flatten = flattened; });
tpl.close();
fos.close();
Desktop.getDesktop().open(tmp);
}

//this is just a simplistic implementation
//a better implementation would take care of methods, dictionaries and various other types
static class SortExpression implements IDocumentFactoryBuilder.INavigate {
static class SortExpression implements IDocumentFactoryBuilder.Navigate {
@Override
public Object navigate(Object parent, Object value, String member, String metadata) {
if (!metadata.startsWith("sort(") || value instanceof Object[] == false) return value;
Expand Down Expand Up @@ -135,6 +149,35 @@ public Object navigate(Object parent, Object value, String member, String metada
}
}

private static final ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByName("JavaScript");

static class FilterExpression implements IDocumentFactoryBuilder.Navigate {
@Override
public Object navigate(Object parent, Object value, String member, String metadata) {
if (!metadata.startsWith("filter(") || value instanceof List == false) return value;
List<Object> elements = (List) value;
if (elements.size() == 0 || elements.get(0) == null) return value;
String expression = metadata.substring(7, metadata.length() - 1);
Field[] fields = elements.get(0).getClass().getFields();
List<Object> result = new ArrayList<>();
Bindings bindings = scriptEngine.createBindings();
for (Object el : elements) {
try {
for (Field f : fields) {
bindings.put(f.getName(), f.get(el));
}
Object eval = scriptEngine.eval(expression, bindings);
if (Boolean.TRUE.equals(eval)) {
result.add(el);
}
} catch (IllegalAccessException | ScriptException e) {
throw new RuntimeException(e);
}
}
return result;
}
}

private static Company getCompany() {
Company company = new Company();
company.name = "Sweat shop ltd.";
Expand Down
Binary file modified Advanced/DepartmentReport/template/departments.xlsx
Binary file not shown.
2 changes: 1 addition & 1 deletion Advanced/DoubleProcessing/DoubleProcessing.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="NGS.Templater">
<HintPath>..\..\packages\Templater.6.1.0\lib\Net40\NGS.Templater.dll</HintPath>
<HintPath>..\..\packages\Templater.6.2.0\lib\Net40\NGS.Templater.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
Expand Down
2 changes: 1 addition & 1 deletion Advanced/DoubleProcessing/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Templater" version="6.1.0" targetFramework="net40-Client" />
<package id="Templater" version="6.2.0" targetFramework="net40-Client" />
</packages>
2 changes: 1 addition & 1 deletion Advanced/DoubleProcessing/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>hr.ngs.templater.example</groupId>
<artifactId>double-processing-example</artifactId>
<packaging>jar</packaging>
<version>6.1.0</version>
<version>6.2.0</version>
<name>DoubleProcessingExample</name>
<url>https://github.com/ngs-doo/TemplaterExamples</url>

Expand Down
2 changes: 1 addition & 1 deletion Advanced/PowerQuery/PowerQuery.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="NGS.Templater">
<HintPath>..\..\packages\Templater.6.1.0\lib\Net40\NGS.Templater.dll</HintPath>
<HintPath>..\..\packages\Templater.6.2.0\lib\Net40\NGS.Templater.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
Expand Down
2 changes: 1 addition & 1 deletion Advanced/PowerQuery/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Templater" version="6.1.0" targetFramework="net40-Client" />
<package id="Templater" version="6.2.0" targetFramework="net40-Client" />
</packages>
2 changes: 1 addition & 1 deletion Advanced/PowerQuery/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>hr.ngs.templater.example</groupId>
<artifactId>power-query-example</artifactId>
<packaging>jar</packaging>
<version>6.1.0</version>
<version>6.2.0</version>
<name>PowerQueryExample</name>
<url>https://github.com/ngs-doo/TemplaterExamples</url>

Expand Down
2 changes: 1 addition & 1 deletion Advanced/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Templater makes it very convenient to manage report templates.

### [Complex Excel report](DepartmentReport/Readme.md)

Combining various Excel features to create good looking report.
Combining various Excel features to create good-looking report. Using expression parser for filtering.

[template](DepartmentReport/template/departments.xlsx?raw=true) - [result](DepartmentReport/result.xlsx?raw=true)

Expand Down
2 changes: 1 addition & 1 deletion Advanced/SalesOrderMVP (.NET)/SalesOrderMVP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
<Private>True</Private>
</Reference>
<Reference Include="NGS.Templater">
<HintPath>..\..\packages\Templater.6.1.0\lib\Net40\NGS.Templater.dll</HintPath>
<HintPath>..\..\packages\Templater.6.2.0\lib\Net40\NGS.Templater.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
Expand Down
2 changes: 1 addition & 1 deletion Advanced/SalesOrderMVP (.NET)/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Fluent.Ribbon" version="3.6.1.236" targetFramework="net40-Client" />
<package id="Templater" version="6.1.0" targetFramework="net40-Client" />
<package id="Templater" version="6.2.0" targetFramework="net40-Client" />
</packages>
2 changes: 1 addition & 1 deletion Advanced/SheetReport/SheetReport.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
<Private>True</Private>
</Reference>
<Reference Include="NGS.Templater">
<HintPath>..\..\packages\Templater.6.1.0\lib\Net40\NGS.Templater.dll</HintPath>
<HintPath>..\..\packages\Templater.6.2.0\lib\Net40\NGS.Templater.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
Expand Down
2 changes: 1 addition & 1 deletion Advanced/SheetReport/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="SharpZipLib" version="0.86.0" targetFramework="net40-Client" />
<package id="Templater" version="6.1.0" targetFramework="net40-Client" />
<package id="Templater" version="6.2.0" targetFramework="net40-Client" />
</packages>
2 changes: 1 addition & 1 deletion Advanced/SheetReport/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>hr.ngs.templater.example</groupId>
<artifactId>sheet-report-example</artifactId>
<packaging>jar</packaging>
<version>6.1.0</version>
<version>6.2.0</version>
<name>SheetReportExample</name>
<url>https://github.com/ngs-doo/TemplaterExamples</url>

Expand Down
Loading

0 comments on commit c1d193c

Please sign in to comment.