org.simpleframework.xml.Element Java Examples

The following examples show how to use org.simpleframework.xml.Element. 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 simpletask-android with GNU General Public License v3.0 6 votes vote down vote up
@Override
public T read(InputNode node) throws Exception {
    Map<String, Field> entityFields = getEntityFields();
    T entity = entityClass.newInstance();
    List<org.w3c.dom.Element> anyElements = entity.getAny();
    InputNode childNode;
    while((childNode = node.getNext()) != null) {
        if (entityFields.containsKey(childNode.getName())) {
            Field field = entityFields.get(childNode.getName());
            getSetterForField(field).invoke(entity, serializer.read(field.getType(), childNode));
        } else if (childNode.getPrefix() != null && !childNode.getPrefix().isEmpty()) {
            org.w3c.dom.Element element = ElementConverter.read(childNode);
            anyElements.add(element);
        } else {
            // Probably a WebDAV field we don't support yet
            skipChildrenOfNode(childNode);
        }
    }
    return entity;
}
 
Example #2
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 #3
Source File: MethodScannerDefaultTest.java    From simplexml with Apache License 2.0 6 votes vote down vote up
public void testMixedAnnotationsWithNoDefaults() throws Exception {
   Map<String, Contact> map = getContacts(MixedAnnotations.class);
   
   assertEquals(map.size(), 4);
   assertFalse(map.get("name").isReadOnly());
   assertFalse(map.get("value").isReadOnly());
   
   assertEquals(int.class, map.get("value").getType());      
   assertEquals(String.class, map.get("name").getType());
   
   assertEquals(Attribute.class, map.get("name").getAnnotation().annotationType());
   assertEquals(Element.class, map.get("value").getAnnotation().annotationType());
   
   assertEquals(Attribute.class, map.get("name").getAnnotation(Attribute.class).annotationType());
   assertEquals(Element.class, map.get("value").getAnnotation(Element.class).annotationType());
   
   assertNull(map.get("name").getAnnotation(Root.class));
   assertNull(map.get("value").getAnnotation(Root.class));
}
 
Example #4
Source File: PackageParserTest.java    From simplexml with Apache License 2.0 6 votes vote down vote up
public void testParser() throws Exception {
   assertEquals("http://util.java/HashMap", parse(HashMap.class));
   assertEquals("http://simpleframework.org/xml/Element", parse(Element.class));
   assertEquals("http://simpleframework.org/xml/ElementList", parse(ElementList.class));
   assertEquals("http://w3c.org/dom/Node", parse(Node.class));
   assertEquals("http://simpleframework.org/xml/strategy/PackageParser", parse(PackageParser.class));
   
   assertEquals(HashMap.class, revert("http://util.java/HashMap"));
   assertEquals(Element.class, revert("http://simpleframework.org/xml/Element"));
   assertEquals(ElementList.class, revert("http://simpleframework.org/xml/ElementList"));
   assertEquals(Node.class, revert("http://w3c.org/dom/Node"));
   assertEquals(PackageParser.class, revert("http://simpleframework.org/xml/strategy/PackageParser"));
   
   long start = System.currentTimeMillis();
   for(int i = 0; i < ITERATIONS; i++) {
      fastParse(ElementList.class);
   }
   long fast = System.currentTimeMillis() - start;
   start = System.currentTimeMillis();
   for(int i = 0; i < ITERATIONS; i++) {
      parse(ElementList.class);
   }
   long normal = System.currentTimeMillis() - start;
   System.out.printf("fast=%sms normal=%sms diff=%s%n", fast, normal, normal / fast);
}
 
Example #5
Source File: Pod.java    From Saiy-PS with GNU Affero General Public License v3.0 6 votes vote down vote up
public Pod(@Attribute(name = "error") final boolean error,
           @Attribute(name = "primary") final boolean primary,
           @Attribute(name = "title") final String title,
           @Attribute(name = "scanner") final String scanner,
           @Attribute(name = "id") final String id,
           @Attribute(name = "position") final long position,
           @Attribute(name = "numsubpods") final long numsubpods,
           @ElementList(inline = true, name = "subpods") final List<SubPod> subpods,
           @Element(name = "states") final States states,
           @Element(name = "infos") final Infos infos,
           @Element(name = "definitions", required = false) final Definitions definitions) {
    this.error = error;
    this.title = title;
    this.scanner = scanner;
    this.id = id;
    this.position = position;
    this.numsubpods = numsubpods;
    this.subpods = subpods;
    this.primary = primary;
    this.states = states;
    this.infos = infos;
    this.definitions = definitions;
}
 
