org.simpleframework.xml.strategy.Type Java Examples

The following examples show how to use org.simpleframework.xml.strategy.Type. 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 write method is used to append the provided object as an
 * element to the given XML element object. This will recursively
 * write the contacts from the provided object as elements. This is
 * done using the <code>Converter</code> acquired from the contact
 * label. If the type of the contact value is not of the same
 * type as the XML schema class a "class" attribute is appended.
 * <p>
 * If the element being written is inline, then this will not 
 * check to see if there is a "class" attribute specifying the
 * name of the class. This is because inline elements do not have
 * an outer class and thus could never have an override.
 * 
 * @param value this is the value to be set as an element
 * @param node this is the XML element to write the element to
 * @param label the label that contains the contact details
 */
private void writeElement(OutputNode node, Object value, Label label) throws Exception {
   if(value != null) {
      Class real = value.getClass();
      Label match = label.getLabel(real);
      String name = match.getName();
      Type type = label.getType(real); 
      OutputNode next = node.getChild(name);

      if(!match.isInline()) {
         writeNamespaces(next, type, match);
      }
      if(match.isInline() || !isOverridden(next, value, type)) {
         Converter convert = match.getConverter(context);
         boolean data = match.isData();
         
         next.setData(data);
         writeElement(next, value, convert);
      }
   }
}
 
Example #2
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 #3
Source File: AliasTest.java    From simplexml with Apache License 2.0 6 votes vote down vote up
public boolean write(Type field, Object value, NodeMap<OutputNode> node, Map map) throws Exception {
    boolean done = strategy.write(field, value, node, map);
    Node entry = node.remove("class");
    
    if(entry != null) {
        String className = entry.getValue();
        Class type = Class.forName(className);
        String name = forward.get(type);

        if(name == null) {
            throw new PersistenceException("Could not find alias for class %s", className);
        }
        node.put("type", name);
    }
    return done;
}
 
Example #4
Source File: PathParser.java    From simplexml with Apache License 2.0 5 votes vote down vote up
/**
 * Constructor for the <code>PathParser</code> object. This must
 * be given a valid XPath expression. Currently only a subset of
 * the XPath syntax is supported by this parser. Once finished
 * the parser will contain all the extracted path segments.
 * 
 * @param path this is the XPath expression to be parsed
 * @param type this is the type the expressions are parsed for
 * @param format this is the format used to style the path
 */
public PathParser(String path, Type type, Format format) throws Exception {
   this.attributes = new ConcurrentCache<String>();
   this.elements = new ConcurrentCache<String>();
   this.indexes = new ArrayList<Integer>();
   this.prefixes = new ArrayList<String>();
   this.names = new ArrayList<String>();
   this.builder = new StringBuilder();
   this.style = format.getStyle();
   this.type = type;
   this.path = path;
   this.parse(path);
}
 
Example #5
Source File: VersionLabel.java    From simplexml with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a <code>Converter</code> that can convert an attribute
 * to a double value. This requires the context object used for
 * the current instance of XML serialization being performed.
 * 
 * @param context this is context object used for serialization
 * 
 * @return this returns the converted for this attribute object
 */
public Converter getConverter(Context context) throws Exception {
   String ignore = getEmpty(context);
   Type type = getContact();
   
   if(!context.isFloat(type)) {
      throw new AttributeException("Cannot use %s to represent %s", label, type);
   }
   return new Primitive(context, type, ignore);
}
 
Example #6
Source File: AliasTest.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 {
    Node entry = node.remove("type");
    
    if(entry != null) {
        String value = entry.getValue();
        Class type = backward.get(value);
        
        if(type == null) {
            throw new PersistenceException("Could not find class for alias %s", value);
        }
        node.put("class", type.getName());
    }
    return strategy.read(field, node, map);
}
 
Example #7
Source File: PathParserTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public void testStyledPathWithPrefixes() throws Exception {
   Type type = new ClassType(PathParserTest.class);
   Expression expression = new PathParser("pre:this/is/a/pre:path", type, new Format(new CamelCaseStyle()));
   String attributePath = expression.getAttribute("final");
   assertEquals(attributePath, "pre:This[1]/Is[1]/A[1]/pre:Path[1]/@final");
   String elementPath = expression.getElement("final");
   assertEquals(elementPath, "pre:This[1]/Is[1]/A[1]/pre:Path[1]/Final[1]");
}
 
Example #8
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 #9
Source File: Entry.java    From simplexml with Apache License 2.0 5 votes vote down vote up
/**
 * This is used to acquire the dependent value for the annotated
 * map. This will simply return the type that the map object is
 * composed to hold. This must be a serializable type, that is,
 * it must be a composite or supported primitive type.
 * 
 * @return this returns the value object type for the map object
 */
