c84b611c1ed9e8686e7b5483c3e131be24b63ddc.svn-base 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. package com.sysmodel.datamodel.xmlmanager;
  2. import java.io.File;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import javax.xml.parsers.DocumentBuilder;
  6. import javax.xml.parsers.DocumentBuilderFactory;
  7. import org.apache.log4j.Logger;
  8. import org.w3c.dom.Document;
  9. import org.w3c.dom.Element;
  10. import org.w3c.dom.NodeList;
  11. import com.persistence.DBdll.adapter.DB2Adapter;
  12. import com.sysmodel.datamodel.xmlmodel.Reference;
  13. import com.sysmodel.datamodel.xmlmodel.impl.MdpAttributeImpl;
  14. import com.sysmodel.datamodel.xmlmodel.impl.MdpClassImpl;
  15. import com.sysmodel.datamodel.xmlmodel.impl.MdpConstantImpl;
  16. import com.sysmodel.datamodel.xmlmodel.impl.MdpDataSourceImpl;
  17. import com.sysmodel.datamodel.xmlmodel.impl.MdpModuleImpl;
  18. import com.sysmodel.datamodel.xmlmodel.impl.PropertyImpl;
  19. import com.sysmodel.datamodel.xmlmodel.impl.RelationImpl;
  20. import com.sysmodel.datamodel.xmlmodel.impl.SysModelImpl;
  21. import com.sysmodel.datamodel.xmlmodel.impl.TemplateImpl;
  22. import com.sysmodel.xformmodel.impl.ValidateImpl;
  23. /**
  24. * datamodel com.sysmodel.datamodel.xmlmanager load datamodel.xml
  25. *
  26. * 需要修改常量表对象的加载
  27. */
  28. public class XMLLoader{
  29. private final static Logger log = Logger.getLogger(DB2Adapter.class);
  30. public boolean loadFromXML(File[] files) {
  31. return true;
  32. }
  33. public boolean loadFromXML(File file) {
  34. DocumentBuilderFactory domfactory = DocumentBuilderFactory.newInstance();
  35. try {
  36. DocumentBuilder builder = domfactory.newDocumentBuilder();
  37. Document document = builder.parse(file);
  38. /**
  39. * <unit unitcode="zyuc" unitname="门头沟分局"/> read unit
  40. */
  41. NodeList unitList = document.getElementsByTagName("unit");
  42. readUnit(unitList);
  43. /**
  44. * <author>nxs</author> <version>1.0</version> read about
  45. */
  46. readAbout(document);
  47. /**
  48. * 模块 <mdpModules> read mdpModules
  49. */
  50. NodeList mdpModulesList = document.getElementsByTagName("module");
  51. readmdpModules(mdpModulesList);
  52. /**
  53. * 数据源 <mdpDataSources> readDataSource
  54. */
  55. NodeList dataSourceList = document.getElementsByTagName("mdpDataSource");
  56. readDataSource(dataSourceList);
  57. /**
  58. * 模板 <templates> readTemplate
  59. */
  60. NodeList templateList = document.getElementsByTagName("template");
  61. readTemplate(templateList);
  62. /**
  63. * 常量根节点 <mdpConstant> readConstant
  64. */
  65. NodeList constantList = document.getElementsByTagName("mdpConstant");
  66. readConstant(constantList);
  67. /**
  68. * 数据表根节点 <mdpClass> readMdpClass
  69. */
  70. NodeList classList = document.getElementsByTagName("mdpClass");
  71. readMdpClass(classList);
  72. } catch (Exception e) {
  73. log.error("load sysmodel error: ", e);
  74. return false;
  75. }
  76. return true;
  77. }
  78. public boolean loadFromXML(String filePath) {
  79. // ... 文件路径
  80. File xmlfile = new File(filePath);
  81. if (!xmlfile.exists())
  82. return false;
  83. // ...创建装载xml对象
  84. return loadFromXML(xmlfile);
  85. }
  86. private void readUnit(NodeList unit) {
  87. Element element = (Element) unit.item(0);
  88. SysModelImpl.getInstance().setUnitcode(element.getAttribute("unitcode"));
  89. SysModelImpl.getInstance().setUnitname(element.getAttribute("unitname"));
  90. }
  91. // ...<author>nxs</author><version>1.0</version>
  92. private void readAbout(Document document) {
  93. NodeList author = document.getElementsByTagName("author");
  94. if (author.getLength() == 1) {
  95. Element element = (Element) author.item(0);
  96. if (element.getFirstChild() != null)
  97. SysModelImpl.getInstance().setAuthor((element.getFirstChild().getNodeValue()));
  98. }
  99. NodeList version = document.getElementsByTagName("version");
  100. if (version.getLength() == 1) {
  101. Element element = (Element) version.item(0);
  102. if (element.getFirstChild() != null)
  103. SysModelImpl.getInstance().setVersion((element.getFirstChild().getNodeValue()));
  104. }
  105. }
  106. // ...<mdpModules><module code="" name="" description=""/></mdpModules>
  107. private void readmdpModules(NodeList mdpModules) {
  108. ArrayList<MdpModuleImpl> arry = new ArrayList<MdpModuleImpl>();
  109. for (int i = 0; i < mdpModules.getLength(); i++) {
  110. MdpModuleImpl mdpmod = new MdpModuleImpl();
  111. Element element = (Element) mdpModules.item(i);
  112. mdpmod.setPcode(element.getAttribute("pcode"));
  113. mdpmod.setCode(element.getAttribute("code"));
  114. mdpmod.setName(element.getAttribute("name"));
  115. mdpmod.setNgoogleEn(element.getAttribute("ngoogleEn"));
  116. mdpmod.setTPrefix(element.getAttribute("tPrefix"));
  117. mdpmod.setDescription(element.getAttribute("description"));
  118. mdpmod.setPackageName(element.getAttribute("packageName"));
  119. arry.add(mdpmod);
  120. log.debug("读取 " + mdpmod + " 完成!");
  121. }
  122. SysModelImpl.getInstance().setmdpModules(arry);
  123. }
  124. // ...<mdpDataSources><mdpDataSource dataSourceid="" description=""
  125. // dataBase="" url="" user="" password="" jndi=""/></mdpDataSources>
  126. private void readDataSource(NodeList dataSourceList) {
  127. ArrayList<MdpDataSourceImpl> alDataSource = new ArrayList<MdpDataSourceImpl>();
  128. for (int i = 0; i < dataSourceList.getLength(); i++) {
  129. MdpDataSourceImpl datasource = new MdpDataSourceImpl();
  130. Element element = (Element) dataSourceList.item(i);
  131. datasource.setDataSourceid(Integer.parseInt(element.getAttribute("dataSourceid")));
  132. datasource.setDescription(element.getAttribute("description"));
  133. datasource.setDataBase(element.getAttribute("dataBase"));
  134. log.info("数据库类型:" + element.getAttribute("dataBase") + "......");
  135. datasource.setURL(element.getAttribute("url"));
  136. datasource.setUser(element.getAttribute("user"));
  137. datasource.setPassword(element.getAttribute("password"));
  138. datasource.setJNDI(element.getAttribute("jndi"));
  139. alDataSource.add(datasource);
  140. log.debug("读取 " + datasource + " 完成!");
  141. }
  142. SysModelImpl.getInstance().setMdpDataSources(alDataSource);
  143. }
  144. // ...<templates><template name="" description=""></templates>
  145. private void readTemplate(NodeList templateList) {
  146. ArrayList<TemplateImpl> alTemplate = new ArrayList<TemplateImpl>();
  147. for (int i = 0; i < templateList.getLength(); i++) {
  148. TemplateImpl template = new TemplateImpl();
  149. Element element = (Element) templateList.item(i);
  150. template.setName(element.getAttribute("name"));
  151. // 数据存放的表名称.
  152. template.setTableName(element.getAttribute("tableName"));
  153. template.setDataSource(Integer.parseInt(element.getAttribute("dataSource")));
  154. template.setDescription(element.getAttribute("description"));
  155. /**
  156. * <template>的子标签 <property>
  157. */
  158. NodeList propertyList = element.getElementsByTagName("property");
  159. template.setListproperty(readProperty(propertyList));
  160. alTemplate.add(template);
  161. log.debug("读取 " + template + " 完成!");
  162. }
  163. SysModelImpl.getInstance().setTemplates(alTemplate);
  164. }
  165. // ...<mdpConstant codeName="" description="" templateName="" rootFrom=""
  166. // dataSource="" keyCodeAuto="" orderProperty="" orderMethod="" type=""/>
  167. @SuppressWarnings("deprecation")
  168. private void readConstant(NodeList constantList) {
  169. ArrayList<MdpConstantImpl> alConstant = new ArrayList<MdpConstantImpl>();
  170. for (int i = 0; i < constantList.getLength(); i++) {
  171. MdpConstantImpl constant = new MdpConstantImpl();
  172. Element constantElement = (Element) constantList.item(i);
  173. constant.setCodeName(constantElement.getAttribute("codeName"));
  174. constant.setNgoogleEn(constantElement.getAttribute("ngoogleEn"));
  175. constant.setDescription(constantElement.getAttribute("description"));
  176. constant.setTemplateName(constantElement.getAttribute("templateName"));
  177. // 树形常量根节点配置信息
  178. constant.setRootFrom(constantElement.getAttribute("rootFrom"));
  179. constant.setDataSource(Integer.parseInt(constantElement.getAttribute("dataSource")));
  180. constant.setKeyCodeAuto(Boolean.valueOf(constantElement.getAttribute("keyCodeAuto"))
  181. .booleanValue());
  182. constant.setOrderProperty(constantElement.getAttribute("orderProperty"));
  183. constant.setOrderMethod(constantElement.getAttribute("orderMethod"));
  184. constant.setType(constantElement.getAttribute("type"));
  185. alConstant.add(constant);
  186. log.debug("读取 " + constant + " 完成!");
  187. }
  188. SysModelImpl.getInstance().setMdpConstants(alConstant);
  189. }
  190. // ...<property name="" description="" dataType="" length="" scale=""
  191. // referenceParentName="" display=" " unique="">
  192. private List<PropertyImpl> readProperty(NodeList propertyList) {
  193. ArrayList<PropertyImpl> alProperty = new ArrayList<PropertyImpl>();
  194. for (int i = 0; i < propertyList.getLength(); i++) {
  195. PropertyImpl property = new PropertyImpl();
  196. Element propertyElement = (Element) propertyList.item(i);
  197. property.setName(propertyElement.getAttribute("name"));
  198. property.setRefConstant(propertyElement.getAttribute("refConstant"));
  199. property.setDescription(propertyElement.getAttribute("description"));
  200. property.setDataType(propertyElement.getAttribute("dataType"));
  201. int length = propertyElement.getAttribute("length").equals("") ? 0 : Integer
  202. .parseInt(propertyElement.getAttribute("length"));
  203. property.setLength(length);
  204. property.setReferenceParentName(propertyElement.getAttribute("referenceParentName"));
  205. property.setDisplay(Boolean.valueOf(propertyElement.getAttribute("display"))
  206. .booleanValue());
  207. property.setUnique(Boolean.valueOf(propertyElement.getAttribute("unique"))
  208. .booleanValue());
  209. property.setScale(propertyElement.getAttribute("scale"));
  210. /**
  211. * PageResouce.xml validate
  212. */
  213. NodeList validateList = propertyElement.getElementsByTagName("validate");
  214. ValidateImpl validate = new ValidateImpl();
  215. for (int j = 0; j < validateList.getLength(); j++) {
  216. Element elementValidate = (Element) validateList.item(j);
  217. validate = readValidate(validate, elementValidate);
  218. }
  219. property.setValidate(validate);
  220. alProperty.add(property);
  221. log.debug("读取 " + property + " 完成!");
  222. }
  223. return alProperty;
  224. }
  225. // ...validate
  226. private ValidateImpl readValidate(ValidateImpl val, Element elementValidate) {
  227. String xmlNodeType = elementValidate.getAttribute("type");
  228. if (xmlNodeType.equals("need")) {
  229. val.setNeed(true);
  230. } else if (xmlNodeType.equals("trim")) {
  231. val.setTrim(true);
  232. } else if (xmlNodeType.equals("regx")) {
  233. val.setRegxValue(elementValidate.getAttribute("regxValue"));
  234. } else if (xmlNodeType.equals("range")) {
  235. val.setMaxValue(elementValidate.getAttribute("maxValue"));
  236. val.setMinValue(elementValidate.getAttribute("minValue"));
  237. } else if (xmlNodeType.equals("length")) {
  238. val.setMaxLength(elementValidate.getAttribute("maxLength"));
  239. } else if (xmlNodeType.equals("dojoType")) {
  240. val.setDojoType(elementValidate.getAttribute("typeValue"));
  241. } else if (xmlNodeType.equals("message")) {
  242. val.setPromptMessage(elementValidate.getAttribute("promptMessage"));
  243. val.setInvalidMessage(elementValidate.getAttribute("invalidMessage"));
  244. }
  245. return val;
  246. }
  247. /**
  248. * <mdpClass classid="99999" name="Sys_TableId" description="系统表" type="Sys"
  249. * primaryKey="" catche="false" exist="true" validate="true" dataSource="1">
  250. */
  251. private void readMdpClass(NodeList classList) {
  252. ArrayList<MdpClassImpl> alClass = new ArrayList<MdpClassImpl>();
  253. for (int i = 0; i < classList.getLength(); i++) {
  254. Element classElement = (Element) classList.item(i);
  255. MdpClassImpl mdpClass = new MdpClassImpl();
  256. mdpClass.setClassid(Integer.parseInt(classElement.getAttribute("classid")));
  257. mdpClass.setType(classElement.getAttribute("type"));
  258. mdpClass.setValidate(Boolean.valueOf(classElement.getAttribute("validate"))
  259. .booleanValue());
  260. mdpClass.setDataSource(Integer.parseInt(classElement.getAttribute("dataSource")));
  261. mdpClass.setDescription(classElement.getAttribute("description"));
  262. /**
  263. * mdpClass.setNgoogleEn(classElement.getAttribute("ngoogleEn"));
  264. * mdpClass.setOldName(classElement.getAttribute("oldName"));
  265. */
  266. mdpClass.setName(classElement.getAttribute("name"));
  267. mdpClass.setExist(Boolean.valueOf(classElement.getAttribute("exist")).booleanValue());
  268. mdpClass.setCatche(Boolean.valueOf(classElement.getAttribute("catche")).booleanValue());
  269. NodeList relationList = classElement.getElementsByTagName("relation");
  270. mdpClass.setRelations(readRelation(relationList, mdpClass.getClassid()));
  271. mdpClass.setPrimaryKey(classElement.getAttribute("primaryKey"));
  272. /**
  273. * <mdpClass>下 <mdpAttribute>
  274. */
  275. NodeList attributeList = classElement.getElementsByTagName("mdpAttribute");
  276. mdpClass.setMdpAttributes(readMdpAttribute(attributeList));
  277. if (mdpClass.getRelations().size() != 0)
  278. SysModelImpl.getInstance().addRelations(mdpClass.getRelations());
  279. alClass.add(mdpClass);
  280. log.debug("读取 " + mdpClass + " 完成!");
  281. }
  282. SysModelImpl.getInstance().setMdpClasses(alClass);
  283. }
  284. // ...
  285. private List<RelationImpl> readRelation(NodeList relationList, int hostClassid) {
  286. ArrayList<RelationImpl> alRelation = new ArrayList<RelationImpl>();
  287. for (int i = 0; i < relationList.getLength(); i++) {
  288. Element realtionElement = (Element) relationList.item(i);
  289. RelationImpl relation = new RelationImpl();
  290. relation.setHostClassid(hostClassid);
  291. relation.setType(Integer.parseInt(realtionElement.getAttribute("type")));
  292. relation.setGuestClassid(Integer.parseInt(realtionElement.getAttribute("guestClassid")));
  293. alRelation.add(relation);
  294. log.debug("读取 " + relation + " 完成!");
  295. }
  296. return alRelation;
  297. }
  298. // ...<mdpAttribute validate="" name="" description="" logicPrimaryKey=""
  299. // dataType="" referenceType="" scale="" precision="" index="" indexType=""
  300. // defaultValue="" notNull="" />
  301. private List<MdpAttributeImpl> readMdpAttribute(NodeList attributeList) {
  302. ArrayList<MdpAttributeImpl> alAttribute = new ArrayList<MdpAttributeImpl>();
  303. MdpAttributeImpl attribute = null;
  304. for (int i = 0; i < attributeList.getLength(); i++) {
  305. Element attributeElement = (Element) attributeList.item(i);
  306. attribute = new MdpAttributeImpl();
  307. attribute.setValidate(Boolean.valueOf(attributeElement.getAttribute("validate"))
  308. .booleanValue());
  309. attribute.setName(attributeElement.getAttribute("name"));
  310. /**
  311. * attribute.setNgoogleEn(attributeElement.getAttribute("ngoogleEn")
  312. * );
  313. * attribute.setOldName(attributeElement.getAttribute("oldName"));
  314. */
  315. attribute.setDescription(attributeElement.getAttribute("description"));
  316. attribute.setUnit(attributeElement.getAttribute("unit"));
  317. attribute.setLogicPrimaryKey(Boolean.valueOf(
  318. attributeElement.getAttribute("logicPrimaryKey")).booleanValue());
  319. attribute.setDataType(attributeElement.getAttribute("dataType"));
  320. attribute.setReferenceType(Integer.parseInt(attributeElement
  321. .getAttribute("referenceType")));
  322. attribute.setScale(attributeElement.getAttribute("scale"));
  323. int precision = attributeElement.getAttribute("precision").equals("") ? 0 : Integer
  324. .parseInt(attributeElement.getAttribute("precision"));
  325. attribute.setPrecision(precision);
  326. attribute.setIndex(Boolean.valueOf(attributeElement.getAttribute("index"))
  327. .booleanValue());
  328. attribute.setIndexType(attributeElement.getAttribute("indexType"));
  329. attribute.setDefaultValue(attributeElement.getAttribute("defaultValue"));
  330. attribute.setNotNull(Boolean.valueOf(attributeElement.getAttribute("notNull"))
  331. .booleanValue());
  332. NodeList referenceList = attributeElement.getElementsByTagName("reference");
  333. attribute.setReference(readReference(referenceList));
  334. attribute.setAutoIncrement(Boolean.valueOf(
  335. attributeElement.getAttribute("isAutoIncrement")).booleanValue());
  336. attribute.setReadOnly(Boolean.valueOf(attributeElement.getAttribute("isReadOnly"))
  337. .booleanValue());
  338. attribute.setSearchable(Boolean.valueOf(attributeElement.getAttribute("isSearchable"))
  339. .booleanValue());
  340. attribute.setSigned(Boolean.valueOf(attributeElement.getAttribute("isSigned"))
  341. .booleanValue());
  342. int columnDisplaySize = attributeElement.getAttribute("columnDisplaySize").equals("") ? 0
  343. : Integer.parseInt(attributeElement.getAttribute("columnDisplaySize"));
  344. attribute.setColumnDisplaySize(columnDisplaySize);
  345. attribute.setFieldType(attributeElement.getAttribute("fieldType"));
  346. log.debug("读取 " + attribute.getReference() + " 完成!");
  347. alAttribute.add(attribute);
  348. log.debug("读取 " + attribute + " 完成!");
  349. }
  350. return alAttribute;
  351. }
  352. // ...
  353. private Reference readReference(NodeList referenceList) {
  354. if (referenceList.getLength() == 1) {
  355. Element referenceElement = (Element) referenceList.item(0);
  356. Reference reference = new Reference();
  357. reference.setReferenceTable(referenceElement.getAttribute("referenceTable"));
  358. reference.setStoreName(referenceElement.getAttribute("storeName"));
  359. reference.setDisplayName(referenceElement.getAttribute("displayName"));
  360. return reference;
  361. }
  362. return null;
  363. }
  364. }