Java Code Examples for org.simpleframework.xml.strategy.Type#getType()

The following examples show how to use org.simpleframework.xml.strategy.Type#getType() . 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: 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 2
Source File: RegistryStrategy.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 binding
 * 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 = lookup(type, value);
   InputNode source = node.getNode();
   
   if(converter != null) {
      Object data = converter.read(source);
      Class actual = type.getType();
   
      if(value != null) {
         value.setValue(data);
      }
      return new Reference(value, data, actual);
   }
   return value;
}
 
Example 3
Source File: TreeStrategyWithoutArrayLength.java    From openkeepass with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
@Override
public boolean write(Type type, Object value, NodeMap node, Map map){
    Class actual = value.getClass();
    Class expect = type.getType();
    Class real = actual;
    
    if(actual != expect) {
       node.put("class", real.getName());
    }       
    return false;
 }
 
Example 4
Source File: ElementsStrategyCallbackTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public void write(Type type, NodeMap<OutputNode> node) throws Exception {
   Class key = type.getType();
   String name = binding.get(key);
   if(name != null) {
      node.put("type", name);
   }
}
 
Example 5
Source File: ConversionTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public Value read(Type field, NodeMap<InputNode> node, Map map) throws Exception {
   Value value = strategy.read(field, node, map);
   Class type = value == null ? field.getType() : value.getType();
   Converter converter = registry.resolve(type);
   if(converter != null) {
      InputNode source = node.getNode();
      Object data = converter.read(source);
      return new Wrapper(value, data);
   }
   return value;
}
 
Example 6
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 7
Source File: Introspector.java    From simplexml with Apache License 2.0 5 votes vote down vote up
/**
 * This method is used to get the entry name of a label using 
 * the type of the label. This ensures that if there is no
 * entry XML element name declared by the annotation that a
 * suitable name can be calculated from the annotated type.
 * 
 * @return this returns a suitable XML entry element name
 */
public String getEntry() throws Exception {
   Type depend = getDependent();   
   Class type = depend.getType();
   
   if(type.isArray()) {
      type = type.getComponentType();
   }
   return getName(type);
}
 
Example 8
Source File: StructureBuilder.java    From simplexml with Apache License 2.0 5 votes vote down vote up
/**
 * This is used to validate the configuration of the scanned class.
 * If an <code>ElementListUnion</code> annotation has been used with 
 * a <code>Text</code> annotation this validates to ensure there are
 * no other elements declared and no <code>Path</code> annotations 
 * have been used, which ensures free text can be processed.
 * 
 * @param type this is the object type that is being scanned
 */
private void validateTextList(Class type) throws Exception {
   Label label = root.getText();
   
   if(label != null) {
      if(label.isTextList()) {
         Object key = label.getKey();
         
         for(Label element : elements) {
            Object identity = element.getKey();
            
            if(!identity.equals(key)) {
               throw new TextException("Elements used with %s in %s", label, type);
            }
            Type dependent = element.getDependent();
            Class actual = dependent.getType();
            
            if(actual == String.class) {
               throw new TextException("Illegal entry of %s with text annotations on %s in %s", actual, label, type);
            }
         }
         if(root.isComposite()) {
            throw new TextException("Paths used with %s in %s", label, type);
         }
      }
   } 
}
 
Example 9
Source File: StrategyTest.java    From simplexml with Apache License 2.0 4 votes vote down vote up
public boolean write(Type field, Object value, NodeMap node, Map map) throws Exception {            
   if(field.getType() != value.getClass()) {                       
      node.put(ELEMENT_NAME, value.getClass().getName());
   }  
   return false;
}
 
Example 10
Source File: Primitive.java    From simplexml with Apache License 2.0 3 votes vote down vote up
/**
 * Constructor for the <code>Primitive</code> object. This is used
 * to convert an XML node to a primitive object and vice versa. To
 * perform deserialization the primitive object requires the context
 * object used for the instance of serialization to performed.
 *
 * @param context the context object used for the serialization
 * @param type this is the type of primitive this represents
 * @param empty this is the value used to represent a null value
 */ 
public Primitive(Context context, Type type, String empty) {
   this.factory = new PrimitiveFactory(context, type);  
   this.expect = type.getType();
   this.context = context; 
   this.empty = empty;     
   this.type = type;     
}
 