Example #6
Source File: PathWithTextAndElementTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
private OtherPathWithTextAndElementExample(
      @Path("valuePath/path") @Attribute(name="name") String a,
      @Path("someOtherPath/path") @Element(name="name") String b,
      @Text String c) 
{
   this.a = a;
   this.b = b;
   this.c = c;
}
 
Example #7
Source File: States.java    From Saiy-PS with GNU Affero General Public License v3.0 5 votes vote down vote up
public States(@Attribute(name = "count") final long count,
              @ElementList(inline = true, name = "state") final List<State> state,
              @Element(name = "statelist", required = false) final StateList stateList) {
    this.count = count;
    this.state = state;
    this.stateList = stateList;
}
 
Example #8
Source File: SourceCommandTag.java    From c2mon with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Creates a SourceCommandTag from a DOM element. The provided element
 * MUST be a element CommandTag element.
 *
 * @param domElement The DOM element to use.
 * @return The created SourceCommandTag.
 */
public static SourceCommandTag fromConfigXML(final org.w3c.dom.Element domElement) {
    Long id = Long.valueOf(domElement.getAttribute("id"));
    String name = domElement.getAttribute("name");

    SourceCommandTag result = new SourceCommandTag(id, name);

    Node fieldNode = null;
    String fieldName = null;
    String fieldValueString = null;
    NodeList fields = domElement.getChildNodes();

    int fieldsCount = fields.getLength();

    for (int i = 0; i < fieldsCount; i++) {
        fieldNode = fields.item(i);

        if (fieldNode.getNodeType() == 1) {
            // extract name of the XML node
            fieldName = fieldNode.getNodeName();
            // extract contents of the XML node
            fieldValueString = fieldNode.getFirstChild().getNodeValue();

            if (fieldName.equals("source-timeout")) {
                result.sourceTimeout = Integer.parseInt(fieldValueString);
            }

            if (fieldName.equals("source-retries")) {
                result.sourceRetries = Integer.parseInt(fieldValueString);
            }

            if (fieldName.equals("HardwareAddress")) {
                result.hardwareAddress = HardwareAddressFactory.getInstance().fromConfigXML((org.w3c.dom.Element) fieldNode);
            }
        }
    }
    return result;
}
 
Example #9
Source File: MethodScannerDefaultTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public void testExtendedAnnotations() throws Exception {
   Map<String, Contact> map = getContacts(ExtendedAnnotations.class);
   
   assertFalse(map.get("array").isReadOnly());
   assertFalse(map.get("map").isReadOnly());
   assertFalse(map.get("name").isReadOnly());      
   assertFalse(map.get("value").isReadOnly());
   
   assertEquals(String[].class, map.get("array").getType());
   assertEquals(Map.class, map.get("map").getType());
   assertEquals(int.class, map.get("value").getType());      
   assertEquals(String.class, map.get("name").getType());
   
   assertEquals(Attribute.class, map.get("name").getAnnotation().annotationType());
   assertEquals(Element.class, map.get("value").getAnnotation().annotationType());
   assertEquals(ElementMap.class, map.get("map").getAnnotation().annotationType());
   assertEquals(Element.class, map.get("array").getAnnotation().annotationType());
   
   assertEquals(Attribute.class, map.get("name").getAnnotation(Attribute.class).annotationType());
   assertEquals(Element.class, map.get("value").getAnnotation(Element.class).annotationType());
   assertEquals(ElementMap.class, map.get("map").getAnnotation(ElementMap.class).annotationType());
   assertEquals(Element.class, map.get("array").getAnnotation(Element.class).annotationType());
   
   assertNull(map.get("name").getAnnotation(Root.class));
   assertNull(map.get("value").getAnnotation(Root.class));
   assertNull(map.get("map").getAnnotation(Root.class));
   assertNull(map.get("array").getAnnotation(Root.class));
}
 
Example #10
Source File: StructureBuilder.java    From simplexml with Apache License 2.0 5 votes vote down vote up
/**
 * This reflectively checks the annotation to determine the type 
 * of annotation it represents. If it represents an XML schema
 * annotation it is used to create a <code>Label</code> which can
 * be used to represent the field within the context object.
 * 
 * @param field the field that the annotation comes from
 * @param label the annotation used to model the XML schema
 * 
 * @throws Exception if there is more than one text annotation
 */   
