Java Code Examples for org.simpleframework.xml.stream.InputNode#getNext()

The following examples show how to use org.simpleframework.xml.stream.InputNode#getNext() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EntityWithAnyElementConverter.java    From sardine-android with Apache License 2.0 6 votes vote down vote up
@Override
public T read(InputNode node) throws Exception {
    Map<String, Field> entityFields = getEntityFields();
    T entity = entityClass.newInstance();
    List<org.w3c.dom.Element> anyElements = entity.getAny();
    InputNode childNode;
    while((childNode = node.getNext()) != null) {
        if (entityFields.containsKey(childNode.getName())) {
            Field field = entityFields.get(childNode.getName());
            getSetterForField(field).invoke(entity, serializer.read(field.getType(), childNode));
        } else if (childNode.getPrefix() != null && !childNode.getPrefix().isEmpty()) {
            org.w3c.dom.Element element = ElementConverter.read(childNode);
            anyElements.add(element);
        } else {
            // Probably a WebDAV field we don't support yet
            skipChildrenOfNode(childNode);
        }
    }
    return entity;
}
 
Example 2
Source File: NodeReaderTest.java    From simplexml with Apache License 2.0 6 votes vote down vote up
public void testEmptySource() throws Exception {
   InputNode event = NodeBuilder.read(new StringReader(EMPTY_SOURCE));

   assertTrue(event.isRoot());
   assertFalse(event.isEmpty());
   assertEquals("root", event.getName());
   
   InputNode child  = event.getNext();
   
   assertTrue(child.isEmpty());
   assertEquals("empty", child.getName());
   
   child = event.getNext();
   
   assertFalse(child.isEmpty());
   assertEquals("notEmpty", child.getName());
   assertEquals("foo", child.getAttribute("name").getValue());   
   
   child = event.getNext();
   
   assertTrue(child.isEmpty());
   assertEquals("empty", child.getName());
}
 
Example 3
Source File: CompositeInlineMap.java    From simplexml with Apache License 2.0 6 votes vote down vote up
/**
 * This <code>read</code> method will read the XML element map from
 * the provided node and deserialize its children as entry types.
 * Each entry type must contain a key and value so that the entry 
 * can be inserted in to the map as a pair. If either the key or 
 * value is composite it is read as a root object, which means its
 * <code>Root</code> annotation must be present and the name of the
 * object element must match that root element name.
 * 
 * @param node this is the XML element that is to be deserialized
 * 
 * @return this returns the item to attach to the object contact
 */
public boolean validate(InputNode node) throws Exception{
   InputNode from = node.getParent();
   String name = node.getName();                
   
   while(node != null) {
      if(!key.validate(node)) {
         return false;
      }
      if(!value.validate(node)) {
         return false;
      }
      node = from.getNext(name);
   }
   return true;
}
 
Example 4
Source File: CompositeMap.java    From simplexml with Apache License 2.0 6 votes vote down vote up
/**
 * This <code>validate</code> method will validate the XML element 
 * map from the provided node and validate its children as entry 
 * types. Each entry type must contain a key and value so that the 
 * entry can be inserted in to the map as a pair. If either the key 
 * or value is composite it is read as a root object, which means its
 * <code>Root</code> annotation must be present and the name of the
 * object element must match that root element name.
 * 
 * @param node this is the XML element that is to be validate
 * @param type this is the type to validate the input node against
 * 
 * @return true if the element matches the XML schema class given 
 */
private boolean validate(InputNode node, Class type) throws Exception {
   while(true) {
      InputNode next = node.getNext();
     
      if(next == null) {
         return true;
      }
      if(!key.validate(next)) {
         return false;
      }
      if(!value.validate(next)) {
         return false;
      }                     
   }
}
 
Example 5
Source File: CompositeInlineMap.java    From simplexml with Apache License 2.0 6 votes vote down vote up
/**
 * This <code>read</code> method will read the XML element map from
 * the provided node and deserialize its children as entry types.
 * Each entry type must contain a key and value so that the entry 
 * can be inserted in to the map as a pair. If either the key or 
 * value is composite it is read as a root object, which means its
 * <code>Root</code> annotation must be present and the name of the
 * object element must match that root element name.
 * 
 * @param node this is the XML element that is to be deserialized
 * @param map this is the map object that is to be populated
 * 
 * @return this returns the item to attach to the object contact
 */
private Object read(InputNode node, Map map) throws Exception {
   InputNode from = node.getParent();
   String name = node.getName();                

   while(node != null) {         
      Object index = key.read(node);
      Object item = value.read(node);
         
      if(map != null) {
         map.put(index, item);
      }
      node = from.getNext(name);
   }
   return map;
}
 
