Java Code Examples for org.simpleframework.xml.stream.OutputNode#getChild()

The following examples show how to use org.simpleframework.xml.stream.OutputNode#getChild() . 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: EntityWithAnyElementConverter.java    From sardine-android with Apache License 2.0 6 votes vote down vote up
@Override
public void write(OutputNode node, T entity) throws Exception {
    for(org.w3c.dom.Element domElement : entity.getAny()) {
        ElementConverter.write(node, domElement);
    }
    Map<String, Field> entityFields = getEntityFields();
    for (String fieldName : entityFields.keySet()) {
        Field field = entityFields.get(fieldName);
        Object value = getGetterForField(field).invoke(entity);
        if (value == null) {
            continue;
        }
        if (value instanceof String) {
            OutputNode childNode = node.getChild(fieldName);
            childNode.setReference(SardineUtil.DEFAULT_NAMESPACE_URI);
            childNode.setValue((String)value);
        } else {
            serializer.write(value, node);
        }
    }
}
 
Example 2
Source File: Traverser.java    From simplexml with Apache License 2.0 6 votes vote down vote up
/**
 * This <code>write</code> method is used to convert the provided
 * object to an XML element. This creates a child node from the
 * given <code>OutputNode</code> object. Once this child element 
 * is created it is populated with the fields of the source object
 * in accordance with the XML schema class.  
 * 
 * @param source this is the object to be serialized to XML
 * @param expect this is the class that is expected to be written
 * @param name this is the name of the root annotation used 
 * 
 * @throws Exception thrown if there is a problem serializing
 */
public void write(OutputNode node, Object source, Class expect, String name) throws Exception {
   OutputNode child = node.getChild(name);
   Type type = getType(expect);
   
   if(source != null) {
      Class actual = source.getClass();
      Decorator decorator = getDecorator(actual);
      
      if(decorator != null) {
         decorator.decorate(child);
      }
      if(!context.setOverride(type, source, child)) {
         getComposite(actual).write(child, source);         
      }
   }         
   child.commit();      
}
 
Example 3
Source File: PrimitiveKey.java    From simplexml with Apache License 2.0 6 votes vote down vote up
/**
 * This method is used to write the value to the specified node.
 * This will write the item as an element to the provided node,
 * also this enables references to be used during serialization.
 * 
 * @param node this is the node that the value is written to
 * @param item this is the item that is to be written
 */
private void writeElement(OutputNode node, Object item) throws Exception {
   Class expect = type.getType();
   String key = entry.getKey();
   
   if(key == null) {
      key = context.getName(expect);
   }    
   String name = style.getElement(key);
   OutputNode child = node.getChild(name);  
   
   if(item != null) {
      if(!isOverridden(child, item)) {
         root.write(child, item);
      }
   }
}
 
Example 4
Source File: CompositeListUnionTest.java    From simplexml with Apache License 2.0 6 votes vote down vote up
public void testLargeList() throws Exception {
   Context context = getContext();
   Group group = getGroup();
   Type type = new ClassType(CompositeListUnionTest.class);
   List<Shape> list = new ArrayList<Shape>();
   Expression expression = new PathParser("some/path", type, new Format());
   CompositeListUnion union = new CompositeListUnion(
         context,
         group,
         expression,
         type);
   InputNode node = NodeBuilder.read(new StringReader(LARGE_SOURCE));
   
   for(InputNode next = node.getNext(); next != null; next = node.getNext()) {
      union.read(next, list);
   }
   OutputNode output = NodeBuilder.write(new PrintWriter(System.err), new Format(3));
   OutputNode parent = output.getChild("parent");
   
   union.write(parent, list);
}
 
Example 5
Source File: Composite.java    From simplexml with Apache License 2.0 6 votes vote down vote up
/**
 * This write method is used to write all the element contacts from
 * the provided source object to the XML element. This visits all
 * the contacts marked with the <code>Element</code> annotation in
 * the source object. All annotated contacts are written as children
 * to the XML element. This will throw an exception if a required
 * contact within the source object is null. 
 * 
 * @param source this is the source object to be serialized
 * @param node this is the XML element to write elements to
 * @param section this is the section that defines the XML structure
 */
private void writeElements(OutputNode node, Object source, Section section) throws Exception {
    for(String name : section) {
      Section child = section.getSection(name);
      
      if(child != null) {
         OutputNode next = node.getChild(name);

         writeSection(next, source, child);
      } else {
         String path = section.getPath(name);
         Label label = section.getElement(path);
         Class expect = context.getType(type, source);
         Object value = criteria.get(label);
         
         if(value == null) {
            if(label == null) {
              throw new ElementException("Element '%s' not defined in %s", name, expect);
            }
            writeUnion(node, source, section, label);
         }
      }            
   }
}
 
