package com.sysmodel.collectmodel.xmlmanager; import java.io.File; import java.util.ArrayList; 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.sysmodel.XFormModelImpl; import com.sysmodel.collectmodel.xmlmodel.able.PageBody; import com.sysmodel.collectmodel.xmlmodel.able.PageHead; import com.sysmodel.collectmodel.xmlmodel.able.PageRoot; import com.sysmodel.collectmodel.xmlmodel.impl.ActionImpl; import com.sysmodel.collectmodel.xmlmodel.impl.ContainerImpl; import com.sysmodel.collectmodel.xmlmodel.impl.FormCollectionImpl; import com.sysmodel.collectmodel.xmlmodel.impl.HiddenFieldImpl; import com.sysmodel.collectmodel.xmlmodel.impl.PageBodyImpl; import com.sysmodel.collectmodel.xmlmodel.impl.PageHeadImpl; import com.sysmodel.collectmodel.xmlmodel.impl.PageRootImpl; import com.sysmodel.collectmodel.xmlmodel.impl.ReportFieldImpl; import com.sysmodel.collectmodel.xmlmodel.impl.TabItemImpl; import com.sysmodel.xformmodel.impl.ValidateImpl; import com.sysmodel.xformmodel.importcontrol.AbstractControl; import com.sysmodel.xformmodel.importcontrol.CheckBoxControl; import com.sysmodel.xformmodel.importcontrol.DateControl; import com.sysmodel.xformmodel.importcontrol.RadioControl; import com.sysmodel.xformmodel.importcontrol.SelectControl; import com.sysmodel.xformmodel.importcontrol.TextAreaControl; import com.sysmodel.xformmodel.importcontrol.TextControl; import com.sysmodel.xformmodel.importcontrol.TreeControl; public class CollectXMLLoader{ private final static Logger log = Logger.getLogger(CollectXMLLoader.class); private String templistid = ""; public boolean readConfigFile(String filePath) { File xmlfile = new File(filePath); if (!xmlfile.exists()) { log.error("file: " + filePath + " not exist"); return false; } // Map retMap = new HashMap(); DocumentBuilderFactory domfactory = DocumentBuilderFactory.newInstance(); try { DocumentBuilder builder = domfactory.newDocumentBuilder(); Document document = builder.parse(xmlfile); // read FormCollection NodeList FormCollectionList = document.getElementsByTagName("FormCollection"); for (int i = 0; i < FormCollectionList.getLength(); i++) { Element element = (Element) FormCollectionList.item(i); FormCollectionImpl form = new FormCollectionImpl(); form.setPageid(element.getAttribute("pageid")); // templistid = element.getAttribute("pageid"); form.setClassid(element.getAttribute("classid")); form.setAction(element.getAttribute("action")); form.setMultipart(Boolean.valueOf(element.getAttribute("multipart")).booleanValue()); form.setDescription(element.getAttribute("description")); form.setType(element.getAttribute("type")); // read head form.setPageHead(readPageHead(element)); // read body form.setPageBody(readPageBody(element)); // read root form.setPageRoot(readPageRoot(element)); // retMap.put(form.getPageid(),form); XFormModelImpl.getInstance().addFormCollection(form.getPageid(), form); } } catch (Exception e) { log.error("load pageCollection error: ", e); } return true; } /** * @return PageHead */ private PageHead readPageHead(Element element) { NodeList headList = element.getElementsByTagName("pageHead"); if (headList.getLength() == 1) { Element headElement = (Element) headList.item(0); PageHeadImpl head = new PageHeadImpl(); head.setPageTitle(headElement.getAttribute("pageTitle") + templistid); head.setWidth(headElement.getAttribute("width")); head.setBackURL(headElement.getAttribute("backURL")); head.setHeight(headElement.getAttribute("height")); return head; } return null; } /** * @return PageRoot */ private PageRoot readPageRoot(Element element) { NodeList rootList = element.getElementsByTagName("pageRoot"); if (rootList.getLength() == 1) { Element rootElement = (Element) rootList.item(0); PageRootImpl root = new PageRootImpl(); root.setWidth(rootElement.getAttribute("width")); root.setImage(Boolean.valueOf(rootElement.getAttribute("image")).booleanValue()); root.setAlign(rootElement.getAttribute("align")); root.setHeight(rootElement.getAttribute("height")); // read action NodeList actionList = rootElement.getElementsByTagName("action"); for (int i = 0; i < actionList.getLength(); i++) { Element elementAction = (Element) actionList.item(i); ActionImpl actionTemp = new ActionImpl(); actionTemp.setType(elementAction.getAttribute("type")); actionTemp.setReturnURL(elementAction.getAttribute("successURL")); actionTemp.setValue(elementAction.getAttribute("value")); actionTemp.setMethod(elementAction.getAttribute("method")); String space = elementAction.getAttribute("appendSpace"); if (!space.equals("")) actionTemp.setAppendSpace(Integer.parseInt(space)); root.addAction(actionTemp); } return root; } return null; } // 定义全局变量,采集页面的所有字段信息 private ArrayList alReportFields = new ArrayList(); /** * @return PageBody */ private PageBody readPageBody(Element element) { NodeList bodyList = element.getElementsByTagName("pageBody"); if (bodyList.getLength() == 1) { Element bodyElement = (Element) bodyList.item(0); PageBodyImpl body = new PageBodyImpl(); // body.setWidth(bodyElement.getAttribute("width")); body.setHeight(bodyElement.getAttribute("height")); body.setLabelWidth(bodyElement.getAttribute("labelWidth")); body.setControlWidth(bodyElement.getAttribute("controlWidth")); body.setColumnCount(Integer.parseInt(bodyElement.getAttribute("columnCount"))); body.setLabelAlign(bodyElement.getAttribute("labelAlign")); body.setControlAlign(bodyElement.getAttribute("controlAlign")); // read ReportField // NodeList fieldHiddenList = bodyElement.getElementsByTagName("hiddenField"); if (fieldHiddenList != null) { for (int i = 0; i < fieldHiddenList.getLength(); i++) { Element elementField = (Element) fieldHiddenList.item(i); HiddenFieldImpl field = new HiddenFieldImpl(); field.setName(elementField.getAttribute("name")); field.setValue(elementField.getAttribute("value")); field.setValueMethod(elementField.getAttribute("valueMethod")); body.setAlHiddenFields(field); } } // 每读取一个采集页面配置,初始化一次 this.alReportFields = new ArrayList(); // read container body.setAlContainers(readPageBodyContainer(bodyElement)); // 设置所有的对应表的录入字段 // 临时性解决方案 09 10 12 body.setAlReportFields(this.alReportFields); return body; } return null; } /** * @param element * @return */ private ArrayList readPageBodyContainer(Element bodyElement) { ArrayList alContainers = new ArrayList(); NodeList containerList = bodyElement.getElementsByTagName("container"); for (int i = 0; i < containerList.getLength(); i++) { Element elementContainer = (Element) containerList.item(i); ContainerImpl container = new ContainerImpl(); container.setXtype(elementContainer.getAttribute("xtype")); container.setTitle(elementContainer.getAttribute("title")); container.setDefaults(elementContainer.getAttribute("defaults")); if (!elementContainer.getAttribute("collapsed").equals("")) { container.setCollapsed(new Boolean(elementContainer.getAttribute("collapsed")) .booleanValue()); } if (!elementContainer.getAttribute("autoHeight").equals("")) { container.setAutoHeight(new Boolean(elementContainer.getAttribute("autoHeight")) .booleanValue()); } container.setObjElements(readContainerChildren(elementContainer)); alContainers.add(container); } return alContainers; } /** * @param elementContainer * @return */ @SuppressWarnings({ "rawtypes", "unchecked" }) private ArrayList readContainerChildren(Element elementContainer) { ArrayList objElements = new ArrayList(); if (elementContainer.getAttribute("xtype").equals("tabpanel")) { NodeList tabitemList = elementContainer.getElementsByTagName("tabitem"); for (int i = 0; i < tabitemList.getLength(); i++) { Element elementTabitem = (Element) tabitemList.item(i); TabItemImpl tabitem = new TabItemImpl(); tabitem.setXtype(elementTabitem.getAttribute("xtype")); tabitem.setTitle(elementTabitem.getAttribute("title")); tabitem.setDefaults(elementTabitem.getAttribute("defaults")); tabitem.setObjElements(readReportField(elementTabitem)); objElements.add(tabitem); } } else { objElements = readReportField(elementContainer); } return objElements; } private ArrayList readReportField(Element element) { ArrayList alReportFields = new ArrayList(); NodeList fieldList = element.getElementsByTagName("reportField"); for (int i = 0; i < fieldList.getLength(); i++) { Element elementField = (Element) fieldList.item(i); ReportFieldImpl field = new ReportFieldImpl(); if (!elementField.getAttribute("classid").equals("")) { field.setClassid(Integer.parseInt(elementField.getAttribute("classid"))); } field.setName(elementField.getAttribute("name")); field.setLabel(elementField.getAttribute("label")); field.setControlType(elementField.getAttribute("controlType")); field.setColumnWidth(elementField.getAttribute("columnWidth")); field.setColspan(elementField.getAttribute("colspan")); // read control field.setControl(readControlByType(field.getControlType(), elementField)); // read validate NodeList validateList = elementField.getElementsByTagName("validate"); ValidateImpl validate = new ValidateImpl(); for (int j = 0; j < validateList.getLength(); j++) { Element elementValidate = (Element) validateList.item(j); validate = readValidate(validate, elementValidate); } field.setValidate(validate); alReportFields.add(field); } // 临时性解决方案 09 10 12 this.alReportFields.addAll(alReportFields); return alReportFields; } /** * @param controlType * @param elementField * @return AbstractControl */ private AbstractControl readControlByType(String controlType, Element elementField) { NodeList controlList = elementField.getElementsByTagName(controlType); if (controlList.getLength() == 1) { Element controlElement = (Element) controlList.item(0); if (controlType.equals("tree")) { // TreeControl tree = new TreeControl(); tree.setType(controlElement.getAttribute("type")); tree.setSize(controlElement.getAttribute("size")); tree.setReferenceTable(controlElement.getAttribute("referenceTable")); tree.setStoreCode(controlElement.getAttribute("storeCode")); tree.setDisplayName(controlElement.getAttribute("displayName")); tree.setParentCode(controlElement.getAttribute("parentCode")); tree.setCustomDefine(controlElement.getAttribute("customDefine")); tree.setCondition(controlElement.getAttribute("condition")); tree.setOrder(controlElement.getAttribute("order")); tree.setLeafonly(Boolean.valueOf(controlElement.getAttribute("leafonly")) .booleanValue()); return tree; } else if (controlType.equals("text")) { // TextControl text = new TextControl(); text.setSize(controlElement.getAttribute("size")); text.setReadOnly(Boolean.valueOf(controlElement.getAttribute("readonly")) .booleanValue()); return text; } else if (controlType.equals("select")) { //