123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412 |
- package com.sysmodel.datamodel.xmlmanager;
- import java.io.File;
- import java.util.ArrayList;
- import java.util.List;
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- import org.apache.log4j.Logger;
- import org.w3c.dom.Document;
- import org.w3c.dom.Element;
- import org.w3c.dom.NodeList;
- import com.persistence.DBdll.adapter.DB2Adapter;
- import com.sysmodel.datamodel.xmlmodel.Reference;
- import com.sysmodel.datamodel.xmlmodel.impl.MdpAttributeImpl;
- import com.sysmodel.datamodel.xmlmodel.impl.MdpClassImpl;
- import com.sysmodel.datamodel.xmlmodel.impl.MdpConstantImpl;
- import com.sysmodel.datamodel.xmlmodel.impl.MdpDataSourceImpl;
- import com.sysmodel.datamodel.xmlmodel.impl.MdpModuleImpl;
- import com.sysmodel.datamodel.xmlmodel.impl.PropertyImpl;
- import com.sysmodel.datamodel.xmlmodel.impl.RelationImpl;
- import com.sysmodel.datamodel.xmlmodel.impl.SysModelImpl;
- import com.sysmodel.datamodel.xmlmodel.impl.TemplateImpl;
- import com.sysmodel.xformmodel.impl.ValidateImpl;
- /**
- * datamodel com.sysmodel.datamodel.xmlmanager load datamodel.xml
- *
- * 需要修改常量表对象的加载
- */
- public class XMLLoader{
- private final static Logger log = Logger.getLogger(DB2Adapter.class);
- public boolean loadFromXML(File[] files) {
- return true;
- }
- public boolean loadFromXML(File file) {
- DocumentBuilderFactory domfactory = DocumentBuilderFactory.newInstance();
- try {
- DocumentBuilder builder = domfactory.newDocumentBuilder();
- Document document = builder.parse(file);
- /**
- * <unit unitcode="zyuc" unitname="门头沟分局"/> read unit
- */
- NodeList unitList = document.getElementsByTagName("unit");
- readUnit(unitList);
- /**
- * <author>nxs</author> <version>1.0</version> read about
- */
- readAbout(document);
- /**
- * 模块 <mdpModules> read mdpModules
- */
- NodeList mdpModulesList = document.getElementsByTagName("module");
- readmdpModules(mdpModulesList);
- /**
- * 数据源 <mdpDataSources> readDataSource
- */
- NodeList dataSourceList = document.getElementsByTagName("mdpDataSource");
- readDataSource(dataSourceList);
- /**
- * 模板 <templates> readTemplate
- */
- NodeList templateList = document.getElementsByTagName("template");
- readTemplate(templateList);
- /**
- * 常量根节点 <mdpConstant> readConstant
- */
- NodeList constantList = document.getElementsByTagName("mdpConstant");
- readConstant(constantList);
- /**
- * 数据表根节点 <mdpClass> readMdpClass
- */
- NodeList classList = document.getElementsByTagName("mdpClass");
- readMdpClass(classList);
- } catch (Exception e) {
- log.error("load sysmodel error: ", e);
- return false;
- }
- return true;
- }
- public boolean loadFromXML(String filePath) {
- // ... 文件路径
- File xmlfile = new File(filePath);
- if (!xmlfile.exists())
- return false;
- // ...创建装载xml对象
- return loadFromXML(xmlfile);
- }
- private void readUnit(NodeList unit) {
- Element element = (Element) unit.item(0);
- SysModelImpl.getInstance().setUnitcode(element.getAttribute("unitcode"));
- SysModelImpl.getInstance().setUnitname(element.getAttribute("unitname"));
- }
- // ...<author>nxs</author><version>1.0</version>
- private void readAbout(Document document) {
- NodeList author = document.getElementsByTagName("author");
- if (author.getLength() == 1) {
- Element element = (Element) author.item(0);
- if (element.getFirstChild() != null)
- SysModelImpl.getInstance().setAuthor((element.getFirstChild().getNodeValue()));
- }
- NodeList version = document.getElementsByTagName("version");
- if (version.getLength() == 1) {
- Element element = (Element) version.item(0);
- if (element.getFirstChild() != null)
- SysModelImpl.getInstance().setVersion((element.getFirstChild().getNodeValue()));
- }
- }
- // ...<mdpModules><module code="" name="" description=""/></mdpModules>
- private void readmdpModules(NodeList mdpModules) {
- ArrayList<MdpModuleImpl> arry = new ArrayList<MdpModuleImpl>();
- for (int i = 0; i < mdpModules.getLength(); i++) {
- MdpModuleImpl mdpmod = new MdpModuleImpl();
- Element element = (Element) mdpModules.item(i);
- mdpmod.setPcode(element.getAttribute("pcode"));
- mdpmod.setCode(element.getAttribute("code"));
- mdpmod.setName(element.getAttribute("name"));
- mdpmod.setNgoogleEn(element.getAttribute("ngoogleEn"));
- mdpmod.setTPrefix(element.getAttribute("tPrefix"));
- mdpmod.setDescription(element.getAttribute("description"));
- mdpmod.setPackageName(element.getAttribute("packageName"));
- arry.add(mdpmod);
- log.debug("读取 " + mdpmod + " 完成!");
- }
- SysModelImpl.getInstance().setmdpModules(arry);
- }
- // ...<mdpDataSources><mdpDataSource dataSourceid="" description=""
- // dataBase="" url="" user="" password="" jndi=""/></mdpDataSources>
- private void readDataSource(NodeList dataSourceList) {
- ArrayList<MdpDataSourceImpl> alDataSource = new ArrayList<MdpDataSourceImpl>();
- for (int i = 0; i < dataSourceList.getLength(); i++) {
- MdpDataSourceImpl datasource = new MdpDataSourceImpl();
- Element element = (Element) dataSourceList.item(i);
- datasource.setDataSourceid(Integer.parseInt(element.getAttribute("dataSourceid")));
- datasource.setDescription(element.getAttribute("description"));
- datasource.setDataBase(element.getAttribute("dataBase"));
- log.info("数据库类型:" + element.getAttribute("dataBase") + "......");
- datasource.setURL(element.getAttribute("url"));
- datasource.setUser(element.getAttribute("user"));
- datasource.setPassword(element.getAttribute("password"));
- datasource.setJNDI(element.getAttribute("jndi"));
- alDataSource.add(datasource);
- log.debug("读取 " + datasource + " 完成!");
- }
- SysModelImpl.getInstance().setMdpDataSources(alDataSource);
- }
- // ...<templates><template name="" description=""></templates>
- private void readTemplate(NodeList templateList) {
- ArrayList<TemplateImpl> alTemplate = new ArrayList<TemplateImpl>();
- for (int i = 0; i < templateList.getLength(); i++) {
- TemplateImpl template = new TemplateImpl();
- Element element = (Element) templateList.item(i);
- template.setName(element.getAttribute("name"));
- // 数据存放的表名称.
- template.setTableName(element.getAttribute("tableName"));
- template.setDataSource(Integer.parseInt(element.getAttribute("dataSource")));
- template.setDescription(element.getAttribute("description"));
- /**
- * <template>的子标签 <property>
- */
- NodeList propertyList = element.getElementsByTagName("property");
- template.setListproperty(readProperty(propertyList));
- alTemplate.add(template);
- log.debug("读取 " + template + " 完成!");
- }
- SysModelImpl.getInstance().setTemplates(alTemplate);
- }
- // ...<mdpConstant codeName="" description="" templateName="" rootFrom=""
- // dataSource="" keyCodeAuto="" orderProperty="" orderMethod="" type=""/>
- @SuppressWarnings("deprecation")
- private void readConstant(NodeList constantList) {
- ArrayList<MdpConstantImpl> alConstant = new ArrayList<MdpConstantImpl>();
- for (int i = 0; i < constantList.getLength(); i++) {
- MdpConstantImpl constant = new MdpConstantImpl();
- Element constantElement = (Element) constantList.item(i);
- constant.setCodeName(constantElement.getAttribute("codeName"));
- constant.setNgoogleEn(constantElement.getAttribute("ngoogleEn"));
- constant.setDescription(constantElement.getAttribute("description"));
- constant.setTemplateName(constantElement.getAttribute("templateName"));
- // 树形常量根节点配置信息
- constant.setRootFrom(constantElement.getAttribute("rootFrom"));
- constant.setDataSource(Integer.parseInt(constantElement.getAttribute("dataSource")));
- constant.setKeyCodeAuto(Boolean.valueOf(constantElement.getAttribute("keyCodeAuto"))
- .booleanValue());
- constant.setOrderProperty(constantElement.getAttribute("orderProperty"));
- constant.setOrderMethod(constantElement.getAttribute("orderMethod"));
- constant.setType(constantElement.getAttribute("type"));
- alConstant.add(constant);
- log.debug("读取 " + constant + " 完成!");
- }
- SysModelImpl.getInstance().setMdpConstants(alConstant);
- }
- // ...<property name="" description="" dataType="" length="" scale=""
- // referenceParentName="" display=" " unique="">
- private List<PropertyImpl> readProperty(NodeList propertyList) {
- ArrayList<PropertyImpl> alProperty = new ArrayList<PropertyImpl>();
- for (int i = 0; i < propertyList.getLength(); i++) {
- PropertyImpl property = new PropertyImpl();
- Element propertyElement = (Element) propertyList.item(i);
- property.setName(propertyElement.getAttribute("name"));
- property.setRefConstant(propertyElement.getAttribute("refConstant"));
- property.setDescription(propertyElement.getAttribute("description"));
- property.setDataType(propertyElement.getAttribute("dataType"));
- int length = propertyElement.getAttribute("length").equals("") ? 0 : Integer
- .parseInt(propertyElement.getAttribute("length"));
- property.setLength(length);
- property.setReferenceParentName(propertyElement.getAttribute("referenceParentName"));
- property.setDisplay(Boolean.valueOf(propertyElement.getAttribute("display"))
- .booleanValue());
- property.setUnique(Boolean.valueOf(propertyElement.getAttribute("unique"))
- .booleanValue());
- property.setScale(propertyElement.getAttribute("scale"));
- /**
- * PageResouce.xml validate
- */
- NodeList validateList = propertyElement.getElementsByTagName("validate");
- ValidateImpl validate = new ValidateImpl();
- for (int j = 0; j < validateList.getLength(); j++) {
- Element elementValidate = (Element) validateList.item(j);
- validate = readValidate(validate, elementValidate);
- }
- property.setValidate(validate);
- alProperty.add(property);
- log.debug("读取 " + property + " 完成!");
- }
- return alProperty;
- }
- // ...validate
- private ValidateImpl readValidate(ValidateImpl val, Element elementValidate) {
- String xmlNodeType = elementValidate.getAttribute("type");
- if (xmlNodeType.equals("need")) {
- val.setNeed(true);
- } else if (xmlNodeType.equals("trim")) {
- val.setTrim(true);
- } else if (xmlNodeType.equals("regx")) {
- val.setRegxValue(elementValidate.getAttribute("regxValue"));
- } else if (xmlNodeType.equals("range")) {
- val.setMaxValue(elementValidate.getAttribute("maxValue"));
- val.setMinValue(elementValidate.getAttribute("minValue"));
- } else if (xmlNodeType.equals("length")) {
- val.setMaxLength(elementValidate.getAttribute("maxLength"));
- } else if (xmlNodeType.equals("dojoType")) {
- val.setDojoType(elementValidate.getAttribute("typeValue"));
- } else if (xmlNodeType.equals("message")) {
- val.setPromptMessage(elementValidate.getAttribute("promptMessage"));
- val.setInvalidMessage(elementValidate.getAttribute("invalidMessage"));
- }
- return val;
- }
- /**
- * <mdpClass classid="99999" name="Sys_TableId" description="系统表" type="Sys"
- * primaryKey="" catche="false" exist="true" validate="true" dataSource="1">
- */
- private void readMdpClass(NodeList classList) {
- ArrayList<MdpClassImpl> alClass = new ArrayList<MdpClassImpl>();
- for (int i = 0; i < classList.getLength(); i++) {
- Element classElement = (Element) classList.item(i);
- MdpClassImpl mdpClass = new MdpClassImpl();
- mdpClass.setClassid(Integer.parseInt(classElement.getAttribute("classid")));
- mdpClass.setType(classElement.getAttribute("type"));
- mdpClass.setValidate(Boolean.valueOf(classElement.getAttribute("validate"))
- .booleanValue());
- mdpClass.setDataSource(Integer.parseInt(classElement.getAttribute("dataSource")));
- mdpClass.setDescription(classElement.getAttribute("description"));
- /**
- * mdpClass.setNgoogleEn(classElement.getAttribute("ngoogleEn"));
- * mdpClass.setOldName(classElement.getAttribute("oldName"));
- */
- mdpClass.setName(classElement.getAttribute("name"));
- mdpClass.setExist(Boolean.valueOf(classElement.getAttribute("exist")).booleanValue());
- mdpClass.setCatche(Boolean.valueOf(classElement.getAttribute("catche")).booleanValue());
- NodeList relationList = classElement.getElementsByTagName("relation");
- mdpClass.setRelations(readRelation(relationList, mdpClass.getClassid()));
- mdpClass.setPrimaryKey(classElement.getAttribute("primaryKey"));
- /**
- * <mdpClass>下 <mdpAttribute>
- */
- NodeList attributeList = classElement.getElementsByTagName("mdpAttribute");
- mdpClass.setMdpAttributes(readMdpAttribute(attributeList));
- if (mdpClass.getRelations().size() != 0)
- SysModelImpl.getInstance().addRelations(mdpClass.getRelations());
- alClass.add(mdpClass);
- log.debug("读取 " + mdpClass + " 完成!");
- }
- SysModelImpl.getInstance().setMdpClasses(alClass);
- }
- // ...
- private List<RelationImpl> readRelation(NodeList relationList, int hostClassid) {
- ArrayList<RelationImpl> alRelation = new ArrayList<RelationImpl>();
- for (int i = 0; i < relationList.getLength(); i++) {
- Element realtionElement = (Element) relationList.item(i);
- RelationImpl relation = new RelationImpl();
- relation.setHostClassid(hostClassid);
- relation.setType(Integer.parseInt(realtionElement.getAttribute("type")));
- relation.setGuestClassid(Integer.parseInt(realtionElement.getAttribute("guestClassid")));
- alRelation.add(relation);
- log.debug("读取 " + relation + " 完成!");
- }
- return alRelation;
- }
- // ...<mdpAttribute validate="" name="" description="" logicPrimaryKey=""
- // dataType="" referenceType="" scale="" precision="" index="" indexType=""
- // defaultValue="" notNull="" />
- private List<MdpAttributeImpl> readMdpAttribute(NodeList attributeList) {
- ArrayList<MdpAttributeImpl> alAttribute = new ArrayList<MdpAttributeImpl>();
- MdpAttributeImpl attribute = null;
- for (int i = 0; i < attributeList.getLength(); i++) {
- Element attributeElement = (Element) attributeList.item(i);
- attribute = new MdpAttributeImpl();
- attribute.setValidate(Boolean.valueOf(attributeElement.getAttribute("validate"))
- .booleanValue());
- attribute.setName(attributeElement.getAttribute("name"));
- /**
- * attribute.setNgoogleEn(attributeElement.getAttribute("ngoogleEn")
- * );
- * attribute.setOldName(attributeElement.getAttribute("oldName"));
- */
- attribute.setDescription(attributeElement.getAttribute("description"));
- attribute.setUnit(attributeElement.getAttribute("unit"));
- attribute.setLogicPrimaryKey(Boolean.valueOf(
- attributeElement.getAttribute("logicPrimaryKey")).booleanValue());
- attribute.setDataType(attributeElement.getAttribute("dataType"));
- attribute.setReferenceType(Integer.parseInt(attributeElement
- .getAttribute("referenceType")));
- attribute.setScale(attributeElement.getAttribute("scale"));
- int precision = attributeElement.getAttribute("precision").equals("") ? 0 : Integer
- .parseInt(attributeElement.getAttribute("precision"));
- attribute.setPrecision(precision);
- attribute.setIndex(Boolean.valueOf(attributeElement.getAttribute("index"))
- .booleanValue());
- attribute.setIndexType(attributeElement.getAttribute("indexType"));
- attribute.setDefaultValue(attributeElement.getAttribute("defaultValue"));
- attribute.setNotNull(Boolean.valueOf(attributeElement.getAttribute("notNull"))
- .booleanValue());
- NodeList referenceList = attributeElement.getElementsByTagName("reference");
- attribute.setReference(readReference(referenceList));
- attribute.setAutoIncrement(Boolean.valueOf(
- attributeElement.getAttribute("isAutoIncrement")).booleanValue());
- attribute.setReadOnly(Boolean.valueOf(attributeElement.getAttribute("isReadOnly"))
- .booleanValue());
- attribute.setSearchable(Boolean.valueOf(attributeElement.getAttribute("isSearchable"))
- .booleanValue());
- attribute.setSigned(Boolean.valueOf(attributeElement.getAttribute("isSigned"))
- .booleanValue());
- int columnDisplaySize = attributeElement.getAttribute("columnDisplaySize").equals("") ? 0
- : Integer.parseInt(attributeElement.getAttribute("columnDisplaySize"));
- attribute.setColumnDisplaySize(columnDisplaySize);
- attribute.setFieldType(attributeElement.getAttribute("fieldType"));
- log.debug("读取 " + attribute.getReference() + " 完成!");
- alAttribute.add(attribute);
- log.debug("读取 " + attribute + " 完成!");
- }
- return alAttribute;
- }
- // ...
- private Reference readReference(NodeList referenceList) {
- if (referenceList.getLength() == 1) {
- Element referenceElement = (Element) referenceList.item(0);
- Reference reference = new Reference();
- reference.setReferenceTable(referenceElement.getAttribute("referenceTable"));
- reference.setStoreName(referenceElement.getAttribute("storeName"));
- reference.setDisplayName(referenceElement.getAttribute("displayName"));
- return reference;
- }
- return null;
- }
- }
|