Example 6
Source File: EntityWithAnyElementConverter.java    From simpletask-android with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void write(OutputNode node, T entity) throws Exception {
    for(org.w3c.dom.Element domElement : entity.getAny()) {
        ElementConverter.write(node, domElement);
    }
    Map<String, Field> entityFields = getEntityFields();
    for (String fieldName : entityFields.keySet()) {
        Field field = entityFields.get(fieldName);
        Object value = getGetterForField(field).invoke(entity);
        if (value == null) {
            continue;
        }
        if (value instanceof String) {
            OutputNode childNode = node.getChild(fieldName);
            childNode.setReference(SardineUtil.DEFAULT_NAMESPACE_URI);
            childNode.setValue((String)value);
        } else {
            serializer.write(value, node);
        }
    }
}
 
Example 7
Source File: CompositeMap.java    From simplexml with Apache License 2.0 5 votes vote down vote up
/**
 * This <code>write</code> method will write the key value pairs
 * within the provided map to the specified XML node. This will 
 * write each entry type must contain a key and value so that
 * the entry can be deserialized in to the map as a pair. If the
 * key or value object is composite it is read as a root object 
 * so its <code>Root</code> annotation must be present.
 * 
 * @param node this is the node the map is to be written to
 * @param source this is the source map that is to be written 
 */
public void write(OutputNode node, Object source) throws Exception {
   Map map = (Map) source;                
   
   for(Object index : map.keySet()) {
      String root = entry.getEntry();
      String name = style.getElement(root);
      OutputNode next = node.getChild(name);
      Object item = map.get(index);            
      
      key.write(next, index);            
      value.write(next, item);                  
   }
}
 
Example 8
Source File: CompositeListUnionTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public void testSimpleList() throws Exception {
   Context context = getContext();
   Group group = getGroup();
   Type type = new ClassType(CompositeListUnionTest.class);
   List<Shape> list = new ArrayList<Shape>();
   Expression expression = new PathParser("some/path", type, new Format());
   CompositeListUnion union = new CompositeListUnion(
         context,
         group,
         expression,
         type);
   InputNode node = NodeBuilder.read(new StringReader(SOURCE));
   
   InputNode circle = node.getNext();
   union.read(circle, list);
   
   InputNode square = node.getNext();
   union.read(square, list);
   
   InputNode triangle = node.getNext();
   union.read(triangle, list);
   
   assertEquals(list.get(0).getClass(), Circle.class);
   assertEquals(list.get(1).getClass(), Square.class);
   assertEquals(list.get(2).getClass(), Triangle.class);
   
   OutputNode output = NodeBuilder.write(new PrintWriter(System.out), new Format(3));
   OutputNode parent = output.getChild("parent");
   
   union.write(parent, list);
}
 
Example 9
Source File: NamespaceDecoratorTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public void testQualifier() throws Exception {
   NamespaceDecorator global = new NamespaceDecorator();
   NamespaceDecorator qualifier = new NamespaceDecorator();
   NamespaceDecorator attribute = new NamespaceDecorator();
   
   global.add(new MockNamespace("global", "http://www.domain.com/global")); 
   qualifier.add(new MockNamespace("a", "http://www.domain.com/a"));
   qualifier.add(new MockNamespace("b", "http://www.domain.com/b"));
   qualifier.add(new MockNamespace("c", "http://www.domain.com/c"));
   attribute.add(new MockNamespace("d", "http://www.domain.com/d"));
   
   global.set(new MockNamespace("first", "http://www.domain.com/ignore"));
   qualifier.set(new MockNamespace("a", "http://www.domain.com/a"));
   attribute.set(new MockNamespace("b", "http://www.domain.com/b"));

   StringWriter out = new StringWriter(); 
   
   OutputNode top = NodeBuilder.write(out);
   OutputNode root = top.getChild("root");
   
   root.setAttribute("version", "1.0");
   qualifier.decorate(root, global);
   
   OutputNode child = root.getChild("child");
   child.setAttribute("name", "John Doe");
   
   OutputNode name = child.getAttributes().get("name");
   attribute.decorate(name);
   
   OutputNode grandChild = child.getChild("grandChild");
   grandChild.setValue("this is the grand child");
   
   root.commit();
   validate(out.toString());     
}
 
Example 10
Source File: ConverterMapTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public void write(OutputNode node, Map map) throws Exception {
   Set keys = map.keySet();
   
   for(Object key : keys) {
      OutputNode next = node.getChild("entry");
      next.setAttribute("key", key.toString());
      OutputNode value = next.getChild("value");
      value.setValue(map.get(key).toString());
   }
}
 
Example 11
Source File: PrimitiveList.java    From simplexml with Apache License 2.0 5 votes vote down vote up
/**
 * This <code>write</code> method will write the specified object
 * to the given XML element as as list entries. Each entry within
 * the given list must be assignable to the given primitive type.
 * This will deserialize each entry type as a primitive value. In
 * order to do this the parent string provided forms the element.
 * 
 * @param source this is the source object array to be serialized 
 * @param node this is the XML element container to be populated
 */ 
public void write(OutputNode node, Object source) throws Exception {
   Collection list = (Collection) source;                
   
   for(Object item : list) {
      if(item != null) {
         OutputNode child = node.getChild(parent);         

         if(!isOverridden(child, item)) { 
            root.write(child, item);
         }
      }
   }
}
 
