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

The following examples show how to use org.simpleframework.xml.stream.OutputNode#setAttribute() . 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: WrapperTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public void write(OutputNode node, Wrapper wrapper) throws Exception {
   Object value = wrapper.get();
   Class type = value.getClass();
   String className = type.getName();
   node.setAttribute("type", className);
   serializer.write(value, node);
}
 
Example 2
Source File: DynamicMapOfAttributesTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public void write(OutputNode node, Car car) throws Exception {
   Set<String> keys = car.furtherAttributes.keySet();
   
   for(String name : keys) {
      String value = car.furtherAttributes.get(name);
      
      node.setAttribute(name, value);
   }
}
 
Example 3
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 4
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 5
Source File: PrimitiveKey.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.
 * This will write the item as an attribute to the provided node,
 * the name of the attribute is taken from the annotation.
 * 
 * @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 writeAttribute(OutputNode node, Object item) throws Exception { 
   Class expect = type.getType();
   String text = factory.getText(item);
   String key = entry.getKey();  
   
   if(key == null) {
      key = context.getName(expect);
   }     
   String name = style.getAttribute(key);
   
   if(text != null) {
      node.setAttribute(name, text);
   }
}
 
Example 6
Source File: ExampleConverters.java    From simplexml with Apache License 2.0 4 votes vote down vote up
public void write(OutputNode node, Dog dog)throws Exception {
   node.setAttribute(ELEMENT_NAME, dog.getName());
   node.setAttribute(ELEMENT_AGE, String.valueOf(dog.getAge()));
}
 
Example 7
Source File: ConversionTest.java    From simplexml with Apache License 2.0 4 votes vote down vote up
public void write(OutputNode node, Dog dog)throws Exception {
   node.setAttribute(ELEMENT_NAME, dog.getName());
   node.setAttribute(ELEMENT_AGE, String.valueOf(dog.getAge()));
}
 
Example 8
Source File: PathWithConverterTest.java    From simplexml with Apache License 2.0 4 votes vote down vote up
public void write(OutputNode node, ServerDetails value) throws Exception {
   node.setAttribute("host", value.host);
   node.setAttribute("port", String.valueOf(value.port));
   node.setValue(value.name);
}
 
Example 9
Source File: AnnotationConverterTest.java    From simplexml with Apache License 2.0 4 votes vote down vote up
public void write(OutputNode node, Chicken chicken) throws Exception {
   node.setAttribute("name", chicken.getName());
   node.setAttribute("age", String.valueOf(chicken.getAge()));
   node.setAttribute("legs", String.valueOf(chicken.getLegs()));
}
 
Example 10
Source File: AnnotationConverterTest.java    From simplexml with Apache License 2.0 4 votes vote down vote up
public void write(OutputNode node, Cow cow) throws Exception {
   node.setAttribute("name", cow.getName());
   node.setAttribute("age", String.valueOf(cow.getAge()));
   node.setAttribute("legs", String.valueOf(cow.getLegs()));
}
 
Example 11
Source File: CombinedStrategyTest.java    From simplexml with Apache License 2.0 4 votes vote down vote up
public void write(OutputNode node, ExtendedItem value) throws Exception {
   node.setAttribute("value", String.valueOf(value.getValue()));
   node.setAttribute("type", getClass().getName());
}
 
Example 12
Source File: CombinedStrategyTest.java    From simplexml with Apache License 2.0 4 votes vote down vote up
public void write(OutputNode node, Item value) throws Exception {
   node.setAttribute("value", String.valueOf(value.getValue()));
   node.setAttribute("type", getClass().getName());
}
 
Example 13
Source File: ExampleConverters.java    From simplexml with Apache License 2.0 4 votes vote down vote up
public void write(OutputNode node, Entry entry) throws Exception {
   node.setAttribute("name", entry.getName());
   node.setAttribute("value", entry.getValue());
}
 
Example 14
Source File: ExampleConverters.java    From simplexml with Apache License 2.0 4 votes vote down vote up
public void write(OutputNode node, ExtendedEntry entry) throws Exception {
   node.setAttribute("name", entry.getName());
   node.setAttribute("value", entry.getValue());
   node.setAttribute("code", String.valueOf(entry.getCode()));
}
 
Example 15
Source File: ExampleConverters.java    From simplexml with Apache License 2.0 4 votes vote down vote up
public void write(OutputNode node, Chicken chicken) throws Exception {
   node.setAttribute("name", chicken.getName());
   node.setAttribute("age", String.valueOf(chicken.getAge()));
   node.setAttribute("legs", String.valueOf(chicken.getLegs()));
}
 
Example 16
Source File: ExampleConverters.java    From simplexml with Apache License 2.0 4 votes vote down vote up
public void write(OutputNode node, Cow cow) throws Exception {
   node.setAttribute("name", cow.getName());
   node.setAttribute("age", String.valueOf(cow.getAge()));
   node.setAttribute("legs", String.valueOf(cow.getLegs()));
}
 
Example 17
Source File: PrimitiveValue.java    From simplexml with Apache License 2.0 3 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 attribute to be created
 */   
private void writeAttribute(OutputNode node, Object item, String key) throws Exception {
   if(item != null) {
      if(key != null) {
         key = style.getAttribute(key);
         node = node.setAttribute(key, null);
      }     
      root.write(node, item);
   }
}
 
Example 18
Source File: Composite.java    From simplexml with Apache License 2.0 3 votes vote down vote up
/**
 * This write method is used to set the value of the provided object
 * as an attribute to the XML element. This will acquire the string
 * value of the object using <code>toString</code> only if the
 * object provided is not an enumerated type. If the object is an
 * enumerated type then the <code>Enum.name</code> method is used.
 * 
 * @param value this is the value to be set as an attribute
 * @param node this is the XML element to write the attribute to
 * @param label the label that contains the contact details
 */
private void writeAttribute(OutputNode node, Object value, Label label) throws Exception {
   if(value != null) {         
      Decorator decorator = label.getDecorator();
      String name = label.getName();
      String text = factory.getText(value);
      OutputNode done = node.setAttribute(name, text);
      
      decorator.decorate(done);
   }
}