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

The following examples show how to use org.simpleframework.xml.stream.InputNode#getPosition() . 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 <code>readAttribute</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 attribute from
 * @param map this is the map that contains the label objects
 */
private void readAttribute(InputNode node, Object source, Section section, LabelMap map) throws Exception {
   String name = node.getName();
   String path = section.getAttribute(name);
   Label label = map.getLabel(path);
   
   if(label == null) {
      Position line = node.getPosition();
      Class expect = context.getType(type, source);

      if(map.isStrict(context) && revision.isEqual()) {              
         throw new AttributeException("Attribute '%s' does not have a match in %s at %s", path, expect, line);
      }            
   } else {
      readInstance(node, source, label);
   }         
}
 
Example 2
Source File: PrimitiveArray.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 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 array to read the array values in to
 * 
 * @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);
      }
      Array.set(list, pos, root.read(next));
   } 
}
 
Example 3
Source File: Composite.java    From simplexml with Apache License 2.0 6 votes vote down vote up
/**
 * This <code>validateElement</code> method performs a 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 section this is the section to validate this element in
 * @param map this is the map that contains the label objects
 */
private void validateElement(InputNode node, 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 = type.getType();
      
      if(map.isStrict(context) && revision.isEqual()) {              
         throw new ElementException("Element '%s' does not exist for %s at %s", path, expect, line);
      } else {
         node.skip();                 
      }
   } else {
      validateUnion(node, map, label);
   }         
}
 
Example 4
Source File: Composite.java    From simplexml with Apache License 2.0 6 votes vote down vote up
/**
 * This <code>validateAttribute</code> method performs a 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 section this is the section to validate this attribute in
 * @param map this is the map that contains the label objects
 */
private void validateAttribute(InputNode node, Section section, LabelMap map) throws Exception {
   Position line = node.getPosition();
   String name = node.getName();
   String path = section.getAttribute(name);
   Label label = map.getLabel(path);
   
   if(label == null) {
      Class expect = type.getType();
      
      if(map.isStrict(context) && revision.isEqual()) {              
         throw new AttributeException("Attribute '%s' does not exist for %s at %s", path, expect, line);
      }            
   } else {
      validate(node, label);
   }         
}
 
Example 5
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 6
Source File: Composite.java    From simplexml with Apache License 2.0 6 votes vote down vote up
/**
 * This <code>read</code> method is used to perform 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 label this is the label used to create the converter
 */
private Object readInstance(InputNode node, Object source, Label label) throws Exception {    
   Object object = readVariable(node, source, label);
 
   if(object == null) {     
      Position line = node.getPosition();
      Class expect = context.getType(type, source);
      
      if(label.isRequired() && revision.isEqual()) {              
         throw new ValueRequiredException("Empty value for %s in %s at %s", label, expect, line);
      }
   } else {
      if(object != label.getEmpty(context)) {  
         criteria.set(label, object);
      }
   }
   return object;
}
 
Example 7
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 8
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 9
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 10
Source File: Factory.java    From simplexml with Apache License 2.0 5 votes vote down vote up
/**
 * This is used to get a possible override from the provided node.
 * If the node provided is an element then this checks for a  
 * specific class override using the <code>Strategy</code> object.
 * If the strategy cannot resolve a class then this will return 
 * null. If the resolved <code>Value</code> is not assignable to 
 * the field then this will thrown an exception.
 * 
 * @param node this is the node used to search for the override
 * 
 * @return this returns null if no override type can be found
 * 
 * @throws Exception if the override type is not compatible
 */
protected Value getOverride(InputNode node) throws Exception {
   Value value = getConversion(node);      

   if(value != null) {
      Position line = node.getPosition();
      Class proposed = value.getType();
      Class expect = getType();
  
      if(!isCompatible(expect, proposed)) {
         throw new InstantiationException("Incompatible %s for %s at %s", proposed, type, line);              
      }
   }         
   return value; 
}
 
Example 11
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 12
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 Object read(InputNode node, Object value) throws Exception {
   Position line = node.getPosition();
   String name = node.getName();         
   
   if(reader instanceof Repeater) {
      Repeater repeat = (Repeater) reader;
      
      return repeat.read(node, value);
   }
   throw new PersistenceException("Element '%s' is already used with %s at %s", name, label, line);
}
 
Example 13
Source File: CompositeKey.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 null is assumed and the node 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 { 
   Position line = node.getPosition();
   Class expect = type.getType();
   String name = entry.getKey();
   
   if(name == null) {
      name = context.getName(expect);
   }
   if(entry.isAttribute()) {
      throw new ElementException("Can not have %s as an attribute for %s at %s", expect, entry, line);
   }
   return validate(node, name);
}
 
Example 14
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 15
Source File: Composite.java    From simplexml with Apache License 2.0 5 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 validation is performed to ensure the the XML element 
 * validated 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 composite data
 * @param map this contains the converters to perform validation
 */
private void validate(InputNode node, LabelMap map) throws Exception {     
   Position line = node.getPosition();

   for(Label label : map) {
      Class expect = type.getType();
      
      if(label.isRequired() && revision.isEqual()) {
         throw new ValueRequiredException("Unable to satisfy %s for %s at %s", label, expect, line);
      }
   }      
}
 
Example 16
Source File: CompositeKey.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 null is assumed and returned.
 * 
 * @param node this is the node to read the key value from
 * 
 * @return this returns the value deserialized from the node
 */ 
public Object read(InputNode node) throws Exception { 
   Position line = node.getPosition();
   Class expect = type.getType();
   String name = entry.getKey();
   
   if(name == null) {
      name = context.getName(expect);
   }
   if(entry.isAttribute()) {
      throw new AttributeException("Can not have %s as an attribute for %s at %s", expect, entry, line);
   }
   return read(node, name);
}
 
Example 17
Source File: CompositeKey.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 value this is the value to deserialize in to
 * 
 * @return this returns the value deserialized from the node
 * 
 * @throws Exception if value is not null an exception is thrown
 */ 
public Object read(InputNode node, Object value) throws Exception {
   Position line = node.getPosition();
   Class expect = type.getType();
   
   if(value != null) {
      throw new PersistenceException("Can not read key of %s for %s at %s", expect, entry, line);
   }
   return read(node);
}