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

The following examples show how to use org.simpleframework.xml.stream.InputNode#getAttributes() . 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 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 2
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 3
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 4
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 5
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 6
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 7
Source File: NodeReaderTest.java    From simplexml with Apache License 2.0 4 votes vote down vote up
public void testLargeSource() throws Exception {
   InputNode event = NodeBuilder.read(new StringReader(LARGE_SOURCE));

   assertTrue(event.isRoot());
   assertEquals("root", event.getName());         
   assertEquals("2.1", event.getAttribute("version").getValue());
   assertEquals("234", event.getAttribute("id").getValue());

   NodeMap attrList = event.getAttributes();

   assertEquals("2.1", attrList.get("version").getValue());
   assertEquals("234", attrList.get("id").getValue());

   InputNode list = event.getNext();

   assertFalse(list.isRoot());
   assertEquals("list", list.getName());
   assertEquals("sorted", list.getAttribute("type").getValue());
   
   InputNode entry = list.getNext();
   InputNode value = list.getNext(); // same as entry.getNext()
   
   assertEquals("entry", entry.getName());
   assertEquals("1", entry.getAttribute("name").getValue());
   assertEquals("value", value.getName());
   assertEquals("value 1", value.getValue());
   assertEquals(null, value.getAttribute("name"));
   assertEquals(null, entry.getNext());
   assertEquals(null, value.getNext());
   
   entry = list.getNext();
   value = entry.getNext(); // same as list.getNext()
   
   assertEquals("entry", entry.getName());
   assertEquals("2", entry.getAttribute("name").getValue());
   assertEquals("value", value.getName());
   assertEquals("value 2", value.getValue());
   assertEquals(null, value.getAttribute("name"));
   assertEquals(null, entry.getNext());

   entry = list.getNext();
   value = entry.getNext(); // same as list.getNext()
   
   assertEquals("entry", entry.getName());
   assertEquals("3", entry.getAttribute("name").getValue());
   assertEquals("value", value.getName());
   assertEquals("value 3", value.getValue());
   assertEquals(null, value.getAttribute("name"));
   assertEquals(null, entry.getNext());
   assertEquals(null, list.getNext());

   InputNode object = event.getNext();
   InputNode integer = event.getNext(); // same as object.getNext()

   assertEquals("object", object.getName());
   assertEquals("name", object.getAttribute("name").getValue());
   assertEquals("integer", integer.getName());
   assertEquals("123", integer.getValue());
   
   object = object.getNext(); // same as event.getNext()
   integer = object.getNext();

   assertEquals("object", object.getName());
   assertEquals("key", object.getAttribute("name").getValue());
   assertEquals("integer", integer.getName());
   assertEquals("12345", integer.getValue());
}
 
Example 8
Source File: NodeReaderTest.java    From simplexml with Apache License 2.0 4 votes vote down vote up
public void testSkip() throws Exception {
   InputNode event = NodeBuilder.read(new StringReader(LARGE_SOURCE));

   assertTrue(event.isRoot());
   assertEquals("root", event.getName());         
   assertEquals("2.1", event.getAttribute("version").getValue());
   assertEquals("234", event.getAttribute("id").getValue());

   NodeMap attrList = event.getAttributes();

   assertEquals("2.1", attrList.get("version").getValue());
   assertEquals("234", attrList.get("id").getValue());

   InputNode list = event.getNext();

   assertFalse(list.isRoot());
   assertEquals("list", list.getName());
   assertEquals("sorted", list.getAttribute("type").getValue());
   
   InputNode entry = list.getNext();
   InputNode value = list.getNext(); // same as entry.getNext()
   
   assertEquals("entry", entry.getName());
   assertEquals("1", entry.getAttribute("name").getValue());
   assertEquals("value", value.getName());
   assertEquals("value 1", value.getValue());
   assertEquals(null, value.getAttribute("name"));
   assertEquals(null, entry.getNext());
   assertEquals(null, value.getNext());
   
   entry = list.getNext();
   entry.skip();

   assertEquals(entry.getNext(), null);

   entry = list.getNext();
   value = entry.getNext(); // same as list.getNext()
   
   assertEquals("entry", entry.getName());
   assertEquals("3", entry.getAttribute("name").getValue());
   assertEquals("value", value.getName());
   assertEquals("value 3", value.getValue());
   assertEquals(null, value.getAttribute("name"));
   assertEquals(null, entry.getNext());
   assertEquals(null, list.getNext());

   InputNode object = event.getNext();
   InputNode integer = event.getNext(); // same as object.getNext()

   assertEquals("object", object.getName());
   assertEquals("name", object.getAttribute("name").getValue());
   assertEquals("integer", integer.getName());
   assertEquals("123", integer.getValue());
   
   object = object.getNext(); // same as event.getNext()
   integer = object.getNext();

   assertEquals("object", object.getName());
   assertEquals("key", object.getAttribute("name").getValue());
   assertEquals("integer", integer.getName());
   assertEquals("12345", integer.getValue());
}
 