Example 6
Source File: Composite.java    From simplexml with Apache License 2.0 6 votes vote down vote up
/**
 * This <code>validateElements</code> method validates the elements 
 * from the provided XML element. This will iterate over all elements
 * within the element and validate those elements as primitives or
 * composite objects depending on the contact annotation.
 * <p>
 * Once all elements within the XML element have been evaluated
 * the <code>Schema</code> is checked to ensure that there are no
 * required contacts annotated with the <code>Element</code> that
 * remain. If any required element remains an exception is thrown.
 * 
 * @param node this is the XML element to be evaluated
 * @param section this is the section that defines the XML structure
 */
private void validateElements(InputNode node, Section section) throws Exception {
   LabelMap map = section.getElements();
   InputNode next = node.getNext();
   
   while(next != null) {         
      String name = next.getName();
      Section child = section.getSection(name);         
      
      if(child != null) {
         validateSection(next, child);
      } else {         
         validateElement(next, section, map);
      }
      next = node.getNext();
   } 
   validate(node, map);
}
 
Example 7
Source File: Composite.java    From simplexml with Apache License 2.0 6 votes vote down vote up
/**
 * This <code>readElements</code> method reads the elements from 
 * the provided XML element. This will iterate over all elements
 * within the element and convert those elements to primitives or
 * composite objects depending on the contact annotation.
 * <p>
 * Once all elements within the XML element have been evaluated
 * the <code>Schema</code> is checked to ensure that there are no
 * required contacts annotated with the <code>Element</code> that
 * remain. If any required element remains an exception is thrown. 
 * 
 * @param node this is the XML element to be evaluated
 * @param source the type of the object that is being deserialized
 * @param section the XML section that contains the structure
 */
private void readElements(InputNode node, Object source, Section section) throws Exception {
   LabelMap map = section.getElements();
   InputNode child = node.getNext();
   
   while(child != null) {         
      String name = child.getName();
      Section block = section.getSection(name);         
      
      if(block != null) {
         readSection(child, source, block);
      } else { 
         readElement(child, source, section, map);
      }
      child = node.getNext();
   } 
   validate(node, map, source);
}
 
Example 8
Source File: CompositeMap.java    From simplexml with Apache License 2.0 6 votes vote down vote up
/**
 * This <code>populate</code> method will read the XML element map 
 * from the provided node and deserialize its children as entry types.
 * Each entry type must contain a key and value so that the entry 
 * can be inserted in to the map as a pair. If either the key or 
 * value is composite it is read as a root object, which means its
 * <code>Root</code> annotation must be present and the name of the
 * object element must match that root element name.
 * 
 * @param node this is the XML element that is to be deserialized
 * @param result this is the map object that is to be populated
 * 
 * @return this returns the item to attach to the object contact
 */
private Object populate(InputNode node, Object result) throws Exception {
   Map map = (Map) result;                 
   
   while(true) {
      InputNode next = node.getNext();
     
      if(next == null) {
         return map;
      }
      Object index = key.read(next);
      Object item = value.read(next);
         
      map.put(index, item); 
   }
}
 
Example 9
Source File: ExampleConverters.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public List<Entry> read(InputNode node) throws Exception {
   List<Entry> entryList = new ArrayList<Entry>();
   while(true) {
      InputNode item = node.getNext("entry");
      if(item == null) {
         break;
      }
      entryList.add(converter.read(item));
   }
   return entryList;
}
 
Example 10
Source File: PrimitiveInlineList.java    From simplexml with Apache License 2.0 5 votes vote down vote up
/**
 * This <code>read</code> method wll read the XML element list from
 * the provided node and deserialize its children as entry types.
 * This will deserialize each entry type as a primitive value. In
 * order to do this the parent string provided forms the element.
 * 
 * @param node this is the XML element that is to be deserialized
 * @param list this is the collection that is to be populated
 * 
 * @return this returns the item to attach to the object contact
 */ 
private Object read(InputNode node, Collection list) throws Exception {              
   InputNode from = node.getParent();
   String name = node.getName();
   
   while(node != null) {
      Object item = root.read(node);
      
      if(item != null) {
         list.add(item);
      }      
      node = from.getNext(name);
   }
   return list;
}
 
Example 11
Source File: WrapperTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public Wrapper read(InputNode node) throws Exception {
   InputNode type = node.getAttribute("type");
   InputNode child = node.getNext();
   String className = type.getValue();
   Object value = null;
   if(child != null) {
      value = serializer.read(Class.forName(className), child);
   }
   return new Wrapper(value);
}
 
Example 12
Source File: ConversionTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public Cat read(InputNode source) throws Exception{
   int age = 0;
   String name = null;     
   while(true) {
      InputNode node = source.getNext();
      if(node == null) {
         break;
      }else if(node.getName().equals(ELEMENT_NAME)) {
         name = node.getValue();
      }else if(node.getName().equals(ELEMENT_AGE)){
         age = Integer.parseInt(node.getValue().trim());
      } 
   }
   return new Cat(name, age);
}
 
