Java Code Examples for org.simpleframework.xml.stream.NodeMap#put()

The following examples show how to use org.simpleframework.xml.stream.NodeMap#put() . 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: WriteGraph.java    From simplexml with Apache License 2.0 6 votes vote down vote up
/**
 * This is used to write the XML element attributes representing
 * the serialized object instance. If the object has already been
 * serialized to the XML document then a reference attribute is
 * inserted and this returns true, if not, then this will write
 * a unique identity marker attribute and return false.
 *
 * @param value this is the instance that is to be serialized    
 * @param node this is the node that contains the attributes
 * 
 * @return returns true if the element has been fully written
 */   
private boolean writeReference(Object value, NodeMap node) {
   String name = get(value);
   int size = size();
   
   if(name != null) {
      node.put(refer, name);
      return true;
   } 
   String unique = String.valueOf(size);
   
   node.put(mark, unique);
   put(value, unique);
   
   return false;   
}
 
Example 2
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 3
Source File: WriteGraph.java    From simplexml with Apache License 2.0 5 votes vote down vote up
/**
 * This is used to write the XML element attributes representing
 * the serialized object instance. If the object has already been
 * serialized to the XML document then a reference attribute is
 * inserted and this returns true, if not, then this will write
 * a unique identity marker attribute and return false.
 * 
 * @param type this is the type of the object to be serialized
 * @param value this is the instance that is to be serialized    
 * @param node this is the node that contains the attributes
 * 
 * @return returns true if the element has been fully written
 */
public boolean write(Type type, Object value, NodeMap node){
   Class actual = value.getClass();
   Class expect = type.getType();
   Class real = actual;
   
   if(actual.isArray()) {
      real = writeArray(actual, value, node);
   }
   if(actual != expect) {
      node.put(label, real.getName());
   }       
   return writeReference(value, node);
}
 
Example 4
Source File: AnnotationTypeTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public boolean write(Type type, Object value, NodeMap<OutputNode> node, Map map) throws Exception {
   Component component = type.getAnnotation(Component.class);
   
   if(component != null) {
      String name = component.name();
   
      if(name != null) {
         node.put(KEY, name);
      }
   }
   return strategy.write(type, value, node, map);
}
 
Example 5
Source File: ClassToNamespaceVisitor.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public void read(Type field, NodeMap<InputNode> node) throws Exception {
   String namespace = node.getNode().getReference();
   if(namespace != null && namespace.length() > 0) {
      String type = new PackageParser().revert(namespace).getName();
      if(type == null) {
         throw new PersistenceException("Could not match name %s", namespace);
      }
      node.put("class", type);
   }
}
 
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: DecoratorTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public void read(Class field, NodeMap<InputNode> node) throws Exception{
    InputNode value = node.remove(replace);
    if(value != null) {
        String name = value.getValue();
        String type = read.get(name);
        if(type == null) {
            throw new PersistenceException("Could not match name %s", name);
        }
        node.put(label, type);
    }
}
 
Example 8
Source File: DecoratorTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public void write(Class field, NodeMap<OutputNode> node) throws Exception {
    OutputNode value = node.remove(label);
    if(value != null) {
        String type = value.getValue();
        String name = write.get(type);
        if(name == null) {
            throw new PersistenceException("Could not match class %s", type);
        }
        node.put(replace, name);
    }
}
 
Example 9
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 10
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 11
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 12
Source File: TreeStrategy.java    From simplexml with Apache License 2.0 3 votes vote down vote up
/**
 * This is used to attach a attribute to the provided element
 * that is used to identify the class. The attribute name is
 * "class" and has the value of the fully qualified class 
 * name for the object provided. This will only be invoked
 * if the object class is different from the field class.
 *
 * @param type this is the declared class for the field used
 * @param value this is the instance variable being serialized
 * @param node this is the element used to represent the value
 * @param map this is used to maintain contextual information
 * 
 * @return this returns true if serialization is complete
 */   
public boolean write(Type type, Object value, NodeMap node, Map map){
   Class actual = value.getClass();
   Class expect = type.getType();
   Class real = actual;
   
   if(actual.isArray()) {
      real = writeArray(expect, value, node);
   }
   if(actual != expect) {
      node.put(label, real.getName());
   }       
   return false;
}
 
Example 13
Source File: TreeStrategy.java    From simplexml with Apache License 2.0 3 votes vote down vote up
/**
 * This is used to add a length attribute to the element due to
 * the fact that the serialized value is an array. The length
 * of the array is acquired and inserted in to the attributes.
 * 
 * @param field this is the field type for the array to set
 * @param value this is the actual value for the array to set
 * @param node this is the map of attributes for the element
 * 
 * @return returns the array component type that is set
 */
private Class writeArray(Class field, Object value, NodeMap node){
   int size = Array.getLength(value);
   
   if(length != null) {       
      node.put(length, String.valueOf(size));
   }
   return field.getComponentType();
}
 
Example 14
Source File: WriteGraph.java    From simplexml with Apache License 2.0 3 votes vote down vote up
/**
 * This is used to add a length attribute to the element due to
 * the fact that the serialized value is an array. The length
 * of the array is acquired and inserted in to the attributes.
 * 
 * @param field this is the field type for the array to set
 * @param value this is the actual value for the array to set
 * @param node this is the map of attributes for the element
 * 
 * @return returns the array component type that is set
 */
private Class writeArray(Class field, Object value, NodeMap node){
   int size = Array.getLength(value);
   
   if(!containsKey(value)) {       
      node.put(length, String.valueOf(size));
   }
   return field.getComponentType();
}