1
|
|
|
/* |
2
|
|
|
* Copyright 2014, Armenak Grigoryan, and individual contributors as indicated |
3
|
|
|
* by the @authors tag. See the copyright.txt in the distribution for a |
4
|
|
|
* full listing of individual contributors. |
5
|
|
|
* |
6
|
|
|
* This is free software; you can redistribute it and/or modify it |
7
|
|
|
* under the terms of the GNU Lesser General Public License as |
8
|
|
|
* published by the Free Software Foundation; either version 2.1 of |
9
|
|
|
* the License, or (at your option) any later version. |
10
|
|
|
* |
11
|
|
|
* This software is distributed in the hope that it will be useful, |
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14
|
|
|
* Lesser General Public License for more details. |
15
|
|
|
*/ |
16
|
|
|
package com.strider.datadefender.requirement; |
17
|
|
|
|
18
|
|
|
import com.strider.datadefender.requirement.registry.ClassAndFunctionRegistry; |
19
|
|
|
import javax.xml.bind.annotation.adapters.XmlAdapter; |
20
|
|
|
|
21
|
|
|
import org.apache.commons.lang3.ClassUtils; |
22
|
|
|
import org.apache.commons.lang3.StringUtils; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* XmlAdapter to get a Class type from a String, and vice-versa. |
26
|
|
|
* |
27
|
|
|
* @author Zaahid Bateson |
28
|
|
|
*/ |
29
|
|
|
public class ClassAdapter extends XmlAdapter<String, Class<?>> { |
30
|
|
|
|
31
|
|
|
private ClassAndFunctionRegistry registry; |
32
|
|
|
|
33
|
|
|
public ClassAdapter() { |
34
|
|
|
this(ClassAndFunctionRegistry.singleton()); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public ClassAdapter(ClassAndFunctionRegistry registry) { |
38
|
|
|
this.registry = registry; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
@Override |
42
|
|
|
public Class<?> unmarshal(String value) throws Exception { |
43
|
|
|
String t = StringUtils.trimToEmpty(value); |
44
|
|
|
if (StringUtils.isBlank(t)) { |
45
|
|
|
return String.class; |
46
|
|
|
} |
47
|
|
|
return registry.getClassForName(value); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
@Override |
51
|
|
|
public String marshal(Class<?> bt) throws Exception { |
52
|
|
|
return registry.getNameForClass(bt); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|