protected Type getValueType() throws Exception {
   if(valueType == null) {
      valueType = label.valueType();
      
      if(valueType == void.class) {
         valueType = getDependent(1);
      }
   }
   return new ClassType(valueType);
}
 
Example #10
Source File: ExpressionBuilder.java    From simplexml with Apache License 2.0 5 votes vote down vote up
/**
 * This is used to create an <code>Expression</code> from the
 * provided path. If the path does not conform to the syntax
 * supported then an exception is thrown to indicate the error.
 * 
 * @param path this is the XPath expression to be parsed
 * 
 * @return this returns the resulting expression object
 */
private Expression create(String path) throws Exception {
   Type detail = new ClassType(type);
   Expression expression = new PathParser(path, detail, format);
   
   if(cache != null) {
      cache.cache(path, expression);
   }
   return expression;
}
 
Example #11
Source File: EmptyExpressionTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public void testEmptyPath() throws Exception {
	Format format = new Format();
	Type type = new ClassType(EmptyExpressionTest.class);
	Expression path = new PathParser(".", type, format);
	Expression empty = new EmptyExpression(format);
	
	assertEquals(path.isEmpty(), empty.isEmpty());
	assertEquals(path.isAttribute(), empty.isAttribute());
	assertEquals(path.isPath(), empty.isPath());
	assertEquals(path.getAttribute("a"), empty.getAttribute("a"));
	assertEquals(path.getElement("a"), empty.getElement("a"));
	assertEquals(path.getPath(), empty.getPath());
}
 
Example #12
Source File: ElementUnionLabel.java    From simplexml with Apache License 2.0 5 votes vote down vote up
/**
 * This is used to acquire the <code>Type</code> that the type
 * provided is represented by. Typically this will return the
 * field or method represented by the label. However, in the 
 * case of unions this will provide an override type.
 * 
 * @param type this is the class to acquire the type for
 * 
 * @return this returns the type represented by this class
 */
public Type getType(Class type) throws Exception {
   Type contact = getContact();
   
   if(!extractor.isValid(type)) {
      throw new UnionException("No type matches %s in %s for %s", type, union, contact);
   }
   if(extractor.isDeclared(type)) {
 	  return new OverrideType(contact, type);
   }
   return contact;
}
 
Example #13
Source File: ElementListLabel.java    From simplexml with Apache License 2.0 5 votes vote down vote up
/**
 * This is used to acquire the dependent type for the annotated
 * list. This will simply return the type that the collection is
 * composed to hold. This must be a serializable type, that is,
 * a type that is annotated with the <code>Root</code> class.
 * 
 * @return this returns the component type for the collection
 */
public Type getDependent() throws Exception  {      
   Contact contact = getContact();
  
   if(item == void.class) {
      item = contact.getDependent();
   }        
   if(item == null) {
      throw new ElementException("Unable to determine generic type for %s", contact);           
   }     
   return new ClassType(item);
}
 
Example #14
Source File: PathParserTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public void testEmptyPath() throws Exception {
   Type type = new ClassType(PathParserTest.class);
   Expression expression = new PathParser( ".", type, new Format());
   String element = expression.getElement("a");
   assertEquals(expression.getPath(), "");
   assertEquals(element, "a");
   String attribute = expression.getAttribute("a");
   assertEquals(attribute, "a");
   assertTrue(expression.isEmpty());
   Expression single = new PathParser("name", type, new Format());
   Expression empty = single.getPath(1);
   assertTrue(empty.isEmpty());
}
 
Example #15
Source File: ConversionTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public boolean write(Type field, Object value, NodeMap<OutputNode> node, Map map) throws Exception {
   boolean reference = strategy.write(field, value, node, map);
   if(!reference) {
      Class type = value.getClass();
      Converter converter = registry.resolve(type);
      OutputNode source = node.getNode();
      if(converter != null) {
         converter.write(source, value);
         return true;
      }
      return false;
   }
   return reference;
}
 
Example #16
Source File: ElementMapLabel.java    From simplexml with Apache License 2.0 5 votes vote down vote up
/**
 * This is used to acquire the dependent type for the annotated
 * list. This will simply return the type that the map object is
 * composed to hold. This must be a serializable type, that is,
 * a type that is annotated with the <code>Root</code> class.
 * 
 * @return this returns the component type for the map object
 */
public Type getDependent() throws Exception  {
   Contact contact = getContact();
  
   if(items == null) {
      items = contact.getDependents();
   }        
   if(items == null) {
      throw new ElementException("Unable to determine type for %s", contact);           
   }    
   if(items.length == 0) {
      return new ClassType(Object.class);
   }
   return new ClassType(items[0]);
}
 
