Updates SettingsManager to use jackson xml serialization. Final

testing on all changes to xml serialization.
This commit is contained in:
Denny
2017-12-21 19:59:11 -05:00
parent 1d2d9e8630
commit 26b97f09b7
21 changed files with 469 additions and 433 deletions

View File

@@ -18,12 +18,10 @@
package io.github.dsheirer.settings;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import java.awt.*;
@JsonSubTypes.Type(value = ColorSetting.class, name = "colorSetting")
public class ColorSetting extends Setting
{
private static final int NO_TRANSLUCENCY = 255;
@@ -45,6 +43,13 @@ public class ColorSetting extends Setting
setColorSettingName(name);
}
@JacksonXmlProperty(isAttribute = true, localName = "type", namespace = "http://www.w3.org/2001/XMLSchema-instance")
@Override
public SettingType getType()
{
return SettingType.COLOR_SETTING;
}
@JacksonXmlProperty(isAttribute = true, localName = "colorSettingName")
public ColorSettingName getColorSettingName()
{

View File

@@ -1,28 +1,26 @@
/*******************************************************************************
* SDR Trunk
* Copyright (C) 2014 Dennis Sheirer
/*
* *********************************************************************************************************************
* sdrtrunk
* Copyright (C) 2014-2017 Dennis Sheirer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
******************************************************************************/
* You should have received a copy of the GNU General Public License along with this program.
* If not, see <http://www.gnu.org/licenses/>
* *********************************************************************************************************************
*/
package io.github.dsheirer.settings;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import java.io.File;
@JsonSubTypes.Type(value = FileSetting.class, name = "fileSetting")
public class FileSetting extends Setting
{
private String mPath;
@@ -38,6 +36,14 @@ public class FileSetting extends Setting
setPath(path);
}
@JacksonXmlProperty(isAttribute = true, localName = "type", namespace = "http://www.w3.org/2001/XMLSchema-instance")
@Override
public SettingType getType()
{
return SettingType.FILE_SETTING;
}
@JacksonXmlProperty(isAttribute = true, localName = "path")
public String getPath()
{
return mPath;
@@ -48,6 +54,7 @@ public class FileSetting extends Setting
mPath = value;
}
@JsonIgnore
public File getFile()
{
return new File(mPath);

View File

@@ -1,3 +1,20 @@
/*
* *********************************************************************************************************************
* sdrtrunk
* Copyright (C) 2014-2017 Dennis Sheirer
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program.
* If not, see <http://www.gnu.org/licenses/>
* *********************************************************************************************************************
*/
package io.github.dsheirer.settings;
import com.fasterxml.jackson.annotation.JsonIgnore;
@@ -33,6 +50,13 @@ public class MapViewSetting extends Setting
mZoom = zoom;
}
@JacksonXmlProperty(isAttribute = true, localName = "type", namespace = "http://www.w3.org/2001/XMLSchema-instance")
@Override
public SettingType getType()
{
return SettingType.MAP_VIEW_SETTING;
}
@JacksonXmlProperty(isAttribute = true, localName = "latitude")
public double getLatitude()
{

View File

@@ -1,27 +1,39 @@
/*******************************************************************************
* SDR Trunk
* Copyright (C) 2014 Dennis Sheirer
/*
* *********************************************************************************************************************
* sdrtrunk
* Copyright (C) 2014-2017 Dennis Sheirer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
******************************************************************************/
* You should have received a copy of the GNU General Public License along with this program.
* If not, see <http://www.gnu.org/licenses/>
* *********************************************************************************************************************
*/
package io.github.dsheirer.settings;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import io.github.dsheirer.map.DefaultIcon;
import io.github.dsheirer.map.MapIcon;
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "xsi:type")
public class Setting
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include= JsonTypeInfo.As.PROPERTY, property="type")
@JsonSubTypes({
@JsonSubTypes.Type(value=ColorSetting.class, name="colorSetting"),
@JsonSubTypes.Type(value=DefaultIcon.class, name="defaultIcon"),
@JsonSubTypes.Type(value=FileSetting.class, name="fileSetting"),
@JsonSubTypes.Type(value=MapIcon.class, name="mapIcon"),
@JsonSubTypes.Type(value=MapViewSetting.class, name="mapViewSetting"),
})
@JacksonXmlRootElement( localName = "setting" )
public abstract class Setting
{
protected String mName;
@@ -44,4 +56,7 @@ public class Setting
{
mName = name;
}
@JsonIgnore
public abstract SettingType getType();
}

