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 java.io.StringReader;
019    import java.io.StringWriter;
020    
021    import javax.xml.parsers.DocumentBuilderFactory;
022    import javax.xml.transform.TransformerFactory;
023    import javax.xml.transform.dom.DOMSource;
024    import javax.xml.transform.stream.StreamResult;
025    
026    import org.springframework.richclient.settings.SettingsException;
027    import org.springframework.util.StringUtils;
028    import org.w3c.dom.Document;
029    import org.w3c.dom.Element;
030    import org.xml.sax.InputSource;
031    
032    /**
033     * Helper class, used for testing. The read method returns the xml settings
034     * parsed from the xml passed in the constructor, the write method writes to a
035     * buffer which can be read with the getBuffer method.
036     * 
037     * @author Peter De Bruycker
038     */
039    public class StringXmlSettingsReaderWriter implements XmlSettingsReaderWriter {
040    
041            private String xml;
042    
043            private StringWriter buffer;
044    
045            /**
046             * Creates a new instance. The xml will be parsed when the read method is
047             * invoked.
048             * 
049             * @param xml
050             *            the xml
051             */
052            public StringXmlSettingsReaderWriter(String xml) {
053                    this.xml = xml;
054            }
055    
056            /**
057             * Creates a new instance.
058             */
059            public StringXmlSettingsReaderWriter() {
060                    this(null);
061            }
062    
063            public void write(RootXmlSettings settings) throws SettingsException {
064                    try {
065                            buffer = new StringWriter();
066                            TransformerFactory.newInstance().newTransformer().transform(new DOMSource(settings.getDocument()),
067                                            new StreamResult(buffer));
068                    } catch (Exception e) {
069                            throw new SettingsException("Unable to write xml", e);
070                    }
071            }
072    
073            /**
074             * Returns the buffered xml.
075             * 
076             * @return the buffered xml
077             */
078            public String getBuffer() {
079                    return buffer.getBuffer().toString();
080            }
081    
082            public RootXmlSettings read(String key) throws SettingsException {
083                    try {
084                            Document doc = null;
085                            if (StringUtils.hasText(xml)) {
086                                    doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(
087                                                    new InputSource(new StringReader(xml)));
088                            } else {
089                                    doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
090                                    Element element = doc.createElement("settings");
091                                    element.setAttribute("name", key);
092                                    doc.appendChild(element);
093                            }
094    
095                            return new RootXmlSettings(doc, this);
096                    } catch (Exception e) {
097                            throw new SettingsException("Unable to parse xml " + xml, e);
098                    }
099            }
100    
101    }