org.simpleframework.xml.stream.InputNode Java Examples

The following examples show how to use org.simpleframework.xml.stream.InputNode. 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: Composite.java    From simplexml with Apache License 2.0 6 votes vote down vote up
/**
 * This method checks to see if there are any <code>Label</code>
 * objects remaining in the provided map that are required. This is
 * used when deserialization is performed to ensure the the XML
 * element deserialized contains sufficient details to satisfy the
 * XML schema class annotations. If there is a required label that
 * remains it is reported within the exception thrown.
 * 
 * @param node this is the node that contains the contact value
 * @param map this is the map to check for remaining labels
 * @param source this is the object that has been deserialized 
 */
private void validate(InputNode node, LabelMap map, Object source) throws Exception {
   Class expect = context.getType(type, source);
   Position line = node.getPosition();

   for(Label label : map) {
      if(label.isRequired() && revision.isEqual()) {
         throw new ValueRequiredException("Unable to satisfy %s for %s at %s", label, expect, line);
      }
      Object value = label.getEmpty(context);
      
      if(value != null) {
         criteria.set(label, value);
      }
   }      
}
 
Example #2
Source File: Composite.java    From simplexml with Apache License 2.0 6 votes vote down vote up
/**
 * This method is used to read the version from the provided input
 * node. Once the version has been read it is used to determine how
 * to deserialize the object. If the version is not the initial
 * version then it is read in a manner that ignores excessive XML
 * elements and attributes. Also none of the annotated fields or
 * methods are required if the version is not the initial version.
 * 
 * @param node the XML element contact values are deserialized from
 * @param source this object whose contacts are to be deserialized
 * @param schema this object visits the objects contacts
 */
private void readVersion(InputNode node, Object source, Schema schema) throws Exception {
   Label label = schema.getVersion();
   Class expect = type.getType();
   
   if(label != null) {
      String name = label.getName();
      NodeMap<InputNode> map = node.getAttributes();
      InputNode value = map.remove(name);
      
      if(value != null) {
         readVersion(value, source, label);
      } else {
         Version version = context.getVersion(expect);
         Double start = revision.getDefault();
         Double expected = version.revision();
         
         criteria.set(label, start);
         revision.compare(expected, start);
      }
   }
}
 
Example #3
Source File: HackJobToGrabFloatingTextTest.java    From simplexml with Apache License 2.0 6 votes vote down vote up
public Something read(InputNode node) throws Exception {
   Object source = node.getSource();
   Element element = (Element)source;
   NodeList elements = element.getChildNodes();
   String child1 = null;
   String child2 = null;
   String text = "";
   for(int i = 0; i < elements.getLength(); i++) {
      Node next = elements.item(i);
      if(next.getNodeType() == Node.TEXT_NODE) {
         text += next.getNodeValue();
      }
      if(next.getNodeType() == Node.ELEMENT_NODE) {
         if(next.getNodeName().equals("child1")) {
            child1 = next.getTextContent();
         }
         if(next.getNodeName().equals("child2")) {
            child2 = next.getTextContent();
         }
      }
   }
   return new Something(child1, child2, text.trim());
}
 
Example #4
Source File: AnnotationStrategy.java    From simplexml with Apache License 2.0 6 votes vote down vote up
/**
 * This is used to read the <code>Value</code> which will be used 
 * to represent the deserialized object. If there is an annotation
 * present then the value will contain an object instance. If it
 * does not then it is up to the internal strategy to determine 
 * what the returned value contains.
 * 
 * @param type this is the type that represents a method or field
 * @param node this is the node representing the XML element
 * @param value this is the value from the internal strategy
 * 
 * @return the value representing the deserialized value
 */
private Value read(Type type, NodeMap<InputNode> node, Value value) throws Exception {
   Converter converter = scanner.getConverter(type, value);
   InputNode parent = node.getNode();
   
   if(converter != null) {
      Object data = converter.read(parent);
      Class actual = type.getType();
      
      if(value != null) {
         value.setValue(data);
      }
      return new Reference(value, data, actual);
   }
   return value;
}
 
