View Javadoc

1   /*
2    * Copyright 2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl1.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * An XML adapter that simply validates the NameAndNamespacePair to ensure that the name and namespace are non-blank
28   * and that the namespace code maps to a valid namespace in the system. This adapter will also pass the name to
29   * a NormalizedStringAdapter instance for marshalling/unmarshalling.
30   * 
31   * @author Kuali Rice Team (rice.collab@kuali.org)
32   */
33  public class NameAndNamespacePairValidatingAdapter extends XmlAdapter<NameAndNamespacePair,NameAndNamespacePair> {
34  
35      /**
36       * @see javax.xml.bind.annotation.adapters.XmlAdapter#unmarshal(java.lang.Object)
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       * @see javax.xml.bind.annotation.adapters.XmlAdapter#marshal(java.lang.Object)
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  }