public void process(Contact field, Annotation label) throws Exception {
   if(label instanceof Attribute) {
      process(field, label, attributes);
   }
   if(label instanceof ElementUnion) {
      union(field, label, elements);
   }
   if(label instanceof ElementListUnion) {
      union(field, label, elements);
   }
   if(label instanceof ElementMapUnion) {
      union(field, label, elements);
   }
   if(label instanceof ElementList) {
      process(field, label, elements);
   }
   if(label instanceof ElementArray) {
      process(field, label, elements);
   }
   if(label instanceof ElementMap) {
      process(field, label, elements);
   }
   if(label instanceof Element) {
      process(field, label, elements);
   }    
   if(label instanceof Version) {
      version(field, label);
   }
   if(label instanceof Text) {
      text(field, label);
   }
}
 
Example #11
Source File: UnionParameterTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public ParameterExample(
      @ElementUnion({
         @Element(name="int", type=Integer.class),
         @Element(name="double", type=Double.class),
         @Element(name="string", type=String.class)
      })
      Object value)
{
   this.value = value;
   this.name = "[NOT SET]";
}
 
Example #12
Source File: ConstructorInjectionWithMissingValuesTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unused")
public ManyConstructorsWithMissingValues(
      @Element(name="a", required=false) String a)
{
   this.a = a;
   this.b = "Default B";
   this.c = "Default C";
   this.d = 10;
}
 
Example #13
Source File: UnionWithSameNamesAndDifferentPathsTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public AmbiguousConstructorParameterExample(
      @Element(name="b") String ambiguous, 
      @Path("path[2]") @Element(name="b") String y)
{
   this.y = y;
   this.x = ambiguous;
}
 
Example #14
Source File: UnionParameterTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public ParameterExample(
      @Element(name="name")
      String name,
      
      @ElementUnion({
         @Element(name="int", type=Integer.class),
         @Element(name="double", type=Double.class),
         @Element(name="string", type=String.class)
      })
      Object value)
{
   this.value = value;
   this.name = name;
}
 
Example #15
Source File: MethodScannerDefaultTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public void testMixedAnnotations() throws Exception {
   Map<String, Contact> map = getContacts(MixedAnnotations.class);
   
   assertFalse(map.get("array").isReadOnly());
   assertFalse(map.get("map").isReadOnly());
   assertFalse(map.get("name").isReadOnly());      
   assertFalse(map.get("value").isReadOnly());
   
   assertEquals(String[].class, map.get("array").getType());
   assertEquals(Map.class, map.get("map").getType());
   assertEquals(int.class, map.get("value").getType());      
   assertEquals(String.class, map.get("name").getType());
   
   assertEquals(Attribute.class, map.get("name").getAnnotation().annotationType());
   assertEquals(Element.class, map.get("value").getAnnotation().annotationType());
   assertEquals(ElementMap.class, map.get("map").getAnnotation().annotationType());
   assertEquals(ElementArray.class, map.get("array").getAnnotation().annotationType());
   
   assertEquals(Attribute.class, map.get("name").getAnnotation(Attribute.class).annotationType());
   assertEquals(Element.class, map.get("value").getAnnotation(Element.class).annotationType());
   assertEquals(ElementMap.class, map.get("map").getAnnotation(ElementMap.class).annotationType());
   assertEquals(ElementArray.class, map.get("array").getAnnotation(ElementArray.class).annotationType());
   
   assertNull(map.get("name").getAnnotation(Root.class));
   assertNull(map.get("value").getAnnotation(Root.class));
   assertNull(map.get("map").getAnnotation(Root.class));
   assertNull(map.get("array").getAnnotation(Root.class));
}
 
Example #16
Source File: ConstructorInjectionAdjustmentFactorTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unused")
public ConstructorAdjustmentExample(
      @Element(name="a") String a,
      @Element(name="b") String b)
{
   this.a = a;
   this.b = b;
}
 
Example #17
Source File: ConstructorInjectionWithMissingValuesTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unused")
public ManyConstructorsWithMissingValues(
   @Element(name="a", required=false) String a,
   @Element(name="b", required=false) String b,
   @Element(name="c", required=false) String c,
   @Element(name="d") int d)
{
   this.a = a;
   this.b = b;
   this.c = c;
   this.d = d;
}
 
Example #18
Source File: ConstructorInjectionWithMissingValuesTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unused")
public ManyConstructorsWithMissingValues(
   @Element(name="a", required=false) String a,
   @Element(name="d") int d)
{
   this.a = a;
   this.b = "Default B";
   this.c = "Default C";
   this.d = d;
}
 
Example #19
Source File: UnionWithSameNamesAndDifferentPathsTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public Example(
      @Path("path[1]") @Element(name="b") String x, 
      @Path("path[2]") @Element(name="b") String y)
{
   this.y = y;
   this.x = x;
}
 
Example #20
Source File: MethodTest.java    From simplexml with Apache License 2.0 4 votes vote down vote up
@Element(name="long")
public long getLongValue() {
   return longValue;
}
 
Example #21
Source File: AliasTest.java    From simplexml with Apache License 2.0 4 votes vote down vote up
public MultiValueEntry(@Attribute(name="name") String name,
                        @Element(name="value") String value) {
    this.name = name;
    this.value = value;            
}
 
Example #22
Source File: QueryResult.java    From Saiy-PS with GNU Affero General Public License v3.0 4 votes vote down vote up
public QueryResult(@Attribute(name = "datatypes") final String datatypes,
                   @Attribute(name = "success") final boolean success,
                   @Attribute(name = "nodata") final boolean noData,
                   @Attribute(name = "error") final boolean error,
                   @Attribute(name = "numpods") final int numpods,
                   @Attribute(name = "timedout") final String timedout,
                   @Attribute(name = "timedoutpods") final String timedoutpods,
                   @Attribute(name = "timing") final double timing,
                   @Attribute(name = "parsetimedout") final boolean parsetimedout,
                   @Attribute(name = "parsetiming") final double parsetiming,
                   @Attribute(name = "recalculate") final String recalculate,
                   @Attribute(name = "id") final String id,
                   @Attribute(name = "host") final String host,
                   @Attribute(name = "server") final int server,
                   @Attribute(name = "related") final String related,
                   @Attribute(name = "version") final double version,
                   @ElementList(inline = true, name = "pod") final List<Pod> pods,
                   @Element(name = "assumptions") final Assumptions assumptions,
                   @Element(name = "sources", required = false) final Sources sources,
                   @Element(name = "warnings", required = false) final Warnings warnings) {
    this.datatypes = datatypes;
    this.success = success;
    this.noData = noData;
    this.error = error;
    this.numpods = numpods;
    this.timedout = timedout;
    this.timedoutpods = timedoutpods;
    this.timing = timing;
    this.parsetimedout = parsetimedout;
    this.parsetiming = parsetiming;
    this.recalculate = recalculate;
    this.id = id;
    this.host = host;
    this.server = server;
    this.related = related;
    this.version = version;
    this.pods = pods;
    this.assumptions = assumptions;
    this.sources = sources;
    this.warnings = warnings;
}
 
Example #23
Source File: PathCaseTest.java    From simplexml with Apache License 2.0 4 votes vote down vote up
public CaseExample(
      @Path("SomePath[1]") @Element(name="a") String a, 
      @Path("somePath[1]") @Element(name="b") String b) {
   this.a = a;
   this.b = b;
}
 
Example #24
Source File: DecoratorTest.java    From simplexml with Apache License 2.0 4 votes vote down vote up
public Member(@Attribute(name="name") String name, @Attribute(name="age") int age, @Element(name="address") Address address) {
    this.address = address;
    this.name = name;
    this.age = age;
}
 
Example #25
Source File: MethodScannerTest.java    From simplexml with Apache License 2.0 4 votes vote down vote up
@Element(name="version")
public int getVersion() {
   return version;
}
 
Example #26
Source File: PathAsNamespacePrefixTest.java    From simplexml with Apache License 2.0 4 votes vote down vote up
public BringPrefixInScope(@Element(name="example") PathWithPrefixOutOfScope example){
   this.example = example;
}
 
Example #27
Source File: EmptyTest.java    From simplexml with Apache License 2.0 4 votes vote down vote up
@Element
private void setEmpty(String text) {
   this.text = text;
}
 
Example #28
Source File: EmptyTest.java    From simplexml with Apache License 2.0 4 votes vote down vote up
@Element
private String getEmpty() {
   return text;
}
 
Example #29
Source File: Info.java    From Saiy-PS with GNU Affero General Public License v3.0 4 votes vote down vote up
public Info(@Element(name = "link", required = false) final Link link,
            @Element(name = "units", required = false) final Units units) {
    this.link = link;
    this.units = units;
}
 
Example #30
Source File: EnumTest.java    From simplexml with Apache License 2.0 4 votes vote down vote up
public EnumBug(@Element(name="type") PartType type) {
   this.type = type;
}