Example #5
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 #6
Source File: PrimitiveListTest.java    From simplexml with Apache License 2.0 6 votes vote down vote up
public void testTwo() throws Exception {
   Context context = new Source(new TreeStrategy(), new Support(), new Session());
   PrimitiveList primitive = new PrimitiveList(context, new ClassType(List.class), new ClassType(String.class), "entry");
   InputNode node = NodeBuilder.read(new StringReader(TWO));
   Object value = primitive.read(node);
   
   assertEquals(value.getClass(), Vector.class);
   
   Vector vector = (Vector) value;
   
   assertEquals(vector.get(0), "one");
   assertEquals(vector.get(1), "two");
   
   InputNode newNode = NodeBuilder.read(new StringReader(TWO));
   
   assertTrue(primitive.validate(newNode)); 
}
 
Example #7
Source File: EntityWithAnyElementConverter.java    From simpletask-android with GNU General Public License v3.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 #8
Source File: Composite.java    From simplexml with Apache License 2.0 6 votes vote down vote up
/**
 * This <code>readElement</code> method is used for deserialization
 * of the provided node object using a delegate converter. This is
 * typically another <code>Composite</code> converter, or if the
 * node is an attribute a <code>Primitive</code> converter. When
 * the delegate converter has completed the deserialized value is
 * assigned to the contact.
 * 
 * @param node this is the node that contains the contact value
 * @param source the type of the object that is being deserialized
 * @param section this is the section to read the element from
 * @param map this is the map that contains the label objects
 */
private void readElement(InputNode node, Object source, Section section, LabelMap map) throws Exception {
   String name = node.getName();
   String path = section.getPath(name); 
   Label label = map.getLabel(path);      

   if(label == null) {
      label = criteria.resolve(path);
   }
   if(label == null) {
      Position line = node.getPosition();
      Class expect = context.getType(type, source);
      
      if(map.isStrict(context) && revision.isEqual()) {              
         throw new ElementException("Element '%s' does not have a match in %s at %s", path, expect, line);
      } else {
         node.skip();                 
      }
   } else {
      readUnion(node, source, map, label);
   }         
}
 
Example #9
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 #10
Source File: CompositeArray.java    From simplexml with Apache License 2.0 6 votes vote down vote up
/**
 * This <code>read</code> method will read the XML element list from
 * the provided node and deserialize its children as entry types.
 * This ensures each entry type is deserialized 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 deserialized
 * @param list this is the array that is to be deserialized
 * 
 * @return this returns the item to attach to the object contact
 */  
public Object read(InputNode node, Object list) throws Exception{
   int length = Array.getLength(list);
    
   for(int pos = 0; true; pos++) {
      Position line = node.getPosition();
      InputNode next = node.getNext();
     
      if(next == null) {
         return list;
      }
      if(pos >= length){
          throw new ElementException("Array length missing or incorrect for %s at %s", type, line);
      }
      read(next, list, pos);
   } 
}
 
Example #11
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 #12
Source File: PropertyValueXmlAdapter.java    From openkeepass with Apache License 2.0 5 votes vote down vote up
private boolean isProtected(InputNode node) throws Exception {
    InputNode isProtectedNode = node.getAttribute("Protected");
    if (isProtectedNode == null) {
        return false;
    }

    String value = isProtectedNode.getValue();
    return value.equalsIgnoreCase("true");
}
 
Example #13
Source File: PrimitiveList.java    From simplexml with Apache License 2.0 5 votes vote down vote up
/**
 * This <code>validate</code> method wll validate the XML element list 
 * from the provided node and validate its children as entry types.
 * This will validate 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
 * 
 * @return true if the element matches the XML schema class given 
 */ 
public boolean validate(InputNode node) throws Exception{
   Instance value = factory.getInstance(node);
   
   if(!value.isReference()) {
      Object result = value.setInstance(null);
      Class expect = value.getType();
         
      return validate(node, expect);
   }
   return true;      

}
 
