Java "domlib"

Admin User, created Mar 04. 2024
         
package liblet.misc;
import liblet.util.auxlib;
import nova.Machine;
import nova.Runtime;
import nova.Special;
import nova.Store;
import java.io.IOException;
import java.util.HashMap;
/**
* Modern Albufeira Prolog Interpreter
* <p>
* Warranty & Liability
* To the extent permitted by applicable law and unless explicitly
* otherwise agreed upon, XLOG Technologies AG makes no warranties
* regarding the provided information. XLOG Technologies AG assumes
* no liability that any problems might be solved with the information
* provided by XLOG Technologies AG.
* <p>
* Rights & License
* All industrial property rights regarding the information - copyright
* and patent rights in particular - are the sole property of XLOG
* Technologies AG. If the company was not the originator of some
* excerpts, XLOG Technologies AG has at least obtained the right to
* reproduce, change and translate the information.
* <p>
* Reproduction is restricted to the whole unaltered document. Reproduction
* of the information is only allowed for non-commercial uses. Selling,
* giving away or letting of the execution of the library is prohibited.
* The library can be distributed as part of your applications and libraries
* for execution provided this comment remains unchanged.
* <p>
* Restrictions
* Only to be distributed with programs that add significant and primary
* functionality to the library. Not to be distributed with additional
* software intended to replace any components of the library.
* <p>
* Trademarks
* Jekejeke is a registered trademark of XLOG Technologies AG.
*/
public final class domlib {
/*****************************************************************/
/* Sink API */
/*****************************************************************/
/**
* ir_is_sink(O):
* The predicate succeeds if O is a sink.
*/
private static boolean test_ir_is_sink(Object[] args) {
Object obj = Machine.deref(Machine.exec_build(args[0]));
return (obj instanceof Runtime.Sink);
}
/**
* ir_data_current(W, S):
* The predicate succeeds in S with the underlying stream of the writer W.
*/
private static boolean test_ir_data_current(Object[] args) {
Object obj = Machine.deref(Machine.exec_build(args[0]));
Runtime.check_sink(obj);
return Machine.exec_unify(args[1], ((Runtime.Sink) obj).data);
}
/**
* ir_flush_buffer(W):
* The predicate succeeds. As a side effect the buffer of W is flushed.
*/
private static boolean test_ir_flush_buffer(Object[] args) {
Object obj = Machine.deref(Machine.exec_build(args[0]));
Runtime.check_sink(obj);
Runtime.flush_buffer((Runtime.Sink) obj);
return true;
}
/**
* ir_indent_current(W, I):
* The predicate succeeds in I with the indent of the writer W.
*/
private static boolean test_ir_indent_current(Object[] args) {
Object obj = Machine.deref(Machine.exec_build(args[0]));
Runtime.check_sink(obj);
return Machine.exec_unify(args[1], Integer.valueOf(((Runtime.Sink) obj).indent));
}
/**
* ir_indent_set(W, I):
* The predicate succeeds. As a side effect it sets the indent of the writer W to I.
*/
private static boolean test_ir_indent_set(Object[] args) {
Object obj = Machine.deref(Machine.exec_build(args[0]));
Runtime.check_sink(obj);
Object alpha = Machine.deref(Machine.exec_build(args[1]));
Special.check_integer(alpha);
if (Special.is_bigint(alpha))
throw Machine.make_error(new Store.Compound("representation_error",
new Object[]{"int"}));
((Runtime.Sink) obj).indent = ((Integer) alpha).intValue();
return true;
}
/*****************************************************************/
/* DOM API */
/*****************************************************************/
/**
* dom_output_new(S, W):
* The predicate succeeds in W with a new DOM writer for the underlying stream S.
*/
private static boolean test_dom_output_new(Object[] args) {
Object obj = Machine.deref(Machine.exec_build(args[0]));
Runtime.check_sink(obj);
Runtime.Sink dst = new Runtime.Sink();
dst.data = obj;
dst.send = domlib::dom_send;
dst.release = domlib::dom_release;
dst.notify = domlib::dom_notify;
return Machine.exec_unify(args[1], dst);
}
private static void dom_send(Object data, StringBuilder buf) {
String text = auxlib.xml_escape(buf.toString());
Runtime.put_atom((Runtime.Sink) data, text);
}
private static void dom_notify(Object data) {
Runtime.flush((Runtime.Sink) data);
}
private static void dom_release(Object data) {
Runtime.close(data);
}
/**
* dom_error_new(S, W):
* The predicate succeeds in W with a new DOM writer for the underlying stream S.
*/
private static boolean test_dom_error_new(Object[] args) {
Object obj = Machine.deref(Machine.exec_build(args[0]));
Runtime.check_sink(obj);
Runtime.Sink dst = new Runtime.Sink();
dst.data = obj;
dst.send = domlib::dom_alert;
dst.release = domlib::dom_release;
dst.notify = domlib::dom_notify;
return Machine.exec_unify(args[1], dst);
}
private static void dom_alert(Object data, StringBuilder buf) {
String text = "<span style='color: #A52A2A'>" + auxlib.xml_escape(buf.toString()) + "</span>";
Runtime.put_atom((Runtime.Sink) data, text);
}
/******************************************************************/
/* Dom Lib Init */
/******************************************************************/
public static void main() {
Store.add("ir_is_sink", 1, Special.make_check(domlib::test_ir_is_sink));
Store.add("ir_data_current", 2, Special.make_check(domlib::test_ir_data_current));
Store.add("ir_flush_buffer", 1, Special.make_check(domlib::test_ir_flush_buffer));
Store.add("ir_indent_current", 2, Special.make_check(domlib::test_ir_indent_current));
Store.add("ir_indent_set", 2, Special.make_check(domlib::test_ir_indent_set));
Store.add("dom_output_new", 2, Special.make_check(domlib::test_dom_output_new));
Store.add("dom_error_new", 2, Special.make_check(domlib::test_dom_error_new));
}
}