Skip to content

Commit

Permalink
Refactors XML to template conversion
Browse files Browse the repository at this point in the history
Previous implemtation was re-implementing the full XML parser.
This version uses Stax as XML parser and adds parsing of the characters
events to extract JTwig code islands.
  • Loading branch information
emeka committed Feb 12, 2020
1 parent 663679c commit 34984be
Show file tree
Hide file tree
Showing 41 changed files with 3,155 additions and 45 deletions.
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
/build/
/.idea/
/logs/
out/
document-service.jar
**/.DS_Store
.classpath
.project
.settings/
bin/

# Ignore Gradle GUI config
gradle-app.setting
Expand All @@ -11,4 +17,4 @@ gradle-app.setting
!gradle-wrapper.jar

# Cache of project
.gradletasknamecache
.gradletasknamecache
5 changes: 5 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
plugins {
id "com.github.hierynomus.license" version "0.15.0"
}

apply plugin: 'groovy'
apply plugin: 'java'

version = '1.0'
sourceCompatibility = 1.8
targetCompatibility = 1.8


allprojects {
gradle.projectsEvaluated {
tasks.withType(JavaCompile) {
Expand Down
Empty file modified client/cmd/bin/dsclient
100644 → 100755
Empty file.
Empty file modified client/cmd/bin/dsclient.mac
100644 → 100755
Empty file.
24 changes: 24 additions & 0 deletions conf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"protocol":"http",
"host":"0.0.0.0",
"port":2115,
"tmpFolder":"docsCache",/** cache during a request, it will be deleted when the request is finished **/
"min": 10,/** min workers **/
"max": 100,/** max workers **/
"timeout":"30s",/** timeout duration **/
"logConfig":{
"level":"TRACE", /** OFF, FATAL, ERROR, WARN, INFO, DEBUG, TRACE, ALL **/
"level_console":"DEBUG", /** OFF, FATAL, ERROR, WARN, INFO, DEBUG, TRACE, ALL **/
"maxFileSize": "5MB",
"maxBackupIndex": 5,
"pattern": "%d{dd.MM.yyyy HH:mm:ss} %-5p %c{1}:%L - %m%n",
"filePath":"./logs/document-service.log"
},
"libreConfig":{
"librepath":"/Applications/LibreOffice.app/Contents/MacOS/soffice", /** the libreoffice executable folder path **/
"min" : 10, /** default 8 | min executables ready to be ready. An executable is mainly needed to convert to PDF. It is recommended to use one exe for a request at the time.**/
"max" : 100, /** default 40 | max capacity of executable running. The next request will be on hold until one is freed or until request timeout..**/
"highLoad": 55 /** highLoad defines the percentage of executables in use, when it is reached prepare new ones to be ready for high availability and fast response.**/
/** Please note! LibreOffice likes to fail sometimes, to have a stable failover, you might want to keep the highLoad value around 50% or even lower.**/
}
}
61 changes: 61 additions & 0 deletions doc/dependency_decisions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
---
- - :permit
- BSD
- :who:
:why:
:versions: []
:when: 2019-12-20 09:24:59.598985000 Z
- - :permit
- MIT
- :who:
:why:
:versions: []
:when: 2019-12-20 09:25:04.086031000 Z
- - :permit
- Apache 2.0
- :who:
:why:
:versions: []
:when: 2019-12-20 09:25:50.159396000 Z
- - :permit
- Apache 2.0, Eclipse Public License - Version 1.0
- :who:
:why:
:versions: []
:when: 2019-12-20 09:26:16.280768000 Z
- - :approve
- juh
- :who:
:why: LibreOffice MPL 2.0
:versions: []
:when: 2019-12-20 13:55:54.070224000 Z
- - :approve
- jurt
- :who:
:why: LibreOffice MPL 2.0
:versions: []
:when: 2019-12-20 13:55:58.446785000 Z
- - :approve
- ridl
- :who:
:why: LibreOffice MPL 2.0
:versions: []
:when: 2019-12-20 13:56:05.186925000 Z
- - :approve
- unoil
- :who:
:why: LibreOffice MPL 2.0
:versions: []
:when: 2019-12-20 13:56:10.946673000 Z
- - :approve
- javax.servlet-api
- :who:
:why: GPLv2 with CPE
:versions: []
:when: 2019-12-20 13:56:57.264730000 Z
- - :approve
- jaxb-api
- :who:
:why: GPLv2 with CPE
:versions: []
:when: 2019-12-20 14:04:55.702653000 Z
Empty file modified gradlew
100644 → 100755
Empty file.
Binary file added output.pdf
Binary file not shown.
3 changes: 2 additions & 1 deletion src/main/java/com/proxeus/document/odt/ODTCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ private FileResult compile(Template template, VarParser varParser) throws Except
return result;
} catch (Exception e) {
cfc.waitForImageTasksToFinish();
throw new UnavailableException("LibreOffice error during convert to " + cfc.template.format + " please try again.");
e.printStackTrace();
throw new UnavailableException("LibreOffice error during convert to " + cfc.template.format + ": " + e.getMessage());
}
}
return null;
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/proxeus/office/libre/LibreConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
public class LibreConfig {
/**
* "/opt/libreoffice5.4/program" "C:/Program Files/LibreOffice 5/program" "/usr/lib/libreoffice/program"
* "/Applications/LibreOffice.app/Contents/MacOS/soffice"
**/
public String librepath = "/usr/lib/libreoffice/program";
/**
Expand All @@ -20,4 +21,4 @@ public class LibreConfig {

/** highLoad defines the percentage of executables in use, when it is reached prepare new ones to be ready for high availability and fast response.**/
public int highLoad = 60;
}
}
19 changes: 16 additions & 3 deletions src/main/java/com/proxeus/office/libre/LibreOfficeAssistant.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,34 @@ public String Convert(File src, File dst, String format) throws Exception {
* @return contentType
*/
public String Convert(File src, File dst, String format, boolean newFontsInstalled) throws Exception {
LibreOffice lo = null;
try{
lo = libreOfficePool.take(newFontsInstalled);
newFontsInstalled = false;
}catch(Exception e){
throw new UnavailableException("Please try again later.", e);
}finally {
libreOfficePool.release();
}

if (lo == null){
throw new UnavailableException("Cannot initialize LibreOffice instance. Please try again later.");
}

int count = 0;
do{
try{
LibreOffice lo = libreOfficePool.take(newFontsInstalled);
newFontsInstalled = false;
return lo.Convert(src, dst, format);
}catch(ExceptionInInitializerError wiie){
wiie.printStackTrace();
++count;
}catch(Exception e){
throw new UnavailableException("Please try again later.", e);
}finally {
libreOfficePool.release();
}
}while(count < 10);
throw new UnavailableException("Please try again later.");
throw new UnavailableException("Cannot initialize LibreOffice instance. Please try again later.");
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/proxeus/office/libre/exe/LibreOffice.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ private void exportDocument(File src, File dst, LibreOfficeFormat outputFormat)
//InputStream input = new FileInputStream(src);
//OOInputStream ooInputStream = new OOInputStream(input);
String sUrl = src.toURI().toString();
System.out.println("DEBUG SURL: " + sUrl);
XComponent oDocToStore = null;
try {
oDocToStore = con.getCompLoader().loadComponentFromURL(sUrl, "_blank", 0, createProps(
Expand All @@ -111,10 +112,10 @@ private void exportDocument(File src, File dst, LibreOfficeFormat outputFormat)
));
if (oDocToStore == null) {
lastReconnect = -1;//force reconnect
throw new ExceptionInInitializerError("Please try again later.");
throw new ExceptionInInitializerError("No doc to store. No Please try again later.");
}
} catch (NullPointerException eee) {
throw new ExceptionInInitializerError("Please try again later.");
throw new ExceptionInInitializerError("Internal error. Please try again later.");
}

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ public LibreOffice take(boolean reconnect) throws Exception {
//looks like the service is under heavy load, lets throw and exceptions saying try again later
//holding the request longer doesn't make sense as it takes more resources
if (lo == null) {
throw new UnavailableException("Please try again later.");
throw new UnavailableException("All LibreOffice instances busy. Please try again later.");
}
} else {
//try poll
Expand All @@ -282,7 +282,7 @@ public LibreOffice take(boolean reconnect) throws Exception {
offerNew();
lo = executables.poll(8, TimeUnit.SECONDS);
if (lo == null) {
throw new UnavailableException("Please try again later.");
throw new UnavailableException("Cannot get LibreOffice instance. Please try again later.");
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/proxeus/xml/jtwig/ExtractorState.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.proxeus.xml.jtwig;

public enum ExtractorState {
XML,
MAYBE_BEGIN_ISLAND,
ISLAND,
SINGLE_QUOTE_STRING,
DOUBLE_QUOTE_STRING,
MAYBE_END_ISLAND,
}
117 changes: 117 additions & 0 deletions src/main/java/com/proxeus/xml/jtwig/ExtractorXMLEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package com.proxeus.xml.jtwig;

import sun.nio.cs.ext.ISCII91;

import javax.xml.namespace.QName;
import javax.xml.stream.Location;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Characters;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
import java.io.Writer;

public class ExtractorXMLEvent implements XMLEvent {

private XMLEvent event;
private ExtractorState state;
private IslandType islandType;

public ExtractorXMLEvent(XMLEvent event, ExtractorState state, IslandType islandType) {
this.event = event;
this.state = state;
this.islandType = islandType;
}

public XMLEvent getEvent() {
return event;
}

public ExtractorState getState() {
return state;
}

@Override
public int getEventType() {
return event.getEventType();
}

@Override
public Location getLocation() {
return event.getLocation();
}

@Override
public boolean isStartElement() {
return event.isStartElement();
}

@Override
public boolean isAttribute() {
return event.isAttribute();
}

@Override
public boolean isNamespace() {
return event.isNamespace();
}

@Override
public boolean isEndElement() {
return event.isEndElement();
}

@Override
public boolean isEntityReference() {
return event.isEntityReference();
}

@Override
public boolean isProcessingInstruction() {
return event.isProcessingInstruction();
}

@Override
public boolean isCharacters() {
return event.isCharacters();
}

@Override
public boolean isStartDocument() {
return event.isStartDocument();
}

@Override
public boolean isEndDocument() {
return event.isEndDocument();
}

@Override
public StartElement asStartElement() {
return event.asStartElement();
}

@Override
public EndElement asEndElement() {
return event.asEndElement();
}

@Override
public Characters asCharacters() {
return event.asCharacters();
}

@Override
public QName getSchemaType() {
return event.getSchemaType();
}

@Override
public void writeAsEncodedUnicode(Writer writer) throws XMLStreamException {
event.writeAsEncodedUnicode(writer);
}

public String toString() {
return event.toString();
}
}
7 changes: 7 additions & 0 deletions src/main/java/com/proxeus/xml/jtwig/IslandType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.proxeus.xml.jtwig;

public enum IslandType {
CODE,
OUTPUT,
COMMENT
}
Loading

0 comments on commit 34984be

Please sign in to comment.