org.simpleframework.xml.stream.NodeMap Java Examples

The following examples show how to use org.simpleframework.xml.stream.NodeMap. 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: 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 #2
Source File: AnnotationConverterTest.java    From simplexml with Apache License 2.0 6 votes vote down vote up
public boolean write(Type type, Object value, NodeMap<OutputNode> node, Map map) throws Exception {
   Convert convert = type.getAnnotation(Convert.class);
   OutputNode parent = node.getNode();
   
   if(convert != null) {
      Class<? extends Converter> converterClass = convert.value();
      Constructor<? extends Converter> converterConstructor = converterClass.getDeclaredConstructor();
      
      if(!converterConstructor.isAccessible()) {
         converterConstructor.setAccessible(true);
      }
      Converter converter = converterConstructor.newInstance();
      converter.write(parent, value);
      return true;
   }
   return strategy.write(type, value, node, map);
}
 
Example #3
Source File: AnnotationConverterTest.java    From simplexml with Apache License 2.0 6 votes vote down vote up
public Value read(Type type, NodeMap<InputNode> node, Map map) throws Exception {
   Value value = strategy.read(type, node, map);
   Convert convert = type.getAnnotation(Convert.class);
   InputNode parent = node.getNode();
   
   if(convert != null) {
      Class<? extends Converter> converterClass = convert.value();
      Constructor<? extends Converter> converterConstructor = converterClass.getDeclaredConstructor();
      
      if(!converterConstructor.isAccessible()) {
         converterConstructor.setAccessible(true);
      }
      Converter converter = converterConstructor.newInstance();
      Object result = converter.read(parent);
      return new Wrapper(result);
   }
   return value;
}
 
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: 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 #6
Source File: CycleStrategyTest.java    From simplexml with Apache License 2.0 6 votes vote down vote up
public void testReference() throws Exception {
   StringReader reader = new StringReader(REFERENCE);
   Contract contract = new Contract("id", "reference", "class", "length");
   ReadGraph graph = new ReadGraph(contract, new Loader());
   InputNode event = NodeBuilder.read(reader);
   NodeMap attributes = event.getAttributes();
   
   graph.put("1", "first text");
   graph.put("2", "second text");
   graph.put("3", "third text");
   
   Value value = graph.read(new Entry(String.class), attributes);
   
   assertEquals(0, value.getLength());
   assertEquals("first text", value.getValue());
   assertEquals(String.class, value.getType());
   assertEquals(true, value.isReference());     
}
 
Example #7
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 #8
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 #9
Source File: AnnotationTypeTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public Value read(Type type, NodeMap<InputNode> node, Map map) throws Exception {
   Component component = type.getAnnotation(Component.class);
   
   if(component != null) {
      String name = component.name();
      InputNode value = node.get(KEY);
      
      if(!value.getValue().equals(name)) {
         throw new IllegalStateException("Component name incorrect, expected '"+name+"' but was '"+value.getValue()+"'");
      }
   }
   return strategy.read(type, node, map);
}
 
Example #10
Source File: ReadGraph.java    From simplexml with Apache License 2.0 5 votes vote down vote up
/**
 * This is used to recover the object references from the document
 * using the special attributes specified. This allows the element
 * specified by the <code>NodeMap</code> to be used to discover
 * exactly which node in the object graph the element represents.
 * 
 * @param type the type of the field or method in the instance
 * @param real this is the overridden type from the XML element
 * @param node this is the XML element to be deserialized
 * 
 * @return this is used to return the type to acquire the value
 */
private Value readInstance(Type type, Class real, NodeMap node) throws Exception {      
   Node entry = node.remove(mark);
   
   if(entry == null) {
      return readReference(type, real, node);
   }      
   String key = entry.getValue();
   
   if(containsKey(key)) {
      throw new CycleException("Element '%s' already exists", key);
   }
   return readValue(type, real, node, key);
}
 
Example #11
Source File: CommentTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public void write(Type type, NodeMap<OutputNode> node) throws Exception {
   if(!node.getNode().isRoot()) {
      Comment comment = type.getAnnotation(Comment.class);
      if(comment != null) {
         node.getNode().setComment(comment.value());
      }
   }
}
 
Example #12
Source File: DynamicMapOfAttributesTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public Car read(InputNode node) throws Exception {
   Car car = persister.read(Car.class, node, false);
   NodeMap<InputNode> attributes = node.getAttributes();
   
   for(String name : attributes) {
      InputNode attribute = attributes.get(name);
      String value = attribute.getValue();
      
      car.furtherAttributes.put(name, value);
   }
   return car;
}
 
