use of io.github.ihongs.HongsExemption in project HongsCORE by ihongs.
the class Model method allow.
/**
* 递归提取字段
* @param table 顶层模型库表
* @param assoc 当前关联库表
* @param ac 当前下级关联
* @param pc 当前层级参数
* @param caze 当前查询用例
* @param tn 当前表名
* @param qn 层级名称
* @param pn 相对层级
* @param al 字段集合, 结构: {参数: [字段, 别名, 查询用例]}
*/
private void allow(Table table, Table assoc, Map ac, Map pc, FetchCase caze, String tn, String qn, String pn, Map al) {
String tx, ax, az;
tx = "`" + tn + "`.";
if (null == qn) {
qn = "";
ax = "";
} else if ("".equals(qn)) {
qn = tn;
ax = qn + ".";
} else {
qn = qn + "." + tn;
ax = qn + ".";
}
if (null == pn) {
pn = "";
az = "";
} else if ("".equals(pn)) {
pn = tn;
az = pn + ".";
} else {
pn = pn + "." + tn;
az = pn + ".";
}
if (pc != null && pc.containsKey("fields")) {
al.put(az + "*", new Object[] { null, null, caze });
} else
try {
Map fs = assoc.getFields();
for (Object n : fs.keySet()) {
String f = (String) n;
String k = f;
String l = f;
// 字段完整名
f = tx + "`" + f + "`";
// 字段别名
l = az + /**/
l;
// 外部键
k = ax + /**/
k;
al.put(k, new Object[] { f, l, caze });
}
} catch (HongsException e) {
throw e.toExemption();
}
if (ac == null || ac.isEmpty()) {
return;
}
Iterator it = ac.entrySet().iterator();
while (it.hasNext()) {
Map.Entry et = (Map.Entry) it.next();
Map tc = (Map) et.getValue();
String jn = (String) tc.get("join");
// 不是 JOIN 的重置 pn, 层级名随之改变
if (!"INNER".equals(jn) && !"LEFT".equals(jn) && !"RIGHT".equals(jn) && !"FULL".equals(jn)) {
jn = null;
} else {
jn = pn;
}
tn = (String) et.getKey();
ac = (Map) tc.get("assocs");
pc = (Map) tc.get("params");
// 获取真实的表名, 构建关联表实例
String rn;
rn = (String) tc.get("tableName");
if (rn == null || "".equals(rn)) {
rn = (String) tc.get("name");
}
FetchCase caxe;
try {
assoc = table.db.getTable(rn);
caxe = caze.gotJoin(tn);
} catch (HongsException e) {
throw e.toExemption();
}
if (null == assoc) {
throw new HongsExemption(1026, "Can not get table '" + rn + "' in DB '" + table.db.name + "'");
}
allow(table, assoc, ac, pc, caxe, tn, qn, jn, al);
}
}
use of io.github.ihongs.HongsExemption in project HongsCORE by ihongs.
the class IsDate method verify.
@Override
public Object verify(Value watch) throws Wrong {
// 跳过空值和空串
Object value = watch.get();
if (value == null) {
return STAND;
}
if (value.equals("")) {
return null;
}
String typa = Synt.declare(getParam("__type__"), "");
String type = Synt.declare(getParam("type"), "");
// 日期格式
int off = Synt.declare(getParam("offset"), 0);
String fmt = Synt.declare(getParam("format"), "");
String fwt = fmt;
if ("".equals(fmt)) {
fwt = CoreLocale.getInstance().getProperty("core.default." + typa + ".format");
if (fwt == null) {
throw new HongsExemption("Can not recognize date type '" + typa + "'.");
}
}
Date day = getDate(value, typa, type, fwt, off);
// 区间校验
String min = Synt.declare(getParam("min"), "");
String max = Synt.declare(getParam("max"), "");
long now = new Date().getTime();
if (!"".equals(min)) {
long tim = getTime(min, now);
if (tim > day.getTime()) {
throw new Wrong("fore.form.lt.mindate", new SimpleDateFormat(fwt).format(new Date(tim)));
}
}
if (!"".equals(max)) {
long tim = getTime(max, now);
if (tim < day.getTime()) {
throw new Wrong("fore.form.gt.maxdate", new SimpleDateFormat(fwt).format(new Date(tim)));
}
}
if ("date".equals(type)) {
return day;
}
if ("time".equals(type)) {
return day.getTime();
}
if ("datestamp".equals(type)) {
return day;
}
if ("timestamp".equals(type)) {
return day.getTime() / 1000;
}
if (!"".equals(fmt)) {
Locale loc = Core.getLocality();
return new SimpleDateFormat(fmt, loc).format(day);
}
return value;
}
use of io.github.ihongs.HongsExemption in project HongsCORE by ihongs.
the class IsDate method getTime.
private long getTime(String tim, long now) {
Matcher mat = Pattern.compile("^([+\\-])?(\\d+)$").matcher(tim);
if (!mat.matches()) {
throw new HongsExemption("Can not recognize time '" + tim + "'.");
}
long msc = Synt.declare(mat.group(2), 0L);
String sym = mat.group(1);
if ("+".equals(sym)) {
return now + msc;
} else if ("-".equals(sym)) {
return now - msc;
} else {
return msc;
}
}
use of io.github.ihongs.HongsExemption in project HongsCORE by ihongs.
the class Dict method splitKeys.
/**
* 拆分键
*
* <pre>
* 可以将 a.b[c][.d][]. 解析为 a b c .d null null
* 参考自 Javascript 和 PHP 的语法,
* Javascript 方括号里面用的是变量,
* 而 PHP 对象键并不支持点符号写法.
* </pre>
*
* @param path
* @return
*/
public static Object[] splitKeys(String path) {
if (path == null) {
throw new NullPointerException("`path` can not be null");
}
/**
* 原代码为
* <code>
* path.replaceAll("([^\\.\\]])!", "$1.!")
* .replace("[", ".")
* .replace("]", "" )
* .split("\\.")
* </code>
* 然后遍历将空串换成 null 空值,
* 无法处理 [] 里面有 .! 的情况;
* 采用下面的程序可规避此种问题,
* 且无需正则而仅做一次遍历即可.
* 2018/03/09 已将 '!' 换为 ':'.
* 2019/07/28 已将 ':' 等同 '.'.
*/
List lst = new ArrayList();
int len = path.length();
int beg = 0;
int end = 0;
char pnt;
boolean fkh = false;
while (end < len) {
pnt = path.charAt(end);
switch(pnt) {
case ':':
/*
if (fkh) {
break; // [] 内可以用 :
}
if (beg != end) {
lst.add(path.substring(beg, end));
beg = end;
}
break;*/
case '.':
if (fkh) {
// [] 内可以用 .
break;
}
if (beg != end) {
lst.add(path.substring(beg, end));
} else if (beg == 0) {
lst.add("");
} else if (']' != path.charAt(beg - 1)) {
// 规避 a[b].c. 中的 ].
lst.add(null);
}
beg = end + 1;
break;
case '[':
if (fkh) {
throw new HongsExemption(855, "Syntax error at " + end + " in " + path);
}
if (beg != end) {
lst.add(path.substring(beg, end));
} else if (beg == 0) {
lst.add("");
} else if (']' != path.charAt(beg - 1)) {
// 规避 a[b][c] 中的 ][
throw new HongsExemption(855, "Syntax error at " + end + " in " + path);
}
beg = end + 1;
fkh = true;
break;
case ']':
if (!fkh) {
throw new HongsExemption(855, "Syntax error at " + end + " in " + path);
}
if (beg != end) {
lst.add(path.substring(beg, end));
} else if (beg != 0) {
lst.add(null);
} else
// if ('[' != path.charAt(beg - 1)) { // 这种情况其实并不存在
throw new HongsExemption(855, "Syntax error at " + end + " in " + path);
// }
beg = end + 1;
fkh = false;
break;
}
end++;
}
if (beg == 0 && end == 0) {
lst.add(path);
} else if (beg != len) {
lst.add(path.substring(beg));
} else if ('.' == path.charAt(-1 + len)) {
// 点结尾列表.
lst.add(null);
}
return lst.toArray();
}
use of io.github.ihongs.HongsExemption in project HongsCORE by ihongs.
the class Thumb method setColor.
/**
* 设置背景颜色
* 可以用 #RRGGBB 或 #AARRGGBB 或 R,G,B 或 R,G,B,A 的形式
* @param str
* @return
*/
public Thumb setColor(String str) {
if (str == null) {
col = null;
return this;
}
str = str.trim();
if (str.length() == 0) {
col = null;
return this;
}
try {
if (str.startsWith("#")) {
// 16 进制表示法
str = str.substring(1);
int c = Integer.parseInt(str, 16);
if (str.length() == 6) {
return setColor(new Color(c, false));
} else if (str.length() == 8) {
return setColor(new Color(c, true));
}
} else {
// RGBA 表示方法
String[] x = str.split(",");
int r = Integer.parseInt(x[0].trim());
int g = Integer.parseInt(x[1].trim());
int b = Integer.parseInt(x[2].trim());
int a = (x.length == 3) ? 255 : Integer.parseInt(x[3].trim());
return setColor(new Color(r, g, b, a));
}
throw new HongsExemption("Unable to parse color value: " + str);
} catch (NumberFormatException | IndexOutOfBoundsException e) {
throw new HongsExemption("Unable to parse color value: " + str);
}
}
Aggregations