View Javadoc

1   /*
2    * Copyright 2006 the original author or authors.
3    *
4    * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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  
17  package org.springframework.oxm.xmlbeans;
18  
19  import java.util.Iterator;
20  import java.util.Map;
21  
22  import org.apache.xmlbeans.XmlOptions;
23  import org.springframework.beans.factory.FactoryBean;
24  import org.springframework.beans.factory.InitializingBean;
25  
26  /**
27   * Factory bean that configures an XMLBeans <code>XmlOptions</code> object and provides it as a bean reference.
28   * <p/>
29   * Typical usage will be to set XMLBeans options on this bean, and refer to it in the <code>XmlBeansMarshaller</code>.
30   *
31   * @author Arjen Poutsma
32   * @see XmlOptions
33   * @see #setOptions(java.util.Map)
34   * @see XmlBeansMarshaller#setXmlOptions(org.apache.xmlbeans.XmlOptions)
35   * @since 1.0.0
36   */
37  public class XmlOptionsFactoryBean implements FactoryBean, InitializingBean {
38  
39      private XmlOptions xmlOptions;
40  
41      private Map options;
42  
43      /** Returns the singleton <code>XmlOptions</code>. */
44      public Object getObject() throws Exception {
45          return xmlOptions;
46      }
47  
48      /** Returns the class of <code>XmlOptions</code>. */
49      public Class getObjectType() {
50          return XmlOptions.class;
51      }
52  
53      /** Returns <code>true</code>. */
54      public boolean isSingleton() {
55          return true;
56      }
57  
58      /**
59       * Sets options on the underlying <code>XmlOptions</code> object. The keys of the supplied map should be one of the
60       * string constants defined in <code>XmlOptions</code>, the values vary per option.
61       *
62       * @see XmlOptions#put(Object,Object)
63       * @see XmlOptions#SAVE_PRETTY_PRINT
64       * @see XmlOptions#LOAD_STRIP_COMMENTS
65       */
66      public void setOptions(Map options) {
67          this.options = options;
68      }
69  
70      public void afterPropertiesSet() throws Exception {
71          xmlOptions = new XmlOptions();
72          if (options != null) {
73              for (Iterator iterator = options.keySet().iterator(); iterator.hasNext();) {
74                  Object option = iterator.next();
75                  xmlOptions.put(option, options.get(option));
76              }
77          }
78      }
79  }