Skip to content

Commit

Permalink
WIP: integrate new parser in compiler
Browse files Browse the repository at this point in the history
This is a big job:
* added more processor utilities
* converted optional behaviour configured in config to processors
* refactored compiler to separate getting the vars and rendering
* create the var and adjuster image processor and removed the
corresponding code from the compiler
* added factories

Still a few TODOs in the code before it can run.
Still more tests to write.
  • Loading branch information
emeka committed Mar 2, 2020
1 parent 34984be commit 285fe7d
Show file tree
Hide file tree
Showing 87 changed files with 4,516 additions and 2,700 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
/.idea/
/logs/
out/
/data

document-service.jar
**/.DS_Store
.classpath
Expand Down
11 changes: 10 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
FROM eu.gcr.io/blockfactory-01/document-service-base
FROM ubuntu:18.04

## Fix installation of openjdk-8-jre-headless (https://github.com/nextcloud/docker/issues/380)
RUN mkdir -p /usr/share/man/man1
RUN apt-get update && apt-get install -y \
software-properties-common \
language-pack-en-base \
openjdk-8-jre-headless \
libreoffice \
&& apt-get clean && rm -rf /var/cache/* /var/lib/apt/lists/*

#font configuration
COPY ./00-fontconfig.conf /etc/fonts/conf.d/
Expand Down
34 changes: 0 additions & 34 deletions Dockerfile.dev

This file was deleted.

4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
.PHONY: all build build-docker clean clean-docker

all: build build-docker run-docker
all: build build-docker

build:
gradle buildJar
build-docker:
docker image build -f Dockerfile.dev -t document-service .
docker image build -t document-service .
clean:
rm -rf .gradle build document-service.jar
clean-docker:
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,5 @@ dependencies {
compile "org.libreoffice:juh:6.2.3", ex
compile "org.libreoffice:ridl:6.2.3", ex

testCompile "junit:junit:4.10"
testCompile "junit:junit:4.13"
}
7 changes: 7 additions & 0 deletions docker-compose.override.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
version: '3.7'

services:
document-service:
build:
context: .
23 changes: 23 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
version: '3.7'

networks:
xes-platform-network:
name: xes-platform-network

services:
document-service:
image: proxeus/document-service:latest
container_name: xes_document_service
networks:
- xes-platform-network
restart: unless-stopped
environment:
TZ: Europe/Zurich
ports:
- "2115:2115"
- "58082:58082"
volumes:
- ${PROXEUS_DATA_DIR:-./data}/document-service/logs:/document-service/logs
- ${PROXEUS_DATA_DIR:-./data}/document-service/fonts:/document-service/fonts
- ${PROXEUS_DATA_DIR:-./data}/document-service/tmp:/tmp
42 changes: 27 additions & 15 deletions src/main/java/com/proxeus/document/TemplateCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
import com.proxeus.util.zip.EntryFilter;
import com.proxeus.util.zip.Zip;

import com.proxeus.xml.template.TemplateHandlerFactory;
import com.proxeus.xml.template.TemplateVarParserFactory;
import com.proxeus.xml.template.jtwig.JTwigTemplateHandlerFactory;
import com.proxeus.xml.template.jtwig.JTwigTemplateVarParserFactory;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

Expand All @@ -28,54 +32,61 @@ public class TemplateCompiler {
private ODTCompiler odtCompiler;
private DOCXCompiler docxCompiler;
private MyJTwigCompiler compiler;
private TemplateHandlerFactory templateHandlerFactory;
private TemplateVarParserFactory templateVarParserFactory;

public TemplateCompiler(String cacheFolder, LibreOfficeAssistant libreOfficeAssistant) throws Exception{
public TemplateCompiler(String cacheFolder, LibreOfficeAssistant libreOfficeAssistant) throws Exception {
compiler = new MyJTwigCompiler();
odtCompiler = new ODTCompiler(cacheFolder, compiler, libreOfficeAssistant);
docxCompiler = new DOCXCompiler(cacheFolder, compiler, new MicrosoftOfficeAssistant());
templateHandlerFactory = new JTwigTemplateHandlerFactory();
templateVarParserFactory = new JTwigTemplateVarParserFactory();
odtCompiler = new ODTCompiler(cacheFolder, compiler, libreOfficeAssistant, templateHandlerFactory, templateVarParserFactory);
docxCompiler = new DOCXCompiler(cacheFolder, compiler, new MicrosoftOfficeAssistant(), templateHandlerFactory);
}

public FileResult compile(InputStream zipStream, String format, boolean embedError) throws Exception{
public FileResult compile(InputStream zipStream, String format, boolean embedError) throws Exception {
Template template = provideTemplateFromZIP(zipStream, format);
template.embedError = embedError;
return getCompiler(template).Compile(template);
}

public Set<String> vars(InputStream odtStream, String varPrefix) throws Exception{
public Set<String> vars(InputStream odtStream, String varPrefix) throws Exception {
Template template = provideTemplateFromODT(odtStream);
return getCompiler(template).Vars(template, varPrefix);
}

private DocumentCompilerIF getCompiler(Template template){
private DocumentCompilerIF getCompiler(Template template) {
switch (template.type) {
case ODT:return odtCompiler;
case DOCX:return docxCompiler;
case ODT:
return odtCompiler;
case DOCX:
return docxCompiler;

default: return odtCompiler;
default:
return odtCompiler;
}
}

private Template provideTemplateFromZIP(InputStream zipStream, String format) throws Exception {
try{
try {
if (format == null) {
format = "pdf";
}
Template template = extractZIP(zipStream);
template.format = format;
return template;
}catch (Exception e){
} catch (Exception e) {
throw new BadRequestException("Please read the specification for creating the request with the zip package. zip[tmpl.odt,data.json,assets1,asset2...]");
}
}

private Template provideTemplateFromODT(InputStream zipStream) throws Exception {
try{
try {
Template template = new Template();
template.src = new File(template.tmpDir, "tmpl");
template.type = TemplateType.ODT;
FileUtils.copyToFile(zipStream, template.src);
return template;
}catch (Exception e){
} catch (Exception e) {
throw new BadRequestException("Please read the specification for the vars request.");
}
}
Expand All @@ -89,6 +100,7 @@ private Template provideTemplateFromODT(InputStream zipStream) throws Exception
* ---- asset1 // assets that should be referenced in the json data
* ---- asset2
* ---- asset3
*
* @param zipStream ZIP package
* @return a Template that should be ready to be compiled
*/
Expand All @@ -102,14 +114,14 @@ public void next(ZipEntry zipEntry, InputStream zipInputStream) throws Exception
template.type = ODT;
template.src = Zip.zipEntryToFile(zipEntry, zipInputStream, template.tmpDir, "tmpl.odt");
if (!template.src.exists() || template.src.isDirectory()) {
throw new BadRequestException("couldn't extract template odt");
throw new BadRequestException("couldn't process template odt");
}
} else if (zipEntry.getName().toLowerCase().endsWith(".docx")) {
//found a docx template inside the zip
template.type = DOCX;
template.src = Zip.zipEntryToFile(zipEntry, zipInputStream, template.tmpDir, "tmpl.docx");
if (!template.src.exists() || template.src.isDirectory()) {
throw new BadRequestException("couldn't extract template docx");
throw new BadRequestException("couldn't process template docx");
}
} else if (zipEntry.getName().toLowerCase().endsWith(".json")) {
//the json data the template is going to be resolved with
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/proxeus/document/docx/DOCXCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@
import com.proxeus.document.Template;
import com.proxeus.error.NotImplementedException;
import com.proxeus.office.microsoft.MicrosoftOfficeAssistant;
import com.proxeus.xml.template.TemplateHandlerFactory;

import java.util.Set;

public class DOCXCompiler implements DocumentCompilerIF {
private MyJTwigCompiler compiler;
private MicrosoftOfficeAssistant microsoftOfficeAssistant;
private TemplateHandlerFactory templateHandlerFactory;

public DOCXCompiler(String cacheFolder, MyJTwigCompiler compiler, MicrosoftOfficeAssistant msAssistant) throws Exception {
public DOCXCompiler(String cacheFolder, MyJTwigCompiler compiler, MicrosoftOfficeAssistant msAssistant, TemplateHandlerFactory templateHandlerFactory) throws Exception {
this.microsoftOfficeAssistant = msAssistant;
this.compiler = compiler;
this.templateHandlerFactory = templateHandlerFactory;
//TODO impl. if demanded
}

Expand Down
23 changes: 15 additions & 8 deletions src/main/java/com/proxeus/document/odt/ODTCompileRunnable.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
import com.proxeus.compiler.jtwig.MyJTwigCompiler;
import com.proxeus.xml.Compiled;
import com.proxeus.xml.Node;
import com.proxeus.xml.XmlTemplateHandler;
import com.proxeus.xml.template.TemplateHandler;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.*;
import java.util.Map;
import java.util.Queue;

Expand All @@ -16,12 +14,12 @@
*/
public class ODTCompileRunnable implements Runnable {
private File xmlFile;
private XmlTemplateHandler xml;
private TemplateHandler xml;
private Map<String, Object> data;
private MyJTwigCompiler compiler;
private Queue<Exception> exceptions;

public ODTCompileRunnable(MyJTwigCompiler compiler, File xmlFile, XmlTemplateHandler xml, Map<String, Object> data, Queue<Exception> exceptions){
public ODTCompileRunnable(MyJTwigCompiler compiler, File xmlFile, TemplateHandler xml, Map<String, Object> data, Queue<Exception> exceptions) {
this.compiler = compiler;
//copy data as it will be executed async
this.data = data;
Expand All @@ -33,7 +31,9 @@ public ODTCompileRunnable(MyJTwigCompiler compiler, File xmlFile, XmlTemplateHan
}

public void run() {
try(FileOutputStream fos = new FileOutputStream(xmlFile)){
try (FileOutputStream fos = new FileOutputStream(xmlFile)) {

/*
if(xml.containsCode()){
xml.fixCodeStructures();
Node rootNodeContainingCode = xml.getRootNodeContainingCode();
Expand All @@ -45,7 +45,14 @@ public void run() {
rootNodeContainingCode.replaceWith(compiled);
}
}
xml.toOutputStream(fos);
*/

ByteArrayOutputStream out = new ByteArrayOutputStream();
xml.toOutputStream(out);
System.out.printf("DEBUG XML OUTPUT %s\n", xmlFile);
InputStream in = new ByteArrayInputStream(out.toByteArray());
compiler.Compile(in, data, fos, xml.getCharset());

fos.flush();
xml.free();
xmlFile = null;
Expand Down
Loading

0 comments on commit 285fe7d

Please sign in to comment.