Example #13
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 #14
Source File: NodeReaderTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public void testSmallSource() throws Exception {
   InputNode event = NodeBuilder.read(new StringReader(SMALL_SOURCE));

   assertTrue(event.isRoot());
   assertEquals("override", event.getName());         
   assertEquals("12", event.getAttribute("id").getValue());
   assertEquals("true", event.getAttribute("flag").getValue());

   NodeMap list = event.getAttributes();

   assertEquals("12", list.get("id").getValue());
   assertEquals("true", list.get("flag").getValue());

   InputNode text = event.getNext();
   
   assertFalse(text.isRoot());
   assertTrue(event.isRoot());
   assertEquals("text", text.getName());
   assertEquals("entry text", text.getValue());
   assertEquals(null, text.getNext());
   
   InputNode name = event.getNext();
   
   assertFalse(name.isRoot());
   assertEquals("name", name.getName());
   assertEquals("some name", name.getValue());
   assertEquals(null, name.getNext());
   assertEquals(null, text.getNext());
   
   InputNode third = event.getNext();
   
   assertTrue(event.isRoot());
   assertFalse(third.isRoot());
   assertEquals("third", third.getName());
   assertEquals("text", text.getName());
   assertEquals(null, text.getNext());
   assertEquals("added to schema", third.getValue());
   assertEquals(null, event.getNext());
}
 
Example #15
Source File: ReadGraph.java    From simplexml with Apache License 2.0 5 votes vote down vote up
/**
 * This is used to recover the object references from the document
 * using the special attributes specified. This allows the element
 * specified by the <code>NodeMap</code> to be used to discover
 * exactly which node in the object graph the element represents.
 * 
 * @param type the type of the field or method in the instance
 * @param node this is the XML element to be deserialized
 * 
 * @return this is used to return the type to acquire the value
 */
public Value read(Type type, NodeMap node) throws Exception {
   Node entry = node.remove(label);
   Class expect = type.getType();
   
   if(expect.isArray()) {
      expect = expect.getComponentType();
   }
   if(entry != null) {      
      String name = entry.getValue();
      expect = loader.load(name);
   }  
   return readInstance(type, expect, node); 
}
 
Example #16
Source File: ReadGraph.java    From simplexml with Apache License 2.0 5 votes vote down vote up
/**
 * This is used to recover the object references from the document
 * using the special attributes specified. This allows the element
 * specified by the <code>NodeMap</code> to be used to discover
 * exactly which node in the object graph the element represents.
 * 
 * @param type the type of the field or method in the instance
 * @param real this is the overridden type from the XML element
 * @param node this is the XML element to be deserialized    
 * 
 * @return this is used to return the type to acquire the value
 */ 
private Value readReference(Type type, Class real, NodeMap node) throws Exception {
   Node entry = node.remove(refer);
   
   if(entry == null) {
      return readValue(type, real, node);
   }
   String key = entry.getValue();
   Object value = get(key); 
      
   if(!containsKey(key)) {        
      throw new CycleException("Invalid reference '%s' found", key);
   }
   return new Reference(value, real);
}
 
Example #17
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 #18
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 #19
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 #20
Source File: ClassToNamespaceVisitor.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public void write(Type field, NodeMap<OutputNode> node) throws Exception {
   OutputNode value = node.remove("class");
   if(value != null) {
      String type = value.getValue();
      String name = new PackageParser().parse(type);
      if(name == null) {
         throw new PersistenceException("Could not match class %s", type);
      }
      if(comment) {
         node.getNode().setComment(type);
      }
      node.getNode().getNamespaces().setReference(name, "class");
      node.getNode().setReference(name);
   }
}
 
Example #21
Source File: CycleStrategyTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public void testArray() throws Exception {
   Map map = new HashMap();
   StringReader reader = new StringReader(ARRAY);
   CycleStrategy strategy = new CycleStrategy();
   InputNode event = NodeBuilder.read(reader);
   NodeMap attributes = event.getAttributes();
   Value value = strategy.read(new Entry(String[].class), attributes, map);
   
   assertEquals(12, value.getLength());
   assertEquals(null, value.getValue());
   assertEquals(String.class, value.getType());
   assertEquals(false, value.isReference());
}
 
Example #22
Source File: CycleStrategyTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public void testObject() throws Exception {
   Map map = new HashMap();
   StringReader reader = new StringReader(OBJECT);
   CycleStrategy strategy = new CycleStrategy();
   InputNode event = NodeBuilder.read(reader);
   NodeMap attributes = event.getAttributes();
   Value value = strategy.read(new Entry(String.class), attributes, map);
   
   assertEquals(0, value.getLength());
   assertEquals(null, value.getValue());
   assertEquals(String.class, value.getType());
   assertEquals(false, value.isReference());     
}
 
Example #23
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 #24
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 #25
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 #26
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 #27
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 #28
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 #29
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 #30
Source File: UARTActivity.java    From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void write(final Type type, final NodeMap<OutputNode> node) {
	if (type.getType().equals(Command[].class)) {
		final OutputNode element = node.getNode();

		final StringBuilder builder = new StringBuilder("A configuration must have 9 commands, one for each button.\n        Possible icons are:");
		for (Command.Icon icon : Command.Icon.values())
			builder.append("\n          - ").append(icon.toString());
		element.setComment(builder.toString());
	}
}