123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390 |
- 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<ReportFieldImpl> alReportFields = new ArrayList<ReportFieldImpl>();
- /**
- * @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();
- // <pageBody width="100%" height="" labelWidth="" controlWidth=""
- // columnCount="2" labelAlign="" controlAlign="">
- 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
- // <hiddenField name="INCIDENT_ID" value="" valueMethod="idmake"/>
- 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<ReportFieldImpl>();
- // read container
- body.setAlContainers(readPageBodyContainer(bodyElement));
- // 设置所有的对应表的录入字段
- // 临时性解决方案 09 10 12
- body.setAlReportFields(this.alReportFields);
- return body;
- }
- return null;
- }
- /**
- * @param element
- * @return
- */
- private ArrayList<ContainerImpl> readPageBodyContainer(Element bodyElement) {
- ArrayList<ContainerImpl> alContainers = new ArrayList<ContainerImpl>();
- 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<ReportFieldImpl> readReportField(Element element) {
- ArrayList<ReportFieldImpl> alReportFields = new ArrayList<ReportFieldImpl>();
- 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")) {
- // <tree size="" type="" referenceTable="" storeCode=""
- // displayName="" parentCode="" customDefine="" condition=""
- // order=""/>
- 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")) {
- // <text size="" maxlength="" readonly=""/>
- TextControl text = new TextControl();
- text.setSize(controlElement.getAttribute("size"));
- text.setReadOnly(Boolean.valueOf(controlElement.getAttribute("readonly"))
- .booleanValue());
- return text;
- } else if (controlType.equals("select")) {
- // <select constantName=""/>
- SelectControl select = new SelectControl();
- select.setConstantName(controlElement.getAttribute("constantName"));
- select.setType(controlElement.getAttribute("type"));
- select.setOnChange(controlElement.getAttribute("onChange"));
- select.setIclassid(controlElement.getAttribute("classid"));
- select.setSize(controlElement.getAttribute("size"));
- if (!controlElement.getAttribute("isMultiple").equals("")) {
- select.setMultiple(Boolean.valueOf(controlElement.getAttribute("isMultiple"))
- .booleanValue());
- }
- if (!controlElement.getAttribute("isInitOption").equals("")) {
- select.setIsInitOption(Boolean.valueOf(
- controlElement.getAttribute("isInitOption")).booleanValue());
- }
- return select;
- } else if (controlType.equals("radio")) {
- // <radio constantName="" singleLine=""/>
- RadioControl radio = new RadioControl();
- radio.setConstantName(controlElement.getAttribute("constantName"));
- radio.setSingleLine(Boolean.valueOf(controlElement.getAttribute("singleLine"))
- .booleanValue());
- return radio;
- } else if (controlType.equals("checkbox")) {
- // <checkbox constantName="" singleLine=""/>
- CheckBoxControl checkbox = new CheckBoxControl();
- checkbox.setConstantName(controlElement.getAttribute("constantName"));
- checkbox.setSingleLine(Boolean.valueOf(controlElement.getAttribute("singleLine"))
- .booleanValue());
- return checkbox;
- } else if (controlType.equals("textarea")) {
- // <textarea cols="" rows="" readonly=""/>
- TextAreaControl textarea = new TextAreaControl();
- textarea.setHeight(controlElement.getAttribute("height"));
- textarea.setWidth(controlElement.getAttribute("width"));
- textarea.setReadOnly(Boolean.valueOf(controlElement.getAttribute("readonly"))
- .booleanValue());
- return textarea;
- } else if (controlType.equals("date")) {
- // <date size="" format=""/>
- DateControl date = new DateControl();
- date.setFormat(controlElement.getAttribute("format"));
- date.setSize(controlElement.getAttribute("size"));
- return date;
- }
- }
- return null;
- }
- /**
- * @return 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("vType")) {
- val.setvType(elementValidate.getAttribute("typeValue"));
- } else if (xmlNodeType.equals("message")) {
- val.setPromptMessage(elementValidate.getAttribute("promptMessage"));
- val.setInvalidMessage(elementValidate.getAttribute("invalidMessage"));
- }
- return val;
- }
- }
|