Example #14
Source File: TimeConverter.java    From KeePassJava2 with Apache License 2.0 5 votes vote down vote up
@Override
public Date read(InputNode inputNode) throws Exception {
    String value = inputNode.getValue();
    if (value.equals("${creationDate}")) {
        return new Date();
    }
    return Helpers.toDate(value);
}
 
Example #15
Source File: EmptyElementConverterTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public Object read(InputNode node) throws Exception {
   String value = node.getValue();
   
   if(value == null) {
      return "";
   }
   return value;
}
 
Example #16
Source File: CompositeArray.java    From simplexml with Apache License 2.0 5 votes vote down vote up
/**
 * This <code>validate</code> method wll validate the XML element 
 * list against the provided node and validate its children as entry 
 * types. This ensures each entry type is validated 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 array type used to create the array
 * 
 * @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(!next.isEmpty()) {
         root.validate(next, type);
      }
   }
}
 
Example #17
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 #18
Source File: Composite.java    From simplexml with Apache License 2.0 5 votes vote down vote up
/**
 * This <code>validate</code> method is used to perform validation
 * of the provided node object using a delegate converter. This is
 * typically another <code>Composite</code> converter, or if the
 * node is an attribute a <code>Primitive</code> converter. If this
 * fails validation then an exception is thrown to report the issue.
 * 
 * @param node this is the node that contains the contact value
 * @param label this is the label used to create the converter
 */
private void validate(InputNode node, Label label) throws Exception {    
   Converter reader = label.getConverter(context);      
   Position line = node.getPosition();
   Class expect = type.getType();
   boolean valid = reader.validate(node);
 
   if(valid == false) {     
     throw new PersistenceException("Invalid value for %s in %s at %s", label, expect, line);
   }
   criteria.set(label, null);
}
 
Example #19
Source File: ObjectFactory.java    From simplexml with Apache License 2.0 5 votes vote down vote up
/**
 * This method will instantiate an object of the field type, or if
 * the <code>Strategy</code> object can resolve a class from the
 * XML element then this is used instead. If the resulting type is
 * abstract or an interface then this method throws an exception.
 * 
 * @param node this is the node to check for the override
 * 
 * @return this returns an instance of the resulting type
 */       
@Override
public Instance getInstance(InputNode node) throws Exception {
   Value value = getOverride(node);
   Class expect = getType();
 
   if(value == null) { 
      if(!isInstantiable(expect)) {
         throw new InstantiationException("Cannot instantiate %s for %s", expect, type);              
      }
      return context.getInstance(expect);         
   }
   return new ObjectInstance(context, value);      
}
 
Example #20
Source File: Property.java    From simpletask-android with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Property read(InputNode node) throws Exception {
	Property property = new Property();
	InputNode childNode = node.getNext();
	if (childNode != null) {
		property.setProperty(ElementConverter.read(childNode));
	}
	return property;
}
 
Example #21
Source File: Composite.java    From simplexml with Apache License 2.0 5 votes vote down vote up
/**
 * The <code>readResolve</code> method is used to determine if there 
 * is a resolution method which can be used to substitute the object
 * deserialized. The resolve method is used when an object wishes 
 * to provide a substitute within the deserialized object graph.
 * This acts as an equivalent to the Java Object Serialization
 * <code>readResolve</code> method for the object deserialization.
 * 
 * @param node the XML element object provided as a replacement
 * @param source the type of the object that is being deserialized
 * @param caller this is used to invoke the callback methods
 * 
 * @return this returns a replacement for the deserialized object
 */
private Object readResolve(InputNode node, Object source, Caller caller) throws Exception {
   if(source != null) {
      Position line = node.getPosition();
      Object value = caller.resolve(source);
      Class expect = type.getType();
      Class real = value.getClass();
   
      if(!expect.isAssignableFrom(real)) {
         throw new ElementException("Type %s does not match %s at %s", real, expect, line);              
      }
      return value;
   }
   return source;
}
 