Example 11
Source File: Composite.java    From simplexml with Apache License 2.0 3 votes vote down vote up
/**
 * This is used to apply <code>Decorator</code> objects to the
 * provided node before it is written. Application of decorations
 * before the node is written allows namespaces and comments to be
 * applied to the node. Decorations such as this do not affect the
 * overall structure of the XML that is written.
 * 
 * @param node this is the node that decorations are applied to
 * @param type this is the type to acquire the decoration for
 * @param label this contains the primary decorator to be used
 */
private void writeNamespaces(OutputNode node, Type type, Label label) throws Exception {
   Class expect = type.getType();
   Decorator primary = context.getDecorator(expect);
   Decorator decorator = label.getDecorator();
   
   decorator.decorate(node, primary);
}
 
Example 12
Source File: Source.java    From simplexml with Apache License 2.0 3 votes vote down vote up
/**
 * This is used to determine the type of an object given the 
 * source instance. To provide a best match approach this will
 * first attempt to get the value for the actual instance, if
 * however the instance is null the type is delegated to.
 * 
 * @param type this is the type used in the serialization
 * @param value this is the source instance being used
 * 
 * @return the best match given the criteria
 */
public Class getType(Type type, Object value) {
   if(value != null) {
      return value.getClass();
   }
   return type.getType();
}
 
Example 13
Source File: Factory.java    From simplexml with Apache License 2.0 3 votes vote down vote up
/**
 * This method is used to set the override class within an element.
 * This delegates to the <code>Strategy</code> implementation, which
 * depending on the implementation may add an attribute of a child
 * element to describe the type of the object provided to this.
 * 
 * @param type this is the class of the field type being serialized
 * @param node the XML element that is to be given the details
 *
 * @throws Exception thrown if an error occurs within the strategy
 */
public boolean setOverride(Type type, Object value, OutputNode node) throws Exception {
   Class expect = type.getType();
   
   if(expect.isPrimitive()) {
      type = getPrimitive(type, expect);
   }
   return context.setOverride(type, value, node);
}
 
Example 14
Source File: ConverterScanner.java    From simplexml with Apache License 2.0 3 votes vote down vote up
/**
 * This is used to acquire the class that should be scanned. The
 * type is found either on the method or field, or should there
 * be a subtype then the class is taken from the provided value.
 * 
 * @param type this is the type representing the field or method
 * @param value this contains the type if it was overridden
 * 
 * @return this returns the class that has been scanned
 */
private Class getType(Type type, Object value) {
   Class real = type.getType();
   
   if(value != null) {
      return value.getClass();
   }
   return real;
}
 
Example 15
Source File: ConverterScanner.java    From simplexml with Apache License 2.0 3 votes vote down vote up
/**
 * This is used to acquire the class that should be scanned. The
 * type is found either on the method or field, or should there
 * be a subtype then the class is taken from the provided value.
 * 
 * @param type this is the type representing the field or method
 * @param value this contains the type if it was overridden
 * 
 * @return this returns the class that has been scanned
 */
private Class getType(Type type, Value value) {
   Class real = type.getType();
   
   if(value != null) {
      return value.getType();
   }
   return real;
}
 
Example 16
Source File: RegistryStrategy.java    From simplexml with Apache License 2.0 3 votes vote down vote up
/**
 * This is used to acquire a <code>Converter</code> instance for 
 * the provided object instance. The instance class is used to
 * resolve the converter to use for the serialization process.
 * 
 * @param type this is the type representing the field or method
 * @param value this is the value that is to be serialized
 * 
 * @return this returns the converter instance that is matched
 */
private Converter lookup(Type type, Object value) throws Exception {
   Class real = type.getType();
   
   if(value != null) {
      real = value.getClass();
   }
   return registry.lookup(real);
}
 
Example 17
Source File: RegistryStrategy.java    From simplexml with Apache License 2.0 3 votes vote down vote up
/**
 * This is used to acquire a <code>Converter</code> instance for 
 * the provided value object. The value object is used to resolve
 * the converter to use for the serialization process.
 * 
 * @param type this is the type representing the field or method
 * @param value this is the value that is to be serialized
 * 
 * @return this returns the converter instance that is matched
 */
private Converter lookup(Type type, Value value) throws Exception {
   Class real = type.getType();
   
   if(value != null) {
      real = value.getType();
   }
   return registry.lookup(real);
}