01: /*
02: * Copyright 2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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,
38: InitializingBean {
39:
40: private XmlOptions xmlOptions;
41:
42: private Map options;
43:
44: /** Returns the singleton <code>XmlOptions</code>. */
45: public Object getObject() throws Exception {
46: return xmlOptions;
47: }
48:
49: /** Returns the class of <code>XmlOptions</code>. */
50: public Class getObjectType() {
51: return XmlOptions.class;
52: }
53:
54: /** Returns <code>true</code>. */
55: public boolean isSingleton() {
56: return true;
57: }
58:
59: /**
60: * Sets options on the underlying <code>XmlOptions</code> object. The keys of the supplied map should be one of the
61: * string constants defined in <code>XmlOptions</code>, the values vary per option.
62: *
63: * @see XmlOptions#put(Object,Object)
64: * @see XmlOptions#SAVE_PRETTY_PRINT
65: * @see XmlOptions#LOAD_STRIP_COMMENTS
66: */
67: public void setOptions(Map options) {
68: this .options = options;
69: }
70:
71: public void afterPropertiesSet() throws Exception {
72: xmlOptions = new XmlOptions();
73: if (options != null) {
74: for (Iterator iterator = options.keySet().iterator(); iterator
75: .hasNext();) {
76: Object option = iterator.next();
77: xmlOptions.put(option, options.get(option));
78: }
79: }
80: }
81: }
|