View File

@@ -0,0 +1,28 @@
/*
* *********************************************************************************************************************
* sdrtrunk
* Copyright (C) 2014-2017 Dennis Sheirer
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program.
* If not, see <http://www.gnu.org/licenses/>
* *********************************************************************************************************************
*/
package io.github.dsheirer.settings;
public enum SettingType
{
COLOR_SETTING,
DEFAULT_ICON,
FILE_SETTING,
MAP_ICON,
MAP_VIEW_SETTING,
UNKNOWN;
}

View File

@@ -1,22 +1,22 @@
/*******************************************************************************
* SDR Trunk
* Copyright (C) 2014-2016 Dennis Sheirer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
******************************************************************************/
/*
* *********************************************************************************************************************
* sdrtrunk
* Copyright (C) 2014-2017 Dennis Sheirer
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program.
* If not, see <http://www.gnu.org/licenses/>
* *********************************************************************************************************************
*/
package io.github.dsheirer.settings;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import io.github.dsheirer.map.DefaultIcon;
@@ -28,183 +28,183 @@ import io.github.dsheirer.source.tuner.configuration.TunerConfiguration;
import java.util.ArrayList;
import java.util.List;
@JacksonXmlRootElement( localName = "SDRTrunk_settings" )
@JacksonXmlRootElement(localName = "SDRTrunk_settings")
public class Settings
{
private List<Setting> mSettings = new ArrayList<Setting>();
private List<Setting> mSettings = new ArrayList<Setting>();
private List<TunerConfiguration> mTunerConfiguration =
new ArrayList<TunerConfiguration>();
private List<RecordingConfiguration> mRecordingConfigurations =
new ArrayList<RecordingConfiguration>();
private List<TunerConfiguration> mTunerConfiguration = new ArrayList<TunerConfiguration>();
public Settings()
{
}
private List<RecordingConfiguration> mRecordingConfigurations = new ArrayList<RecordingConfiguration>();
@JacksonXmlProperty(isAttribute = false, localName = "recording_configuration")
public List<RecordingConfiguration> getRecordingConfigurations()
{
return mRecordingConfigurations;
}
public void setRecordingConfigurations( ArrayList<RecordingConfiguration> configs )
{
mRecordingConfigurations = configs;
}
public void addRecordingConfiguration( RecordingConfiguration config )
{
mRecordingConfigurations.add( config );
}
public void removeRecordingConfiguration( RecordingConfiguration config )
{
mRecordingConfigurations.remove( config );
}
@JacksonXmlProperty(isAttribute = false, localName = "tuner_configuration")
public List<TunerConfiguration> getTunerConfigurations()
{
return mTunerConfiguration;
}
public void setTunerConfigurations( List<TunerConfiguration> configs )
{
mTunerConfiguration = configs;
}
@JacksonXmlProperty(isAttribute = false, localName = "setting")
public List<Setting> getSettings()
{
return mSettings;
}
public void setSettings( ArrayList<Setting> settings )
{
mSettings = settings;
}
public Setting getSetting( String name )
{
for( Setting setting: mSettings )
{
if( setting.getName().contentEquals( name ) )
{
return setting;
}
}
return null;
}
public void addSetting( Setting setting )
{
mSettings.add( setting );
}
public void removeSetting( Setting setting )
{
mSettings.remove( setting );
}
public DefaultIcon getDefaultIcon()
{
for( Setting setting: mSettings )
{
if( setting instanceof DefaultIcon )
{
return (DefaultIcon)setting;
}
}
return null;
}
public ColorSetting getColorSetting( ColorSettingName name )
{
for( Setting setting: mSettings )
{
if( setting instanceof ColorSetting &&
((ColorSetting)setting).getColorSettingName() == name )
{
return (ColorSetting)setting;
}
}
return null;
}
public List<ColorSetting> getColorSettings()
{
ArrayList<ColorSetting> colors = new ArrayList<ColorSetting>();
for( Setting setting: mSettings )
{
if( setting instanceof ColorSetting )
{
colors.add( (ColorSetting)setting );
}
}
return colors;
}
public FileSetting getFileSetting( String name )
{
for( Setting setting: mSettings )
{
if( setting instanceof FileSetting &&
setting.getName().contentEquals( name ) )
{
return (FileSetting)setting;
}
}
return null;
}
public MapIcon getMapIcon( String name )
{
for( Setting setting: mSettings )
{
if( setting instanceof MapIcon &&
setting.getName().contentEquals( name ) )
{
return (MapIcon)setting;
}
}
return null;
}
public ArrayList<MapIcon> getMapIcons()
{
ArrayList<MapIcon> icons = new ArrayList<MapIcon>();
for( Setting setting: mSettings )
{
if( setting instanceof MapIcon )
{
icons.add( (MapIcon)setting );
}
}
return icons;
}
public MapViewSetting getMapViewSetting( String name )
public Settings()
{
for( Setting setting: mSettings )
{
if( setting instanceof MapViewSetting &&
setting.getName().contentEquals( name ) )
{
return (MapViewSetting)setting;
}
}
return null;
}
@JacksonXmlProperty(isAttribute = false, localName = "recording_configuration")
public List<RecordingConfiguration> getRecordingConfigurations()
{
return mRecordingConfigurations;
}
public void setRecordingConfigurations(ArrayList<RecordingConfiguration> configs)
{
mRecordingConfigurations = configs;
}
public void addRecordingConfiguration(RecordingConfiguration config)
{
mRecordingConfigurations.add(config);
}
public void removeRecordingConfiguration(RecordingConfiguration config)
{
mRecordingConfigurations.remove(config);
}
@JacksonXmlProperty(isAttribute = false, localName = "tuner_configuration")
public List<TunerConfiguration> getTunerConfigurations()
{
return mTunerConfiguration;
}
public void setTunerConfigurations(List<TunerConfiguration> configs)
{
mTunerConfiguration = configs;
}
@JacksonXmlProperty(isAttribute = false, localName = "setting")
public List<Setting> getSettings()
{
return mSettings;
}
public void setSettings(ArrayList<Setting> settings)
{
mSettings = settings;
}
public Setting getSetting(String name)
{
for(Setting setting : mSettings)
{
if(setting.getName().contentEquals(name))
{
return setting;
}
}
return null;
}
public void addSetting(Setting setting)
{
mSettings.add(setting);
}
public void removeSetting(Setting setting)
{
mSettings.remove(setting);
}
@JsonIgnore
public DefaultIcon getDefaultIcon()
{
for(Setting setting : mSettings)
{
if(setting instanceof DefaultIcon)
{
return (DefaultIcon)setting;
}
}
return null;
}
public ColorSetting getColorSetting(ColorSettingName name)
{
for(Setting setting : mSettings)
{
if(setting instanceof ColorSetting &&
((ColorSetting)setting).getColorSettingName() == name)
{
return (ColorSetting)setting;
}
}
return null;
}
@JsonIgnore
public List<ColorSetting> getColorSettings()
{
ArrayList<ColorSetting> colors = new ArrayList<ColorSetting>();
for(Setting setting : mSettings)
{
if(setting instanceof ColorSetting)
{
colors.add((ColorSetting)setting);
}
}
return colors;
}
public FileSetting getFileSetting(String name)
{
for(Setting setting : mSettings)
{
if(setting instanceof FileSetting &&
setting.getName().contentEquals(name))
{
return (FileSetting)setting;
}
}
return null;
}
public MapIcon getMapIcon(String name)
{
for(Setting setting : mSettings)
{
if(setting instanceof MapIcon &&
setting.getName().contentEquals(name))
{
return (MapIcon)setting;
}
}
return null;
}
public ArrayList<MapIcon> getMapIcons()
{
ArrayList<MapIcon> icons = new ArrayList<MapIcon>();
for(Setting setting : mSettings)
{
if(setting instanceof MapIcon)
{
icons.add((MapIcon)setting);
}
}
return icons;
}
public MapViewSetting getMapViewSetting(String name)
{
for(Setting setting : mSettings)
{
if(setting instanceof MapViewSetting &&
setting.getName().contentEquals(name))
{
return (MapViewSetting)setting;
}
}
return null;
}
}

