queryByClass(String className);
}
\ No newline at end of file
diff --git a/src/main/java/org/openautonomousconnection/luascript/hosts/EventHost.java b/src/main/java/org/openautonomousconnection/luascript/hosts/EventHost.java
index e2c65a8..c275464 100644
--- a/src/main/java/org/openautonomousconnection/luascript/hosts/EventHost.java
+++ b/src/main/java/org/openautonomousconnection/luascript/hosts/EventHost.java
@@ -1,37 +1,41 @@
package org.openautonomousconnection.luascript.hosts;
+import java.util.Map;
+
/**
- * Event subscription abstraction (implemented by the client UI layer).
+ * Abstraction over DOM event subscription for scripting.
+ *
+ * Implementations forward DOM events into a {@code LuaEventRouter}.
*/
public interface EventHost {
/**
- * Subscribes to an element event.
+ * Adds a listener for an element's event.
*
* @param elementId element id
- * @param eventName event name (e.g. click)
+ * @param eventName normalized event name
*/
void addListener(String elementId, String eventName);
/**
- * Unsubscribes from an element event.
+ * Removes a listener for an element's event.
*
* @param elementId element id
- * @param eventName event name
+ * @param eventName normalized event name
*/
void removeListener(String elementId, String eventName);
/**
- * Subscribes to a global event (app/window scope).
+ * Adds a global (document-level) listener.
*
- * @param eventName event name
+ * @param eventName normalized event name
*/
void addGlobalListener(String eventName);
/**
- * Unsubscribes from a global event.
+ * Removes a global (document-level) listener.
*
- * @param eventName event name
+ * @param eventName normalized event name
*/
void removeGlobalListener(String eventName);
-}
+}
\ No newline at end of file
diff --git a/src/main/java/org/openautonomousconnection/luascript/hosts/HostServices.java b/src/main/java/org/openautonomousconnection/luascript/hosts/HostServices.java
index da1b43d..d7e6b33 100644
--- a/src/main/java/org/openautonomousconnection/luascript/hosts/HostServices.java
+++ b/src/main/java/org/openautonomousconnection/luascript/hosts/HostServices.java
@@ -1,165 +1,145 @@
package org.openautonomousconnection.luascript.hosts;
-import javafx.scene.web.WebEngine;
-import org.openautonomousconnection.luascript.fx.FxDomHost;
-import org.openautonomousconnection.luascript.fx.FxEventHost;
-import org.openautonomousconnection.luascript.fx.FxUiHost;
-import org.openautonomousconnection.luascript.fx.FxWebViewResourceHost;
-import org.openautonomousconnection.luascript.runtime.LuaRuntime;
-
import java.util.Objects;
import java.util.Optional;
/**
- * Service container holding optional host capabilities.
+ * Container for host-side services exposed to Lua.
*
- * This avoids one huge "bridge" interface.
+ * All services are optional to allow different embedding scenarios.
*/
-public final class HostServices {
-
- private final ConsoleHost console;
- private final DomHost dom;
- private final EventHost events;
- private final ResourceHost resources;
- private final UiHost ui;
-
- private HostServices(Builder b) {
- this.console = b.console;
- this.ui = b.ui;
- this.dom = b.dom;
- this.events = b.events;
- this.resources = b.resources;
- }
+public interface HostServices {
/**
- * Builds a JavaFX WebView preset of HostServices (DOM + events + resources + UI).
+ * Returns an optional UI host.
*
- * Important: This method creates the {@link EventHost} using the provided {@link LuaRuntime}'s
- * {@link org.openautonomousconnection.luascript.runtime.LuaEventRouter}. Therefore, the runtime must already
- * be constructed before calling this method.
+ * @return ui host
+ */
+ Optional ui();
+
+ /**
+ * Returns an optional DOM host.
*
- * Note: You should call {@link FxDomHost#ensureAllElementsHaveId()} after the WebEngine finished loading.
+ * @return dom host
+ */
+ Optional dom();
+
+ /**
+ * Returns an optional event host.
*
- * @param engine JavaFX WebEngine
- * @param runtime Lua runtime used for event routing
- * @param console optional console host (may be null)
- * @return HostServices preset for JavaFX WebView
+ * @return event host
*/
- public static HostServices fxPreset(WebEngine engine, LuaRuntime runtime, ConsoleHost console) {
- Objects.requireNonNull(engine, "engine");
- Objects.requireNonNull(runtime, "runtime");
-
- FxDomHost dom = new FxDomHost(engine);
- FxWebViewResourceHost resources = new FxWebViewResourceHost(engine);
- FxUiHost ui = new FxUiHost(engine, dom);
- FxEventHost events = new FxEventHost(dom, runtime.eventRouter());
-
- Builder b = builder().dom(dom).events(events).resources(resources).ui(ui);
-
- if (console != null) {
- b.console(console);
- }
-
- return b.build();
- }
+ Optional events();
/**
- * @return builder
+ * Returns an optional resource host.
+ *
+ * @return resource host
*/
- public static Builder builder() {
- return new Builder();
- }
+ Optional resources();
/**
- * @return optional DomHost capability
+ * Returns an optional console host.
+ *
+ * @return console host
*/
- public Optional dom() {
- return Optional.ofNullable(dom);
- }
+ Optional console();
/**
- * @return optional EventHost capability
+ * Simple immutable implementation.
*/
- public Optional events() {
- return Optional.ofNullable(events);
- }
-
- /**
- * @return optional ResourceHost capability
- */
- public Optional resources() {
- return Optional.ofNullable(resources);
- }
-
- /**
- * @return optional console host
- */
- public Optional console() {
- return Optional.ofNullable(console);
- }
-
- /**
- * @return optional ui host
- */
- public Optional ui() {
- return Optional.ofNullable(ui);
- }
-
- /**
- * Builder for HostServices.
- */
- public static final class Builder {
- private ConsoleHost console;
- private DomHost dom;
- private EventHost events;
- private ResourceHost resources;
- private UiHost ui;
-
- public Builder console(ConsoleHost console) {
- this.console = Objects.requireNonNull(console, "console");
- return this;
- }
-
- public Builder ui(UiHost ui) {
- this.ui = Objects.requireNonNull(ui, "ui");
- return this;
- }
+ final class Default implements HostServices {
+ private final UiHost ui;
+ private final DomHost dom;
+ private final EventHost events;
+ private final ResourceHost resources;
+ private final ConsoleHost console;
/**
- * Provides dom capability.
+ * Creates a HostServices container.
*
+ * @param ui ui host
* @param dom dom host
- * @return this
- */
- public Builder dom(DomHost dom) {
- this.dom = Objects.requireNonNull(dom, "dom");
- return this;
- }
-
- /**
- * Provides event subscription capability.
- *
* @param events event host
- * @return this
- */
- public Builder events(EventHost events) {
- this.events = Objects.requireNonNull(events, "events");
- return this;
- }
-
- /**
- * Provides resource loading capability.
- *
* @param resources resource host
- * @return this
+ * @param console console host
*/
- public Builder resources(ResourceHost resources) {
- this.resources = Objects.requireNonNull(resources, "resources");
- return this;
+ public Default(UiHost ui, DomHost dom, EventHost events, ResourceHost resources, ConsoleHost console) {
+ this.ui = ui;
+ this.dom = dom;
+ this.events = events;
+ this.resources = resources;
+ this.console = console;
}
- public HostServices build() {
- return new HostServices(this);
+ @Override
+ public Optional ui() {
+ return Optional.ofNullable(ui);
+ }
+
+ @Override
+ public Optional dom() {
+ return Optional.ofNullable(dom);
+ }
+
+ @Override
+ public Optional events() {
+ return Optional.ofNullable(events);
+ }
+
+ @Override
+ public Optional resources() {
+ return Optional.ofNullable(resources);
+ }
+
+ @Override
+ public Optional console() {
+ return Optional.ofNullable(console);
}
}
-}
+
+ /**
+ * Stdout-based console host.
+ */
+ final class StdoutConsole implements ConsoleHost {
+ private final String prefix;
+
+ /**
+ * Creates a new stdout console with a prefix.
+ *
+ * @param prefix prefix (may be empty)
+ */
+ public StdoutConsole(String prefix) {
+ this.prefix = Objects.requireNonNull(prefix, "prefix");
+ }
+
+ @Override
+ public void info(String message) {
+ System.out.println(prefix + "[info] " + safe(message));
+ }
+
+ @Override
+ public void log(String message) {
+ System.out.println(prefix + "[log] " + safe(message));
+ }
+
+ @Override
+ public void warn(String message) {
+ System.out.println(prefix + "[warn] " + safe(message));
+ }
+
+ @Override
+ public void error(String message) {
+ System.err.println(prefix + "[error] " + safe(message));
+ }
+
+ @Override
+ public void exception(String message) {
+ System.err.println(prefix + "[exception] " + safe(message));
+ }
+
+ private static String safe(String s) {
+ return s == null ? "" : s;
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/org/openautonomousconnection/luascript/hosts/ResourceHost.java b/src/main/java/org/openautonomousconnection/luascript/hosts/ResourceHost.java
index 3528c48..4fb33a4 100644
--- a/src/main/java/org/openautonomousconnection/luascript/hosts/ResourceHost.java
+++ b/src/main/java/org/openautonomousconnection/luascript/hosts/ResourceHost.java
@@ -1,16 +1,16 @@
package org.openautonomousconnection.luascript.hosts;
/**
- * Resource loading abstraction for LuaScript (e.g. script src).
+ * Abstraction for loading external resources (e.g. {@code