1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.core.util.jaxb; |
17 | |
|
18 | |
import javax.xml.bind.MarshalException; |
19 | |
import javax.xml.bind.UnmarshalException; |
20 | |
import javax.xml.bind.annotation.adapters.NormalizedStringAdapter; |
21 | |
import javax.xml.bind.annotation.adapters.XmlAdapter; |
22 | |
|
23 | |
import org.apache.commons.lang.StringUtils; |
24 | |
import org.kuali.rice.core.api.CoreApiServiceLocator; |
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | 0 | public class NameAndNamespacePairValidatingAdapter extends XmlAdapter<NameAndNamespacePair,NameAndNamespacePair> { |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
@Override |
39 | |
public NameAndNamespacePair unmarshal(NameAndNamespacePair v) throws Exception { |
40 | 0 | if (v != null) { |
41 | |
|
42 | 0 | if (StringUtils.isBlank(v.getName())) { |
43 | 0 | throw new UnmarshalException("Cannot import a name-and-namespace pair with a blank name"); |
44 | 0 | } else if (StringUtils.isBlank(v.getNamespaceCode())) { |
45 | 0 | throw new UnmarshalException("Cannot import a name-and-namespace pair with a blank namespace code"); |
46 | 0 | } if (CoreApiServiceLocator.getNamespaceService().getNamespace(v.getNamespaceCode()) == null) { |
47 | 0 | throw new UnmarshalException("Cannot import a name-and-namespace pair with invalid or unknown namespace \"" + |
48 | |
v.getNamespaceCode() + "\""); |
49 | |
} |
50 | |
|
51 | 0 | v.setName(new NormalizedStringAdapter().unmarshal(v.getName())); |
52 | 0 | v.setNamespaceCode(v.getNamespaceCode()); |
53 | |
} |
54 | 0 | return v; |
55 | |
} |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
@Override |
61 | |
public NameAndNamespacePair marshal(NameAndNamespacePair v) throws Exception { |
62 | 0 | if (v != null) { |
63 | 0 | if (StringUtils.isBlank(v.getName())) { |
64 | 0 | throw new MarshalException("Cannot export a name-and-namespace pair with a blank name"); |
65 | 0 | } else if (StringUtils.isBlank(v.getNamespaceCode())) { |
66 | 0 | throw new MarshalException("Cannot export a name-and-namespace pair with a blank namespace code"); |
67 | 0 | } else if (CoreApiServiceLocator.getNamespaceService().getNamespace(v.getNamespaceCode()) == null) { |
68 | 0 | throw new MarshalException("Cannot export a name-and-namespace pair with invalid or unknown namespace \"" + v.getNamespaceCode() + "\""); |
69 | |
} |
70 | |
|
71 | 0 | v.setName(new NormalizedStringAdapter().marshal(v.getName())); |
72 | 0 | v.setNamespaceCode(v.getNamespaceCode()); |
73 | |
} |
74 | 0 | return v; |
75 | |
} |
76 | |
|
77 | |
} |