View Javadoc
1   /*
2    * The Kuali Financial System, a comprehensive financial management system for higher education.
3    * 
4    * Copyright 2005-2014 The Kuali Foundation
5    * 
6    * This program is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Affero General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   * 
11   * This program 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
14   * GNU Affero General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Affero General Public License
17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.kuali.rice.core.util.jaxb;
20  
21  import javax.xml.bind.MarshalException;
22  import javax.xml.bind.UnmarshalException;
23  import javax.xml.bind.annotation.adapters.NormalizedStringAdapter;
24  import javax.xml.bind.annotation.adapters.XmlAdapter;
25  
26  import org.apache.commons.lang.StringUtils;
27  import org.kuali.rice.coreservice.api.CoreServiceApiServiceLocator;
28  
29  /**
30   * An XML adapter that simply validates the NameAndNamespacePair to ensure that the name and namespace are non-blank
31   * and that the namespace code maps to a valid namespace in the system. This adapter will also pass the name to
32   * a NormalizedStringAdapter instance for marshalling/unmarshalling.
33   * 
34   * @author Kuali Rice Team (rice.collab@kuali.org)
35   */
36  public class NameAndNamespacePairValidatingAdapter extends XmlAdapter<NameAndNamespacePair,NameAndNamespacePair> {
37  
38      /**
39       * @see javax.xml.bind.annotation.adapters.XmlAdapter#unmarshal(java.lang.Object)
40       */
41      @Override
42      public NameAndNamespacePair unmarshal(NameAndNamespacePair v) throws Exception {
43          if (v != null) {
44              
45              if (StringUtils.isBlank(v.getName())) {
46                  throw new UnmarshalException("Cannot import a name-and-namespace pair with a blank name");
47              } else if (StringUtils.isBlank(v.getNamespaceCode())) {
48                  throw new UnmarshalException("Cannot import a name-and-namespace pair with a blank namespace code");
49              } if (CoreServiceApiServiceLocator.getNamespaceService().getNamespace(v.getNamespaceCode()) == null) {
50                  throw new UnmarshalException("Cannot import a name-and-namespace pair with invalid or unknown namespace \"" +
51                          v.getNamespaceCode() + "\"");
52              }
53              
54              v.setName(new NormalizedStringAdapter().unmarshal(v.getName()));
55              v.setNamespaceCode(v.getNamespaceCode());
56          }
57          return v;
58      }
59  
60      /**
61       * @see javax.xml.bind.annotation.adapters.XmlAdapter#marshal(java.lang.Object)
62       */
63      @Override
64      public NameAndNamespacePair marshal(NameAndNamespacePair v) throws Exception {
65          if (v != null) {
66              if (StringUtils.isBlank(v.getName())) {
67                  throw new MarshalException("Cannot export a name-and-namespace pair with a blank name");
68              } else if (StringUtils.isBlank(v.getNamespaceCode())) {
69                  throw new MarshalException("Cannot export a name-and-namespace pair with a blank namespace code");
70              } else if (CoreServiceApiServiceLocator.getNamespaceService().getNamespace(v.getNamespaceCode()) == null) {
71                  throw new MarshalException("Cannot export a name-and-namespace pair with invalid or unknown namespace \"" + v.getNamespaceCode() + "\"");
72              }
73              
74              v.setName(new NormalizedStringAdapter().marshal(v.getName()));
75              v.setNamespaceCode(v.getNamespaceCode());
76          }
77          return v;
78      }
79  
80  }