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.coreservice.api.CoreServiceApiServiceLocator;
25
26
27
28
29
30
31
32
33 public class NameAndNamespacePairValidatingAdapter extends XmlAdapter<NameAndNamespacePair,NameAndNamespacePair> {
34
35
36
37
38 @Override
39 public NameAndNamespacePair unmarshal(NameAndNamespacePair v) throws Exception {
40 if (v != null) {
41
42 if (StringUtils.isBlank(v.getName())) {
43 throw new UnmarshalException("Cannot import a name-and-namespace pair with a blank name");
44 } else if (StringUtils.isBlank(v.getNamespaceCode())) {
45 throw new UnmarshalException("Cannot import a name-and-namespace pair with a blank namespace code");
46 } if (CoreServiceApiServiceLocator.getNamespaceService().getNamespace(v.getNamespaceCode()) == null) {
47 throw new UnmarshalException("Cannot import a name-and-namespace pair with invalid or unknown namespace \"" +
48 v.getNamespaceCode() + "\"");
49 }
50
51 v.setName(new NormalizedStringAdapter().unmarshal(v.getName()));
52 v.setNamespaceCode(v.getNamespaceCode());
53 }
54 return v;
55 }
56
57
58
59
60 @Override
61 public NameAndNamespacePair marshal(NameAndNamespacePair v) throws Exception {
62 if (v != null) {
63 if (StringUtils.isBlank(v.getName())) {
64 throw new MarshalException("Cannot export a name-and-namespace pair with a blank name");
65 } else if (StringUtils.isBlank(v.getNamespaceCode())) {
66 throw new MarshalException("Cannot export a name-and-namespace pair with a blank namespace code");
67 } else if (CoreServiceApiServiceLocator.getNamespaceService().getNamespace(v.getNamespaceCode()) == null) {
68 throw new MarshalException("Cannot export a name-and-namespace pair with invalid or unknown namespace \"" + v.getNamespaceCode() + "\"");
69 }
70
71 v.setName(new NormalizedStringAdapter().marshal(v.getName()));
72 v.setNamespaceCode(v.getNamespaceCode());
73 }
74 return v;
75 }
76
77 }