001/** 002 * Copyright 2005-2016 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.rice.core.util.jaxb; 017 018import javax.xml.bind.MarshalException; 019import javax.xml.bind.UnmarshalException; 020import javax.xml.bind.annotation.adapters.NormalizedStringAdapter; 021import javax.xml.bind.annotation.adapters.XmlAdapter; 022 023import org.apache.commons.lang.StringUtils; 024import org.kuali.rice.coreservice.api.CoreServiceApiServiceLocator; 025 026/** 027 * An XML adapter that simply validates the NameAndNamespacePair to ensure that the name and namespace are non-blank 028 * and that the namespace code maps to a valid namespace in the system. This adapter will also pass the name to 029 * a NormalizedStringAdapter instance for marshalling/unmarshalling. 030 * 031 * @author Kuali Rice Team (rice.collab@kuali.org) 032 */ 033public class NameAndNamespacePairValidatingAdapter extends XmlAdapter<NameAndNamespacePair,NameAndNamespacePair> { 034 035 /** 036 * @see javax.xml.bind.annotation.adapters.XmlAdapter#unmarshal(java.lang.Object) 037 */ 038 @Override 039 public NameAndNamespacePair unmarshal(NameAndNamespacePair v) throws Exception { 040 if (v != null) { 041 042 if (StringUtils.isBlank(v.getName())) { 043 throw new UnmarshalException("Cannot import a name-and-namespace pair with a blank name"); 044 } else if (StringUtils.isBlank(v.getNamespaceCode())) { 045 throw new UnmarshalException("Cannot import a name-and-namespace pair with a blank namespace code"); 046 } if (CoreServiceApiServiceLocator.getNamespaceService().getNamespace(v.getNamespaceCode()) == null) { 047 throw new UnmarshalException("Cannot import a name-and-namespace pair with invalid or unknown namespace \"" + 048 v.getNamespaceCode() + "\""); 049 } 050 051 v.setName(new NormalizedStringAdapter().unmarshal(v.getName())); 052 v.setNamespaceCode(v.getNamespaceCode()); 053 } 054 return v; 055 } 056 057 /** 058 * @see javax.xml.bind.annotation.adapters.XmlAdapter#marshal(java.lang.Object) 059 */ 060 @Override 061 public NameAndNamespacePair marshal(NameAndNamespacePair v) throws Exception { 062 if (v != null) { 063 if (StringUtils.isBlank(v.getName())) { 064 throw new MarshalException("Cannot export a name-and-namespace pair with a blank name"); 065 } else if (StringUtils.isBlank(v.getNamespaceCode())) { 066 throw new MarshalException("Cannot export a name-and-namespace pair with a blank namespace code"); 067 } else if (CoreServiceApiServiceLocator.getNamespaceService().getNamespace(v.getNamespaceCode()) == null) { 068 throw new MarshalException("Cannot export a name-and-namespace pair with invalid or unknown namespace \"" + v.getNamespaceCode() + "\""); 069 } 070 071 v.setName(new NormalizedStringAdapter().marshal(v.getName())); 072 v.setNamespaceCode(v.getNamespaceCode()); 073 } 074 return v; 075 } 076 077}