forked from highsource/jaxb2-basics
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Context.java
223 lines (198 loc) · 5.55 KB
/
Context.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package org.example.platce;
import static jakarta.xml.bind.JAXBContext.newInstance;
import static org.example.platce.util.XmlTypeUtils.getNamespace;
import static org.example.platce.util.XmlTypeUtils.isJAXBElement;
import static org.example.platce.util.XmlTypeUtils.isXmlRootElement;
import static org.jvnet.basicjaxb.xml.bind.model.util.XmlTypeUtils.getTypeName;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBElement;
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.Marshaller;
import jakarta.xml.bind.Unmarshaller;
public class Context
{
public static final Class<org.example.platce.Response> RESPONSE_CLASS = org.example.platce.Response.class;
public static final Class<org.example.platce.MethodXX1> METHODXX1_CLASS = org.example.platce.MethodXX1.class;
public static final Class<org.example.platce.MethodXX2> METHODXX2_CLASS = org.example.platce.MethodXX2.class;
public static final String RESPONSE_NS = getNamespace(RESPONSE_CLASS.getPackage());
public static final QName METHOD_QNAME = new QName(RESPONSE_NS, "method");
public static final QName METHODXX1_QNAME = getTypeName(METHODXX1_CLASS);
public static final QName METHODXX2_QNAME = getTypeName(METHODXX2_CLASS);
public static JAXBContext MAIN_CONTEXT = null;
static
{
try
{
MAIN_CONTEXT = newInstance
(
RESPONSE_CLASS,
METHODXX1_CLASS,
METHODXX2_CLASS
);
}
catch (JAXBException ex)
{
ex.printStackTrace();
}
}
private static final DocumentBuilderFactory DBF = DocumentBuilderFactory.newInstance();
public Context()
{
}
public static Unmarshaller createMainUnmarshaller()
{
return createUnmarshaller(MAIN_CONTEXT);
}
public static Marshaller createMainMarshaller()
{
return createMarshaller(MAIN_CONTEXT);
}
public static Object mainUnmarshal(Element element, Class<?> clazz)
{
return unmarshal(MAIN_CONTEXT, element, clazz);
}
public static Document mainMarshal(Object obj)
{
return marshal(MAIN_CONTEXT, obj);
}
public static Element mainMarshalWrap(Object value)
{
Document doc = mainMarshal(mainWrap(value));
return doc.getDocumentElement();
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Object mainWrap(Object value)
{
Object wrap = null;
if ( value != null )
{
if ( isXmlRootElement(value) || isJAXBElement(value) )
wrap = value;
else
{
if ( METHODXX1_CLASS.equals(value.getClass()) )
wrap = new JAXBElement(METHODXX1_QNAME, METHODXX1_CLASS, value);
else if ( METHODXX2_CLASS.equals(value.getClass()) )
wrap = new JAXBElement(METHODXX2_QNAME, METHODXX2_CLASS, value);
}
}
return wrap;
}
public static Element mainMarshalWrapAsMethod(Object value)
{
Document doc = mainMarshal(mainWrapAsMethod(value));
return doc.getDocumentElement();
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Object mainWrapAsMethod(Object value)
{
Object wrap = null;
if ( value != null )
{
if ( isJAXBElement(value) )
value = ((JAXBElement) value).getValue();
if ( METHODXX1_CLASS.equals(value.getClass()) )
wrap = new JAXBElement(METHOD_QNAME, METHODXX1_CLASS, value);
else if ( METHODXX2_CLASS.equals(value.getClass()) )
wrap = new JAXBElement(METHOD_QNAME, METHODXX2_CLASS, value);
}
return wrap;
}
public static Object mainUnmarshalByName(Element element)
{
Object value = null;
String name = element.getAttribute("name");
switch (name)
{
case "platce-info":
value = mainUnmarshal(element, METHODXX1_CLASS);
break;
case "platce-data":
value = mainUnmarshal(element, METHODXX2_CLASS);
break;
default:
value = element;
break;
}
return value;
}
@SuppressWarnings("rawtypes")
private static Object unmarshal(JAXBContext jc, Element element, Class<?> clazz)
{
Object obj = null;
if ( jc != null )
{
try
{
Unmarshaller unm = createUnmarshaller(jc);
obj = unm.unmarshal(element, clazz);
if ( obj instanceof JAXBElement )
obj = ((JAXBElement) obj).getValue();
}
catch (JAXBException ex)
{
throw new IllegalAccessError(createMessage(ex, obj));
}
}
return obj;
}
private static Document marshal(JAXBContext jc, Object obj)
{
Document doc = null;
if ( jc != null )
{
try
{
DocumentBuilder builder = DBF.newDocumentBuilder();
doc = builder.newDocument();
Marshaller mar = createMarshaller(jc);
mar.marshal(obj, doc);
}
catch (JAXBException | ParserConfigurationException ex)
{
throw new IllegalAccessError(createMessage(ex, obj));
}
}
return doc;
}
private static Unmarshaller createUnmarshaller(JAXBContext jc)
{
try
{
return jc.createUnmarshaller();
}
catch (JAXBException ex)
{
throw new IllegalAccessError(createMessage(ex));
}
}
private static Marshaller createMarshaller(JAXBContext jc)
{
try
{
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
return marshaller;
}
catch (JAXBException ex)
{
throw new IllegalAccessError(createMessage(ex));
}
}
private static String createMessage(Exception ex)
{
Throwable th = (ex.getCause() != null) ? ex.getCause() : ex;
String msg = th.getClass().getSimpleName() + ": " + th.getMessage();
return msg;
}
private static String createMessage(Exception ex, Object obj)
{
return createMessage(ex) + "(" + obj + ")";
}
}