Example #17
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 #18
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 #19
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 #20
Source File: PathParserTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public void testStyledPath() throws Exception {
   Type type = new ClassType(PathParserTest.class);
   Expression expression = new PathParser( "this/is/a/path", type, new Format(new CamelCaseStyle()));
   String attributePath = expression.getAttribute("final");
   assertEquals(attributePath, "This[1]/Is[1]/A[1]/Path[1]/@final");
   String elementPath = expression.getElement("final");
   assertEquals(elementPath, "This[1]/Is[1]/A[1]/Path[1]/Final[1]");
}
 
Example #21
Source File: StrategyTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public Value read(Type field, NodeMap node, Map map) throws Exception {
   Node value = node.remove(ELEMENT_NAME);
   
   if(value == null) {
  	 return null;
   }
   String name = value.getValue();
   Class type = Class.forName(name);
   
   return new SimpleType(type);
}
 
Example #22
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 #23
Source File: UARTActivity.java    From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void read(final Type type, final NodeMap<InputNode> node) {
	// do nothing
}
 
Example #24
Source File: DebugLabel.java    From simplexml with Apache License 2.0 4 votes vote down vote up
public Type getType(Class type) throws Exception {
   showMethodInvocation();
   return label.getType(type);
}
 
Example #25
Source File: CompositeMap.java    From simplexml with Apache License 2.0 3 votes vote down vote up
/**
 * Constructor for the <code>CompositeMap</code> object. This will
 * create a converter that is capable of writing map objects to 
 * and from XML. The resulting XML is configured by an annotation
 * such that key values can attributes and values can be inline. 
 * 
 * @param context this is the root context for the serialization
 * @param entry this provides configuration for the resulting XML
 * @param type this is the map type that is to be converted
 */
public CompositeMap(Context context, Entry entry, Type type) throws Exception {
   this.factory = new MapFactory(context, type);
   this.value = entry.getValue(context);
   this.key = entry.getKey(context);
   this.style = context.getStyle();
   this.entry = entry;
}
 
Example #26
Source File: CompositeListUnion.java    From simplexml with Apache License 2.0 3 votes vote down vote up
/**
 * Constructor for the <code>CompositeListUnion</code> object. This
 * is used to create a converter that delegates to other associated
 * converters within the union group depending on the XML element
 * name being read or the instance type that is being written.
 * 
 * @param context this is the context used for the serialization
 * @param group this is the union group used for delegation
 * @param path this is the path expression representing this union
 * @param type this is the annotated field or method to be used
 */
public CompositeListUnion(Context context, Group group, Expression path, Type type) throws Exception {
   this.elements = group.getElements();
   this.style = context.getStyle();
   this.context = context;
   this.group = group;
   this.type = type;
   this.path = path;
}
 
Example #27
Source File: ElementLabel.java    From simplexml with Apache License 2.0 3 votes vote down vote up
/**
 * This is used to acquire the <code>Type</code> that the type
 * provided is represented by. Typically this will return the
 * field or method represented by the label. However, in the 
 * case of unions this will provide an override type.
 * 
 * @param type this is the class to acquire the type for
 * 
 * @return this returns the type represented by this class
 */
public Type getType(Class type){
   Type contact = getContact();
   
   if(expect == void.class) {
      return contact;
   }
   return new OverrideType(contact, expect);
}
 
Example #28
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 #29
Source File: CompositeInlineMap.java    From simplexml with Apache License 2.0 3 votes vote down vote up
/**
 * Constructor for the <code>CompositeMap</code> object. This will
 * create a converter that is capable of writing map objects to 
 * and from XML. The resulting XML is configured by an annotation
 * such that key values can attributes and values can be inline. 
 * 
 * @param context this is the root context for the serialization
 * @param entry this provides configuration for the resulting XML
 * @param type this is the map type that is to be converted
 */
public CompositeInlineMap(Context context, Entry entry, Type type) throws Exception {
   this.factory = new MapFactory(context, type);
   this.value = entry.getValue(context);
   this.key = entry.getKey(context);
   this.style = context.getStyle();
   this.entry = entry;
}
 
Example #30
Source File: CompositeList.java    From simplexml with Apache License 2.0 3 votes vote down vote up
/**
 * Constructor for the <code>CompositeList</code> object. This is
 * given the list type and entry type to be used. The list type is
 * the <code>Collection</code> implementation that deserialized
 * entry objects are inserted into. 
 *
 * @param context this is the context object used for serialization
 * @param type this is the collection type for the list used
 * @param entry the entry type to be stored within the list
 */    
public CompositeList(Context context, Type type, Type entry, String name) {
   this.factory = new CollectionFactory(context, type); 
   this.root = new Traverser(context);      
   this.entry = entry;
   this.type = type;
   this.name = name;
}