commit | author | age
|
ca8e1e
|
1 |
/* |
U |
2 |
Transit - Remote procedure calls made simple |
|
3 |
Copyright (c) 2012 Ulrich Hilger |
|
4 |
|
|
5 |
This program is free software: you can redistribute it and/or modify |
|
6 |
it under the terms of the GNU Affero General Public License as published by |
|
7 |
the Free Software Foundation, either version 3 of the License, or |
|
8 |
(at your option) any later version. |
|
9 |
|
|
10 |
This program is distributed in the hope that it will be useful, |
|
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
GNU Affero General Public License for more details. |
|
14 |
|
|
15 |
You should have received a copy of the GNU Affero General Public License |
|
16 |
along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17 |
*/ |
|
18 |
|
|
19 |
package de.uhilger.transit; |
|
20 |
|
|
21 |
import java.util.List; |
|
22 |
|
|
23 |
import com.thoughtworks.xstream.XStream; |
|
24 |
import com.thoughtworks.xstream.io.xml.StaxDriver; |
|
25 |
|
|
26 |
public abstract class AbstractWandler implements Wandler { |
|
27 |
|
|
28 |
/** |
|
29 |
* Ein Java-Objekt in das Format verwandeln, das dieser Wandler |
|
30 |
* verarbeitet |
|
31 |
* |
|
32 |
* @param obj das Objekt, das verwandelt werden soll |
|
33 |
* @param typ der Datentyp, von dem verwandelt werden soll |
|
34 |
* @return das Objekt, in das das uebergebene Objekt verwandelt wurde |
|
35 |
*/ |
|
36 |
public Object vonJava(Object obj, Class typ) { |
|
37 |
XStream xstream = getXStream(); |
|
38 |
aliasSubtypes(xstream, obj); |
|
39 |
xstream.alias(typ.getSimpleName(), typ); |
|
40 |
String xml = xstream.toXML(obj); |
|
41 |
return xml; |
|
42 |
} |
|
43 |
|
|
44 |
protected abstract XStream getXStream(); |
|
45 |
|
|
46 |
protected void aliasSubtypes(XStream xstream, Object obj) { |
|
47 |
if(obj instanceof List) { |
|
48 |
List list = (List) obj; |
|
49 |
if(list.size() > 0) { |
|
50 |
Object subobj = list.get(0); |
|
51 |
if(subobj instanceof List) { |
|
52 |
List sublist = (List) subobj; |
|
53 |
if(sublist.size() > 0) { |
|
54 |
Class subsubtyp = sublist.get(0).getClass(); |
|
55 |
xstream.alias(subsubtyp.getSimpleName(), subsubtyp); |
|
56 |
} |
|
57 |
} |
|
58 |
Class subtyp = list.get(0).getClass(); |
|
59 |
xstream.alias(subtyp.getSimpleName(), subtyp); |
|
60 |
} |
|
61 |
} |
|
62 |
} |
|
63 |
|
|
64 |
/** |
|
65 |
* Das Format, das dieser Wandler verarbeitet, zu Java wandeln |
|
66 |
* |
|
67 |
* @param obj das Objekt, das verwandelt werden soll |
|
68 |
* @param typ der Datentyp, in den verwandelt werden soll |
|
69 |
* @return das Objekt, in das das uebergebene Objekt verwandelt wurde |
|
70 |
*/ |
|
71 |
public Object zuJava(Object obj, Class typ) { |
|
72 |
//XStream xstream = new XStream(new StaxDriver()); |
|
73 |
XStream xstream = getXStream(); |
|
74 |
xstream.alias(typ.getSimpleName(), typ); |
|
75 |
Object newObject = xstream.fromXML(obj.toString()); |
|
76 |
return newObject; |
|
77 |
} |
|
78 |
|
|
79 |
/** |
|
80 |
* Das Format ermitteln, das dieser Wandler verarbeitet |
|
81 |
* @return das Format, das dieser Wandler verarbeitet |
|
82 |
*/ |
|
83 |
public abstract String getFormat(); |
|
84 |
|
|
85 |
/** |
|
86 |
* Pruefen, ob ein anderes Objekt gleich diesem Objekt ist |
|
87 |
* @param other das Objekt, mit dem dieses Objekt verglichen werden soll |
|
88 |
* @return true, wenn beide Objekte gleich sind, false wenn nicht |
|
89 |
*/ |
|
90 |
public boolean equals(Object other) { |
|
91 |
boolean isEqual = false; |
|
92 |
if(other != null && other instanceof Wandler) { |
|
93 |
Wandler w = (Wandler) other; |
|
94 |
isEqual = this.getFormat().equals(w.getFormat()); |
|
95 |
} |
|
96 |
return isEqual; |
|
97 |
} |
|
98 |
|
|
99 |
public void aufloesen() { |
|
100 |
// hier muss bei diesem Wandler nichts geschehen |
|
101 |
} |
|
102 |
|
|
103 |
} |