From 366adee43297f2e6fdb75501478dd0fc6af12f4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=CC=88rg=20Prante?= Date: Thu, 27 Jan 2022 10:35:00 +0100 Subject: [PATCH] clean up scripting --- .../layout/element/scripting/Engine.java | 34 +++++++++---------- .../layout/element/scripting/State.java | 21 ++++++++---- .../scripting/command/BarcodeCommand.java | 2 +- .../scripting/command/CellCommand.java | 4 +-- .../scripting/command/ChartCommand.java | 2 +- .../command/ColumnlayoutCommand.java | 2 +- .../element/scripting/command/Command.java | 1 + .../scripting/command/DocumentCommand.java | 2 +- .../scripting/command/FrameCommand.java | 2 +- .../command/HorizontalrulerCommand.java | 2 +- .../scripting/command/ImageCommand.java | 3 +- .../command/MarkpositionCommand.java | 2 +- .../command/MovepositionCommand.java | 2 +- .../scripting/command/NewcolumnCommand.java | 3 +- .../scripting/command/NewpageCommand.java | 3 +- .../scripting/command/ParagraphCommand.java | 6 ++-- .../scripting/command/PathCommand.java | 2 +- .../command/ResetpositionCommand.java | 2 +- .../element/scripting/command/RowCommand.java | 6 ++-- .../scripting/command/SetpositionCommand.java | 5 ++- .../scripting/command/TableCommand.java | 6 ++-- .../scripting/command/TextCommand.java | 4 +-- .../scripting/command/TransformCommand.java | 8 +++-- .../command/VerticalspacerCommand.java | 3 +- .../pdfbox/layout/test/ScriptingTest.java | 7 ++-- 25 files changed, 72 insertions(+), 62 deletions(-) diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/Engine.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/Engine.java index 5fa186d..d2c13ee 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/Engine.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/Engine.java @@ -4,11 +4,10 @@ import java.util.logging.Level; import org.xbib.graphics.pdfbox.layout.element.scripting.command.Command; import org.xbib.settings.Settings; -import java.io.Closeable; import java.io.IOException; import java.util.logging.Logger; -public class Engine implements Closeable { +public class Engine { private static final Logger logger = Logger.getLogger(Engine.class.getName()); @@ -19,8 +18,8 @@ public class Engine implements Closeable { private final State state; public Engine() { - packageName = getClass().getPackageName(); - classLoader = getClass().getClassLoader(); + this.packageName = getClass().getPackageName(); + this.classLoader = getClass().getClassLoader(); this.state = new State(); } @@ -30,12 +29,20 @@ public class Engine implements Closeable { public void executeElements(Settings settings) throws IOException { execute(settings.getAsSettings("elements")); - // limit to 1000 elements, abort if no element is present - for (int i = 0; i < 1000; i++) { + // limit to 1000 elements and abort if no element is present + int i = 0; + for (i = 0; i < 1000; i++) { if (!executeElement(i, settings)) { break; } } + if (i == 1000) { + throw new IllegalStateException("limit of 1000 elements reached"); + } + } + + public State getState() { + return state; } private boolean executeElement(int i, Settings settings) throws IOException { @@ -48,11 +55,11 @@ public class Engine implements Closeable { } private void executeSettings(Settings settings) throws IOException { + String type = settings.get("type"); + if (type == null) { + throw new IllegalArgumentException("missing type in " + settings.getAsMap()); + } 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(); @@ -65,11 +72,4 @@ public class Engine implements Closeable { } } - @Override - public void close() throws IOException { - } - - public State getState() { - return state; - } } diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/State.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/State.java index c805b41..197e44f 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/State.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/State.java @@ -9,18 +9,25 @@ import java.util.stream.Collectors; public class State { - public Stack elements = new Stack<>(); + private final Stack elements = new Stack<>(); - public Document getDocument() { - List list = getDocuments(); - int size = list.size(); - return size > 0 ? list.get(size - 1) : null; + public Stack getElements() { + return elements; } - public List getDocuments() { - return elements.stream() + /** + * Get last document. + * + * @return the last document or null if there is no document. + */ + public Document getDocument() { + // there is no stream function to find last element, so we build the stream of documents into a list + // and get the last element from the list + List list = elements.stream() .filter(e -> e instanceof Document) .map(e -> (Document) e) .collect(Collectors.toList()); + int size = list.size(); + return size == 0 ? null : list.get(size - 1); } } diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/BarcodeCommand.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/BarcodeCommand.java index 46e9e68..3dabd74 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/BarcodeCommand.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/BarcodeCommand.java @@ -53,6 +53,6 @@ public class BarcodeCommand implements Command { float margintop = Float.parseFloat(margins[2]); float marginbottom = Float.parseFloat(margins[3]); VerticalLayoutHint verticalLayoutHint = new VerticalLayoutHint(alignment, marginleft, marginright, margintop, marginbottom, true); - state.elements.peek().add(element, verticalLayoutHint); + state.getElements().peek().add(element, verticalLayoutHint); } } diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/CellCommand.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/CellCommand.java index c53a878..95cf316 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/CellCommand.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/CellCommand.java @@ -65,7 +65,7 @@ public class CellCommand implements Command { } cell.colSpan(settings.getAsInt("colspan", 1)); cell.rowSpan(settings.getAsInt("rowspan", 1)); - state.elements.peek().add(cell.build()); + state.getElements().peek().add(cell.build()); } else if (settings.containsSetting("markup")) { ParagraphCell.Builder cell = ParagraphCell.builder(); cell.colSpan(settings.getAsInt("colspan", 1)); @@ -90,7 +90,7 @@ public class CellCommand implements Command { if (lineSpacing != null && lineSpacing >= 0f) { cell.lineSpacing(lineSpacing); } - state.elements.peek().add(cell.build()); + state.getElements().peek().add(cell.build()); } } } diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/ChartCommand.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/ChartCommand.java index e32f586..f507131 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/ChartCommand.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/ChartCommand.java @@ -50,6 +50,6 @@ public class ChartCommand implements Command { float margintop = Float.parseFloat(margins[2]); float marginbottom = Float.parseFloat(margins[3]); VerticalLayoutHint verticalLayoutHint = new VerticalLayoutHint(alignment, marginleft, marginright, margintop, marginbottom, true); - state.elements.peek().add(element, verticalLayoutHint); + state.getElements().peek().add(element, verticalLayoutHint); } } diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/ColumnlayoutCommand.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/ColumnlayoutCommand.java index 7fce7b7..ecc9059 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/ColumnlayoutCommand.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/ColumnlayoutCommand.java @@ -14,6 +14,6 @@ public class ColumnlayoutCommand implements Command { ColumnLayout columnLayout = new ColumnLayout(); columnLayout.setColumnCount(settings.getAsInt("columns", 2)); columnLayout.setColumnSpacing(settings.getAsFloat("spacing", 10f)); - state.elements.peek().add(columnLayout); + state.getElements().peek().add(columnLayout); } } diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/Command.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/Command.java index bd23db0..90c5f3c 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/Command.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/Command.java @@ -7,5 +7,6 @@ import org.xbib.settings.Settings; import java.io.IOException; public interface Command { + void execute(Engine engine, State state, Settings settings) throws IOException; } diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/DocumentCommand.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/DocumentCommand.java index b9e3685..d3391e6 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/DocumentCommand.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/DocumentCommand.java @@ -67,7 +67,7 @@ public class DocumentCommand implements Command { } else { document.setModificationDate(instant); } - state.elements.push(document); + state.getElements().push(document); engine.executeElements(settings); } } diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/FrameCommand.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/FrameCommand.java index 6046f2b..48a7679 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/FrameCommand.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/FrameCommand.java @@ -23,6 +23,6 @@ public class FrameCommand implements Command { Color borderColor = ColorFactory.web(settings.get("bordercolor", "black")); frame.setBorderColor(borderColor); } - state.elements.peek().add(frame); + state.getElements().peek().add(frame); } } diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/HorizontalrulerCommand.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/HorizontalrulerCommand.java index c156eb4..2694514 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/HorizontalrulerCommand.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/HorizontalrulerCommand.java @@ -32,6 +32,6 @@ public class HorizontalrulerCommand implements Command { if (settings.containsSetting("width")) { horizontalRuler.setMaxWidth(mmToPt(settings.getAsFloat("width", -1f))); } - state.elements.peek().add(horizontalRuler); + state.getElements().peek().add(horizontalRuler); } } diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/ImageCommand.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/ImageCommand.java index 959d92b..5abb469 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/ImageCommand.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/ImageCommand.java @@ -15,6 +15,7 @@ import java.util.Locale; import static org.xbib.graphics.pdfbox.layout.util.PdfUtil.mmToPt; public class ImageCommand implements Command { + @Override public void execute(Engine engine, State state, Settings settings) throws IOException { ImageElement imageElement = new ImageElement(); @@ -48,6 +49,6 @@ public class ImageCommand implements Command { float margintop = Float.parseFloat(margins[2]); float marginbottom = Float.parseFloat(margins[3]); VerticalLayoutHint verticalLayoutHint = new VerticalLayoutHint(alignment, marginleft, marginright, margintop, marginbottom, true); - state.elements.peek().add(imageElement, verticalLayoutHint); + state.getElements().peek().add(imageElement, verticalLayoutHint); } } diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/MarkpositionCommand.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/MarkpositionCommand.java index f05ad85..f6c59a8 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/MarkpositionCommand.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/MarkpositionCommand.java @@ -12,6 +12,6 @@ public class MarkpositionCommand implements Command { @Override public void execute(Engine engine, State state, Settings settings) throws IOException { PositionElement.MarkPosition markPosition = PositionElement.createMarkPosition(); - state.elements.peek().add(markPosition); + state.getElements().peek().add(markPosition); } } diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/MovepositionCommand.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/MovepositionCommand.java index 81fc066..755997a 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/MovepositionCommand.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/MovepositionCommand.java @@ -15,6 +15,6 @@ public class MovepositionCommand implements Command { public void execute(Engine engine, State state, Settings settings) throws IOException { PositionElement.MovePosition movePosition = PositionElement.createMovePosition(mmToPt(settings.getAsFloat("x", 0f)), mmToPt(settings.getAsFloat("y", null))); - state.elements.peek().add(movePosition); + state.getElements().peek().add(movePosition); } } diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/NewcolumnCommand.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/NewcolumnCommand.java index 2e3f368..d0296a5 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/NewcolumnCommand.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/NewcolumnCommand.java @@ -8,9 +8,10 @@ import org.xbib.settings.Settings; import java.io.IOException; public class NewcolumnCommand implements Command { + @Override public void execute(Engine engine, State state, Settings settings) throws IOException { ControlElement controlElement = ControlElement.NEWCOLUMN; - state.elements.peek().add(controlElement); + state.getElements().peek().add(controlElement); } } diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/NewpageCommand.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/NewpageCommand.java index 2cc7abc..769a98e 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/NewpageCommand.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/NewpageCommand.java @@ -8,9 +8,10 @@ import org.xbib.settings.Settings; import java.io.IOException; public class NewpageCommand implements Command { + @Override public void execute(Engine engine, State state, Settings settings) throws IOException { ControlElement controlElement = ControlElement.NEWPAGE; - state.elements.peek().add(controlElement); + state.getElements().peek().add(controlElement); } } diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/ParagraphCommand.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/ParagraphCommand.java index 9f42d1a..d2091a5 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/ParagraphCommand.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/ParagraphCommand.java @@ -33,9 +33,9 @@ public class ParagraphCommand implements Command { if (settings.containsSetting("rotation")) { paragraph.setRotation(settings.getAsFloat("rotation", 0f)); } - state.elements.push(paragraph); + state.getElements().push(paragraph); engine.executeElements(settings); - state.elements.pop(); + state.getElements().pop(); Alignment alignment = Alignment.valueOf(settings.get("layout.alignment", "left").toUpperCase(Locale.ROOT)); String margin = settings.get("layout.margin", "0 0 0 0"); String[] margins = margin.split("\\s+"); @@ -45,6 +45,6 @@ public class ParagraphCommand implements Command { float marginbottom = Float.parseFloat(margins[3]); boolean resetY = settings.getAsBoolean("layout.resety", false); VerticalLayoutHint verticalLayoutHint = new VerticalLayoutHint(alignment, marginleft, marginright, margintop, marginbottom, resetY); - state.elements.peek().add(paragraph, verticalLayoutHint); + state.getElements().peek().add(paragraph, verticalLayoutHint); } } diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/PathCommand.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/PathCommand.java index d8a01d2..9c12c14 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/PathCommand.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/PathCommand.java @@ -61,6 +61,6 @@ public class PathCommand implements Command { strokeBuilder.dashPattern(new Stroke.DashPattern(settings.getAsFloat("dash", 1f))); } Color color = ColorFactory.web(settings.get("color", "black")); - state.elements.peek().add(new PathElement(path, strokeBuilder.build(), color, position), LayoutHint.NOP); + state.getElements().peek().add(new PathElement(path, strokeBuilder.build(), color, position), LayoutHint.NOP); } } diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/ResetpositionCommand.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/ResetpositionCommand.java index 45fdf12..2f87de3 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/ResetpositionCommand.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/ResetpositionCommand.java @@ -12,6 +12,6 @@ public class ResetpositionCommand implements Command { @Override public void execute(Engine engine, State state, Settings settings) throws IOException { PositionElement.ResetPosition resetPosition = PositionElement.createResetPosition(); - state.elements.peek().add(resetPosition); + state.getElements().peek().add(resetPosition); } } diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/RowCommand.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/RowCommand.java index 2c58a8a..db1d695 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/RowCommand.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/RowCommand.java @@ -52,9 +52,9 @@ public class RowCommand implements Command { if (settings.containsSetting("verticalalignment")) { row.verticalAlignment(VerticalAlignment.valueOf(settings.get("verticalalignment", "left").toUpperCase(Locale.ROOT))); } - state.elements.push(row); + state.getElements().push(row); engine.executeElements(settings); - state.elements.pop(); - state.elements.peek().add(row); + state.getElements().pop(); + state.getElements().peek().add(row); } } diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/SetpositionCommand.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/SetpositionCommand.java index 781212d..df9e49d 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/SetpositionCommand.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/SetpositionCommand.java @@ -13,9 +13,8 @@ public class SetpositionCommand implements Command { @Override public void execute(Engine engine, State state, Settings settings) throws IOException { - PositionElement.SetPosition setPosition = - PositionElement.createSetPosition(mmToPt(settings.getAsFloat("x", 0f)), + PositionElement.SetPosition setPosition = PositionElement.createSetPosition(mmToPt(settings.getAsFloat("x", 0f)), mmToPt(settings.getAsFloat("y", 0f))); - state.elements.peek().add(setPosition); + state.getElements().peek().add(setPosition); } } diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/TableCommand.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/TableCommand.java index 5f4309a..4779ff8 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/TableCommand.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/TableCommand.java @@ -34,9 +34,9 @@ public class TableCommand implements Command { float paddingBottom = Float.parseFloat(paddings[3]); tableElement.padding(paddingLeft, paddingRight, paddingTop, paddingBottom); } - state.elements.push(tableElement); + state.getElements().push(tableElement); engine.executeElements(settings); - state.elements.pop(); - state.elements.peek().add(tableElement); + state.getElements().pop(); + state.getElements().peek().add(tableElement); } } diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/TextCommand.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/TextCommand.java index 5725e3f..87e8ce8 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/TextCommand.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/TextCommand.java @@ -25,11 +25,11 @@ public class TextCommand implements Command { float fontsize = settings.getAsFloat("fontsize", 11.0f); Document document = state.getDocument(); Font font = Fonts.valueOf(settings.get("font", "helvetica").toUpperCase(Locale.ROOT)).getFont(document); - Element element = state.elements.peek(); + Element element = state.getElements().peek(); if (element instanceof Paragraph) { element.add(new TextElement(value, font, fontsize)); } else if (element instanceof Document) { - // wrap text into a paragraph + // wrap text into a standard paragraph Paragraph paragraph = new Paragraph(); if (settings.containsSetting("x") && settings.containsSetting("y")) { paragraph.setAbsolutePosition(new Position(mmToPt(settings.getAsFloat("x", 0f)), mmToPt(settings.getAsFloat("y", 0f)))); diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/TransformCommand.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/TransformCommand.java index 6f58e5d..861e7d7 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/TransformCommand.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/TransformCommand.java @@ -17,11 +17,13 @@ public class TransformCommand implements Command { element.setScale(settings.getAsFloat("scalex", null), settings.getAsFloat("scaley", null)); } if (settings.containsSetting("translatex") && settings.containsSetting("translatey")) { - element.setTranslate(mmToPt(settings.getAsFloat("translatex", null)), mmToPt(settings.getAsFloat("translatey", null))); + element.setTranslate(mmToPt(settings.getAsFloat("translatex", null)), + mmToPt(settings.getAsFloat("translatey", null))); } if (settings.containsSetting("angle") && settings.containsSetting("rotatex") && settings.containsSetting("rotatey")) { - element.setRotate(settings.getAsFloat("angle", null), mmToPt(settings.getAsFloat("rotatex", null)), mmToPt(settings.getAsFloat("rotatey", null))); + element.setRotate(settings.getAsFloat("angle", null), mmToPt(settings.getAsFloat("rotatex", null)), + mmToPt(settings.getAsFloat("rotatey", null))); } - state.elements.peek().add(element); + state.getElements().peek().add(element); } } diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/VerticalspacerCommand.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/VerticalspacerCommand.java index c6db456..9ffc740 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/VerticalspacerCommand.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/scripting/command/VerticalspacerCommand.java @@ -8,9 +8,10 @@ import org.xbib.settings.Settings; import java.io.IOException; public class VerticalspacerCommand implements Command { + @Override public void execute(Engine engine, State state, Settings settings) throws IOException { VerticalSpacer verticalSpacer = new VerticalSpacer(settings.getAsFloat("height", 0f)); - state.elements.peek().add(verticalSpacer); + state.getElements().peek().add(verticalSpacer); } } diff --git a/graphics-pdfbox-layout/src/test/java/org/xbib/graphics/pdfbox/layout/test/ScriptingTest.java b/graphics-pdfbox-layout/src/test/java/org/xbib/graphics/pdfbox/layout/test/ScriptingTest.java index 2347e04..72da077 100644 --- a/graphics-pdfbox-layout/src/test/java/org/xbib/graphics/pdfbox/layout/test/ScriptingTest.java +++ b/graphics-pdfbox-layout/src/test/java/org/xbib/graphics/pdfbox/layout/test/ScriptingTest.java @@ -16,10 +16,7 @@ public class ScriptingTest { .build(); Engine engine = new Engine(); engine.execute(settings); - int i = 0; - for (Document document : engine.getState().getDocuments()) { - document.render().save(new FileOutputStream("build/scripting" + (i++) + ".pdf")).close(); - } - engine.close(); + Document document = engine.getState().getDocument(); + document.render().save(new FileOutputStream("build/scripting.pdf")).close(); } }