Java tutorial
/* Copyright 2011 spypunk (spypunk@tailthis-project.org) Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package spypunk.tailthis.configuration.xml; import java.io.File; import java.io.FileWriter; import javax.xml.stream.XMLOutputFactory; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamWriter; import org.apache.commons.io.FileUtils; import org.apache.log4j.Logger; import spypunk.tailthis.configuration.Configuration; import spypunk.tailthis.highlighting.Highlighting; import com.sun.xml.txw2.output.IndentingXMLStreamWriter; public class ConfigurationXMLWriter { private static final Logger LOGGER = Logger.getLogger(ConfigurationXMLWriter.class); private final File configurationFile; public ConfigurationXMLWriter(final File configurationFile) { super(); this.configurationFile = configurationFile; } public void save(final Configuration configuration) throws Exception { FileUtils.forceMkdir(configurationFile.getParentFile()); final XMLOutputFactory xof = XMLOutputFactory.newInstance(); XMLStreamWriter xtw = null; try { xtw = new IndentingXMLStreamWriter(xof.createXMLStreamWriter(new FileWriter(configurationFile))); xtw.writeStartElement(ConfigurationXMLConstants.TAILTHIS_ELEMENT_NAME); xtw.writeStartElement(ConfigurationXMLConstants.REMEMBER_LAST_OPENED_FILES_ELEMENT_NAME); xtw.writeCharacters(configuration.getRememberLastOpenedFiles().toString()); xtw.writeEndElement(); xtw.writeStartElement(ConfigurationXMLConstants.LAST_FOLDER_ELEMENT_NAME); xtw.writeCharacters(configuration.getLastFolder().getAbsolutePath()); xtw.writeEndElement(); xtw.writeStartElement(ConfigurationXMLConstants.LAST_OPENED_FILES_ELEMENT_NAME); for (final File lastOpenedFile : configuration.getLastOpenedFiles()) { xtw.writeStartElement(ConfigurationXMLConstants.LAST_OPENED_FILE_ELEMENT_NAME); xtw.writeCharacters(lastOpenedFile.getAbsolutePath()); xtw.writeEndElement(); } xtw.writeEndElement(); xtw.writeStartElement(ConfigurationXMLConstants.UPDATE_PERIOD_ELEMENT_NAME); xtw.writeCharacters(configuration.getUpdatePeriod().toString()); xtw.writeEndElement(); xtw.writeStartElement(ConfigurationXMLConstants.ALWAYS_ON_TOP_ELEMENT_NAME); xtw.writeCharacters(configuration.getAlwaysOnTop().toString()); xtw.writeEndElement(); xtw.writeStartElement(ConfigurationXMLConstants.LOCALE_ELEMENT_NAME); xtw.writeCharacters(configuration.getLocale().getKey()); xtw.writeEndElement(); xtw.writeStartElement(ConfigurationXMLConstants.HIGHLIGHTINGS_ELEMENT_NAME); for (final Highlighting highlighting : configuration.getHighlightings()) { xtw.writeStartElement(ConfigurationXMLConstants.HIGHLIGHTING_ELEMENT_NAME); xtw.writeAttribute(ConfigurationXMLConstants.BACKGROUND_COLOR_ATTRIBUTE_NAME, highlighting.getBackgroundColor().getKey()); xtw.writeAttribute(ConfigurationXMLConstants.FOREGROUND_COLOR_ATTRIBUTE_NAME, highlighting.getForegroundColor().getKey()); xtw.writeCharacters(highlighting.getPattern().pattern()); xtw.writeEndElement(); } xtw.writeEndElement(); xtw.writeEndElement(); xtw.flush(); } finally { if (xtw != null) { try { xtw.close(); } catch (final XMLStreamException e) { LOGGER.warn(e.getMessage(), e); } } } } }