Created
April 23, 2013 18:23
-
-
Save afterhill/5446066 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package exercise; | |
| @Exportable("address") | |
| public class AddressForTest { | |
| @Persistent | |
| private String country = null; | |
| @Persistent | |
| private String province = null; | |
| @Persistent | |
| private String city = null; | |
| @Persistent | |
| private String street = null; | |
| @Persistent | |
| private String door = null; | |
| public AddressForTest(String country, String province, String city, | |
| String street, String door) { | |
| this.country = country; | |
| this.province = province; | |
| this.city = city; | |
| this.street = street; | |
| this.door = door; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package exercise; | |
| import java.util.ArrayList; | |
| @Exportable(name="addressList", description="address list") | |
| public class AddressListForTest { | |
| @Persistent | |
| private String friendName = null; | |
| @Persistent | |
| private int age = 0; | |
| @Persistent | |
| private ArrayList<String> telephone = null; | |
| @Persistent | |
| private ArrayList<AddressForTest> addressForTest = null; | |
| @Persistent | |
| private String note = null; | |
| public AddressListForTest(String friendName, int age, ArrayList<String> telephone, | |
| ArrayList<AddressForTest> addressList, String note) { | |
| this.friendName = friendName; | |
| this.age = age; | |
| this.telephone = telephone; | |
| this.addressForTest = addressList; | |
| this.note = note; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package exercise; | |
| import java.lang.annotation.Retention; | |
| import java.lang.annotation.RetentionPolicy; | |
| @Retention(RetentionPolicy.RUNTIME) | |
| public @interface Exportable { | |
| String name() default ""; | |
| String value() default ""; | |
| String description() default ""; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package exercise; | |
| import java.lang.reflect.Field; | |
| import java.util.ArrayList; | |
| import java.util.Collection; | |
| import java.util.Iterator; | |
| import java.util.Map; | |
| public class ExportToXML { | |
| public void exportFields(Object obj) throws IllegalArgumentException, | |
| IllegalAccessException { | |
| Exportable exportable = obj.getClass().getAnnotation(Exportable.class); | |
| if (exportable != null) { | |
| if (exportable.value().length() > 0) { | |
| //System.out.println("Class annoation Name: " + exportable.name()); | |
| } else { | |
| //System.out.println("Class annoation Name: " | |
| // + exportable.value()); | |
| } | |
| } else { | |
| //System.out.println(obj.getClass().getName() | |
| // + " class does not yet having" + " an annotation!"); | |
| } | |
| Field[] fields = obj.getClass().getDeclaredFields(); | |
| for (Field field : fields) { | |
| Persistent fieldAnnotation = field.getAnnotation(Persistent.class); | |
| if (fieldAnnotation == null) { | |
| continue; | |
| } | |
| field.setAccessible(true); | |
| Class<?> typeClass = field.getType(); | |
| String name = field.getName(); | |
| String value = getFieldValue(field, typeClass, obj); | |
| if (value != null) { | |
| System.out | |
| .println(getIndent() + "<" + name + ">\n" + getIndent() | |
| + "\t" + value + "\n" + getIndent() + "</" | |
| + name + ">"); | |
| } else if ((field.get(obj) instanceof Collection) | |
| || (field.get(obj) instanceof Map)) { | |
| exportObject(field.get(obj)); | |
| } | |
| } | |
| } | |
| private void exportObject(Object object) throws IllegalArgumentException, | |
| IllegalAccessException { | |
| Exportable exportable = null; | |
| String elementName = null; | |
| if (cyclicChecker.contains(object)) { | |
| return; | |
| } | |
| cyclicChecker.add(object); | |
| if (object instanceof Collection) { | |
| Iterator<?> i = ((Collection<?>) object).iterator(); | |
| for (; i.hasNext();) { | |
| exportObject(i.next()); | |
| } | |
| } else if (object instanceof Map) { | |
| for (Iterator<?> i = ((Map<?, ?>) object).keySet().iterator(); i | |
| .hasNext();) { | |
| exportObject(i.next()); | |
| } | |
| } else { | |
| exportable = object.getClass().getAnnotation(Exportable.class); | |
| if (exportable != null) { | |
| if (exportable.value().length() > 0) { | |
| elementName = exportable.value(); | |
| } else { | |
| elementName = exportable.name(); | |
| } | |
| } | |
| if (exportable == null || elementName.length() == 0) { | |
| elementName = object.getClass().getSimpleName(); | |
| } | |
| System.out.println(getIndent() + "<" + elementName + ">"); | |
| levelDepth++; | |
| if (exportable == null) { | |
| System.out.println(getIndent() + object.toString()); | |
| } else { | |
| exportFields(object); | |
| } | |
| levelDepth--; | |
| System.out.println(getIndent() + "</" + elementName + ">"); | |
| } | |
| cyclicChecker.remove(object); | |
| } | |
| int levelDepth = 0; | |
| Collection<Object> cyclicChecker = new ArrayList<Object>(); | |
| private String getIndent() { | |
| String s = ""; | |
| for (int i = 0; i < levelDepth; i++) { | |
| s += "\t"; | |
| } | |
| return s; | |
| } | |
| private String getFieldValue(Field field, Class<?> typeClass, Object obj) { | |
| String value = null; | |
| try { | |
| if (typeClass == String.class) | |
| value = (String) field.get(obj); | |
| else if (typeClass == int.class) | |
| value = Integer.toString(field.getInt(obj)); | |
| else if (typeClass == long.class) | |
| value = Long.toString(field.getLong(obj)); | |
| else if (typeClass == float.class) | |
| value = Float.toString(field.getFloat(obj)); | |
| else if (typeClass == double.class) | |
| value = Double.toString(field.getDouble(obj)); | |
| else if (typeClass == short.class) | |
| value = Short.toString(field.getShort(obj)); | |
| else if (typeClass == byte.class) | |
| value = Byte.toString(field.getByte(obj)); | |
| else if (typeClass == char.class) | |
| value = Character.toString(field.getChar(obj)); | |
| else if (typeClass == boolean.class) | |
| value = Boolean.toString(field.getBoolean(obj)); | |
| } catch (Exception e) { | |
| e.printStackTrace(); | |
| value = null; | |
| } | |
| return value; | |
| } | |
| public static void main(String[] args) throws IllegalArgumentException, | |
| IllegalAccessException { | |
| AddressForTest address = new AddressForTest("China", "Beijing", | |
| "Beijing", "Big", "180"); | |
| ExportToXML etx = new ExportToXML(); | |
| ArrayList<String> telephoneList = new ArrayList<String>(); | |
| telephoneList.add("61324124"); | |
| telephoneList.add("13424231"); | |
| ArrayList<AddressForTest> addressList = new ArrayList<>(); | |
| addressList.add(address); | |
| AddressListForTest adl = new AddressListForTest("coolbaby", 18, | |
| telephoneList, addressList, "I LOVE U EVER!"); | |
| etx.exportObject(adl); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package exercise; | |
| import java.lang.annotation.Retention; | |
| import java.lang.annotation.RetentionPolicy; | |
| @Retention(RetentionPolicy.RUNTIME) | |
| public @interface Persistent { | |
| String value() default ""; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment