Prolog "xml"

Admin User, erstellt 16. März 2024
         
/**
* 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.
*
* 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.
*
* 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.
*
* 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.
*
* Trademarks
* Jekejeke is a registered trademark of XLOG Technologies AG.
*/
:- ensure_loaded(library(tester/beautify)).
:- ensure_loaded(library(misc/markup)).
/*************************************************************/
/* Colorize Utility XML */
/*************************************************************/
/**
* colorizex_file(A, B):
* The predicate succeeds. As side effect it colorizes the XML
* text A into the file B. An already existing file B is silently
* overwritten.
*/
% colorizex_file(+Atom, +Atom)
colorizex_file(InName, OutName) :-
colorizex_file(InName, OutName, []).
% colorizex_file(+Atom, +Atom, +List)
colorizex_file(InName, OutName, Opts) :-
sys_fancy_opts(Opts, Res),
setup_once_cleanup(
(open(OutName, write, OutStream), dom_output_new(OutStream, DomWriter)),
sys_colorizex_dest(InName, DomWriter, Res),
close(DomWriter)).
% sys_colorizex_dest(+Atom, +Stream, +Term)
sys_colorizex_dest(InName, OutStream, Res) :-
sys_html_header(OutStream, Res),
sys_fancy_begin(OutStream),
setup_once_cleanup(
open(InName, read, InStream),
sys_colorizex_stream(InStream, OutStream),
close(InStream)),
sys_fancy_end(OutStream),
sys_html_footer(OutStream).
% sys_colorizex_stream(+Stream, +Stream)
sys_colorizex_stream(InStream, OutStream) :-
get_atom(InStream, 0'>, A),
sys_colorizex_rest(A, InStream, OutStream).
% sys_colorizex_rest(+Atom, +Stream, +Steam)
sys_colorizex_rest(A, InStream, OutStream) :-
sub_atom(A, _, _, 0, '>'),
sub_atom(A, Pos, Len, Pos2, '<'), !,
sub_atom(A, 0, Pos, _, Text),
sys_fancy_atom(OutStream, Text, ''),
Pos3 is Len+Pos2,
sub_atom(A, _, Pos3, 0, Tag),
sys_colorizex_tag(Tag, InStream, OutStream).
sys_colorizex_rest(A, _, OutStream) :-
sys_fancy_atom(OutStream, A, '').
% sys_colorizex_tag(+Atom, +Stream, +Stream)
sys_colorizex_tag(A, InStream, OutStream) :-
sub_atom(A, 0, _, _, '<!--'), !,
sys_fancy_atom(OutStream, A, 'cm'),
sys_colorizex_stream(InStream, OutStream).
sys_colorizex_tag(A, InStream, OutStream) :-
sys_colorizet_atom(A, OutStream),
sys_colorizex_stream(InStream, OutStream).
/*************************************************************/
/* Colorize Tag XML */
/*************************************************************/
% sys_colorizet_atom(+Atom, +Stream)
sys_colorizet_atom(A, OutStream) :-
open_input_atom_stream(A, MemoryStream),
sys_colorizet_stream(MemoryStream, OutStream).
% sys_colorizet_stream(+Stream, +Stream)
sys_colorizet_stream(InStream, OutStream) :-
get_code(InStream, C),
sys_colorizet_get(C, H, InStream, D),
sys_colorizet_rest(H, D, InStream, OutStream).
% sys_colorizet_rest(+Term, +Integer, +Stream, +Stream)
sys_colorizet_rest(end_of_file, _, _, _) :- !.
sys_colorizet_rest(T, C, InStream, OutStream) :-
sys_colorizet_put(T, OutStream),
sys_colorizet_get(C, H, InStream, D),
sys_colorizet_rest(H, D, InStream, OutStream).
/*************************************************************/
/* Colorize Helper XML */
/*************************************************************/
% sys_colorizet_put(+Term, +Stream)
sys_colorizet_put(atom(A), S) :- !,
tag(S, '<span class="gr">'), put_atom(S, A), tag(S, '</span>').
sys_colorizet_put(T, S) :-
sys_fancy_put(T, S).
% sys_colorizet_get(+Integer, -Term, +Stream, -Integer)
sys_colorizet_get(C, A, S, D) :-
sys_get_comment_t(C, A, S, D), !.
sys_colorizet_get(C, A, S, D) :-
sys_get_token_t(A, S-C, _-D).
% sys_get_comment_t(+Integer, -Term, +Stream, -Integer)
sys_get_comment_t(C, filler([C|L]), S, D) :- sys_is_white(C), !,
get_code(S, H),
sys_get_filler(H, L, S, D).
% sys_get_token_t(-Token, +Pair, -Pair)
sys_get_token_t(T) --> sys_current_code(C),
{sys_code_class(C, D)}, sys_get_class_t(D, C, T).
% sys_get_class_t(+Atom, +Integer, -Token, +Pair, -Pair)
sys_get_class_t(is_solo, C, T) --> !,
sys_get_line_or_solo_t(C, T).
sys_get_class_t(A, C, T) -->
sys_get_class(A, C, T).
/***************************************************************/
/* String Tokens XML */
/***************************************************************/
% sys_get_line_or_solo_t(+Integer, -Token, +Pair, -Pair)
sys_get_line_or_solo_t(0'\', single(R)) --> !,
sys_next_code, sys_get_quote_t(R, 0'\').
sys_get_line_or_solo_t(0'\", codes(R)) --> !,
sys_next_code, sys_get_quote_t(R, 0'\").
sys_get_line_or_solo_t(C, atom(A)) -->
sys_next_code, {char_code(A, C)}.
% sys_get_quote_t(-List, +Code, +Pair, -Pair)
sys_get_quote_t([], Q) --> sys_current_code(Q), !,
sys_next_code.
sys_get_quote_t([C|L], Q) -->
sys_get_char_t(C), sys_get_quote_t(L, Q).
% sys_get_char_t(-Integer, +Pair, -Pair)
sys_get_char_t(_) --> sys_current_code(-1),
{throw(error(syntax_error(unbalanced_quoted),_))}.
sys_get_char_t(C) --> sys_current_code(C),
sys_next_code.