Example 13
Source File: CompositeValue.java    From simplexml with Apache License 2.0 5 votes vote down vote up
/**
 * This method is used to read the value object from the node. The 
 * value read from the node is resolved using the template filter.
 * If the value data can not be found according to the annotation 
 * attributes then null is assumed and returned.
 * 
 * @param node this is the node to read the value object from
 * 
 * @return this returns the value deserialized from the node
 */ 
public Object read(InputNode node) throws Exception { 
   InputNode next = node.getNext();
   Class expect = type.getType();
   
   if(next == null) {
      return null;
   }
   if(next.isEmpty()) {
      return null;
   }
   return root.read(next, expect);
}
 
Example 14
Source File: PrimitiveList.java    From simplexml with Apache License 2.0 5 votes vote down vote up
/**
 * This <code>populate</code> method wll read the XML element list 
 * from the provided node and deserialize its children as entry types.
 * This will deserialize each entry type as a primitive value. In
 * order to do this the parent string provided forms the element.
 * 
 * @param node this is the XML element that is to be deserialized
 * @param result this is the collection that is to be populated
 * 
 * @return this returns the item to attach to the object contact
 */ 
private Object populate(InputNode node, Object result) throws Exception {
   Collection list = (Collection) result;                 
   
   while(true) {
      InputNode next = node.getNext();
     
      if(next == null) {
         return list;
      }
      list.add(root.read(next));
   }
}
 
Example 15
Source File: EntityWithAnyElementConverter.java    From simpletask-android with GNU General Public License v3.0 4 votes vote down vote up
private void skipChildrenOfNode(InputNode node) throws Exception {
    while(node.getNext() != null) {
        // Do nothing
    }
}
 
Example 16
Source File: ConverterMapTest.java    From simplexml with Apache License 2.0 4 votes vote down vote up
private Entry readEntry(InputNode node) throws Exception {
   InputNode key = node.getAttribute("key");
   InputNode value = node.getNext("value");
   
   return new Entry(key.getValue(), value.getValue());
}
 
Example 17
Source File: PrimitiveValue.java    From simplexml with Apache License 2.0 3 votes vote down vote up
/**
 * This method is used to validate the value from the node. The 
 * value read from the node is resolved using the template filter.
 * If the value value can not be found according to the annotation
 * attributes then null is assumed and the node is valid.
 *  
 * @param node this is the node to read the value object from
 * @param key this is the name of the node to be validated
 * 
 * @return this returns true if the primitive key is valid
 */    
private boolean validateAttribute(InputNode node, String key) throws Exception {
   if(key != null) {
      key = style.getAttribute(key);
      node = node.getNext(key);
   }
   if(node == null) {
      return true;        
   }
   return root.validate(node);
}
 
Example 18
Source File: CompositeList.java    From simplexml with Apache License 2.0 3 votes vote down vote up
/**
 * This <code>validate</code> method will validate the XML element 
 * list from the provided node and deserialize its children as entry 
 * types. This takes each entry type and validates it as a root type, 
 * that is, its <code>Root</code> annotation must be present and the
 * name of the entry element must match that root element name.
 * 
 * @param node this is the XML element that is to be validated
 * @param type this is the type to validate against the input node
 * 
 * @return true if the element matches the XML schema class given 
 */ 
private boolean validate(InputNode node, Class type) throws Exception {
   while(true) {
      InputNode next = node.getNext();
      Class expect = entry.getType();
      
      if(next == null) {
         return true;
      }
      root.validate(next, expect);
   }
}
 
Example 19
Source File: PrimitiveKey.java    From simplexml with Apache License 2.0 3 votes vote down vote up
/**
 * This method is used to read the key value from the node. The 
 * value read from the node is resolved using the template filter.
 * If the key value can not be found according to the annotation
 * attributes then null is assumed and returned.
 *  
 * @param node this is the node to read the key value from
 * @param key this is the name of the element used by the key 
 *     
 * @return this returns the value deserialized from the node
 */
private Object readElement(InputNode node, String key) throws Exception {
   String name = style.getElement(key);
   InputNode child = node.getNext(name);
   
   if(child == null) {
      return null;
   }     
   return root.read(child);     
}
 
Example 20
Source File: PrimitiveValue.java    From simplexml with Apache License 2.0 3 votes vote down vote up
/**
 * This method is used to read the element value from the node. The 
 * value read from the node is resolved using the template filter.
 * If the value value can not be found according to the annotation
 * attributes then null is assumed and returned.
 * 
 * @param node this is the node to read the value object from
 * @param key this is the name of the value XML element
 * 
 * @return this returns the value deserialized from the node
 */ 
private Object readElement(InputNode node, String key) throws Exception {
   String name = style.getAttribute(key);
   InputNode child = node.getNext(name);
   
   if(child == null) {
      return null;        
   }
   return root.read(child);      
}