working on elements-based engine

This commit is contained in:
Jörg Prante 2021-12-07 09:52:45 +01:00
parent c36adfa79b
commit 072d5e8727
6 changed files with 97 additions and 38 deletions

View file

@ -5,9 +5,7 @@ import org.xbib.settings.Settings;
import java.io.Closeable;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
public class Engine implements Closeable {
@ -24,35 +22,39 @@ public class Engine implements Closeable {
}
public void execute(Settings settings) throws IOException {
execute("document", state, settings);
executeSettings(settings);
}
public void execute(String prefix, State state, Settings settings) throws IOException {
execute(List.of(prefix), state, settings);
}
public void execute(List<String> prefixes, State state, Settings settings) throws IOException {
Map<String, String> map = new LinkedHashMap<>();
for (String prefix : prefixes) {
Settings subSettings = settings.getByPrefix(prefix);
for (String string : subSettings.getAsStructuredMap().keySet()) {
map.put(prefix + string, prefix);
public void executeElements(Settings settings) throws IOException {
execute(settings.getAsSettings("elements"));
for (int i = 0; i < 256; i++) {
if (!executeElement(i, settings)) {
break;
}
}
for (Map.Entry<String, String> entry : map.entrySet()) {
try {
Settings thisSettings = settings.getAsSettings(entry.getKey());
String type = thisSettings.get("type");
if (type == null) {
type = entry.getValue();
}
String className = packageName + ".command." + type.substring(0, 1).toUpperCase() + type.substring(1) + "Command";
Class<?> cl = classLoader.loadClass(className);
Command command = (Command) cl.getConstructor().newInstance();
command.execute(this, state, thisSettings);
} catch (Exception e) {
throw new IOException(e);
}
private boolean executeElement(int i, Settings settings) throws IOException {
String key = "elements." + i;
if (settings.containsSetting(key)) {
executeSettings(settings.getAsSettings(key));
return true;
}
return false;
}
private void executeSettings(Settings settings) throws IOException {
try {
String type = settings.get("type");
if (type == null) {
return;
}
String className = packageName + ".command." + type.substring(0, 1).toUpperCase() + type.substring(1) + "Command";
Class<?> cl = classLoader.loadClass(className);
Command command = (Command) cl.getConstructor().newInstance();
command.execute(this, state, settings);
} catch (Exception e) {
throw new IOException(e);
}
}

View file

@ -23,9 +23,6 @@ public class DocumentCommand implements Command {
.orientation(settings.get("orientiation", "PORTRAIT"))
.build();
state.documents.push(new Document(pageFormat));
engine.execute("image", state, settings);
engine.execute("barcode", state, settings);
engine.execute("path", state, settings);
engine.execute("paragraph", state, settings);
engine.executeElements(settings);
}
}

View file

@ -21,6 +21,6 @@ public class ParagraphCommand implements Command {
paragraph.setMaxWidth(settings.getAsFloat("width", state.documents.peek().getPageWidth()));
}
state.documents.peek().add(paragraph);
engine.execute("text", state, settings);
engine.executeElements(settings);
}
}

View file

@ -8,7 +8,7 @@ import java.util.logging.Logger;
public class ElementsTest {
@Test
public void script() throws Exception {
public void elements() throws Exception {
Settings settings = Settings.settingsBuilder()
.loadFromResource("json", getClass().getResourceAsStream("elements.json"))
.build();

View file

@ -12,12 +12,12 @@ public class ScriptTest {
@Test
public void script() throws Exception {
Settings settings = Settings.settingsBuilder()
.loadFromResource("json", getClass().getResourceAsStream("script.json"))
.loadFromResource("json", getClass().getResourceAsStream("elements.json"))
.build();
Engine engine = new Engine();
engine.execute(settings);
for (Document document : engine.getState().getDocuments()) {
document.render().save(new FileOutputStream("build/script.pdf")).close();
document.render().save(new FileOutputStream("build/elements.pdf")).close();
}
engine.close();
}

View file

@ -1,21 +1,81 @@
{
"type": "document",
"margin": "10 10 10 10",
"elements": [
{
"type": "image",
"scale": 0.25,
"value": "iVBORw0KGgoAAAANSUhEUgAAAfQAAAC0AQMAAABYN0wRAAAABlBMVEUAAAD///+l2Z/dAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH1gsEDjEFTKgt4wAAAi5JREFUaN7t2s1ygyAQAOClHLiVHnvojI/io+mj+Sg+Qo4eGKmwgPkfdjfTNC05ZEblm+BmxQUFL/ocoPnmhX6Fy0/zzf+kN8L8fXl/Fr8Z9O/wACq1nQAs1S9Q/Mabb/6v+qOd0+O82/3C8eFYvn6X++evrno/lwNr88033/zr+Vlnv8BA99vIOSQ/nvahzs+x58G7OBynglnX+jGO78EfIHSF6FfIv2rDoZ7qHRb0wY/xJkT0odPYawvxVkX0M+RevyMj+rANXWj2BTEURD8W/4lzG6KPjWPUPjhen/uB5t/gJOpbKGkeHu07jteP85bsp+K/ON644uMsas0hqfT9mrPWhG2TvK31g8/ebgJrxYXg/TYCoe+CMzjybRt1Xu2+9zEUuL+v9DrsEniz+zgK6dRoqPR29774Ma5x0L2n+654tXvcYHnly2lU+b547fGvlHuHaVTlhys+TWaI3hz7rtb7K/4g9BgWkR8kfhJ6TF+qt0deiTzUe3XF56tY4I3EO6HPc3muT+nL81rkY+RT+rN9St+n+ZT+PG/2VdWf92sqxfRtn8rOOz6nL8O78C31ZSmL7pdUBHUXfj5dAbztO4mPNKc/w0ea05fhJ6EfA40zIhxHiH5N5SjXu1hEcT2Enpf5H8MjcyKvhd482Vuh74R+EHov80rotdBboe+F3nM9TqU133vMnguPzylVzdPO81Y0f+/9i6d7/vP35ptvvvnmX9i/8PuHzf9f/w3g1VrR1Tf4UwAAAABJRU5ErkJggg=="
},
{
"type": "barcode",
"symbol": "Code3Of9",
"value": "12345678"
},
{
"type": "path",
"absolute": false,
"value": "10 10 100 10 100 20 10 20 10 10",
"color": "red",
"dash": 1
},
{
"type": "paragraph",
"elements": [
{
"type": "text",
"value": "Hello World 1"
"value": "Hello World",
"size": 24,
"font": "HELVETICA"
}
]
},
{
"type": "paragraph",
"elements": {
"elements": [
{
"type": "text",
"value": "Hello World 2"
"value": "Hello World",
"size": 24,
"font": "HELVETICA"
}
]
},
{
"type": "paragraph",
"elements": [
{
"type": "text",
"value": "Hello World",
"size": 24,
"font": "HELVETICA"
}
]
},
{
"type": "horizontalruler"
},
{
"type": "paragraph",
"elements": [
{
"type": "text",
"value": "Hello World",
"size": 16,
"font": "NOTOSANS"
}
]
},
{
"type": "paragraph",
"elements": [
{
"type": "text",
"value": "Hello World",
"size": 20,
"font": "NOTOSANS"
}
]
}
]
}