1
2 /***
3 * Copyright (c) 2004, Tobias Philipp
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without modification,
7 * are permitted provided that the following conditions are met:
8 *
9 * - Redistributions of source code must retain the above copyright notice, this
10 * list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright notice, this
12 * list of conditions and the following disclaimer in the documentation and/or
13 * other materials provided with the distribution.
14 * - Neither the name of the Programmers United nor the names of its contributors
15 * may be used to endorse or promote products derived from this software without
16 * specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
27 * OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 package org.programmersunited.xml;
31
32
33 import java.util.ArrayList;
34
35 import java.lang.reflect.*;
36 import java.lang.annotation.*;
37
38 import org.jdom.Document;
39 import org.jdom.Element;
40 import org.jdom.Attribute;
41 import org.jdom.Content;
42 import org.jdom.output.XMLOutputter;
43
44
45 interface XmlSerializerPlugin {
46 Content invoke(Object obj, Field field, Annotation annotation) throws IllegalAccessException;
47 Class getAnnotationType();
48 }
49
50 class XmlElementPlugin {
51 public Content invoke(Object obj, Field field, Annotation annotation) throws IllegalAccessException {
52 XmlElement xmlElementAnnotation = (XmlElement) annotation;
53 Element element = new Element(xmlElementAnnotation.name());
54 element.addContent((String) field.get(obj));
55
56 return element;
57 }
58
59 public Class getAnnotationType() {
60 return XmlElement.class;
61 }
62 }
63
64
65
66
67
68
69
70
71
72
73
74 /***
75 *
76 *
77 * @author <a href="mail@tphilipp.de">Tobias Philipp</a>
78 *
79 */
80 public class XmlSerializer {
81 ArrayList<XmlSerializerPlugin> plugins;
82
83 public XmlSerializer() {
84 plugins = new ArrayList();
85 }
86
87
88 /***
89 *
90 * @param obj
91 * @return The generated Document
92 */
93 public Document toDom(Object obj) {
94 try {
95 Class cls = obj.getClass();
96
97 XmlRootElement xmlRootElementAnnotation = (XmlRootElement) cls.getAnnotation(XmlRootElement.class);
98 Element rootElement = new Element(xmlRootElementAnnotation.name());
99
100 for (Field aField : cls.getDeclaredFields()) {
101 for (Annotation annotation : aField.getAnnotations()) {
102
103 if (XmlElement.class == annotation.annotationType()) {
104 XmlElement xmlElement = (XmlElement) annotation;
105 Element element = new Element(xmlElement.name());
106 element.addContent((String) aField.get(obj));
107 rootElement.addContent(element);
108 }
109 }
110
111
112
113
114
115
116
117
118
119
120
121
122 }
123
124 return new Document(rootElement);
125
126 } catch (Exception e) {
127 System.out.println("NMo");
128 return null;
129 }
130 }
131 }