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.krad.service.impl;
017
018import com.thoughtworks.xstream.XStream;
019import com.thoughtworks.xstream.mapper.MapperWrapper;
020import org.apache.commons.logging.Log;
021import org.apache.commons.logging.LogFactory;
022import org.apache.ojb.broker.core.proxy.ListProxyDefaultImpl;
023import org.kuali.rice.krad.service.PersistenceService;
024import org.kuali.rice.krad.util.DateTimeConverter;
025
026import java.util.ArrayList;
027
028/**
029 * This class is the service implementation for the XmlObjectSerializer structure. This is the default implementation that gets
030 * delivered with Kuali. It utilizes the XStream open source libraries and framework.
031 */
032public class XmlObjectSerializerIgnoreMissingFieldsServiceImpl extends XmlObjectSerializerServiceImpl {
033        private static final Log LOG = LogFactory.getLog(XmlObjectSerializerIgnoreMissingFieldsServiceImpl.class);
034
035        private PersistenceService persistenceService;
036
037        private XStream xstream;
038
039        public XmlObjectSerializerIgnoreMissingFieldsServiceImpl() {
040
041        xstream = new XStream(new ProxyAwareJavaReflectionProvider()) {
042            @Override
043            protected MapperWrapper wrapMapper(MapperWrapper next) {
044                return new MapperWrapper(next) {
045                    @Override
046                    public boolean shouldSerializeMember(Class definedIn,
047                            String fieldName) {
048                        if (definedIn == Object.class) {
049                            return false;
050                        }
051                      return super.shouldSerializeMember(definedIn, fieldName);
052                   }
053               };
054           }
055       };
056
057                xstream.registerConverter(new ProxyConverter(xstream.getMapper(), xstream.getReflectionProvider() ));
058                xstream.addDefaultImplementation(ArrayList.class, ListProxyDefaultImpl.class);
059        xstream.registerConverter(new DateTimeConverter());
060        }
061
062    /**
063     * @see org.kuali.rice.krad.service.XmlObjectSerializer#fromXml(java.lang.String)
064     *
065     *  Fields on the XML that do not exist on the class will be ignored.
066     */
067    public Object fromXml(String xml) {
068        if ( LOG.isDebugEnabled() ) {
069            LOG.debug( "fromXml() : \n" + xml );
070        }
071        if ( xml != null ) {
072            xml = xml.replaceAll( "--EnhancerByCGLIB--[0-9a-f]{0,8}", "" );
073        }
074        return xstream.fromXML(xml);
075    }
076}