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.util.DateTimeConverter;
25  
26  import java.util.ArrayList;
27  
28  /**
29   * This class is the 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  public class XmlObjectSerializerIgnoreMissingFieldsServiceImpl extends XmlObjectSerializerServiceImpl {
33  	private static final Log LOG = LogFactory.getLog(XmlObjectSerializerIgnoreMissingFieldsServiceImpl.class);
34  
35  	private PersistenceService persistenceService;
36  
37  	private XStream xstream;
38  
39  	public XmlObjectSerializerIgnoreMissingFieldsServiceImpl() {
40  
41          xstream = new XStream(new ProxyAwareJavaReflectionProvider()) {
42              @Override
43              protected MapperWrapper wrapMapper(MapperWrapper next) {
44                  return new MapperWrapper(next) {
45                      @Override
46                      public boolean shouldSerializeMember(Class definedIn,
47                              String fieldName) {
48                          if (definedIn == Object.class) {
49                              return false;
50                          }
51                        return super.shouldSerializeMember(definedIn, fieldName);
52                     }
53                 };
54             }
55         };
56  
57  		xstream.registerConverter(new ProxyConverter(xstream.getMapper(), xstream.getReflectionProvider() ));
58  		xstream.addDefaultImplementation(ArrayList.class, ListProxyDefaultImpl.class);
59          xstream.registerConverter(new DateTimeConverter());
60  	}
61  
62      /**
63       * @see org.kuali.rice.krad.service.XmlObjectSerializer#fromXml(java.lang.String)
64       *
65       *  Fields on the XML that do not exist on the class will be ignored.
66       */
67      public Object fromXml(String xml) {
68          if ( LOG.isDebugEnabled() ) {
69              LOG.debug( "fromXml() : \n" + xml );
70          }
71          if ( xml != null ) {
72              xml = xml.replaceAll( "--EnhancerByCGLIB--[0-9a-f]{0,8}", "" );
73          }
74          return xstream.fromXML(xml);
75      }
76  }