956005ec972b4bd0c4ddac5037bf6ad22da5c63d.svn-base 15 KB

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