PageRenderTime 973ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src_configDesign/org/hxzon/configdesigner/util/CfgUtil.java

https://gitlab.com/BGCX261/zk-full-demo-git
Java | 76 lines | 64 code | 12 blank | 0 comment | 7 complexity | 5d7652600cbd1e383db875cc91b69b3f MD5 | raw file
  1. package org.hxzon.configdesigner.util;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.util.Collection;
  5. import java.util.HashMap;
  6. import java.util.HashSet;
  7. import java.util.Map;
  8. import org.apache.commons.io.FileUtils;
  9. import org.hxzon.configdesigner.core.CfgInfo;
  10. import org.hxzon.configdesigner.core.CfgParser;
  11. import org.hxzon.configdesigner.core.CfgValue;
  12. import org.slf4j.Logger;
  13. import org.slf4j.LoggerFactory;
  14. import org.zkoss.zk.ui.Executions;
  15. import org.zkoss.zk.ui.WebApp;
  16. import com.alibaba.fastjson.JSON;
  17. public class CfgUtil {
  18. private static final Logger logger = LoggerFactory.getLogger(CfgUtil.class);
  19. private static CfgValue configer;
  20. private static Map<Integer, CfgValue> indexer = new HashMap<Integer, CfgValue>();;
  21. static {
  22. try {
  23. WebApp webapp = Executions.getCurrent().getDesktop().getWebApp();
  24. String xmlStr = FileUtils.readFileToString(new File(webapp.getRealPath("/WEB-INF/configDesigner.xml")), "utf8");
  25. CfgInfo info = CfgParser.parseSchema(xmlStr);
  26. String jsonStr = FileUtils.readFileToString(new File(webapp.getRealPath("/WEB-INF/config.json")), "utf8");
  27. Object json = JSON.parse(jsonStr);
  28. configer = CfgParser.buildCfgValue(info, json, 1000, 1);
  29. indexCfg(configer);
  30. } catch (IOException e) {
  31. logger.error(e.getMessage(), e);
  32. throw new RuntimeException(e);
  33. }
  34. }
  35. public static CfgValue getValue() {
  36. return configer;
  37. }
  38. public static void indexCfg(CfgValue cfg) {
  39. indexer.put(cfg.indexCode(), cfg);
  40. if (cfg.getCfgInfo().getType().isCombo()) {
  41. for (CfgValue childCfg : cfg.getValues()) {
  42. indexCfg(childCfg);
  43. }
  44. }
  45. }
  46. public static CfgValue findCfg(int indexCode) {
  47. if (indexCode == 0) {
  48. return configer;
  49. }
  50. return indexer.get(indexCode);
  51. }
  52. public static Collection<CfgValue> cfgs() {
  53. return indexer.values();
  54. }
  55. public static Collection<CfgValue> findCfg(CfgInfo cfgInfo) {
  56. Collection<CfgValue> r = new HashSet<CfgValue>();
  57. for (CfgValue cfg : cfgs()) {
  58. if (cfg.getCfgInfo() == cfgInfo) {
  59. r.add(cfg);
  60. }
  61. }
  62. return r;
  63. }
  64. }