更新为2012年11月5日:
通过org.simpleframework.xml笔者答: 这工作
https://simple.svn.sourceforge.net/svnroot/simple/trunk/download/stream/src/test/java/org/simpleframework/xml/strategy/CommentTest.java
package org.simpleframework.xml.strategy;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import org.simpleframework.xml.Default;
import org.simpleframework.xml.Root;
import org.simpleframework.xml.ValidationTestCase;
import org.simpleframework.xml.core.Persister;
import org.simpleframework.xml.stream.InputNode;
import org.simpleframework.xml.stream.NodeMap;
import org.simpleframework.xml.stream.OutputNode;
public class CommentTest extends ValidationTestCase {
@Retention(RetentionPolicy.RUNTIME)
private static @interface Comment {
public String value();
}
@Root
@Default
private static class CommentExample {
@Comment("This represents the name value")
private String name;
@Comment("This is a value to be used")
private String value;
@Comment("Yet another comment")
private Double price;
}
private static class CommentVisitor implements Visitor {
public void read(Type type, NodeMap<InputNode> node) throws Exception {}
public void write(Type type, NodeMap<OutputNode> node) throws Exception {
if(!node.getNode().isRoot()) {
Comment comment = type.getAnnotation(Comment.class);
if(comment != null) {
node.getNode().setComment(comment.value());
}
}
}
}
public void testComment() throws Exception {
Visitor visitor = new CommentVisitor();
Strategy strategy = new VisitorStrategy(visitor);
Persister persister = new Persister(strategy);
CommentExample example = new CommentExample();
example.name = "Some Name";
example.value = "A value to use";
example.price = 9.99;
persister.write(example, System.out);
}
}
更新为2012-11-01 20:16
这是一个似乎得到预期的效果解决方法 - 在必要FieldHelper在(Get the value of a field, given the hierarchical path)
/**
* write according to this visitor
*/
public void write(Type type, NodeMap<OutputNode> node) {
OutputNode element = node.getNode();
Class ctype = type.getType();
String comment = ctype.getName();
if (!element.isRoot()) {
FieldHelper fh = new FieldHelper();
element.setComment(comment);
try {
if (type.getClass().getSimpleName().startsWith("Override")) {
type = (Type) fh.getFieldValue(type, "type");
}
if (type.getClass().getSimpleName().startsWith("Field")) {
Field field = (Field) fh.getFieldValue(type, "field");
System.out.println(field.getName());
Comment commentAnnotation = field.getAnnotation(Comment.class);
if (commentAnnotation != null) {
element.setComment(commentAnnotation.value());
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这是多远我这个有描述。不幸的是,它不能按预期工作。我写了一封电子邮件给Simpleframwork for XML的作者。
/**
* write according to this visitor
*/
public void write(Type type, NodeMap<OutputNode> node) {
OutputNode element = node.getNode();
Class ctype = type.getType();
String comment = ctype.getName();
if (!element.isRoot()) {
Comment commentAnnotation = type.getAnnotation(Comment.class);
if (commentAnnotation!=null)
element.setComment(commentAnnotation.value());
else
element.setComment(comment);
}
}
@Override
public void read(Type type, NodeMap<InputNode> nodeMap) throws Exception {
}
}
我声明的注释的注释这样的:
package com.bitplan.storage.simplexml;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Comment {
String value();
}
其然后可用这样的:
@Comment("this is the unique identifier")
private long id;
添加访问者是可能是这样的:
/**
* get Serializer
*
* @return
*/
public Serializer getSerializer() {
Serializer serializer = null;
Strategy strategy=null;
VisitorStrategy vstrategy=null;
if ((idname != null) && (refname != null)) {
strategy = new CycleStrategy(idname, refname);
}
CommentVisitor cv=new CommentVisitor();
if (strategy==null) {
vstrategy=new VisitorStrategy(cv);
} else {
vstrategy=new VisitorStrategy(cv,strategy);
}
serializer = new Persister(vstrategy);
return serializer;
}
谢谢,如果你能分享你的答案,如果你愿意,你会很酷你可以得到任何。 –
自从sourceforge地址反弹后,我不得不将问题放在邮件列表中。您是否可以在另一篇文章中提出我的Fieldhelper答案 - 这是由编辑人员降低的,因为当我第一次发布它时,它已经有了我的公司网址。 –