View Javadoc

1   /**
2    * Copyright 2005-2013 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.krad.service.impl;
17  
18  import com.thoughtworks.xstream.XStream;
19  import com.thoughtworks.xstream.mapper.MapperWrapper;
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.apache.ojb.broker.core.proxy.ListProxyDefaultImpl;
23  import org.kuali.rice.krad.service.PersistenceService;
24  import org.kuali.rice.krad.service.util.DateTimeConverter;
25  
26  import java.util.ArrayList;
27  
28  /**
29   * Service implementation for the XmlObjectSerializer structure. This is the default implementation that gets
30   * delivered with Kuali. It utilizes the XStream open source libraries and framework
31   *
32   * @author Kuali Rice Team (rice.collab@kuali.org)
33   */
34  public class XmlObjectSerializerIgnoreMissingFieldsServiceImpl extends XmlObjectSerializerServiceImpl {
35  	private static final Log LOG = LogFactory.getLog(XmlObjectSerializerIgnoreMissingFieldsServiceImpl.class);
36  
37  	private PersistenceService persistenceService;
38  
39  	private XStream xstream;
40  
41  	public XmlObjectSerializerIgnoreMissingFieldsServiceImpl() {
42  
43          xstream = new XStream(new ProxyAwareJavaReflectionProvider()) {
44              @Override
45              protected MapperWrapper wrapMapper(MapperWrapper next) {
46                  return new MapperWrapper(next) {
47                      @Override
48                      public boolean shouldSerializeMember(Class definedIn,
49                              String fieldName) {
50                          if (definedIn == Object.class) {
51                              return false;
52                          }
53                        return super.shouldSerializeMember(definedIn, fieldName);
54                     }
55                 };
56             }
57         };
58  
59  		xstream.registerConverter(new ProxyConverter(xstream.getMapper(), xstream.getReflectionProvider() ));
60  		xstream.addDefaultImplementation(ArrayList.class, ListProxyDefaultImpl.class);
61          xstream.registerConverter(new DateTimeConverter());
62  	}
63  
64      /**
65       * @see org.kuali.rice.krad.service.XmlObjectSerializer#fromXml(java.lang.String)
66       *
67       *  Fields on the XML that do not exist on the class will be ignored.
68       */
69      public Object fromXml(String xml) {
70          if ( LOG.isDebugEnabled() ) {
71              LOG.debug( "fromXml() : \n" + xml );
72          }
73          if ( xml != null ) {
74              xml = xml.replaceAll( "--EnhancerByCGLIB--[0-9a-f]{0,8}", "" );
75          }
76          return xstream.fromXML(xml);
77      }
78  }