use of app.hongs.HongsException in project HongsCORE by ihongs.
the class Data method getFields.
/**
* 获取字段
* 当表单不在管理区域时
* 会用当前表单覆盖管理表单
* 配置文件不存在则抛出异常 0x1104
* @return
*/
@Override
public Map getFields() {
try {
return super.getFields();
} catch (NullPointerException ex) {
// Nothing todo
}
Map fields, fieldx;
/**
* 字段以 centra/data 的字段为基础
* 但可在 centre/data 重设部分字段
*
* 配置文件不得放在资源包里面
* 此处会校验表单文件是否存在
*/
try {
if (!new File(Core.CONF_PATH + "/" + conf + Cnst.FORM_EXT + ".xml").exists()) {
throw new HongsExpedient(0x1104, "Data form conf '" + conf + "' is not exists").setLocalizedOptions(conf);
}
fields = FormSet.getInstance(conf).getForm(form);
if (!comf.equals(conf)) {
if (!new File(Core.CONF_PATH + "/" + comf + Cnst.FORM_EXT + ".xml").exists()) {
throw new HongsExpedient(0x1104, "Data form conf '" + comf + "' is not exists").setLocalizedOptions(comf);
}
fieldx = FormSet.getInstance(comf).getForm(form);
// 补充上额外的字段设置
fieldx = new HashMap(fieldx);
fieldx.putAll(fields);
fields = fieldx;
}
} catch (HongsException ex) {
throw ex.toExpedient();
}
setFields(fields);
return fields;
}
use of app.hongs.HongsException in project HongsCORE by ihongs.
the class Form method saveDocument.
private void saveDocument(String path, Document docm) throws HongsException {
File file = new File(path);
File fold = file.getParentFile();
if (!fold.exists()) {
fold.mkdirs();
}
TransformerFactory tf = TransformerFactory.newInstance();
try {
Transformer tr = tf.newTransformer();
DOMSource ds = new DOMSource(docm);
StreamResult sr = new StreamResult(new OutputStreamWriter(new FileOutputStream(file), "utf-8"));
tr.setOutputProperty(OutputKeys.ENCODING, "utf-8");
tr.setOutputProperty(OutputKeys.METHOD, "xml");
tr.setOutputProperty(OutputKeys.INDENT, "yes");
tr.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
tr.transform(ds, sr);
} catch (TransformerConfigurationException e) {
throw new HongsException.Common(e);
} catch (IllegalArgumentException e) {
throw new HongsException.Common(e);
} catch (TransformerException e) {
throw new HongsException.Common(e);
} catch (FileNotFoundException e) {
throw new HongsException.Common(e);
} catch (UnsupportedEncodingException e) {
throw new HongsException.Common(e);
}
}
use of app.hongs.HongsException in project HongsCORE by ihongs.
the class Unit method makeDocument.
private Document makeDocument() throws HongsException {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.newDocument();
} catch (ParserConfigurationException e) {
throw new HongsException.Common(e);
}
}
use of app.hongs.HongsException in project HongsCORE by ihongs.
the class VerifyHelper method addRulesByForm.
public VerifyHelper addRulesByForm(String conf, String form, Map map) throws HongsException {
FormSet def = FormSet.getInstance("default");
Map tps = def.getEnum("__types__");
Map pts = def.getEnum("__patts__");
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry et = (Map.Entry) it.next();
String code = (String) et.getKey();
Map optz = (Map) et.getValue();
Map opts = new HashMap(optz);
Object o;
opts.put("__conf__", conf);
opts.put("__form__", form);
opts.put("__name__", code);
o = opts.get("defiant");
if (o != null) {
Rule rule = new Defiant();
rule.setParams(opts);
this.addRule(code, rule);
}
o = opts.get("default");
if (o != null) {
Rule rule = new Default();
rule.setParams(opts);
this.addRule(code, rule);
}
o = opts.remove("__required__");
if (!"".equals(o)) {
if (Synt.declare(o, false)) {
Rule rule = new Required();
rule.setParams(opts);
this.addRule(code, rule);
} else {
Rule rule = new Optional();
rule.setParams(opts);
this.addRule(code, rule);
}
}
o = opts.remove("__repeated__");
if (!"".equals(o)) {
if (Synt.declare(o, false)) {
Rule rule = new Repeated();
rule.setParams(opts);
this.addRule(code, rule);
} else {
Rule rule = new NoRepeat();
rule.setParams(opts);
this.addRule(code, rule);
}
}
// 可设多个规则, 缺省情况按字符串处理
List<String> list = Synt.toList(opts.get("__rule__"));
if (list == null || list.isEmpty()) {
String type = (String) opts.get("__type__");
String item;
if (tps.containsKey(type)) {
// 类名转换
item = (String) tps.get(type);
String c = item.substring(0, 1);
String n = item.substring(1);
item = "Is" + c.toUpperCase() + n;
} else if (pts.containsKey(type)) {
// 类型正则
opts.put("pattern", type);
item = "IsString";
} else if (!"@".equals(code)) {
item = "IsString";
} else {
item = "Ignore";
}
list = Synt.listOf(item);
}
// 添加规则实例
for (String item : list) {
if (!item.contains(".")) {
item = Rule.class.getPackage().getName() + "." + item;
}
Rule rule;
try {
rule = (Rule) (Class.forName(item).newInstance());
} catch (ClassNotFoundException ex) {
throw new HongsException(0x10f1, "Failed to get rule: " + item + " in " + conf + ":" + form, ex);
} catch (InstantiationException ex) {
throw new HongsException(0x10f1, "Failed to get rule: " + item + " in " + conf + ":" + form, ex);
} catch (IllegalAccessException ex) {
throw new HongsException(0x10f1, "Failed to get rule: " + item + " in " + conf + ":" + form, ex);
} catch (ClassCastException ex) {
throw new HongsException(0x10f1, "Failed to get rule: " + item + " in " + conf + ":" + form, ex);
}
rule.setParams(opts);
addRule(code, rule);
}
}
return this;
}
use of app.hongs.HongsException in project HongsCORE by ihongs.
the class SelectInvoker method invoke.
@Override
public void invoke(ActionHelper helper, ActionRunner chains, Annotation anno) throws HongsException {
Select ann = (Select) anno;
String conf = ann.conf();
String form = ann.form();
byte mode = ann.mode();
if (mode == -1) {
Set ab = Synt.toTerms(helper.getRequestData().get(Cnst.AB_KEY));
if (ab != null) {
if (ab.contains("!enum")) {
mode = 0;
} else {
if (ab.contains(".enum")) {
mode += 1;
}
if (ab.contains("_enum")) {
mode += 2;
}
if (ab.contains("_time")) {
mode += 4;
}
if (ab.contains("_link")) {
mode += 8;
}
if (mode >= 0) {
mode += 1;
}
}
}
}
// 为 0 则不执行, 仅取 enum 数据
Map rsp;
if (mode == 0) {
mode = 1;
rsp = new HashMap();
} else if (mode == -1) {
chains.doAction();
return;
} else {
chains.doAction();
rsp = helper.getResponseData();
if (!Synt.declare(rsp.get("ok"), false)) {
return;
}
}
// 识别路径
if (form.length() == 0) {
form = chains.getEntity();
}
if (conf.length() == 0) {
conf = chains.getModule();
// 照顾 Module Action 的配置规则
if (FormSet.hasConfFile(conf + "/" + form)) {
conf = conf + "/" + form;
}
}
// 填充数据
try {
Map data = (Map) helper.getAttribute("form:" + conf + "." + form);
if (data == null) {
data = FormSet.getInstance(conf).getForm(form);
}
SelectHelper sup;
sup = new SelectHelper().addItemsByForm(conf, data);
sup.select(rsp, mode);
} catch (HongsException ex) {
int ec = ex.getErrno();
if (ec != 0x10e8 && ec != 0x10e9 && ec != 0x10ea) {
throw ex;
}
}
// 返回数据
helper.reply(rsp);
}
Aggregations