📄 formxml.java
字号:
/* CRMS, customer relationship management system Copyright (C) 2003 Service To Youth Council This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA For further information contact the SYC ICT department on GPL@syc.net.au 98 Kermode Street North Adelaide South Australia SA 5006 +61 (0)8 8367 0755 *//* * FormXML.java * * Created on 11 April 2003, 03:52 */package crms.form;import org.w3c.dom.*;import crms.util.*;import org.apache.log4j.Logger;/** * Object responsible for converting to and from Form xml representation. * @author dmurphy */public class FormXML { static Logger logger = Logger.getLogger(FormXML.class); public static String NODE_FORM = "form"; public static String NODE_FORM_TITLE = "title"; public static String ATTR_NAME = "name"; public static String NODE_FORM_SIZE = "size"; public static String ATTR_SIZE_WIDTH = "width"; public static String ATTR_SIZE_HEIGHT = "width"; public static String NODE_FIELDS = "fields"; public static String NODE_FIELD = "field"; public static String NODE_FIELD_TYPE = "type"; public static String NODE_FIELD_TEXT = "text"; public static String NODE_FIELD_LOCATION = "location"; public static String NODE_PREPROCESSOR = "pre-processor"; public static String ATTR_LOCATION_X = "x"; public static String ATTR_LOCATION_Y = "y"; public static String ATTR_LOCATION_WIDTH = "width"; public static String ATTR_LOCATION_HEIGHT = "height"; public static String NODE_FIELD_INTERNAL_SIZE = "internal-size"; public static String NODE_OPTIONS = "options"; public static String NODE_OPTION = "option"; public static String NODE_OPTION_DEFAULT = "default"; public static String ATTR_CODE = "code"; public static String NODE_CLASS = "class"; /** Creates a new instance of FormXML */ public FormXML() { } public static Form createForm(Node formNode) { if (!formNode.getNodeName().equals(NODE_FORM)) { throw new RuntimeException("Can't create form, node is not 'form' node, it's: " + formNode.getNodeName()); } Form form = new Form( XMLUtil.getAttributeValue(ATTR_NAME, formNode)); logger.debug("Got form named " + form.getName()); form.setTitle(XMLUtil.getTextForNodeNamed(NODE_FORM_TITLE, formNode)); Node sizeNode = XMLUtil.getNode(NODE_FORM_SIZE, formNode); Node preProNode = XMLUtil.getNode(NODE_PREPROCESSOR, formNode); String preProcessor = XMLUtil.getAttributeValue(FormXML.ATTR_NAME, preProNode); if (preProcessor != null) { form.setPreprocessor(preProcessor); logger.debug("Set form preprocessor to " + preProcessor + "."); } else { logger.debug("Form preprocessor not defined. Setting to null."); } EntitySize size = new EntitySize( readIntegerAttribute(ATTR_SIZE_WIDTH, sizeNode), readIntegerAttribute(ATTR_SIZE_HEIGHT, sizeNode) ); NodeList fieldList = XMLUtil.getChildNodesNamed(NODE_FIELDS, NODE_FIELD, formNode); logger.debug("fieldList NodeList is of size: " + fieldList.getLength()); for (int i=0; i < fieldList.getLength(); i++) { Node fieldNode = fieldList.item(i); processField(fieldNode, form); } return form; } static void processField(Node fieldNode, Form form) { if (!fieldNode.getNodeName().equals(NODE_FIELD)) { throw new RuntimeException("Can't create field, node is not 'field' node, it's: " + fieldNode.getNodeName()); } FormField field = new FormField( XMLUtil.getAttributeValue(ATTR_NAME, fieldNode)); field.setType((FieldType) FieldType.decode(XMLUtil.getTextForNodeNamed(NODE_FIELD_TYPE, fieldNode), FieldType.class)); logger.debug("Set field Type: " + field.getType().getCode()); field.setText(XMLUtil.getTextForNodeNamed(NODE_FIELD_TEXT, fieldNode)); Node locationNode = XMLUtil.getNode(NODE_FIELD_LOCATION, fieldNode); EntityLocation location = new EntityLocation( readIntegerAttribute(ATTR_LOCATION_X, locationNode), readIntegerAttribute(ATTR_LOCATION_Y, locationNode), readIntegerAttribute(ATTR_LOCATION_WIDTH, locationNode), readIntegerAttribute(ATTR_LOCATION_HEIGHT, locationNode) ); field.setLocation(location); Node internalSizeNode = XMLUtil.getNode(NODE_FIELD_INTERNAL_SIZE, fieldNode); EntitySize size = new EntitySize( readIntegerAttribute(ATTR_SIZE_WIDTH, internalSizeNode), readIntegerAttribute(ATTR_SIZE_HEIGHT, internalSizeNode) ); field.setInternalSize(size); Node listClassNode = XMLUtil.getNode(NODE_CLASS, fieldNode); field.setListClass(XMLUtil.getAttributeValue(ATTR_NAME, listClassNode)); Node optionsNode = XMLUtil.getNode(NODE_OPTIONS, fieldNode); if (optionsNode != null ) { NodeList optionsList = optionsNode.getChildNodes(); for (int i=0; i < optionsList.getLength(); i++) { Node optionNode = optionsList.item(i); if (optionNode.getNodeName().equals(NODE_OPTION)) { processFieldOption(optionNode, field); } else if (optionNode.getNodeName().equals(NODE_OPTION_DEFAULT)) { field.setDefaultOption(XMLUtil.getAttributeValue(ATTR_CODE, optionNode)); } } } form.addField(field); } static void processFieldOption(Node optionNode, FormField field) { FieldOption option = new FieldOption( XMLUtil.getAttributeValue(ATTR_CODE, optionNode), XMLUtil.getTextForNode(optionNode) ); field.addOption(option); } public static int readIntegerAttribute(String attrName, Node node) { String value = XMLUtil.getAttributeValue(attrName, node); if (value == null) { return -1; } else { return Integer.parseInt(value); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -