如何从使用Simple的自定义Converter访问字段注释? [英] How do you access field annotations from a custom Converter with Simple?

查看:82
本文介绍了如何从使用Simple的自定义Converter访问字段注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Simple(http://simple.sourceforge.net/)库来编组/解组Java中的XML数据。对于我的一些更复杂的数据结构,我需要编写自己的转换器。例如,假设我有一个列表< List< String>> 我需要编组。我写了以下内容:

I'm using the Simple (http://simple.sourceforge.net/) library to marshall/unmarshall XML data in Java. For some of my more complex data structures, I need to write my own Converters. For instance, say I have a List<List<String>> that I need to marshall. I've written the following:

class WorldObject {

   @Element(name="vector-names")
   @Convert(ListListConverter.class)
   private List<List<String>> vectorNames;

   /** Constructor and other details ... **/

}

与ListListConverter一起(我暂时省略了unmarshaller):

Along with the ListListConverter (I've left out the unmarshaller for the moment):

class ListListConverter implements Converter<List<List<String>>> {

   @Override
   public List<List<String>> read(InputNode node) throws Exception {
      // stub
      return null;
   }   

   @Override
   public void write(OutputNode node, List<List<String>> value)
         throws Exception {

      node.setName("list-list-string");

      for (List<String> list : value) {
         OutputNode subList = node.getChild("list-string");

         for (String str : list) {
            OutputNode stringNode = subList.getChild("string");
            stringNode.setValue(str);
         }

         subList.commit();
      }

      node.commit();

   }

}

此设置有效很好,并生成我想要的XML。但是,我希望能够访问 @Element 注释的名称字段,以便我可以为指定的名称(在这种情况下,vector-names)而不是默认名称(list-list-string)。这就是编组适用于Simple处理开箱即用的所有类型的方式,因此必须有一种方法可以从自定义转换器访问该数据。

This setup works fine, and produces the XML I want. I would, however, like to have access to the @Element annotation's name field so that I can give the tags the specified name (in this case, "vector-names") rather than the default name ("list-list-string"). This is how marshalling works for all the types that Simple handles out of the box, so there must be a way to access that data from a custom Converter.

我该怎么办?完成这个?

How can I accomplish this?

推荐答案

不能以这种方式获取注释,因为它无法通过字段 - 转换器中的字段。

解决方案是写一个 WorldObject - 转换器 - 即使你只想写一个字段。

You can't get the annotation that way, because it's not accessible through the field in the field-converter.
The solution is to write a WorldObject-Converter - even if you only want to write a single field.

@Root
@Convert(WorldObjectConverter.class) // specify converter for this class
public class WorldObject
{
    @Element(name = "vector-names")
    private List<List<String>> vectorNames;

    // only for the example below - write whatever constructor(s) you need
    public WorldObject()
    {
        this.vectorNames = new ArrayList<>();
    }

    // constructor, getter / setter, etc.


    // a getter is required to access the field in the converter.
    public List<List<String>> getVectorNames()
    {
        return vectorNames;
    }
}



WorldObjectConverter class:



WorldObjectConverter class:

public class WorldObjectConverter implements Converter<WorldObject>
{
    @Override
    public WorldObject read(InputNode node) throws Exception
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }


    @Override
    public void write(OutputNode node, WorldObject value) throws Exception
    {
        final Field f = value.getClass().getDeclaredField("vectorNames"); // get the field 'vectorNames' of the 'WorldObject' class
        final Element elementAnnotation = f.getAnnotation(Element.class); // get the 'Element' annotation of the Field

        final String name = elementAnnotation.name(); // get the 'name'-value of the annotation
        node.setName(name); // set Nodename


        for( List<String> list : value.getVectorNames() )
        {
            OutputNode subList = node.getChild("list-string");

            for( String str : list )
            {
                OutputNode stringNode = subList.getChild("string");
                stringNode.setValue(str);
            }

            subList.commit();
        }

        node.commit();
    }
}



示例:



Example:

final File f = new File("test.xml"); // output file

WorldObject wo = new WorldObject(); // the object to serialize

// some testdata ...
List<String> l = new ArrayList<>();
l.add("a");
l.add("b");
wo.getVectorNames().add(l);

l = new ArrayList<>();
l.add("c");
l.add("d");
wo.getVectorNames().add(l);


// create the serializer - dont forget the AnnotationStrategy!
Serializer ser = new Persister(new AnnotationStrategy());
ser.write(wo, f); // serialize it to file



输出:



Output:

<vector-names>
   <list-string>
      <string>a</string>
      <string>b</string>
   </list-string>
   <list-string>
      <string>c</string>
      <string>d</string>
   </list-string>
</vector-names>

完成!

这篇关于如何从使用Simple的自定义Converter访问字段注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