View File

@@ -1,22 +1,25 @@
/*******************************************************************************
* SDR Trunk
* Copyright (C) 2014-2016 Dennis Sheirer
/*
* *********************************************************************************************************************
* sdrtrunk
* Copyright (C) 2014-2017 Dennis Sheirer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
******************************************************************************/
* You should have received a copy of the GNU General Public License along with this program.
* If not, see <http://www.gnu.org/licenses/>
* *********************************************************************************************************************
*/
package io.github.dsheirer.settings;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.xml.JacksonXmlModule;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import io.github.dsheirer.properties.SystemProperties;
import io.github.dsheirer.sample.Listener;
import io.github.dsheirer.source.recording.RecordingConfiguration;
@@ -27,14 +30,7 @@ import org.jdesktop.swingx.mapviewer.GeoPosition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.awt.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@@ -256,11 +252,9 @@ public class SettingsManager implements Listener<TunerConfigurationEvent>
{
saveTunerConfigurationModel();
JAXBContext context = null;
SystemProperties props = SystemProperties.getInstance();
Path settingsPath = props.getApplicationFolder("settings");
Path settingsFolder = props.getApplicationFolder("settings");
String settingsDefault = props.get("settings.defaultFilename",
"settings.xml");
@@ -268,63 +262,24 @@ public class SettingsManager implements Listener<TunerConfigurationEvent>
String settingsCurrent = props.get("settings.currentFilename",
settingsDefault);
Path filePath = settingsPath.resolve(settingsCurrent);
Path settingsPath = settingsFolder.resolve(settingsCurrent);
File outputFile = new File(filePath.toString());
try
try(OutputStream out = Files.newOutputStream(settingsPath))
{
if(!outputFile.exists())
{
outputFile.createNewFile();
}
JacksonXmlModule xmlModule = new JacksonXmlModule();
xmlModule.setDefaultUseWrapper(false);
ObjectMapper objectMapper = new XmlMapper(xmlModule);
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
objectMapper.writeValue(out, mSettings);
out.flush();
}
catch(IOException ioe)
{
mLog.error("IO error while writing the settings to a file [" + settingsPath + "]", ioe);
}
catch(Exception e)
{
mLog.error("SettingsManager - couldn't create file to save "
+ "settings [" + filePath.toString() + "]", e);
}
OutputStream out = null;
try
{
out = new FileOutputStream(outputFile);
try
{
context = JAXBContext.newInstance(Settings.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.marshal(mSettings, out);
}
catch(JAXBException e)
{
mLog.error("SettingsManager - jaxb exception while saving " +
"settings", e);
}
}
catch(Exception e)
{
mLog.error("SettingsManager - couldn't open output stream to " +
"save settings [" + filePath.toString() + "]");
}
finally
{
if(out != null)
{
try
{
out.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
mLog.error("Error while saving settings file [" + settingsPath + "]", e);
}
}
@@ -340,47 +295,17 @@ public class SettingsManager implements Listener<TunerConfigurationEvent>
{
mLog.info("SettingsManager - loading settings file [" + settingsPath.toString() + "]");
JAXBContext context = null;
JacksonXmlModule xmlModule = new JacksonXmlModule();
xmlModule.setDefaultUseWrapper(false);
ObjectMapper objectMapper = new XmlMapper(xmlModule);
InputStream in = null;
try
try(InputStream in = Files.newInputStream(settingsPath))
{
in = new FileInputStream(settingsPath.toString());
try
{
context = JAXBContext.newInstance(Settings.class);
Unmarshaller m = context.createUnmarshaller();
mSettings = (Settings) m.unmarshal(in);
}
catch(JAXBException e)
{
mLog.error("SettingsManager - jaxb exception while loading " +
"settings", e);
}
mSettings = objectMapper.readValue(in, Settings.class);
}
catch(Exception e)
catch(IOException ioe)
{
mLog.error("SettingsManager - coulcn't open inputstream to " +
"load settings [" + settingsPath.toString() + "]", e);
}
finally
{
if(in != null)
{
try
{
in.close();
}
catch(IOException e)
{
mLog.error("SettingsManager - exception while closing " +
"the settings file inputstream reader", e);
}
}
mLog.error("IO error while reading settings file", ioe);
}
}
else