001    /*
002     * Copyright 2002-2004 the original author or authors.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005     * use this file except in compliance with the License. You may obtain a copy of
006     * the License at
007     * 
008     * http://www.apache.org/licenses/LICENSE-2.0
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, WITHOUT
012     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013     * License for the specific language governing permissions and limitations under
014     * the License.
015     */
016    package org.springframework.richclient.settings.xml;
017    
018    import org.springframework.richclient.settings.Settings;
019    import org.springframework.richclient.settings.SettingsException;
020    import org.springframework.richclient.settings.SettingsFactory;
021    
022    /**
023     * <code>SettingsFactory</code> for creating <code>XmlSettings</code>.
024     * 
025     * @author Peter De Bruycker
026     */
027    public class XmlSettingsFactory implements SettingsFactory {
028            private String location;
029    
030            private XmlSettingsReaderWriter readerWriter;
031    
032            /**
033             * Returns the <code>XmlSettingsReaderWriter</code> used for persisting
034             * the xml to the backing store. If no <code>XmlSettingsReaderWriter</code>
035             * was set, the default (<code>FileSystemXmlSettingsReaderWriter</code>)
036             * will be used.
037             * 
038             * @return the <code>XmlSettingsReaderWriter</code>
039             */
040            public XmlSettingsReaderWriter getReaderWriter() {
041                    if (readerWriter == null) {
042                            readerWriter = new FileSystemXmlSettingsReaderWriter(getLocation());
043                    }
044    
045                    return readerWriter;
046            }
047    
048            /**
049             * Sets the <code>XmlSettingsReaderWriter</code> to use. If set to
050             * <code>null</code>, the default (<code>FileSystemXmlSettingsReaderWriter</code>)
051             * will be used.
052             * 
053             * @param readerWriter
054             *            the <code>XmlSettingsReaderWriter</code>
055             */
056            public void setReaderWriter(XmlSettingsReaderWriter readerWriter) {
057                    this.readerWriter = readerWriter;
058            }
059    
060            public Settings createSettings(String key) throws SettingsException {
061                    return getReaderWriter().read(key);
062            }
063    
064            /**
065             * Returns the location for the xml files. 
066             * 
067             * @return the location
068             */
069            public String getLocation() {
070                    if (location == null) {
071                            location = "settings";
072                    }
073                    return location;
074            }
075    
076            /**
077             * Sets the location of the xml files.
078             * 
079             * @param location
080             *            the location
081             */
082            public void setLocation(String location) {
083                    this.location = location;
084            }
085    }