コード例 #1
0
 private FieldConfig parseFieldConfig(Node fieldNode) throws ConfigException {
   NamedNodeMap attr = fieldNode.getAttributes();
   Node nameNode = attr.getNamedItem(XMLConfigConstant.NAME);
   if (nameNode == null) {
     throw new ConfigException(ErrorMsg.NO_NAME_PROPERTY_FOR_FIELD);
   }
   FieldConfig fieldConfig = new FieldConfig(nameNode.getNodeValue());
   NodeList validateRule = fieldNode.getChildNodes();
   if (validateRule == null || validateRule.getLength() == 0) {
     return null;
   }
   for (int i = 0; i < validateRule.getLength(); i++) {
     Node node = validateRule.item(i);
     if (node.getNodeType() != Node.ELEMENT_NODE) {
       continue;
     }
     AbstractValidator validator =
         ValidatorRegistry.getInstance().getValidator(node.getNodeName());
     if (validator == null) {
       throw new ConfigException(ErrorMsg.NO_SUCH_VALIDATOR_ERROR + node.getNodeName());
     } else {
       fieldConfig.addRule(validator.parseRuleConfig(node));
     }
   }
   return fieldConfig;
 }
コード例 #2
0
 /**
  * 将XML配置中节点解析出来返回一个validator的list
  *
  * @param doc
  * @return
  * @throws ConfigException
  * @throws InstantiationException
  * @throws IllegalAccessException
  * @throws ClassNotFoundException
  */
 private List<AbstractValidator> parseValidators(Document doc)
     throws ConfigException, InstantiationException, IllegalAccessException,
         ClassNotFoundException {
   NodeList children = doc.getDocumentElement().getChildNodes();
   List<AbstractValidator> validators = new ArrayList<AbstractValidator>();
   for (int i = 0; i < children.getLength(); i++) {
     Node node = children.item(i);
     if (XMLConfigConstant.VALIDATOR.equals(node.getNodeName())) {
       NamedNodeMap attrMap = node.getAttributes();
       Node classNameAttr = attrMap.getNamedItem(XMLConfigConstant.CLASS_NAME);
       if (classNameAttr == null) {
         throw new ConfigException(ErrorMsg.VALIDATOR_CLASS_NAME_EMPTY);
       }
       String className = classNameAttr.getNodeValue();
       Object obj = Class.forName(className).newInstance();
       if (!(obj instanceof AbstractValidator)) {
         throw new ConfigException(ErrorMsg.VALIDATOR_BASE_CLASS_ERROR);
       }
       AbstractValidator validator = (AbstractValidator) obj;
       if (validator != null) {
         validators.add(validator);
         // 如果XML中指定了name属性,则重置name
         Node nameAttr = attrMap.getNamedItem(XMLConfigConstant.NAME);
         if (nameAttr != null) {
           validator.setName(nameAttr.getNodeValue());
         }
         // 如果有property子元素,则赋值给validator
         NodeList properties = node.getChildNodes();
         for (int j = 0; j < properties.getLength(); j++) {
           Node propNode = properties.item(j);
           if (XMLConfigConstant.PROPERTY.equals(propNode.getNodeName())) {
             NamedNodeMap propAttr = propNode.getAttributes();
             Node propName = propAttr.getNamedItem(XMLConfigConstant.NAME);
             if (propName != null) {
               String methodName = propName.getNodeValue();
               methodName = methodName.substring(0, 1).toUpperCase() + methodName.substring(1);
               BeanUtil.invoke(
                   validator, "set" + methodName, String.class, propNode.getTextContent());
             }
           }
         }
       }
     }
   }
   return validators;
 }