Package ghidra.framework.options
Class SaveState
- java.lang.Object
-
- ghidra.framework.options.SaveState
-
- Direct Known Subclasses:
PreferenceState
public class SaveState extends java.lang.Object
Class for saving name/value pairs as XML or Json. Classes that want to be able to save their state can do so using the SaveState object. The idea is that each state variable in the class is first saved into a SaveState object via a String key. Then the SaveState object is written out as XML or Json. When the save state object is restored, the SaveState object is constructed with an XML Element or JsonObject that contains all of the name/value pairs. Since the "get" methods require a default value, the object that is recovering its state variables will be successfully initialized even if the given key,value pair is not found in the SaveState object.Note: Names for options are assumed to be unique. When a putXXX() method is called, if a value already exists for a name, it will be overwritten.
The SaveState supports the following types:
java primitives arrays of java primitives String Color Font KeyStroke File Date Enum SaveState (values can be nested SaveStates)
-
-
Field Summary
Fields Modifier and Type Field Description static java.text.DateFormat
DATE_FORMAT
-
Constructor Summary
Constructors Modifier Constructor Description SaveState()
Default Constructor for SaveState; uses "SAVE_STATE" as the name of the state.protected
SaveState(com.google.gson.JsonObject root)
SaveState(java.io.File file)
Construct a SaveState from a file containing XML from a previously saved SaveState.SaveState(java.lang.String name)
Creates a new SaveState object with a non-default name.SaveState(org.jdom.Element root)
Construct a new SaveState object using the given XML element.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
clear()
Clear all objects from the save state.protected org.jdom.Element
createElementFromElement(java.lang.String internalKey, org.jdom.Element internalElement)
boolean
getBoolean(java.lang.String name, boolean defaultValue)
Gets the boolean value for the given name.boolean[]
getBooleans(java.lang.String name, boolean[] defaultValue)
Gets the boolean array for the given name.byte
getByte(java.lang.String name, byte defaultValue)
Gets the byte value for the given name.byte[]
getBytes(java.lang.String name, byte[] defaultValue)
Gets the byte array for the given name.java.awt.Color
getColor(java.lang.String name, java.awt.Color defaultValue)
Gets the Color value for the given name.java.util.Date
getDate(java.lang.String name, java.util.Date defaultValue)
Gets the Date value for the given name.double
getDouble(java.lang.String name, double defaultValue)
Gets the double value for the given name.double[]
getDoubles(java.lang.String name, double[] defaultValue)
Gets the double array for the given name.<T extends java.lang.Enum<T>>
TgetEnum(java.lang.String name, T defaultValue)
Gets the Enum value for the given name.java.io.File
getFile(java.lang.String name, java.io.File defaultValue)
Gets the File value for the given name.float
getFloat(java.lang.String name, float defaultValue)
Gets the float value for the given name.float[]
getFloats(java.lang.String name, float[] defaultValue)
Gets the float array for the given name.java.awt.Font
getFont(java.lang.String name, java.awt.Font defaultValue)
Gets the Font value for the given name.int
getInt(java.lang.String name, int defaultValue)
Gets the int value for the given name.int[]
getInts(java.lang.String name, int[] defaultValue)
Gets the int array for the given name.javax.swing.KeyStroke
getKeyStroke(java.lang.String name, javax.swing.KeyStroke defaultValue)
Gets the KeyStroke value for the given name.long
getLong(java.lang.String name, long defaultValue)
Gets the long value for the given name.long[]
getLongs(java.lang.String name, long[] defaultValue)
Gets the long array for the given name.java.lang.String[]
getNames()
Return the names of the objects saved in the state.SaveState
getSaveState(java.lang.String name)
Returns the sub SaveState associated with the given name.short
getShort(java.lang.String name, short defaultValue)
Gets the short value for the given name.short[]
getShorts(java.lang.String name, short[] defaultValue)
Gets the short array for the given name.java.lang.String
getString(java.lang.String name, java.lang.String defaultValue)
Gets the String value for the given name.java.lang.String[]
getStrings(java.lang.String name, java.lang.String[] defaultValue)
Gets the String array for the given name.org.jdom.Element
getXmlElement(java.lang.String name)
Returns the root of an XML sub-tree associated with the given name.boolean
hasValue(java.lang.String name)
Returns true if there is a value for the given nameboolean
isEmpty()
Returns true if this list contains no elementsvoid
putBoolean(java.lang.String name, boolean value)
Associates a boolean value with the given name.void
putBooleans(java.lang.String name, boolean[] value)
Associates a boolean array with the given name.void
putByte(java.lang.String name, byte value)
Associates a byte value with the given name.void
putBytes(java.lang.String name, byte[] value)
Associates a byte array with the given name.void
putColor(java.lang.String name, java.awt.Color value)
Associates a Color value with the given name.void
putDate(java.lang.String name, java.util.Date value)
Associates a Date value with the given name.void
putDouble(java.lang.String name, double value)
Associates a double value with the given name.void
putDoubles(java.lang.String name, double[] value)
Associates a double value with the given name.void
putEnum(java.lang.String name, java.lang.Enum<?> value)
Associates an Enum with the given name.void
putFile(java.lang.String name, java.io.File value)
Associates a File value with the given name.void
putFloat(java.lang.String name, float value)
Associates a float value with the given name.void
putFloats(java.lang.String name, float[] value)
Associates a float array with the given name.void
putFont(java.lang.String name, java.awt.Font value)
Associates a Font value with the given name.void
putInt(java.lang.String name, int value)
Associates an integer value with the given name.void
putInts(java.lang.String name, int[] value)
Associates an integer array with the given name.void
putKeyStroke(java.lang.String name, javax.swing.KeyStroke value)
Associates a KeyStroke value with the given name.void
putLong(java.lang.String name, long value)
Associates a long value with the given name.void
putLongs(java.lang.String name, long[] value)
Associates a long array with the given name.void
putSaveState(java.lang.String name, SaveState value)
Associates a sub SaveState value with the given name.void
putShort(java.lang.String name, short value)
Associates a short value with the given name.void
putShorts(java.lang.String name, short[] value)
Associates a short array with the given name.void
putString(java.lang.String name, java.lang.String value)
Associates a String value with the given name.void
putStrings(java.lang.String name, java.lang.String[] value)
Associates a String array with the given name.void
putXmlElement(java.lang.String name, org.jdom.Element element)
Adds an XML element to the saved state object.static SaveState
readJsonFile(java.io.File file)
Creates a SaveState object and populates its values from the given file.void
remove(java.lang.String name)
Remove the object identified by the given namevoid
saveToFile(java.io.File file)
Write the saveState to a file as XMLcom.google.gson.JsonObject
saveToJson()
Save this object to an JsonObjectvoid
saveToJsonFile(java.io.File file)
Outputs this SaveState to a file using Jsonorg.jdom.Element
saveToXml()
Save this object to an XML element.int
size()
Return the number of properties in the save statejava.lang.String
toString()
-
-
-
Constructor Detail
-
SaveState
public SaveState(java.lang.String name)
Creates a new SaveState object with a non-default name. The name serves no real purpose other than as a hint as to what the SaveState represents- Parameters:
name
- of the state
-
SaveState
public SaveState()
Default Constructor for SaveState; uses "SAVE_STATE" as the name of the state.- See Also:
Object()
-
SaveState
public SaveState(java.io.File file) throws java.io.IOException
Construct a SaveState from a file containing XML from a previously saved SaveState.- Parameters:
file
- the file containing the XML to read.- Throws:
java.io.IOException
- if the file can't be read or is not formatted properly for a SaveState
-
SaveState
public SaveState(org.jdom.Element root)
Construct a new SaveState object using the given XML element.- Parameters:
root
- XML contents of the save state
-
SaveState
protected SaveState(com.google.gson.JsonObject root)
-
-
Method Detail
-
readJsonFile
public static SaveState readJsonFile(java.io.File file) throws java.io.IOException
Creates a SaveState object and populates its values from the given file. The file must conform to the format that is created withsaveToJson()
- Parameters:
file
- the file to load values from- Returns:
- a new SaveState object loaded with values from the given file.
- Throws:
java.io.IOException
- if an error occurs reading the given file.
-
saveToFile
public void saveToFile(java.io.File file) throws java.io.FileNotFoundException, java.io.IOException
Write the saveState to a file as XML- Parameters:
file
- the file to write to.- Throws:
java.io.FileNotFoundException
- if the file does not represent a valid file path.java.io.IOException
- if the file could not be written
-
saveToJsonFile
public void saveToJsonFile(java.io.File file) throws java.io.IOException
Outputs this SaveState to a file using JsonFor example, a SaveState that is created with:
ss = new SaveState("foo") ss.putString("Name", "Bob"); ss.putBoolean("Retired", true); ss.putInt("Age", 65); ss.putEnum("Endian", Endian.BIG); would produce a Json file with the following text { "SAVE STATE NAME": "foo", "VALUES": { "Name": "Bob" "Retired": true, "Age": 65, "Endian": "BIG", }, "TYPES": { "Name": "String" "Retired": "boolean", "Age": "int", "Endian": "enum", }, "ENUM CLASSES": { "Endian": "ghidra.program.model.lang.Endian" } }
- Parameters:
file
- the file to save to- Throws:
java.io.IOException
- if an error occurs writing to the given file
-
saveToXml
public org.jdom.Element saveToXml()
Save this object to an XML element.- Returns:
- Element XML element containing the state
-
saveToJson
public com.google.gson.JsonObject saveToJson()
Save this object to an JsonObject- Returns:
- JsonObject containing the state
-
createElementFromElement
protected org.jdom.Element createElementFromElement(java.lang.String internalKey, org.jdom.Element internalElement)
-
isEmpty
public boolean isEmpty()
Returns true if this list contains no elements- Returns:
- true if there are no properties in this save state
-
remove
public void remove(java.lang.String name)
Remove the object identified by the given name- Parameters:
name
- the name of the property to remove
-
clear
public void clear()
Clear all objects from the save state.
-
size
public int size()
Return the number of properties in the save state- Returns:
- The number of properties in the save state
-
getNames
public java.lang.String[] getNames()
Return the names of the objects saved in the state.- Returns:
- String[] array will be zero length if the save state is empty
-
putInt
public void putInt(java.lang.String name, int value)
Associates an integer value with the given name.- Parameters:
name
- The name in the name,value pair.value
- The value in the name,value pair.
-
putByte
public void putByte(java.lang.String name, byte value)
Associates a byte value with the given name.- Parameters:
name
- The name in the name,value pair.value
- The value in the name,value pair.
-
putShort
public void putShort(java.lang.String name, short value)
Associates a short value with the given name.- Parameters:
name
- The name in the name,value pair.value
- The value in the name,value pair.
-
putLong
public void putLong(java.lang.String name, long value)
Associates a long value with the given name.- Parameters:
name
- The name in the name,value pair.value
- The value in the name,value pair.
-
putString
public void putString(java.lang.String name, java.lang.String value)
Associates a String value with the given name.- Parameters:
name
- The name in the name,value pair.value
- The value in the name,value pair.
-
putColor
public void putColor(java.lang.String name, java.awt.Color value)
Associates a Color value with the given name.- Parameters:
name
- The name in the name,value pair.value
- The value in the name,value pair.
-
putDate
public void putDate(java.lang.String name, java.util.Date value)
Associates a Date value with the given name.- Parameters:
name
- The name in the name,value pair.value
- The value in the name,value pair.
-
putFile
public void putFile(java.lang.String name, java.io.File value)
Associates a File value with the given name.- Parameters:
name
- The name in the name,value pair.value
- The value in the name,value pair.
-
putKeyStroke
public void putKeyStroke(java.lang.String name, javax.swing.KeyStroke value)
Associates a KeyStroke value with the given name.- Parameters:
name
- The name in the name,value pair.value
- The value in the name,value pair.
-
putFont
public void putFont(java.lang.String name, java.awt.Font value)
Associates a Font value with the given name.- Parameters:
name
- The name in the name,value pair.value
- The value in the name,value pair.
-
putBoolean
public void putBoolean(java.lang.String name, boolean value)
Associates a boolean value with the given name.- Parameters:
name
- The name in the name,value pair.value
- The value in the name,value pair.
-
putFloat
public void putFloat(java.lang.String name, float value)
Associates a float value with the given name.- Parameters:
name
- The name in the name,value pair.value
- The value in the name,value pair.
-
putDouble
public void putDouble(java.lang.String name, double value)
Associates a double value with the given name.- Parameters:
name
- The name in the name,value pair.value
- The value in the name,value pair.
-
putSaveState
public void putSaveState(java.lang.String name, SaveState value)
Associates a sub SaveState value with the given name.- Parameters:
name
- The name in the name,value pair.value
- The value in the name,value pair.
-
getInt
public int getInt(java.lang.String name, int defaultValue)
Gets the int value for the given name.- Parameters:
name
- the name of the pair.defaultValue
- the default value to be returned if the name does not exist in the map, or it does not contain the proper object type.- Returns:
- the int value associated with the given name or the defaultValue passed in if the name doesn't exist or is the wrong type.
-
getByte
public byte getByte(java.lang.String name, byte defaultValue)
Gets the byte value for the given name.- Parameters:
name
- the name of the pair.defaultValue
- the default value to be returned if the name does not exist in the map, or it does not contain the proper object type.- Returns:
- the byte value associated with the given name or the defaultValue passed in if the name doesn't exist or is the wrong type.
-
getShort
public short getShort(java.lang.String name, short defaultValue)
Gets the short value for the given name.- Parameters:
name
- the name of the pair.defaultValue
- the default value to be returned if the name does not exist in the map, or it does not contain the proper object type.- Returns:
- the short value associated with the given name or the defaultValue passed in if the name doesn't exist or is the wrong type.
-
getLong
public long getLong(java.lang.String name, long defaultValue)
Gets the long value for the given name.- Parameters:
name
- the name of the pair.defaultValue
- the default value to be returned if the name does not exist in the map, or it does not contain the proper object type.- Returns:
- the long value associated with the given name or the defaultValue passed in if the name doesn't exist or is the wrong type.
-
getBoolean
public boolean getBoolean(java.lang.String name, boolean defaultValue)
Gets the boolean value for the given name.- Parameters:
name
- the name of the pair.defaultValue
- the default value to be returned if the name does not exist in the map, or it does not contain the proper object type.- Returns:
- the boolean value associated with the given name or the defaultValue passed in if the name doesn't exist or is the wrong type.
-
getString
public java.lang.String getString(java.lang.String name, java.lang.String defaultValue)
Gets the String value for the given name.- Parameters:
name
- the name of the pair.defaultValue
- the default value to be returned if the name does not exist in the map, or it does not contain the proper object type.- Returns:
- the String value associated with the given name or the defaultValue passed in if the name doesn't exist or is the wrong type.
-
getColor
public java.awt.Color getColor(java.lang.String name, java.awt.Color defaultValue)
Gets the Color value for the given name.- Parameters:
name
- the name of the pair.defaultValue
- the default value to be returned if the name does not exist in the map, or it does not contain the proper object type.- Returns:
- the Color value associated with the given name or the defaultValue passed in if the name doesn't exist or is the wrong type.
-
getDate
public java.util.Date getDate(java.lang.String name, java.util.Date defaultValue)
Gets the Date value for the given name.- Parameters:
name
- the name of the pair.defaultValue
- the default value to be returned if the name does not exist in the map, or it does not contain the proper object type.- Returns:
- the Date value associated with the given name or the defaultValue passed in if the name doesn't exist or is the wrong type.
-
getFile
public java.io.File getFile(java.lang.String name, java.io.File defaultValue)
Gets the File value for the given name.- Parameters:
name
- the name of the pair.defaultValue
- the default value to be returned if the name does not exist in the map, or it does not contain the proper object type.- Returns:
- the File value associated with the given name or the defaultValue passed in if the name doesn't exist or is the wrong type.
-
getKeyStroke
public javax.swing.KeyStroke getKeyStroke(java.lang.String name, javax.swing.KeyStroke defaultValue)
Gets the KeyStroke value for the given name.- Parameters:
name
- the name of the pair.defaultValue
- the default value to be returned if the name does not exist in the map, or it does not contain the proper object type.- Returns:
- the KeyStroke value associated with the given name or the defaultValue passed in if the name doesn't exist or is the wrong type.
-
getFont
public java.awt.Font getFont(java.lang.String name, java.awt.Font defaultValue)
Gets the Font value for the given name.- Parameters:
name
- the name of the pair.defaultValue
- the default value to be returned if the name does not exist in the map, or it does not contain the proper object type.- Returns:
- the Font value associated with the given name or the defaultValue passed in if the name doesn't exist or is the wrong type.
-
getFloat
public float getFloat(java.lang.String name, float defaultValue)
Gets the float value for the given name.- Parameters:
name
- the name of the pair.defaultValue
- the default value to be returned if the name does not exist in the map, or it does not contain the proper object type.- Returns:
- the float value associated with the given name or the defaultValue passed in if the name doesn't exist or is the wrong type.
-
getDouble
public double getDouble(java.lang.String name, double defaultValue)
Gets the double value for the given name.- Parameters:
name
- the name of the pair.defaultValue
- the default value to be returned if the name does not exist in the map, or it does not contain the proper object type.- Returns:
- the double value associated with the given name or the defaultValue passed in if the name doesn't exist or is the wrong type.
-
putInts
public void putInts(java.lang.String name, int[] value)
Associates an integer array with the given name.- Parameters:
name
- The name in the name,value pair.value
- The value in the name,value pair.
-
putBytes
public void putBytes(java.lang.String name, byte[] value)
Associates a byte array with the given name.- Parameters:
name
- The name in the name,value pair.value
- The value in the name,value pair.
-
putShorts
public void putShorts(java.lang.String name, short[] value)
Associates a short array with the given name.- Parameters:
name
- The name in the name,value pair.value
- The value in the name,value pair.
-
putLongs
public void putLongs(java.lang.String name, long[] value)
Associates a long array with the given name.- Parameters:
name
- The name in the name,value pair.value
- The value in the name,value pair.
-
putStrings
public void putStrings(java.lang.String name, java.lang.String[] value)
Associates a String array with the given name.- Parameters:
name
- The name in the name,value pair.value
- The value in the name,value pair.
-
putEnum
public void putEnum(java.lang.String name, java.lang.Enum<?> value)
Associates an Enum with the given name.- Parameters:
name
- The name in the name,value pair.value
- The Enum value in the name,value pair.
-
putBooleans
public void putBooleans(java.lang.String name, boolean[] value)
Associates a boolean array with the given name.- Parameters:
name
- The name in the name,value pair.value
- The value in the name,value pair.
-
putFloats
public void putFloats(java.lang.String name, float[] value)
Associates a float array with the given name.- Parameters:
name
- The name in the name,value pair.value
- The value in the name,value pair.
-
putDoubles
public void putDoubles(java.lang.String name, double[] value)
Associates a double value with the given name.- Parameters:
name
- The name in the name,value pair.value
- The value in the name,value pair.
-
getInts
public int[] getInts(java.lang.String name, int[] defaultValue)
Gets the int array for the given name.- Parameters:
name
- the name of the pair.defaultValue
- the default value to be returned if the name does not exist in the map, or it does not contain the proper object type.- Returns:
- the int array associated with the given name or the defaultValue passed in if the name doesn't exist or is the wrong type.
-
getBytes
public byte[] getBytes(java.lang.String name, byte[] defaultValue)
Gets the byte array for the given name.- Parameters:
name
- the name of the pair.defaultValue
- the default value to be returned if the name does not exist in the map, or it does not contain the proper object type.- Returns:
- the byte array associated with the given name or the defaultValue passed in if the name doesn't exist or is the wrong type.
-
getShorts
public short[] getShorts(java.lang.String name, short[] defaultValue)
Gets the short array for the given name.- Parameters:
name
- the name of the pair.defaultValue
- the default value to be returned if the name does not exist in the map, or it does not contain the proper object type.- Returns:
- the short array associated with the given name or the defaultValue passed in if the name doesn't exist or is the wrong type.
-
getLongs
public long[] getLongs(java.lang.String name, long[] defaultValue)
Gets the long array for the given name.- Parameters:
name
- the name of the pair.defaultValue
- the default value to be returned if the name does not exist in the map, or it does not contain the proper object type.- Returns:
- the long array associated with the given name or the defaultValue passed in if the name doesn't exist or is the wrong type.
-
getBooleans
public boolean[] getBooleans(java.lang.String name, boolean[] defaultValue)
Gets the boolean array for the given name.- Parameters:
name
- the name of the pair.defaultValue
- the default value to be returned if the name does not exist in the map, or it does not contain the proper object type.- Returns:
- the boolean array associated with the given name or the defaultValue passed in if the name doesn't exist or is the wrong type.
-
getStrings
public java.lang.String[] getStrings(java.lang.String name, java.lang.String[] defaultValue)
Gets the String array for the given name.- Parameters:
name
- the name of the pair.defaultValue
- the default value to be returned if the name does not exist in the map, or it does not contain the proper object type.- Returns:
- the String array associated with the given name or the defaultValue passed in if the name doesn't exist or is the wrong type.
-
getEnum
public <T extends java.lang.Enum<T>> T getEnum(java.lang.String name, T defaultValue)
Gets the Enum value for the given name.- Parameters:
name
- the name of the pair.defaultValue
- the default Enum value to be returned if the name does not exist in the map, or it does not contain the proper object type.- Returns:
- the Enum value associated with the given name or the defaultValue passed in if the name doesn't exist or is the wrong type.
-
getFloats
public float[] getFloats(java.lang.String name, float[] defaultValue)
Gets the float array for the given name.- Parameters:
name
- the name of the pair.defaultValue
- the default value to be returned if the name does not exist in the map, or it does not contain the proper object type.- Returns:
- the float array associated with the given name or the defaultValue passed in if the name doesn't exist or is the wrong type.
-
getDoubles
public double[] getDoubles(java.lang.String name, double[] defaultValue)
Gets the double array for the given name.- Parameters:
name
- the name of the pair.defaultValue
- the default value to be returned if the name does not exist in the map, or it does not contain the proper object type.- Returns:
- the double array associated with the given name or the defaultValue passed in if the name doesn't exist or is the wrong type.
-
hasValue
public boolean hasValue(java.lang.String name)
Returns true if there is a value for the given name- Parameters:
name
- true the name of the property to check for a value- Returns:
- true if the SaveState object has a value for the given name
-
putXmlElement
public void putXmlElement(java.lang.String name, org.jdom.Element element)
Adds an XML element to the saved state object. Used by plugins that have more complicated state information that needs to be saved.- Parameters:
name
- the name to associate with the elementelement
- XML element which is the root of an XML sub-tree.
-
getXmlElement
public org.jdom.Element getXmlElement(java.lang.String name)
Returns the root of an XML sub-tree associated with the given name.- Parameters:
name
- The name associated with the desired Element.- Returns:
- The root of an XML sub-tree associated with the given name.
-
getSaveState
public SaveState getSaveState(java.lang.String name)
Returns the sub SaveState associated with the given name.- Parameters:
name
- The name associated with the desired Element.- Returns:
- The SaveState object associated with the given name.
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-