Example #22
Source File: PrimitiveValue.java    From simplexml with Apache License 2.0 5 votes vote down vote up
/**
 * This method is used to read the value 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 an exception is thrown.
 * 
 * @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 {
   Class expect = type.getType();
   String name = entry.getValue();
   
   if(!entry.isInline()) {
      if(name == null) {
         name = context.getName(expect);
      }
      return readElement(node, name);
   }
   return readAttribute(node, name);
}
 
Example #23
Source File: Variable.java    From simplexml with Apache License 2.0 5 votes vote down vote up
/**
 * This <code>read</code> method will perform a read using the
 * provided object with the repeater. Reading with this method
 * ensures that any additional XML elements within the source
 * will be added to the value.
 * 
 *  @param node this is the node that contains the extra data
 *  
 *  @return this will return the original deserialized object
 */
public boolean validate(InputNode node) throws Exception {
   Position line = node.getPosition();
   String name = node.getName();         
   
   if(reader instanceof Repeater) {
      Repeater repeat = (Repeater) reader;
      
      return repeat.validate(node);
   }
   throw new PersistenceException("Element '%s' declared twice at %s", name, line);
}
 
Example #24
Source File: ValidationTestCase.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public void read(Type type, NodeMap<InputNode> node){
   InputNode element = node.getNode();
   if(element.isRoot()) {
      Object source = element.getSource();
      Class sourceType = source.getClass();
      Class itemType = type.getType();
      System.out.printf(">>>>> ELEMENT=[%s]%n>>>>> TYPE=[%s]%n>>>>> SOURCE=[%s]%n", element, itemType, sourceType);
   }
}
 
Example #25
Source File: PrimitiveKey.java    From simplexml with Apache License 2.0 5 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 the node is considered as null and is valid.
 * 
 * @param node this is the node to read the key value from
 * 
 * @return this returns the value deserialized from the node
 */   
public boolean validate(InputNode node) throws Exception {
   Class expect = type.getType();
   String name = entry.getKey();
        
   if(name == null) {
      name = context.getName(expect);
   }
   if(!entry.isAttribute()) {         
      return validateElement(node, name);
   }
   return validateAttribute(node, name);
}
 
Example #26
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 #27
Source File: CompositeList.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 each entry type is deserialized 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 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();
      Class expect = entry.getType();
     
      if(next == null) {
         return list;
      }
      list.add(root.read(next, expect));
   }
}
 
Example #28
Source File: CompositeList.java    From simplexml with Apache License 2.0 5 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
 * 
 * @return true if the element matches the XML schema class given 
 */ 
public boolean validate(InputNode node) throws Exception{
   Instance value = factory.getInstance(node);
   
   if(!value.isReference()) {
      Object result = value.setInstance(null);
      Class type = value.getType();
         
      return validate(node, type);
   }
   return true; 
}
 
Example #29
Source File: CompositeListUnionTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public void testSimpleList() throws Exception {
   Context context = getContext();
   Group group = getGroup();
   Type type = new ClassType(CompositeListUnionTest.class);
   List<Shape> list = new ArrayList<Shape>();
   Expression expression = new PathParser("some/path", type, new Format());
   CompositeListUnion union = new CompositeListUnion(
         context,
         group,
         expression,
         type);
   InputNode node = NodeBuilder.read(new StringReader(SOURCE));
   
   InputNode circle = node.getNext();
   union.read(circle, list);
   
   InputNode square = node.getNext();
   union.read(square, list);
   
   InputNode triangle = node.getNext();
   union.read(triangle, list);
   
   assertEquals(list.get(0).getClass(), Circle.class);
   assertEquals(list.get(1).getClass(), Square.class);
   assertEquals(list.get(2).getClass(), Triangle.class);
   
   OutputNode output = NodeBuilder.write(new PrintWriter(System.out), new Format(3));
   OutputNode parent = output.getChild("parent");
   
   union.write(parent, list);
}
 
Example #30
Source File: PrimitiveValue.java    From simplexml with Apache License 2.0 5 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
 * 
 * @return this returns true if the primitive key is valid
 */ 
public boolean validate(InputNode node) throws Exception {
   Class expect = type.getType();
   String name = entry.getValue();
   
   if(!entry.isInline()) {
      if(name == null) {
         name = context.getName(expect);
      }
      return validateElement(node, name);
   }
   return validateAttribute(node, name);
}