Example 12
Source File: PrimitiveArray.java    From simplexml with Apache License 2.0 5 votes vote down vote up
/**
 * This <code>write</code> method will write the specified object
 * to the given XML element as as array entries. Each entry within
 * the given array must be assignable to the array component type.
 * This will deserialize each entry type as a primitive value. In
 * order to do this the parent string provided forms the element.
 * 
 * @param source this is the source object array to be serialized 
 * @param node this is the XML element container to be populated
 */ 
public void write(OutputNode node, Object source) throws Exception {
   int size = Array.getLength(source);
   
   for(int i = 0; i < size; i++) {
      OutputNode child = node.getChild(parent);
      
      if(child == null) {
         break;
      }
      write(child, source, i);
   }
}
 
Example 13
Source File: PrimitiveValue.java    From simplexml with Apache License 2.0 5 votes vote down vote up
/**
 * This method is used to write the value to the specified node.
 * The value written to the node can be an attribute or an element
 * depending on the annotation attribute values. This method will
 * maintain references for serialized elements.
 * 
 * @param node this is the node that the value is written to
 * @param item this is the item that is to be written
 * @param key this is the name of the element to be created
 */   
private void writeElement(OutputNode node, Object item, String key) throws Exception {
   String name = style.getAttribute(key);
   OutputNode child = node.getChild(name);

   if(item != null) {        
      if(!isOverridden(child, item)) {
         root.write(child, item);
      }
   }
}
 
Example 14
Source File: CompositeInlineMap.java    From simplexml with Apache License 2.0 5 votes vote down vote up
/**
 * This <code>write</code> method will write the key value pairs
 * within the provided map to the specified XML node. This will 
 * write each entry type must contain a key and value so that
 * the entry can be deserialized in to the map as a pair. If the
 * key or value object is composite it is read as a root object 
 * so its <code>Root</code> annotation must be present.
 * 
 * @param node this is the node the map is to be written to
 * @param map this is the source map that is to be written 
 * @param mode this is the mode that has been inherited
 */
private void write(OutputNode node, Map map, Mode mode) throws Exception {   
   String root = entry.getEntry();
   String name = style.getElement(root);
   
   for(Object index : map.keySet()) {
      OutputNode next = node.getChild(name);
      Object item = map.get(index);            
      
      next.setMode(mode); 
      key.write(next, index);            
      value.write(next, item);                  
   }
}
 
Example 15
Source File: PrimitiveInlineList.java    From simplexml with Apache License 2.0 5 votes vote down vote up
/**
 * This <code>write</code> method will write the specified object
 * to the given XML element as as list entries. Each entry within
 * the given list must be assignable to the given primitive type.
 * This will deserialize each entry type as a primitive value. In
 * order to do this the parent string provided forms the element.
 * 
 * @param node this is the parent output node to write values to
 * @param source this is the source collection to be serialized 
 * @param mode this is used to determine whether to output CDATA    
 */ 
private void write(OutputNode node, Object source, Mode mode) throws Exception {
   Collection list = (Collection) source;
   
   for(Object item : list) {
      if(item != null) {
         OutputNode child = node.getChild(parent);
      
         if(!isOverridden(child, item)) { 
            child.setMode(mode);
            root.write(child, item);
         }
      }
   }
}
 
Example 16
Source File: ElementConverter.java    From sardine-android with Apache License 2.0 4 votes vote down vote up
public static void write(OutputNode parent, Element domElement) throws Exception {
    OutputNode child = parent.getChild(domElement.getNodeName());
    child.getNamespaces().setReference(domElement.getNamespaceURI(), domElement.getPrefix());
    child.setValue(domElement.getTextContent());
    child.commit();
}
 
Example 17
Source File: ExampleConverters.java    From simplexml with Apache License 2.0 4 votes vote down vote up
public void write(OutputNode node, List<Entry> entryList) throws Exception {
   for(Entry entry : entryList) {
      OutputNode item = node.getChild("entry");
      converter.write(item, entry);
   }
}
 
Example 18
Source File: ExampleConverters.java    From simplexml with Apache License 2.0 4 votes vote down vote up
public void write(OutputNode node, Cat cat)throws Exception {
   OutputNode name = node.getChild(ELEMENT_NAME);
   name.setValue(cat.getName());
   OutputNode age = node.getChild(ELEMENT_AGE);
   age.setValue(String.valueOf(cat.getAge()));
}
 
Example 19
Source File: ConversionTest.java    From simplexml with Apache License 2.0 4 votes vote down vote up
public void write(OutputNode node, Cat cat)throws Exception {
   OutputNode name = node.getChild(ELEMENT_NAME);
   name.setValue(cat.getName());
   OutputNode age = node.getChild(ELEMENT_AGE);
   age.setValue(String.valueOf(cat.getAge()));
}
 
Example 20
Source File: ElementConverter.java    From simpletask-android with GNU General Public License v3.0 4 votes vote down vote up
public static void write(OutputNode parent, Element domElement) throws Exception {
    OutputNode child = parent.getChild(domElement.getNodeName());
    child.getNamespaces().setReference(domElement.getNamespaceURI(), domElement.getPrefix());
    child.setValue(domElement.getTextContent());
    child.commit();
}