Example 9
Source File: Source.java    From simplexml with Apache License 2.0 3 votes vote down vote up
/**
 * This is used to resolve and load a class for the given element.
 * The class should be of the same type or a subclass of the class
 * specified. It can be resolved using the details within the
 * provided XML element, if the details used do not represent any
 * serializable values they should be removed so as not to disrupt
 * the deserialization process. For example the default strategy
 * removes all "class" attributes from the given elements.
 * 
 * @param type this is the type of the root element expected
 * @param node this is the element used to resolve an override
 * 
 * @return returns the type that should be used for the object
 * 
 * @throws Exception thrown if the class cannot be resolved  
 */
public Value getOverride(Type type, InputNode node) throws Exception {
   NodeMap<InputNode> map = node.getAttributes();
   
   if(map == null) {
      throw new PersistenceException("No attributes for %s", node);
   }           
   return strategy.read(type, map, session);
}
 
Example 10
Source File: Composite.java    From simplexml with Apache License 2.0 3 votes vote down vote up
/**
 * This <code>readAttributes</code> method reads the attributes from
 * the provided XML element. This will iterate over all attributes
 * within the element and convert those attributes as primitives to
 * contact values within the source object.
 * <p>
 * Once all attributes within the XML element have been evaluated
 * the <code>Schema</code> is checked to ensure that there are no
 * required contacts annotated with the <code>Attribute</code> that
 * remain. If any required attribute remains an exception is thrown. 
 * 
 * @param node this is the XML element to be evaluated
 * @param source the type of the object that is being deserialized
 * @param section this is the XML section that contains the structure
 */
private void readAttributes(InputNode node, Object source, Section section) throws Exception {
   NodeMap<InputNode> list = node.getAttributes();
   LabelMap map = section.getAttributes();

   for(String name : list) {         
      InputNode value = node.getAttribute(name);
      
      if(value != null) {
         readAttribute(value, source, section, map);
      }
   }  
   validate(node, map, source);
}
 
Example 11
Source File: Composite.java    From simplexml with Apache License 2.0 3 votes vote down vote up
/**
 * This <code>validateAttributes</code> method validates the attributes 
 * from the provided XML element. This will iterate over all attributes
 * within the element and validate those attributes as primitives to
 * contact values within the source object.
 * <p>
 * Once all attributes within the XML element have been evaluated the
 * <code>Schema</code> is checked to ensure that there are no required 
 * contacts annotated with the <code>Attribute</code> that remain. If 
 * any required attribute remains an exception is thrown. 
 * 
 * @param node this is the XML element to be validated
 * @param section this is the section that defines the XML structure
 */
private void validateAttributes(InputNode node, Section section) throws Exception {
   NodeMap<InputNode> list = node.getAttributes();
   LabelMap map = section.getAttributes();

   for(String name : list) {         
      InputNode value = node.getAttribute(name);
      
      if(value != null) {
         validateAttribute(value, section, map);
      }
   }  
   validate(node, map);
}