mirror of https://github.com/gnosygnu/xowa
parent
794b5a232f
commit
8e041d6e06
@ -0,0 +1,77 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx;
|
||||
public class Err extends RuntimeException {
|
||||
private final boolean is_gplx;
|
||||
private final String trace;
|
||||
private Err_msg[] msgs_ary = new Err_msg[8]; private int msgs_len = 8, msgs_idx = 0;
|
||||
public Err(boolean is_gplx, String trace, String type, String msg, Object... args) {
|
||||
this.is_gplx = is_gplx;
|
||||
this.trace = is_gplx ? Err_.Trace_lang(this) : trace; // NOTE: Err_ factory methods pass in null stack trace for gplx excs; call Stack_trace here, note that trace will not show constructor
|
||||
Msgs_add(type, msg, args);
|
||||
}
|
||||
public int Trace_ignore() {return trace_ignore;} public Err Trace_ignore_add_1_() {++trace_ignore; return this;} private int trace_ignore = 0;
|
||||
public Err Args_add(Object... args) {msgs_ary[msgs_idx - 1].Args_add(args); return this;} // i - 1 to get current
|
||||
@gplx.Internal protected boolean Type_match(String type) {
|
||||
for (int i = 0; i < msgs_len; ++i) {
|
||||
if (String_.Eq(type, msgs_ary[i].Type())) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@gplx.Internal protected void Msgs_add(String type, String msg, Object[] args) {
|
||||
if (msgs_idx == msgs_len) {
|
||||
int new_len = msgs_len * 2;
|
||||
Err_msg[] new_ary = new Err_msg[new_len];
|
||||
Array_.CopyTo(msgs_ary, new_ary, 0);
|
||||
this.msgs_ary = new_ary;
|
||||
this.msgs_len = new_len;
|
||||
}
|
||||
msgs_ary[msgs_idx] = new Err_msg(type, msg, args);
|
||||
++msgs_idx;
|
||||
}
|
||||
public String To_str__full() {return To_str(Bool_.N);}
|
||||
public String To_str__log() {return To_str(Bool_.Y);}
|
||||
private String To_str(boolean called_by_log) {
|
||||
String nl_str = called_by_log ? "\t" : "\n";
|
||||
String rv = ""; //nl_str + "----------------------------------------------------------------------" + nl_str;
|
||||
for (int i = 0; i < msgs_idx; ++i) {
|
||||
rv += "[err " + Int_.Xto_str(i) + "] " + msgs_ary[i].To_str() + nl_str;
|
||||
}
|
||||
rv += "[trace]:" + Trace_to_str(is_gplx, called_by_log, trace_ignore, trace == null ? Err_.Trace_lang(this) : trace);
|
||||
return rv;
|
||||
}
|
||||
@Override public String getMessage() {return To_str__full();}
|
||||
public static String Trace_to_str(boolean is_gplx, boolean called_by_log, int ignore_lines, String trace) {
|
||||
String[] lines = String_.Split_lang(trace, '\n'); int lines_len = lines.length;
|
||||
int line_bgn = 0;
|
||||
if (is_gplx) { // remove Err_.new_wo_type lines from trace for gplx exceptions
|
||||
for (int i = 0; i < lines_len; ++i) {
|
||||
String line = lines[i];
|
||||
if (String_.Has_at_bgn(line, "gplx.Err_.new")) continue; // ignore trace frames with "gplx.Err_.new"; EX: throw Err_.new_unimplemented
|
||||
line_bgn = i + ignore_lines;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// concat lines
|
||||
String rv = "";
|
||||
String line_bgn_dlm = called_by_log ? "\t " : "\n "; // "\n " indents
|
||||
for (int i = line_bgn; i < lines_len; ++i)
|
||||
rv += line_bgn_dlm + lines[i];
|
||||
return rv;
|
||||
}
|
||||
}
|
@ -0,0 +1,71 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx;
|
||||
public class Err_ {
|
||||
private static String Type__gplx = "gplx"; @gplx.Internal protected static String Trace_null = null;
|
||||
public static void Noop(Exception e) {}
|
||||
public static Err as_(Object obj) {return obj instanceof Err ? (Err)obj : null;}
|
||||
public static Err new_(String type, String msg, Object... args) {return new Err(Bool_.Y, Trace_null, type, msg, args);}
|
||||
public static Err new_wo_type(String msg, Object... args) {return new Err(Bool_.Y, Trace_null, Type__gplx, msg, args);}
|
||||
public static Err new_exc(Exception e, String type, String msg, Object... args) {
|
||||
Err rv = cast_or_make(e);
|
||||
rv.Msgs_add(type, msg, args);
|
||||
return rv;
|
||||
}
|
||||
public static Err new_unhandled(Object val) {return new Err(Bool_.Y, Trace_null, Type__gplx, "val is not in switch/if", "val", val);}
|
||||
public static Err new_unimplemented() {return new Err(Bool_.Y, Trace_null, Type__gplx, "method not implemented");}
|
||||
public static Err new_unimplemented_w_msg(String msg, Object... args) {return new Err(Bool_.Y, Trace_null, Type__gplx, msg, args);}
|
||||
public static Err new_deprecated(String s) {return new Err(Bool_.Y, Trace_null, Type__gplx, "deprecated", "method", s);}
|
||||
public static Err new_parse_type(Class<?> c, String raw) {return new_parse(ClassAdp_.FullNameOf_type(c), raw);}
|
||||
public static Err new_parse_exc(Exception e, Class<?> c, String raw) {return new_parse(ClassAdp_.FullNameOf_type(c), raw).Args_add("e", Err_.Message_lang(e));}
|
||||
public static Err new_parse(String type, String raw) {return new Err(Bool_.Y, Trace_null, Type__gplx, "parse failed", "type", type, "raw", raw);}
|
||||
public static Err new_null() {return new Err(Bool_.Y, Trace_null, Type__gplx, "null obj");}
|
||||
public static Err new_missing_idx(int idx, int len) {return new Err(Bool_.Y, Trace_null, Type__gplx, "index is out of bounds", "idx", idx, "len", len);}
|
||||
public static Err new_missing_key(String key) {return new Err(Bool_.Y, Trace_null, Type__gplx, "key not found", "key", key);}
|
||||
public static Err new_invalid_op(String msg) {return new Err(Bool_.Y, Trace_null, Type__gplx, msg);}
|
||||
public static Err new_invalid_arg(String msg, Object... args) {return new Err(Bool_.Y, Trace_null, Type__gplx, msg, args);}
|
||||
public static Err new_op_canceled() {return new Err(Bool_.Y, Trace_null, Type__op_canceled, "canceled by usr");}
|
||||
public static Err new_type_mismatch_w_exc(Exception ignore, Class<?> t, Object o) {return new_type_mismatch(t, o);}
|
||||
public static Err new_type_mismatch(Class<?> t, Object o) {return new Err(Bool_.Y, Trace_null, Type__gplx, "type mismatch", "expdType", ClassAdp_.FullNameOf_type(t), "actlType", ClassAdp_.NameOf_obj(o), "actlObj", Object_.Xto_str_strict_or_null_mark(o));}
|
||||
public static Err new_cast(Exception ignore, Class<?> t, Object o) {
|
||||
String o_str = "";
|
||||
try {o_str = Object_.Xto_str_strict_or_null_mark(o);}
|
||||
catch (Exception e) {o_str = "<ERROR>"; Err_.Noop(e);}
|
||||
return new Err(Bool_.Y, Trace_null, Type__gplx, "cast failed", "type", ClassAdp_.NameOf_type(t), "obj", o_str);
|
||||
}
|
||||
|
||||
public static String Message_lang(Exception e) {return e.getMessage();}
|
||||
public static String Trace_lang(Exception e) {
|
||||
String rv = "";
|
||||
StackTraceElement[] ary = e.getStackTrace();
|
||||
int len = ary.length;
|
||||
for (int i = 0; i < len; i++) {
|
||||
if (i != 0) rv += "\n";
|
||||
rv += ary[i].toString();
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public static boolean Type_match(Exception e, String type) {
|
||||
Err exc = Err_.as_(e);
|
||||
return exc == null ? false : exc.Type_match(type);
|
||||
}
|
||||
public static String Message_gplx_full(Exception e) {return cast_or_make(e).To_str__full();}
|
||||
public static String Message_gplx_log(Exception e) {return cast_or_make(e).To_str__log();}
|
||||
private static Err cast_or_make(Exception e) {return ClassAdp_.Eq_typeSafe(e, Err.class) ? (Err)e : new Err(Bool_.N, Err_.Trace_lang(e), ClassAdp_.NameOf_obj(e), Err_.Message_lang(e));}
|
||||
public static final String Type__op_canceled = "gplx.op_canceled";
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx;
|
||||
import org.junit.*;
|
||||
public class Err_tst {
|
||||
private final Err_fxt fxt = new Err_fxt();
|
||||
@Test public void Trace_to_str__gplx() {
|
||||
fxt.Test_Trace_to_str(Bool_.Y, Bool_.N, 0, String_.Concat_lines_nl_skip_last
|
||||
( "gplx.Err_.new_wo_type(Err_.java:1)" // ignore this line
|
||||
, "gplx.String_.Len(String_.java:2)"
|
||||
), String_.Concat_lines_nl_skip_last
|
||||
( ""
|
||||
, " gplx.String_.Len(String_.java:2)"
|
||||
));
|
||||
}
|
||||
@Test public void Trace_to_str__gplx_ignore() {
|
||||
fxt.Test_Trace_to_str(Bool_.Y, Bool_.N, 1, String_.Concat_lines_nl_skip_last
|
||||
( "gplx.Err_.new_wo_type(Err_.java:1)" // ignore this line
|
||||
, "gplx.String_.Fail(String_.java:2)" // ignore this line also
|
||||
, "gplx.String_.Len(String_.java:3)"
|
||||
), String_.Concat_lines_nl_skip_last
|
||||
( ""
|
||||
, " gplx.String_.Len(String_.java:3)"
|
||||
));
|
||||
}
|
||||
}
|
||||
class Err_fxt {
|
||||
public void Test_Trace_to_str(boolean is_gplx, boolean called_by_log, int ignore_lines, String trace, String expd) {
|
||||
String actl = Err.Trace_to_str(is_gplx, called_by_log, ignore_lines, trace);
|
||||
Tfds.Eq_str_lines(expd, actl);
|
||||
}
|
||||
}
|
@ -1,70 +0,0 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx;
|
||||
public class Exc extends RuntimeException {
|
||||
private final String stack;
|
||||
private Exc_msg[] msgs_ary = new Exc_msg[8]; private int msgs_len = 8, msgs_idx = 0;
|
||||
public Exc(String stack, String type, String msg, Object... args) {
|
||||
Msgs_add(type, msg, args);
|
||||
this.stack = stack;
|
||||
}
|
||||
public int Stack_erase() {return stack_erase;} public Exc Stack_erase_1_() {stack_erase = 1; return this;} private int stack_erase = 0;
|
||||
public Exc Args_add(Object... args) {msgs_ary[msgs_idx - 1].Args_add(args); return this;} // i - 1 to get current
|
||||
@gplx.Internal protected boolean Type_match(String type) {
|
||||
for (int i = 0; i < msgs_len; ++i) {
|
||||
if (String_.Eq(type, msgs_ary[i].Type())) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@gplx.Internal protected void Msgs_add(String type, String msg, Object[] args) {
|
||||
if (msgs_idx == msgs_len) {
|
||||
int new_len = msgs_len * 2;
|
||||
Exc_msg[] new_ary = new Exc_msg[new_len];
|
||||
Array_.CopyTo(msgs_ary, new_ary, 0);
|
||||
this.msgs_ary = new_ary;
|
||||
this.msgs_len = new_len;
|
||||
}
|
||||
msgs_ary[msgs_idx] = new Exc_msg(type, msg, args);
|
||||
++msgs_idx;
|
||||
}
|
||||
public String To_str_all() {
|
||||
String rv = "";
|
||||
for (int i = msgs_idx - 1; i > -1; --i) {
|
||||
rv += "[err " + Int_.Xto_str(i) + "] " + msgs_ary[i].To_str() + "\n";
|
||||
}
|
||||
rv += "[stack]\n " + Stack_to_str(this, stack);
|
||||
return rv;
|
||||
}
|
||||
@Override public String getMessage() {return To_str_all();}
|
||||
private static String Stack_to_str(Exception e, String stack) {
|
||||
String rv = stack;
|
||||
if (rv == Exc_.Stack_null) { // occurs for thrown gplx exceptions; EX: throw Exc_.new_unimplemented
|
||||
rv = ""; // set to "" b/c String concat below;
|
||||
String[] lines = String_.Split_lang(Exc_.Stack_lang(e), '\n');
|
||||
int len = lines.length;
|
||||
for (int i = 0; i < len; ++i) {
|
||||
String line = lines[i];
|
||||
if (String_.Has_at_bgn(line, "gplx.Exc_.new")) continue; // ignore stack frames with "gplx.Exc_.new"; EX: throw Exc_.new_unimplemented
|
||||
if (String_.Len(rv) > 0) rv += "\n";
|
||||
rv += line;
|
||||
}
|
||||
}
|
||||
rv = String_.Replace(rv, "\n", "\n "); // " " is to indent stack stack
|
||||
return rv;
|
||||
}
|
||||
}
|
@ -1,67 +0,0 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx;
|
||||
public class Exc_ {
|
||||
private static String Type__none = ""; @gplx.Internal protected static String Stack_null = null;
|
||||
public static final String Type__op_canceled = "gplx.op_canceled";
|
||||
public static void Noop(Exception e) {}
|
||||
public static Exc as_(Object obj) {return obj instanceof Exc ? (Exc)obj : null;}
|
||||
public static Exc new_(String msg, Object... args) {return new Exc(Stack_null, Type__none, msg, args);}
|
||||
public static Exc new_w_type(String type, String msg, Object... args) {return new Exc(Stack_null, type, msg, args);}
|
||||
public static Exc new_exc(Exception e, String type, String msg, Object... args) {
|
||||
Exc rv = ClassAdp_.Eq_typeSafe(e, Exc.class) ? (Exc)e : new Exc(Exc_.Stack_lang(e), Type__none, Exc_.Message_lang(e));
|
||||
rv.Msgs_add(type, msg, args);
|
||||
return rv;
|
||||
}
|
||||
public static Exc new_unhandled(Object val) {return new Exc(Stack_null, Type__none, "val is not in switch/if", "val", val);}
|
||||
public static Exc new_unimplemented() {return new Exc(Stack_null, Type__none, "method not implemented");}
|
||||
public static Exc new_unimplemented_w_msg(String msg, Object... args) {return new Exc(Stack_null, Type__none, msg, args);}
|
||||
public static Exc new_deprecated(String s) {return new Exc(Stack_null, Type__none, "deprecated", "method", s);}
|
||||
public static Exc new_parse_type(Class<?> c, String raw) {return new_parse(ClassAdp_.FullNameOf_type(c), raw);}
|
||||
public static Exc new_parse_exc(Exception e, Class<?> c, String raw) {return new_parse(ClassAdp_.FullNameOf_type(c), raw).Args_add("e", Err_.Message_lang(e));}
|
||||
public static Exc new_parse(String type, String raw) {return new Exc(Stack_null, Type__none, "parse failed", "type", type, "raw", raw);}
|
||||
public static Exc new_null(String s) {return new Exc(Stack_null, Type__none, "null obj", "obj", s);}
|
||||
public static Exc new_missing_idx(int idx, int len) {return new Exc(Stack_null, Type__none, "index is out of bounds", "idx", idx, "len", len);}
|
||||
public static Exc new_missing_key(String key) {return new Exc(Stack_null, Type__none, "key not found", "key", key);}
|
||||
public static Exc new_invalid_op(String msg) {return new Exc(Stack_null, Type__none, msg);}
|
||||
public static Exc new_op_canceled() {return new Exc(Stack_null, Type__op_canceled, "canceled by usr");}
|
||||
public static Exc new_type_mismatch_w_exc(Exception ignore, Class<?> t, Object o) {return new_type_mismatch(t, o);}
|
||||
public static Exc new_type_mismatch(Class<?> t, Object o) {return new Exc(Stack_null, Type__none, "type mismatch", "expdType", ClassAdp_.FullNameOf_type(t), "actlType", ClassAdp_.NameOf_obj(o), "actlObj", Object_.Xto_str_strict_or_null_mark(o));}
|
||||
public static Exc new_cast(Exception ignore, Class<?> t, Object o) {
|
||||
String o_str = "";
|
||||
try {o_str = Object_.Xto_str_strict_or_null_mark(o);}
|
||||
catch (Exception e) {o_str = "<ERROR>"; Exc_.Noop(e);}
|
||||
return new Exc(Stack_null, Type__none, "cast failed", "type", ClassAdp_.NameOf_type(t), "obj", o_str);
|
||||
}
|
||||
|
||||
public static String Message_lang(Exception e) {return e.getMessage();}
|
||||
public static String Stack_lang(Exception e) {
|
||||
String rv = "";
|
||||
StackTraceElement[] ary = e.getStackTrace();
|
||||
int len = ary.length;
|
||||
for (int i = 0; i < len; i++) {
|
||||
if (i != 0) rv += "\n";
|
||||
rv += ary[i].toString();
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public static boolean Type_match(Exception e, String type) {
|
||||
Exc exc = Exc_.as_(e);
|
||||
return exc == null ? false : exc.Type_match(type);
|
||||
}
|
||||
}
|
@ -0,0 +1,66 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.core.consoles; import gplx.*; import gplx.core.*;
|
||||
public class Console_adp__sys implements Console_adp {
|
||||
private String tmp_txt;
|
||||
public Console_adp__sys() {
|
||||
this.backspace_by_bytes = Op_sys.Cur().Tid_is_lnx(); // bash shows UTF8 by default; backspace in bytes, else multi-byte characters don't show; DATE:2014-03-04
|
||||
}
|
||||
public boolean Enabled() {return true;}
|
||||
public boolean Canceled() {return canceled;} public void Canceled_set(boolean v) {canceled = v;} private boolean canceled = false;
|
||||
public boolean Canceled_chk() {if (canceled) throw Err_.new_op_canceled(); return canceled;}
|
||||
public int Chars_per_line_max() {return chars_per_line_max;} public void Chars_per_line_max_(int v) {chars_per_line_max = v;} int chars_per_line_max = 80;
|
||||
public boolean Backspace_by_bytes() {return backspace_by_bytes;} public Console_adp__sys Backspace_by_bytes_(boolean v) {backspace_by_bytes = v; return this;} private boolean backspace_by_bytes;
|
||||
public void Write_str(String s) {Clear_tmp(); Write_str_lang(s);}
|
||||
public void Write_str_w_nl(String s) {Clear_tmp(); Write_str_w_nl_lang(s);}
|
||||
public void Write_fmt_w_nl(String fmt, Object... args) {Clear_tmp(); Write_str_w_nl_lang(String_.Format(fmt, args));}
|
||||
public char Read_key(String s) {Write_str(s); return Read_key_lang();}
|
||||
public String Read_line(String s) {Write_str(s); return Read_line_lang();}
|
||||
public void Write_tmp(String s) {
|
||||
Clear_tmp();
|
||||
if (String_.Has(s, "\r")) s = String_.Replace(s, "\r", " ");
|
||||
if (String_.Has(s, "\n")) s = String_.Replace(s, "\n", " ");
|
||||
if (String_.Len(s) >= chars_per_line_max) s = String_.Mid(s, 0, chars_per_line_max - String_.Len("...") - 1) + "..."; // NOTE: >= and -1 needed b/c line needs to be 1 less than max; ex: default cmd is 80 width, but writing 80 chars will automatically create lineBreak
|
||||
tmp_txt = s;
|
||||
Write_str_lang(s);
|
||||
}
|
||||
private void Clear_tmp() {
|
||||
if (tmp_txt == null) return;
|
||||
if (Env_.Mode_debug()) {Write_str_lang(String_.CrLf); return;}
|
||||
int count = backspace_by_bytes ? Bry_.new_u8(tmp_txt).length : String_.Len(tmp_txt);
|
||||
String moveBack = String_.Repeat("\b", count);
|
||||
this.Write_str_lang(moveBack); // move cursor back to beginning of line
|
||||
this.Write_str_lang(String_.Repeat(" ", count)); // overwrite tmp_txt with space
|
||||
this.Write_str_lang(moveBack); // move cursor back to beginning of line (so next Write will start at beginning)
|
||||
tmp_txt = null;
|
||||
}
|
||||
private void Write_str_lang(String s) {System.out.print(s);}
|
||||
private void Write_str_w_nl_lang(String s) {System.out.println(s);}
|
||||
private String Read_line_lang() {return System.console() == null ? "" : System.console().readLine();}
|
||||
private char Read_key_lang() {
|
||||
String text = Read_line_lang();
|
||||
return String_.Len(text) == 0 ? '\0' : String_.CharAt(text, 0);
|
||||
}
|
||||
public void Write_str_w_nl_utf8(String s) {
|
||||
java.io.PrintStream ps;
|
||||
try {ps = new java.io.PrintStream(System.out, true, "UTF-8");}
|
||||
catch (java.io.UnsupportedEncodingException e) {throw Err_.new_wo_type("unsupported exception");}
|
||||
ps.println(s);
|
||||
}
|
||||
public static final Console_adp__sys I = new Console_adp__sys();
|
||||
}
|
@ -1,63 +0,0 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx;
|
||||
public class Err extends RuntimeException {
|
||||
@Override public String getMessage() {return Message_gplx();}
|
||||
public String Key() {return key;} public Err Key_(String v) {key = v; return this;} private String key = "";
|
||||
public String Hdr() {return hdr;} public Err Hdr_(String v) {hdr = v; return this;} private String hdr = "";
|
||||
public List_adp Args() {return args;} List_adp args = List_adp_.new_();
|
||||
public Err Add(String k, Object o) {args.Add(KeyVal_.new_(k, o)); return this;}
|
||||
@gplx.Internal protected ErrProcData Proc() {return proc;} ErrProcData proc = ErrProcData.Null;
|
||||
public Ordered_hash CallStack() {return callStack;} Ordered_hash callStack = Ordered_hash_.new_();
|
||||
public int CallLevel() {return callLevel;} public Err CallLevel_(int val) {callLevel = val; return this;} public Err CallLevel_1_() {return CallLevel_(1);} int callLevel;
|
||||
public Err Inner() {return inner;} Err inner;
|
||||
@gplx.Internal protected static Err hdr_(String hdr) {
|
||||
Err rv = new Err();
|
||||
rv.hdr = hdr;
|
||||
return rv;
|
||||
} @gplx.Internal protected Err() {}
|
||||
@gplx.Internal protected static Err exc_(Exception thrown, String hdr) {
|
||||
Err rv = hdr_(hdr + ":" + Err_.Message_lang(thrown)); // add a better error description; DATE:2014-08-15
|
||||
rv.inner = convert_(thrown);
|
||||
for (int i = 0; i < rv.inner.callStack.Count(); i++) {
|
||||
ErrProcData itm = (ErrProcData)rv.inner.callStack.Get_at(i);
|
||||
rv.callStack.Add(itm.Raw(), itm);
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
@gplx.Internal protected static Err convert_(Exception thrown) {
|
||||
Err rv = Err_.as_(thrown);
|
||||
if (rv == null)
|
||||
rv = Err_.new_key_1(ClassAdp_.NameOf_obj(thrown), Err_.Message_lang(thrown));
|
||||
CallStack_fill(rv, Err_.StackTrace_lang(rv));
|
||||
return rv;
|
||||
}
|
||||
static void CallStack_fill(Err err, String stackTrace) {
|
||||
ErrProcData[] ary = ErrProcData.parse_ary_(stackTrace); if (Array_.Len(ary) == 0) return; // no callStack; shouldn't happen, but don't throw error
|
||||
err.proc = ary[0];
|
||||
for (ErrProcData itm : ary) {
|
||||
String key = itm.Raw();
|
||||
if (err.callStack.Has(key)) continue;
|
||||
err.callStack.Add(key, itm);
|
||||
}
|
||||
}
|
||||
String Message_gplx() {
|
||||
try {return Err_.Message_gplx(this);}
|
||||
catch (Exception exc) {Exc_.Noop(exc); return super.getMessage();}
|
||||
}
|
||||
}
|
@ -1,112 +0,0 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx;
|
||||
import gplx.core.strings.*;
|
||||
public class ErrMsgWtr {
|
||||
public String Message_gplx(Exception thrown) {
|
||||
Err err = Err.convert_(thrown); // convert thrown to Err to make rest of class easier
|
||||
Err[] innerAry = InnerAsAry(err);
|
||||
String_bldr sb = String_bldr_.new_();
|
||||
WriteError(innerAry, sb, 0);
|
||||
WriteInner(innerAry, sb);
|
||||
WriteStack(innerAry, sb);
|
||||
return sb.XtoStr();
|
||||
}
|
||||
public String Message_gplx_brief(Exception thrown) {
|
||||
Err err = Err.convert_(thrown); // convert thrown to Err to make rest of proc easier
|
||||
String_bldr sb = String_bldr_.new_();
|
||||
sb.Add(err.Hdr());
|
||||
if (err.Args().Count() > 0) sb.Add(" --");
|
||||
for (Object kvo : err.Args()) {
|
||||
KeyVal kv = (KeyVal)kvo;
|
||||
String key = kv.Key(), val = kv.Val_to_str_or_empty();
|
||||
sb.Add_fmt(" {0}='{1}'", key, val);
|
||||
}
|
||||
sb.Add_fmt(" [{0}]", err.Key());
|
||||
return sb.XtoStr();
|
||||
}
|
||||
void WriteInner(Err[] errAry, String_bldr sb) {
|
||||
int len = Array_.Len(errAry); if (len <= 1) return; // no inners; return;
|
||||
for (int i = 1; i < len; i++)
|
||||
WriteError(errAry, sb, i);
|
||||
}
|
||||
void WriteError(Err[] errAry, String_bldr sb, int i) {
|
||||
Err err = errAry[i];
|
||||
String msg = err.Hdr();
|
||||
String typ = String_.Eq(err.Key(), "") ? "" : String_.Concat(" <", err.Key(), ">");
|
||||
boolean onlyOne = errAry.length == 1;
|
||||
String idxStr = onlyOne ? "" : Int_.Xto_str(i);
|
||||
sb.Add(idxStr).Add("\t").Add(msg).Add(typ).Add_char_crlf(); // ex: " @count must be > 0 <gplx.arg>"
|
||||
WriteKeyValAry(sb, err.Args());
|
||||
sb.Add("\t").Add(err.Proc().SignatureRaw()).Add_char_crlf();
|
||||
// WriteKeyValAry(sb, err.ProcArgs());
|
||||
}
|
||||
void WriteKeyValAry(String_bldr sb, List_adp ary) {
|
||||
// calc keyMax for valIndentLen
|
||||
int keyMax = 0;
|
||||
for (Object o : ary) {
|
||||
KeyVal kv = (KeyVal)o;
|
||||
int keyLen = String_.Len(kv.Key());
|
||||
if (keyLen > keyMax) keyMax = keyLen + 1; // +1 to guarantee one space between key and val
|
||||
}
|
||||
if (keyMax < 8)keyMax = 8; // separate by at least 8 chars
|
||||
for (Object o : ary) {
|
||||
KeyVal kv = (KeyVal)o;
|
||||
String key = kv.Key(); int keyLen = String_.Len(key);
|
||||
String valIndent = String_.Repeat(" ", keyMax - keyLen);
|
||||
sb.Add("\t\t@").Add(key).Add(valIndent).Add(kv.Val_to_str_or_empty()).Add_char_crlf();
|
||||
}
|
||||
}
|
||||
void WriteStack(Err[] errAry, String_bldr sb) {
|
||||
if (Env_.Mode_testing()) return; // only write stack when not testing
|
||||
int len = Array_.Len(errAry); if (len == 0) return; // shouldn't happen, but don't want to throw err
|
||||
Err first = errAry[0];
|
||||
boolean onlyOne = len == 1;
|
||||
sb.Add_str_w_crlf(String_.Repeat("-", 80));
|
||||
List_adp tmp = List_adp_.new_();
|
||||
Ordered_hash callStack = first.CallStack(); int callStackCount = callStack.Count();
|
||||
for (int i = 0; i < callStackCount ; i++) {
|
||||
ErrProcData proc = (ErrProcData)callStack.Get_at(i);
|
||||
// get procIndex
|
||||
int idx = -1;
|
||||
for (int j = 0; j < len; j++) {
|
||||
ErrProcData comp = errAry[j].Proc();
|
||||
if (String_.Eq(proc.Raw(), comp.Raw())) {idx = j; break;}
|
||||
}
|
||||
String idxStr = onlyOne ? "" : Int_.Xto_str(idx);
|
||||
String hdr = idx == -1 ? "\t" : idxStr + "\t";
|
||||
String ideAddressSpr = String_.CrLf + "\t\t";
|
||||
String ideAddress = String_.Eq(proc.IdeAddress(), "") ? "" : ideAddressSpr + proc.IdeAddress(); // NOTE: ideAddress will be blank in compiled mode
|
||||
String msg = String_.Concat(hdr, proc.SignatureRaw(), ideAddress);
|
||||
tmp.Add(msg);
|
||||
}
|
||||
tmp.Reverse();
|
||||
for (Object o : tmp)
|
||||
sb.Add_str_w_crlf((String)o);
|
||||
}
|
||||
static Err[] InnerAsAry(Err err) {
|
||||
List_adp errAry = List_adp_.new_();
|
||||
Err cur = Err_.as_(err);
|
||||
while (cur != null) {
|
||||
errAry.Add(cur);
|
||||
cur = cur.Inner();
|
||||
}
|
||||
return (Err[])errAry.To_ary(Err.class);
|
||||
}
|
||||
public static final ErrMsgWtr _ = new ErrMsgWtr(); ErrMsgWtr() {}
|
||||
}
|
@ -1,67 +0,0 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx;
|
||||
class ErrProcData {
|
||||
public String Raw() {return raw;} public ErrProcData Raw_(String val) {raw = val; return this;} private String raw = String_.Empty;
|
||||
public String SignatureRaw() {return signatureRaw;} public ErrProcData SignatureRaw_(String val) {signatureRaw = val; return this;} private String signatureRaw = String_.Empty;
|
||||
public String SourceFileRaw() {return sourceFileRaw;} public ErrProcData SourceFileRaw_(String val) {sourceFileRaw = val; return this;} private String sourceFileRaw = String_.Empty;
|
||||
public int SourceLine() {return sourceLine;} public ErrProcData SourceLine_(int val) {sourceLine = val; return this;} int sourceLine;
|
||||
public String IdeAddress() {return ideAddress;} public ErrProcData IdeAddress_(String val) {ideAddress = val; return this;} private String ideAddress = String_.Empty;
|
||||
|
||||
public static ErrProcData[] parse_ary_(String stackTrace) {
|
||||
/*
|
||||
.java
|
||||
' at gplx.Err.new_(Err.java:92)
|
||||
at gplx.Err.exc_(Err.java:43)
|
||||
at gplx.Err_.err_(Err_.java:4)
|
||||
at gplx._tst.Err__tst.RdrLoad(Err__tst.java:77)
|
||||
at gplx._tst.Err__tst.MgrInit(Err__tst.java:76)'
|
||||
.cs
|
||||
' at gplx._tst.Err__tst.RdrLoad() in c:\000\200_dev\100.gplx\100.framework\100.core\gplx\tst\gplx\err__tst.cs:line 77
|
||||
at gplx._tst.Err__tst.MgrInit(String s) in c:\000\200_dev\100.gplx\100.framework\100.core\gplx\tst\gplx\err__tst.cs:line 76'
|
||||
*/
|
||||
if (stackTrace == null) return new ErrProcData[0];
|
||||
String[] lines = String_.SplitLines_any(stackTrace);
|
||||
List_adp list = List_adp_.new_();
|
||||
int len = Array_.Len(lines);
|
||||
for (int i = 0; i < len; i++) {
|
||||
ErrProcData md = ErrProcData.parse_(lines[i]);
|
||||
if (md.SourceLine() == 0) break; // ASSUME: java code; not interested
|
||||
if (String_.Has_at_bgn(md.signatureRaw, "gplx.Err_") || String_.Has_at_bgn(md.signatureRaw, "gplx.Err.")) continue; // java includes entire stackTrace from point of creation; only care about point of throw
|
||||
list.Add(md);
|
||||
}
|
||||
return (ErrProcData[])list.To_ary(ErrProcData.class);
|
||||
}
|
||||
public static ErrProcData parse_(String raw) {
|
||||
ErrProcData rv = new ErrProcData().Raw_(raw);
|
||||
// ex:'gplx.Err.new_(Err.java:92)'
|
||||
int sigEnd = String_.FindFwd(raw, "("); if (sigEnd == String_.Find_none) return rv;
|
||||
rv.signatureRaw = String_.Mid(raw, 0, sigEnd);
|
||||
int filBgn = sigEnd + 1; // 1="("
|
||||
int filEnd = String_.FindFwd(raw, ":", filBgn); if (filEnd == String_.Find_none) return rv;
|
||||
rv.sourceFileRaw = String_.Mid(raw, filBgn, filEnd);
|
||||
int linBgn = filEnd + 1; // 1=":"
|
||||
int linEnd = String_.FindFwd(raw, ")", linBgn); if (linEnd == String_.Find_none) return rv;
|
||||
String linRaw = String_.Mid(raw, linBgn, linEnd);
|
||||
rv.sourceLine = Int_.parse_(linRaw);
|
||||
rv.ideAddress = String_.Concat("(", rv.sourceFileRaw, ":", Int_.Xto_str(rv.sourceLine), ")");
|
||||
return rv;
|
||||
}
|
||||
public static ErrProcData new_() {return new ErrProcData();} ErrProcData() {}
|
||||
public static final ErrProcData Null = new ErrProcData();
|
||||
}
|
@ -1,48 +0,0 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx;
|
||||
import org.junit.*;
|
||||
public class ErrProcData_tst {
|
||||
@Test public void parse_() {
|
||||
tst_parse_("gplx._tst.Err__tst.RdrLoad(MethodData_tst.java:1)"
|
||||
, ErrProcData.new_()
|
||||
.SignatureRaw_("gplx._tst.Err__tst.RdrLoad")
|
||||
.SourceFileRaw_("MethodData_tst.java")
|
||||
.SourceLine_(1)
|
||||
.IdeAddress_("(MethodData_tst.java:1)")
|
||||
);
|
||||
}
|
||||
@Test public void parse_ary_() {
|
||||
String stackTrace = "";
|
||||
try {ThrowException();} catch (Exception exc) {stackTrace = Err_.StackTrace_lang(exc);}
|
||||
ErrProcData[] ary = ErrProcData.parse_ary_(stackTrace);
|
||||
Tfds.Eq(2, Array_.Len(ary));
|
||||
Tfds.Eq("gplx.ErrProcData_tst.ThrowException", ary[0].SignatureRaw());
|
||||
Tfds.Eq("gplx.ErrProcData_tst.parse_ary_", ary[1].SignatureRaw());
|
||||
}
|
||||
Exception ThrowException() {
|
||||
throw new RuntimeException("msg");
|
||||
}
|
||||
void tst_parse_(String raw, ErrProcData expd) {
|
||||
ErrProcData actl = ErrProcData.parse_(raw);
|
||||
Tfds.Eq(expd.SignatureRaw(), actl.SignatureRaw());
|
||||
Tfds.Eq(expd.SourceFileRaw(), actl.SourceFileRaw());
|
||||
Tfds.Eq(expd.SourceLine(), actl.SourceLine());
|
||||
Tfds.Eq(expd.IdeAddress(), actl.IdeAddress());
|
||||
}
|
||||
}
|
@ -1,36 +0,0 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx;
|
||||
import gplx.core.strings.*;
|
||||
public class Err_ { //_20110415
|
||||
public static Err as_(Object obj) {return obj instanceof Err ? (Err)obj : null;}
|
||||
@gplx.Internal protected static Err new_key_1(String key, String hdr) {return Err.hdr_(hdr).Key_(key);}
|
||||
public static String Message_lang(Exception e) {return e.getMessage();}
|
||||
public static String Message_gplx(Exception e) {return ErrMsgWtr._.Message_gplx(e);}
|
||||
public static String Message_gplx_brief(Exception e) {return ErrMsgWtr._.Message_gplx_brief(e);}
|
||||
@gplx.Internal protected static String StackTrace_lang(Exception e) {
|
||||
String_bldr sb = String_bldr_.new_();
|
||||
StackTraceElement[] stackTraceAry = e.getStackTrace();
|
||||
int len = stackTraceAry.length;
|
||||
for (int i = 0; i < len; i++) {
|
||||
if (i != 0) sb.Add_char_crlf();
|
||||
sb.Add(stackTraceAry[i].toString());
|
||||
}
|
||||
return sb.XtoStr();
|
||||
}
|
||||
}
|
@ -1,53 +0,0 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx;
|
||||
public class Err_arg extends Err {
|
||||
public String ArgName() {return argName;} private String argName;
|
||||
public Object ArgValue() {return argValue;} Object argValue;
|
||||
public static Err_arg null_(String argName) {
|
||||
Err_arg rv = new Err_arg();
|
||||
rv.Key_("gplx.arg").Hdr_("@" + rv.argName + " cannot be null");
|
||||
rv.argName = argName;
|
||||
return rv;
|
||||
}
|
||||
public static Err_arg cannotBe_(String msg, String argName, Object argValue) {
|
||||
Err_arg rv = new Err_arg();
|
||||
rv.Key_("gplx.arg");
|
||||
rv.Hdr_("val cannot be " + msg);
|
||||
rv.Add("key", argName);
|
||||
rv.Add("val", argValue);
|
||||
return rv;
|
||||
}
|
||||
public static Err_arg notFound_key_(String argName, Object argValue) {
|
||||
Err_arg rv = new Err_arg();
|
||||
rv.Key_("gplx.arg").Hdr_("arg not found").Add(argName, argValue);
|
||||
rv.argName = argName;
|
||||
rv.argValue = argValue;
|
||||
return rv;
|
||||
}
|
||||
public static Err_arg outOfBounds_(String argName, int i, int count) {
|
||||
Err_arg rv = new Err_arg();
|
||||
rv.Key_("gplx.arg").Hdr_("arg out of bounds").Add("argName", argName).Add("argVal", i).Add("count", count);
|
||||
rv.argName = argName;
|
||||
rv.argValue = i;
|
||||
return rv;
|
||||
}
|
||||
public static boolean ClassCheck(Exception e) {
|
||||
return ClassAdp_.Eq_typeSafe(e, Err_arg.class);
|
||||
}
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue