Java "auxlib"

Admin User, created Apr 16. 2024
         
package liblet.util;
import nova.Machine;
import nova.Runtime;
import nova.Special;
import nova.Store;
/**
* 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 auxlib {
/******************************************************************/
/* open_input_atom_stream/2, open_output_atom_stream/1 and close_output_atom_stream/2 */
/******************************************************************/
/**
* open_input_atom_stream(A, S): internal only
* The built-in succeeds in S with a new input stream for the atom A.
*/
private static boolean test_open_input_atom_stream(Object[] args) {
Object text = Machine.deref(Machine.exec_build(args[0]));
Special.check_atom(text);
Runtime.Source src = new Runtime.Source();
src.buf = (String)text;
return Machine.exec_unify(args[1], src);
}
/**
* open_output_atom_stream(S): internal only
* The built-in succeeds in S with a new output stream.
*/
private static boolean test_open_output_atom_stream(Object[] args) {
Runtime.Sink dst = new Runtime.Sink();
dst.data = new StringBuilder();
dst.send = auxlib::mem_send;
return Machine.exec_unify(args[0], dst);
}
private static void mem_send(Object data, StringBuilder buf) {
((StringBuilder) data).append(buf);
}
/**
* close_output_atom_stream(S, A): internal only
* The built-in succeeds in A with the content of the output stream S.
*/
private static boolean test_close_output_atom_stream(Object[] args) {
Object writer = Machine.deref(Machine.exec_build(args[0]));
Runtime.check_sink(writer);
writer = ((StringBuilder) ((Runtime.Sink) writer).data).toString();
return Machine.exec_unify(args[1], writer);
}
/******************************************************************/
/* XML Escape */
/******************************************************************/
/**
* xml_escape(X, Y):
* The predicate succeeds in Y with the XML escape of X.
*/
private static boolean test_xml_escape(Object[] args) {
Object alpha = Machine.deref(Machine.exec_build(args[0]));
Special.check_atom(alpha);
alpha = xml_escape((String) alpha);
return Machine.exec_unify(args[1], alpha);
}
public static String xml_escape(String text) {
StringBuilder res = null;
int back = 0;
int i = 0;
while (i < text.length()) {
int ch = text.codePointAt(i);
String elem;
switch (ch) {
case 34: // "
elem = "&quot;";
break;
case 38: // &
elem = "&amp;";
break;
case 39: // '
elem = "&apos;";
break;
case 60: // <
elem = "&lt;";
break;
case 62: // >
elem = "&gt;";
break;
default:
elem = null;
break;
}
if (elem != null) {
if (res == null)
res = new StringBuilder();
res.append(text, back, i);
res.append(elem);
back = i + 1;
}
i += Character.charCount(ch);
}
if (back != 0) {
res.append(text, back, text.length());
text = res.toString();
}
return text;
}
/******************************************************************/
/* Percent Encode */
/******************************************************************/
/**
* percent_encode(X, Y):
* The predicate succeeds in Y with the percent encode of X.
*/
private static boolean test_percent_encode(Object[] args) {
Object alpha = Machine.deref(Machine.exec_build(args[0]));
if (Store.is_variable(alpha)) {
Object beta = Machine.deref(Machine.exec_build(args[1]));
beta = percent_decode((String) beta);
return Machine.unify(alpha, beta);
} else {
Special.check_atom(alpha);
alpha = percent_encode((String) alpha);
return Machine.exec_unify(args[1], alpha);
}
}
public static String percent_encode(String text) {
StringBuilder res = null;
int back = 0;
int i = 0;
while (i < text.length()) {
int ch = text.codePointAt(i);
i += Character.charCount(ch);
String elem;
switch (ch) {
case 32: // space
elem = "+";
break;
case 35: // #
elem = "%23";
break;
case 37: // %
elem = "%25";
break;
case 38: // &
elem = "%26";
break;
case 43: // +
elem = "%2B";
break;
case 61: // =
elem = "%3D";
break;
case 63: // ?
elem = "%3F";
break;
case 92: // \
elem = "%5C";
break;
default:
elem = null;
break;
}
if (elem != null) {
if (res == null)
res = new StringBuilder();
res.append(text, back, i - 1);
res.append(elem);
back = i;
}
}
if (back != 0) {
res.append(text, back, text.length());
text = res.toString();
}
return text;
}
public static String percent_decode(String text) {
StringBuilder res = null;
int back = 0;
int i = 0;
while (i < text.length()) {
int ch = text.codePointAt(i);
i += Character.charCount(ch);
if (ch == 37) { // %
int octet = octet_hex(text, i);
if (0 <= octet && octet < 128) {
if (res == null)
res = new StringBuilder();
res.append(text, back, i - 1);
res.appendCodePoint(octet);
i += 2;
back = i;
} else if (192 <= octet && octet < 224) {
int octet2 = percent_hex(text, i + 2);
if (128 <= octet2 && octet2 < 192) {
if (res == null)
res = new StringBuilder();
res.append(text, back, i - 1);
res.appendCodePoint((octet - 192) * 64 + (octet2 - 128));
i += 5;
back = i;
}
} else if (224 <= octet && octet < 240) {
int octet2 = percent_hex(text, i + 2);
if (128 <= octet2 && octet2 < 192) {
octet = (octet - 224) * 64 + (octet2 - 128);
octet2 = percent_hex(text, i + 5);
if (128 <= octet2 && octet2 < 192) {
if (res == null)
res = new StringBuilder();
res.append(text, back, i - 1);
res.appendCodePoint(octet * 64 + (octet2 - 128));
i += 8;
back = i;
}
}
} else if (240 <= octet && octet < 248) {
int octet2 = percent_hex(text, i + 2);
if (128 <= octet2 && octet2 < 192) {
octet = (octet - 240) * 64 + (octet2 - 128);
octet2 = percent_hex(text, i + 5);
if (128 <= octet2 && octet2 < 192) {
octet = octet * 64 + (octet2 - 128);
octet2 = percent_hex(text, i + 8);
if (128 <= octet2 && octet2 < 192) {
if (res == null)
res = new StringBuilder();
res.append(text, back, i - 1);
res.appendCodePoint(octet * 64 + (octet2 - 128));
i += 11;
back = i;
}
}
}
}
} else if (ch == 43) { // +
if (res == null)
res = new StringBuilder();
res.append(text, back, i - 1);
res.appendCodePoint(32); // space
back = i;
}
}
if (back != 0) {
res.append(text, back, text.length());
text = res.toString();
}
return text;
}
private static int percent_hex(String text, int i) {
int ch = (i < text.length() ? text.charAt(i) : -1);
if (ch == 37) {
i++;
return octet_hex(text, i);
}
return -1;
}
private static int octet_hex(String text, int i) {
int val = digit_hex(i < text.length() ? text.charAt(i) : -1);
if (val != -1) {
i++;
int val2 = digit_hex(i < text.length() ? text.charAt(i) : -1);
if (val2 != -1) {
return val * 16 + val2;
}
}
return -1;
}
private static int digit_hex(int ch) {
if (48 <= ch && ch <= 57) return ch - 48;
if (65 <= ch && ch <= 70) return ch - 65 + 10;
if (97 <= ch && ch <= 102) return ch - 97 + 10;
return -1;
}
/******************************************************************/
/* Aux Lib Init */
/******************************************************************/
public static void main() {
Store.add("open_input_atom_stream", 2, Special.make_check(auxlib::test_open_input_atom_stream));
Store.add("open_output_atom_stream", 1, Special.make_check(auxlib::test_open_output_atom_stream));
Store.add("close_output_atom_stream", 2, Special.make_check(auxlib::test_close_output_atom_stream));
Store.add("xml_escape", 2, Special.make_check(auxlib::test_xml_escape));
Store.add("percent_encode", 2, Special.make_check(auxlib::test_percent_encode));
}
}