Package org.vietspider.parser.xml

Examples of org.vietspider.parser.xml.XMLNode


  public <T> T toBean(Class<T> clazz, XMLDocument document) throws Exception {
    NodeMap map = clazz.getAnnotation(NodeMap.class);
    if (map == null) return null;
    String name = map.value();
    T object = clazz.newInstance();
    XMLNode node = searchNode(document.getRoot(), name);
    if(node == null) return null;
    toBean(clazz, object, node);
    return object;
  }
View Full Code Here


  private XMLNode searchNode(XMLNode node, String name) {
    if(node.isNode(name)) return node;
    List<XMLNode> children = node.getChildren();
    if(children == null) return null;
    for(XMLNode ele :  children) {
      XMLNode value = searchNode(ele, name);
      if(value != null) return value;
    }
    return null;
  }
View Full Code Here

    for(int i = 0; i < children.size(); i++) {
      if(children.get(i).getChildren() == null || children.get(i).getChildren().size() < 1) {
        Array.set(array, i, null);
        continue;
      }
      XMLNode elementNode  = children.get(i);
      if(elementType.isPrimitive() || reflectUtil.isPrimitiveType(elementType)) {
        Array.set(array, i, toValue(elementType, new String(elementNode.getChild(0).getValue())));
      } else {
        Array.set(array, i, toValue(elementType, mapItemName, elementNode));
      }
    }
    return array;
View Full Code Here

    String mapValueItemName = nodesMap == null ? null : nodesMap.item();
    if(mapValueItemName != null && mapValueItemName.trim().length() < 1) mapValueItemName = null;
   
   
    for(int i = 0; i < children.size(); i++) {
      XMLNode elementNode  = children.get(i);
      if(elementNode.getChildren().size() < 1continue;
     
      XMLNode keyNode = elementNode.getChildren().get(0);
      Object keyObject = null;
     
      if(keyParamType.isPrimitive() || reflectUtil.isPrimitiveType(keyParamType)) {
        String nodeValue = (keyNode.getChildren() == null
                            || keyNode.getChildren().size() < 1) ?
                                  "" : keyNode.getChild(0).getNodeValue();
        keyObject = toValue(keyParamType, nodeValue);
      } else {
        keyObject = toValue(keyParamType, mapKeyItemName, keyNode);
      }
      if(keyObject == null) continue;
     
     
      XMLNode valueNode = elementNode.getChildren().get(1);
      Object valueObject = null;
     
      if(valueParamType.isPrimitive() || reflectUtil.isPrimitiveType(valueParamType)) {
        String nodeValue = (valueNode.getChildren() == null
                             || valueNode.getChildren().size() < 1) ?
                                 "" : valueNode.getChild(0).getNodeValue();
        valueObject = toValue(valueParamType, nodeValue);
      } else {
        valueObject = toValue(valueParamType, mapValueItemName, valueNode);
      }
     
View Full Code Here

  }
 
  public XMLDocument toXMLDocument(Object bean) throws Exception {
    NodeMap map = bean.getClass().getAnnotation(NodeMap.class);
    if(map == null) return null;
    XMLNode node = new XMLNode(map.value().toCharArray(), map.value(), TypeToken.TAG);
    toXML(bean, node);
    return new XMLDocument(node);
  }
View Full Code Here

//        setChild(node, name, value.toString());
//      }
//      return;
//    }
   
    XMLNode child = new XMLNode(name.toCharArray(), name, TypeToken.TAG);  
    node.getChildren().add(child);
    if(value == nullthrow new NullPointerException(field.getName() + " is null");
    NodeMap valueMap = value.getClass().getAnnotation(NodeMap.class);
    if(valueMap == null) {
      toXMLValue(value.getClass(), 1, value, child);
      return;
    }
    XMLNode valueNode  = new XMLNode(valueMap.value().toCharArray(), valueMap.value(), TypeToken.TAG);
    child.addChild(valueNode);
    toXMLValue(value.getClass(), map.depth(), value, valueNode);
  }
View Full Code Here

  private void fromCollection(Field field, Object bean, XMLNode parent) {
    NodesMap map = field.getAnnotation(NodesMap.class);
    if(map == null || bean == null) return;
   
    String name = map.value();
    XMLNode node = new XMLNode(name.toCharArray(), name, TypeToken.TAG);
   
    field.setAccessible(true);
    Object values = getValue(field, bean);
    Collection<?> collection = (Collection<?>) values;
    setAttribute(node, "type", values.getClass().getName());
View Full Code Here

    if(propertiesMap == null || map == null || map.size() < 1) return;
    String itemName = propertiesMap.item();
    boolean isAttribute = propertiesMap.attribute();
    boolean isCDATA = propertiesMap.cdata();
   
    XMLNode node = new XMLNode(propertiesMap.value().toCharArray(), propertiesMap.value(), TypeToken.TAG);
    parent.getChildren().add(node);
    parent = node;
   
    String keyName = "key";
    String keyValue = "value";
   
   
    setAttribute(node, "type", map.getClass().getName());
    Iterator<?> iter = map.keySet().iterator();
   
    while(iter.hasNext()) {
      Object key = iter.next();
      Object value = map.get(key);
      if(value == null) value = "";
      node = new XMLNode(itemName.toCharArray(), itemName, TypeToken.TAG);
      parent.getChildren().add(node);

      mapValue2XMLData(key, keyName, isAttribute, isCDATA, node);
      mapValue2XMLData(value, keyValue, isAttribute, isCDATA, node);
    }
View Full Code Here

    NodeMap valueMap = type.getAnnotation(NodeMap.class);
    if(valueMap == null) {
      toXMLValue(type, 1, value, node);
      return;
    }
    XMLNode valueNode  = new XMLNode(valueMap.value().toCharArray(), valueMap.value(), TypeToken.TAG);
    node.addChild(valueNode);
    toXMLValue(type, valueMap.depth(), value, valueNode);
  }
View Full Code Here

    Object values = getValue(field, bean);
    if(values == null) return;
   
    field.setAccessible(true);
    String name = map.value();
    XMLNode node = new XMLNode(name.toCharArray(), name, TypeToken.TAG);
   
    int length = fromArray(node, values, map);
    if(length > 0) parent.getChildren().add(node);
//    System.out.println(values + " : "+ map.value() +" : " + length);
  }
View Full Code Here

TOP

Related Classes of org.vietspider.parser.xml.XMLNode

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.