3bc726a7c3d964f6fbb3219a6a5ad6cada3cd36b.svn-base 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. package com.sysmodel.configLoader;
  2. import java.io.File;
  3. import java.util.ArrayList;
  4. import javax.xml.parsers.DocumentBuilder;
  5. import javax.xml.parsers.DocumentBuilderFactory;
  6. import org.apache.log4j.Logger;
  7. import org.w3c.dom.Document;
  8. import org.w3c.dom.Element;
  9. import org.w3c.dom.NodeList;
  10. import com.sysmodel.XFormModelImpl;
  11. import com.sysmodel.collectmodel.xmlmodel.able.PageBody;
  12. import com.sysmodel.collectmodel.xmlmodel.able.PageHead;
  13. import com.sysmodel.collectmodel.xmlmodel.able.PageRoot;
  14. import com.sysmodel.collectmodel.xmlmodel.impl.ActionImpl;
  15. import com.sysmodel.collectmodel.xmlmodel.impl.ContainerImpl;
  16. import com.sysmodel.collectmodel.xmlmodel.impl.FormCollectionImpl;
  17. import com.sysmodel.collectmodel.xmlmodel.impl.HiddenFieldImpl;
  18. import com.sysmodel.collectmodel.xmlmodel.impl.PageBodyImpl;
  19. import com.sysmodel.collectmodel.xmlmodel.impl.PageHeadImpl;
  20. import com.sysmodel.collectmodel.xmlmodel.impl.PageRootImpl;
  21. import com.sysmodel.collectmodel.xmlmodel.impl.ReportFieldImpl;
  22. import com.sysmodel.collectmodel.xmlmodel.impl.TabItemImpl;
  23. import com.sysmodel.xformmodel.impl.ValidateImpl;
  24. import com.sysmodel.xformmodel.importcontrol.AbstractControl;
  25. import com.sysmodel.xformmodel.importcontrol.CheckBoxControl;
  26. import com.sysmodel.xformmodel.importcontrol.DateControl;
  27. import com.sysmodel.xformmodel.importcontrol.RadioControl;
  28. import com.sysmodel.xformmodel.importcontrol.SelectControl;
  29. import com.sysmodel.xformmodel.importcontrol.TextAreaControl;
  30. import com.sysmodel.xformmodel.importcontrol.TextControl;
  31. import com.sysmodel.xformmodel.importcontrol.TreeControl;
  32. public class CollectXMLLoader{
  33. private final static Logger log = Logger.getLogger(CollectXMLLoader.class);
  34. private String templistid = "";
  35. public boolean readConfigFiles(File[] files) {
  36. boolean r = true;
  37. for (File f : files) {
  38. r = readConfigFile(f);
  39. if (!r)
  40. break;
  41. }
  42. return r;
  43. }
  44. public boolean readConfigFile(File file) {
  45. DocumentBuilderFactory domfactory = DocumentBuilderFactory.newInstance();
  46. try {
  47. DocumentBuilder builder = domfactory.newDocumentBuilder();
  48. Document document = builder.parse(file);
  49. // read FormCollection
  50. NodeList FormCollectionList = document.getElementsByTagName("FormCollection");
  51. for (int i = 0; i < FormCollectionList.getLength(); i++) {
  52. Element element = (Element) FormCollectionList.item(i);
  53. FormCollectionImpl form = new FormCollectionImpl();
  54. form.setPageid(element.getAttribute("pageid"));
  55. form.setClassid(element.getAttribute("classid"));
  56. form.setAction(element.getAttribute("action"));
  57. form.setMultipart(Boolean.valueOf(element.getAttribute("multipart")).booleanValue());
  58. form.setDescription(element.getAttribute("description"));
  59. form.setType(element.getAttribute("type"));
  60. // read head
  61. form.setPageHead(readPageHead(element));
  62. // read body
  63. form.setPageBody(readPageBody(element));
  64. // read root
  65. form.setPageRoot(readPageRoot(element));
  66. // retMap.put(form.getPageid(),form);
  67. XFormModelImpl.getInstance().addFormCollection(form.getPageid(), form);
  68. }
  69. } catch (Exception e) {
  70. log.error("load pageCollection error: ", e);
  71. } finally {
  72. domfactory = null;
  73. }
  74. return true;
  75. }
  76. public boolean readConfigFile(String filePath) {
  77. File xmlfile = new File(filePath);
  78. if (!xmlfile.exists()) {
  79. log.error("file: " + filePath + " not exist");
  80. return false;
  81. }
  82. DocumentBuilderFactory domfactory = DocumentBuilderFactory.newInstance();
  83. try {
  84. DocumentBuilder builder = domfactory.newDocumentBuilder();
  85. Document document = builder.parse(xmlfile);
  86. // read FormCollection
  87. NodeList FormCollectionList = document.getElementsByTagName("FormCollection");
  88. for (int i = 0; i < FormCollectionList.getLength(); i++) {
  89. Element element = (Element) FormCollectionList.item(i);
  90. FormCollectionImpl form = new FormCollectionImpl();
  91. form.setPageid(element.getAttribute("pageid"));
  92. // templistid = element.getAttribute("pageid");
  93. form.setClassid(element.getAttribute("classid"));
  94. form.setAction(element.getAttribute("action"));
  95. form.setMultipart(Boolean.valueOf(element.getAttribute("multipart")).booleanValue());
  96. form.setDescription(element.getAttribute("description"));
  97. form.setType(element.getAttribute("type"));
  98. // read head
  99. form.setPageHead(readPageHead(element));
  100. // read body
  101. form.setPageBody(readPageBody(element));
  102. // read root
  103. form.setPageRoot(readPageRoot(element));
  104. // retMap.put(form.getPageid(),form);
  105. XFormModelImpl.getInstance().addFormCollection(form.getPageid(), form);
  106. }
  107. } catch (Exception e) {
  108. log.error("load pageCollection error: ", e);
  109. }
  110. return true;
  111. }
  112. /**
  113. * @return PageHead
  114. */
  115. private PageHead readPageHead(Element element) {
  116. NodeList headList = element.getElementsByTagName("pageHead");
  117. if (headList.getLength() == 1) {
  118. Element headElement = (Element) headList.item(0);
  119. PageHeadImpl head = new PageHeadImpl();
  120. head.setPageTitle(headElement.getAttribute("pageTitle") + templistid);
  121. head.setWidth(headElement.getAttribute("width"));
  122. head.setBackURL(headElement.getAttribute("backURL"));
  123. head.setHeight(headElement.getAttribute("height"));
  124. return head;
  125. }
  126. return null;
  127. }
  128. /**
  129. * @return PageRoot
  130. */
  131. private PageRoot readPageRoot(Element element) {
  132. NodeList rootList = element.getElementsByTagName("pageRoot");
  133. if (rootList.getLength() == 1) {
  134. Element rootElement = (Element) rootList.item(0);
  135. PageRootImpl root = new PageRootImpl();
  136. root.setWidth(rootElement.getAttribute("width"));
  137. root.setImage(Boolean.valueOf(rootElement.getAttribute("image")).booleanValue());
  138. root.setAlign(rootElement.getAttribute("align"));
  139. root.setHeight(rootElement.getAttribute("height"));
  140. // read action
  141. NodeList actionList = rootElement.getElementsByTagName("action");
  142. for (int i = 0; i < actionList.getLength(); i++) {
  143. Element elementAction = (Element) actionList.item(i);
  144. ActionImpl actionTemp = new ActionImpl();
  145. actionTemp.setType(elementAction.getAttribute("type"));
  146. actionTemp.setReturnURL(elementAction.getAttribute("successURL"));
  147. actionTemp.setValue(elementAction.getAttribute("value"));
  148. actionTemp.setMethod(elementAction.getAttribute("method"));
  149. String space = elementAction.getAttribute("appendSpace");
  150. if (!space.equals(""))
  151. actionTemp.setAppendSpace(Integer.parseInt(space));
  152. root.addAction(actionTemp);
  153. }
  154. return root;
  155. }
  156. return null;
  157. }
  158. // 定义全局变量,采集页面的所有字段信息
  159. private ArrayList<ReportFieldImpl> alReportFields = new ArrayList<ReportFieldImpl>();
  160. /**
  161. * @return PageBody
  162. */
  163. private PageBody readPageBody(Element element) {
  164. NodeList bodyList = element.getElementsByTagName("pageBody");
  165. if (bodyList.getLength() == 1) {
  166. Element bodyElement = (Element) bodyList.item(0);
  167. PageBodyImpl body = new PageBodyImpl();
  168. // <pageBody width="100%" height="" labelWidth="" controlWidth=""
  169. // columnCount="2" labelAlign="" controlAlign="">
  170. body.setWidth(bodyElement.getAttribute("width"));
  171. body.setHeight(bodyElement.getAttribute("height"));
  172. body.setLabelWidth(bodyElement.getAttribute("labelWidth"));
  173. body.setControlWidth(bodyElement.getAttribute("controlWidth"));
  174. body.setColumnCount(Integer.parseInt(bodyElement.getAttribute("columnCount")));
  175. body.setLabelAlign(bodyElement.getAttribute("labelAlign"));
  176. body.setControlAlign(bodyElement.getAttribute("controlAlign"));
  177. // read ReportField
  178. // <hiddenField name="INCIDENT_ID" value="" valueMethod="idmake"/>
  179. NodeList fieldHiddenList = bodyElement.getElementsByTagName("hiddenField");
  180. if (fieldHiddenList != null) {
  181. for (int i = 0; i < fieldHiddenList.getLength(); i++) {
  182. Element elementField = (Element) fieldHiddenList.item(i);
  183. HiddenFieldImpl field = new HiddenFieldImpl();
  184. field.setName(elementField.getAttribute("name"));
  185. field.setValue(elementField.getAttribute("value"));
  186. field.setValueMethod(elementField.getAttribute("valueMethod"));
  187. body.setAlHiddenFields(field);
  188. }
  189. }
  190. // 每读取一个采集页面配置,初始化一次
  191. this.alReportFields = new ArrayList<ReportFieldImpl>();
  192. // read container
  193. body.setAlContainers(readPageBodyContainer(bodyElement));
  194. // 设置所有的对应表的录入字段
  195. // 临时性解决方案 09 10 12
  196. body.setAlReportFields(this.alReportFields);
  197. return body;
  198. }
  199. return null;
  200. }
  201. /**
  202. * @param element
  203. * @return
  204. */
  205. private ArrayList<ContainerImpl> readPageBodyContainer(Element bodyElement) {
  206. ArrayList<ContainerImpl> alContainers = new ArrayList<ContainerImpl>();
  207. NodeList containerList = bodyElement.getElementsByTagName("container");
  208. for (int i = 0; i < containerList.getLength(); i++) {
  209. Element elementContainer = (Element) containerList.item(i);
  210. ContainerImpl container = new ContainerImpl();
  211. container.setXtype(elementContainer.getAttribute("xtype"));
  212. container.setTitle(elementContainer.getAttribute("title"));
  213. container.setDefaults(elementContainer.getAttribute("defaults"));
  214. if (!elementContainer.getAttribute("collapsed").equals("")) {
  215. container.setCollapsed(new Boolean(elementContainer.getAttribute("collapsed"))
  216. .booleanValue());
  217. }
  218. if (!elementContainer.getAttribute("autoHeight").equals("")) {
  219. container.setAutoHeight(new Boolean(elementContainer.getAttribute("autoHeight"))
  220. .booleanValue());
  221. }
  222. container.setObjElements(readContainerChildren(elementContainer));
  223. alContainers.add(container);
  224. }
  225. return alContainers;
  226. }
  227. /**
  228. * @param elementContainer
  229. * @return
  230. */
  231. @SuppressWarnings({ "unchecked", "rawtypes" })
  232. private ArrayList readContainerChildren(Element elementContainer) {
  233. ArrayList objElements = new ArrayList();
  234. if (elementContainer.getAttribute("xtype").equals("tabpanel")) {
  235. NodeList tabitemList = elementContainer.getElementsByTagName("tabitem");
  236. for (int i = 0; i < tabitemList.getLength(); i++) {
  237. Element elementTabitem = (Element) tabitemList.item(i);
  238. TabItemImpl tabitem = new TabItemImpl();
  239. tabitem.setXtype(elementTabitem.getAttribute("xtype"));
  240. tabitem.setTitle(elementTabitem.getAttribute("title"));
  241. tabitem.setDefaults(elementTabitem.getAttribute("defaults"));
  242. tabitem.setObjElements(readReportField(elementTabitem));
  243. objElements.add(tabitem);
  244. }
  245. } else {
  246. objElements = readReportField(elementContainer);
  247. }
  248. return objElements;
  249. }
  250. private ArrayList<ReportFieldImpl> readReportField(Element element) {
  251. ArrayList<ReportFieldImpl> alReportFields = new ArrayList<ReportFieldImpl>();
  252. NodeList fieldList = element.getElementsByTagName("reportField");
  253. for (int i = 0; i < fieldList.getLength(); i++) {
  254. Element elementField = (Element) fieldList.item(i);
  255. ReportFieldImpl field = new ReportFieldImpl();
  256. if (!elementField.getAttribute("classid").equals("")) {
  257. field.setClassid(Integer.parseInt(elementField.getAttribute("classid")));
  258. }
  259. field.setName(elementField.getAttribute("name"));
  260. field.setLabel(elementField.getAttribute("label"));
  261. field.setControlType(elementField.getAttribute("controlType"));
  262. field.setColumnWidth(elementField.getAttribute("columnWidth"));
  263. field.setColspan(elementField.getAttribute("colspan"));
  264. // read control
  265. field.setControl(readControlByType(field.getControlType(), elementField));
  266. // read validate
  267. NodeList validateList = elementField.getElementsByTagName("validate");
  268. ValidateImpl validate = new ValidateImpl();
  269. for (int j = 0; j < validateList.getLength(); j++) {
  270. Element elementValidate = (Element) validateList.item(j);
  271. validate = readValidate(validate, elementValidate);
  272. }
  273. field.setValidate(validate);
  274. alReportFields.add(field);
  275. }
  276. // 临时性解决方案 09 10 12
  277. this.alReportFields.addAll(alReportFields);
  278. return alReportFields;
  279. }
  280. /**
  281. * @param controlType
  282. * @param elementField
  283. * @return AbstractControl
  284. */
  285. private AbstractControl readControlByType(String controlType, Element elementField) {
  286. NodeList controlList = elementField.getElementsByTagName(controlType);
  287. if (controlList.getLength() == 1) {
  288. Element controlElement = (Element) controlList.item(0);
  289. if (controlType.equals("tree")) {
  290. // <tree size="" type="" referenceTable="" storeCode=""
  291. // displayName="" parentCode="" customDefine="" condition=""
  292. // order=""/>
  293. TreeControl tree = new TreeControl();
  294. tree.setType(controlElement.getAttribute("type"));
  295. tree.setSize(controlElement.getAttribute("size"));
  296. tree.setReferenceTable(controlElement.getAttribute("referenceTable"));
  297. tree.setStoreCode(controlElement.getAttribute("storeCode"));
  298. tree.setDisplayName(controlElement.getAttribute("displayName"));
  299. tree.setParentCode(controlElement.getAttribute("parentCode"));
  300. tree.setCustomDefine(controlElement.getAttribute("customDefine"));
  301. tree.setCondition(controlElement.getAttribute("condition"));
  302. tree.setOrder(controlElement.getAttribute("order"));
  303. tree.setLeafonly(Boolean.valueOf(controlElement.getAttribute("leafonly"))
  304. .booleanValue());
  305. return tree;
  306. } else if (controlType.equals("text")) {
  307. // <text size="" maxlength="" readonly=""/>
  308. TextControl text = new TextControl();
  309. text.setSize(controlElement.getAttribute("size"));
  310. text.setReadOnly(Boolean.valueOf(controlElement.getAttribute("readonly"))
  311. .booleanValue());
  312. return text;
  313. } else if (controlType.equals("select")) {
  314. // <select constantName=""/>
  315. SelectControl select = new SelectControl();
  316. select.setConstantName(controlElement.getAttribute("constantName"));
  317. select.setType(controlElement.getAttribute("type"));
  318. select.setOnChange(controlElement.getAttribute("onChange"));
  319. select.setIclassid(controlElement.getAttribute("classid"));
  320. select.setSize(controlElement.getAttribute("size"));
  321. if (!controlElement.getAttribute("isMultiple").equals("")) {
  322. select.setMultiple(Boolean.valueOf(controlElement.getAttribute("isMultiple"))
  323. .booleanValue());
  324. }
  325. if (!controlElement.getAttribute("isInitOption").equals("")) {
  326. select.setIsInitOption(Boolean.valueOf(
  327. controlElement.getAttribute("isInitOption")).booleanValue());
  328. }
  329. return select;
  330. } else if (controlType.equals("radio")) {
  331. // <radio constantName="" singleLine=""/>
  332. RadioControl radio = new RadioControl();
  333. radio.setConstantName(controlElement.getAttribute("constantName"));
  334. radio.setSingleLine(Boolean.valueOf(controlElement.getAttribute("singleLine"))
  335. .booleanValue());
  336. return radio;
  337. } else if (controlType.equals("checkbox")) {
  338. // <checkbox constantName="" singleLine=""/>
  339. CheckBoxControl checkbox = new CheckBoxControl();
  340. checkbox.setConstantName(controlElement.getAttribute("constantName"));
  341. checkbox.setSingleLine(Boolean.valueOf(controlElement.getAttribute("singleLine"))
  342. .booleanValue());
  343. return checkbox;
  344. } else if (controlType.equals("textarea")) {
  345. // <textarea cols="" rows="" readonly=""/>
  346. TextAreaControl textarea = new TextAreaControl();
  347. textarea.setHeight(controlElement.getAttribute("height"));
  348. textarea.setWidth(controlElement.getAttribute("width"));
  349. textarea.setReadOnly(Boolean.valueOf(controlElement.getAttribute("readonly"))
  350. .booleanValue());
  351. return textarea;
  352. } else if (controlType.equals("date")) {
  353. // <date size="" format=""/>
  354. DateControl date = new DateControl();
  355. date.setFormat(controlElement.getAttribute("format"));
  356. date.setSize(controlElement.getAttribute("size"));
  357. return date;
  358. }
  359. }
  360. return null;
  361. }
  362. /**
  363. * @return Validate
  364. */
  365. private ValidateImpl readValidate(ValidateImpl val, Element elementValidate) {
  366. String xmlNodeType = elementValidate.getAttribute("type");
  367. if (xmlNodeType.equals("need")) {
  368. val.setNeed(true);
  369. } else if (xmlNodeType.equals("trim")) {
  370. val.setTrim(true);
  371. } else if (xmlNodeType.equals("regx")) {
  372. val.setRegxValue(elementValidate.getAttribute("regxValue"));
  373. } else if (xmlNodeType.equals("range")) {
  374. val.setMaxValue(elementValidate.getAttribute("maxValue"));
  375. val.setMinValue(elementValidate.getAttribute("minValue"));
  376. } else if (xmlNodeType.equals("length")) {
  377. val.setMaxLength(elementValidate.getAttribute("maxLength"));
  378. } else if (xmlNodeType.equals("dojoType")) {
  379. val.setDojoType(elementValidate.getAttribute("typeValue"));
  380. } else if (xmlNodeType.equals("vType")) {
  381. val.setvType(elementValidate.getAttribute("typeValue"));
  382. } else if (xmlNodeType.equals("message")) {
  383. val.setPromptMessage(elementValidate.getAttribute("promptMessage"));
  384. val.setInvalidMessage(elementValidate.getAttribute("invalidMessage"));
  385. }
  386. return val;
  387. }
  388. }