Coverage Report - org.kuali.rice.core.util.jaxb.NameAndNamespacePairValidatingAdapter
 
Classes in this File Line Coverage Branch Coverage Complexity
NameAndNamespacePairValidatingAdapter
0%
0/21
0%
0/16
8
 
 1  
 /**
 2  
  * Copyright 2005-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/ecl2.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.core.api.CoreApiServiceLocator;
 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  0
 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  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  
      * @see javax.xml.bind.annotation.adapters.XmlAdapter#marshal(java.lang.Object)
 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  
 }