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.File; 019 import java.io.FileInputStream; 020 import java.io.FileNotFoundException; 021 import java.io.FileOutputStream; 022 import java.io.IOException; 023 024 import javax.xml.parsers.DocumentBuilderFactory; 025 import javax.xml.parsers.FactoryConfigurationError; 026 import javax.xml.parsers.ParserConfigurationException; 027 import javax.xml.transform.TransformerConfigurationException; 028 import javax.xml.transform.TransformerException; 029 import javax.xml.transform.TransformerFactory; 030 import javax.xml.transform.TransformerFactoryConfigurationError; 031 import javax.xml.transform.dom.DOMSource; 032 import javax.xml.transform.stream.StreamResult; 033 034 import org.springframework.richclient.settings.SettingsException; 035 import org.w3c.dom.Document; 036 import org.w3c.dom.Element; 037 import org.xml.sax.SAXException; 038 039 /** 040 * <code>XmlSettingsReaderWriter</code> implementation that reads and writes 041 * the xml from and to the file system. 042 * 043 * @author Peter De Bruycker 044 * 045 */ 046 public class FileSystemXmlSettingsReaderWriter implements XmlSettingsReaderWriter { 047 048 private String location; 049 050 /** 051 * Creates a new instance. 052 * 053 * @param location 054 * the location where the xml files will be located 055 */ 056 public FileSystemXmlSettingsReaderWriter(String location) { 057 this.location = location; 058 } 059 060 public void write(RootXmlSettings settings) throws SettingsException { 061 try { 062 File file = createFile(settings.getName()); 063 file.getParentFile().mkdirs(); 064 065 TransformerFactory.newInstance().newTransformer().transform(new DOMSource(settings.getDocument()), 066 new StreamResult(new FileOutputStream(file))); 067 } catch (TransformerConfigurationException e) { 068 throw new SettingsException("Unable to write document", e); 069 } catch (TransformerException e) { 070 throw new SettingsException("Unable to write document", e); 071 } catch (TransformerFactoryConfigurationError e) { 072 throw new SettingsException("Unable to write document", e); 073 } catch (FileNotFoundException e) { 074 throw new SettingsException("Unable to write document", e); 075 } 076 } 077 078 /* 079 * TODO: create DTD + validate parsing 080 */ 081 public RootXmlSettings read(String key) throws SettingsException { 082 try { 083 File file = createFile(key); 084 Document doc = null; 085 if (file.exists()) { 086 doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new FileInputStream(file)); 087 } else { 088 doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); 089 Element element = doc.createElement("settings"); 090 element.setAttribute("name", key); 091 doc.appendChild(element); 092 } 093 094 RootXmlSettings settings = new RootXmlSettings(doc, this); 095 096 return settings; 097 } catch (SAXException e) { 098 throw new SettingsException("Unable to read xml", e); 099 } catch (IOException e) { 100 throw new SettingsException("Unable to read xml", e); 101 } catch (ParserConfigurationException e) { 102 throw new SettingsException("Unable to read xml", e); 103 } catch (FactoryConfigurationError e) { 104 throw new SettingsException("Unable to read xml", e); 105 } 106 } 107 108 private File createFile(String key) { 109 return new File(location, key + ".settings.xml"); 110 } 111 112 /** 113 * Returns the current location. 114 * 115 * @return the location 116 */ 117 public String getLocation() { 118 return location; 119 } 120 121 /** 122 * Sets the location. 123 * 124 * @param location 125 * the new location 126 */ 127 public void setLocation(String location) { 128 this.location = location; 129 } 130 131 }