Package ghidra.framework.options
Class SaveState
java.lang.Object
ghidra.framework.options.SaveState
- Direct Known Subclasses:
PreferenceState
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 -
Constructor Summary
ConstructorsModifierConstructorDescriptionDefault Constructor for SaveState; uses "SAVE_STATE" as the name of the state.protected
SaveState
(com.google.gson.JsonObject root) Construct a SaveState from a file containing XML from a previously saved SaveState.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
Modifier and TypeMethodDescriptionvoid
clear()
Clear all objects from the save state.protected org.jdom.Element
createElementFromElement
(String internalKey, org.jdom.Element internalElement) boolean
getBoolean
(String name, boolean defaultValue) Gets the boolean value for the given name.boolean[]
getBooleans
(String name, boolean[] defaultValue) Gets the boolean array for the given name.byte
Gets the byte value for the given name.byte[]
Gets the byte array for the given name.Gets the Color value for the given name.Gets the Date value for the given name.double
Gets the double value for the given name.double[]
getDoubles
(String name, double[] defaultValue) Gets the double array for the given name.<T extends Enum<T>>
TGets the Enum value for the given name.Gets the File value for the given name.float
Gets the float value for the given name.float[]
Gets the float array for the given name.Gets the Font value for the given name.int
Gets the int value for the given name.int[]
Gets the int array for the given name.getKeyStroke
(String name, KeyStroke defaultValue) Gets the KeyStroke value for the given name.long
Gets the long value for the given name.long[]
Gets the long array for the given name.String[]
getNames()
Return the names of the objects saved in the state.getSaveState
(String name) Returns the sub SaveState associated with the given name.short
Gets the short value for the given name.short[]
Gets the short array for the given name.Gets the String value for the given name.String[]
getStrings
(String name, String[] defaultValue) Gets the String array for the given name.org.jdom.Element
getXmlElement
(String name) Returns the root of an XML sub-tree associated with the given name.boolean
Returns true if there is a value for the given nameboolean
isEmpty()
Returns true if this list contains no elementsvoid
putBoolean
(String name, boolean value) Associates a boolean value with the given name.void
putBooleans
(String name, boolean[] value) Associates a boolean array with the given name.void
Associates a byte value with the given name.void
Associates a byte array with the given name.void
Associates a Color value with the given name.void
Associates a Date value with the given name.void
Associates a double value with the given name.void
putDoubles
(String name, double[] value) Associates a double value with the given name.void
Associates an Enum with the given name.void
Associates a File value with the given name.void
Associates a float value with the given name.void
Associates a float array with the given name.void
Associates a Font value with the given name.void
Associates an integer value with the given name.void
Associates an integer array with the given name.void
putKeyStroke
(String name, KeyStroke value) Associates a KeyStroke value with the given name.void
Associates a long value with the given name.void
Associates a long array with the given name.void
putSaveState
(String name, SaveState value) Associates a sub SaveState value with the given name.void
Associates a short value with the given name.void
Associates a short array with the given name.void
Associates a String value with the given name.void
putStrings
(String name, String[] value) Associates a String array with the given name.void
putXmlElement
(String name, org.jdom.Element element) Adds an XML element to the saved state object.static SaveState
readJsonFile
(File file) Creates a SaveState object and populates its values from the given file.void
Remove the object identified by the given namevoid
saveToFile
(File file) Write the saveState to a file as XMLcom.google.gson.JsonObject
Save this object to an JsonObjectvoid
saveToJsonFile
(File file) Outputs this SaveState to a file using Jsonorg.jdom.Element
Save this object to an XML element.int
size()
Return the number of properties in the save statetoString()
-
Field Details
-
DATE_FORMAT
-
-
Constructor Details
-
SaveState
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:
-
SaveState
Construct a SaveState from a file containing XML from a previously saved SaveState.- Parameters:
file
- the file containing the XML to read.- Throws:
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 Details
-
readJsonFile
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:
IOException
- if an error occurs reading the given file.
-
saveToFile
Write the saveState to a file as XML- Parameters:
file
- the file to write to.- Throws:
FileNotFoundException
- if the file does not represent a valid file path.IOException
- if the file could not be written
-
saveToJsonFile
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:
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(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
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
Return the names of the objects saved in the state.- Returns:
- String[] array will be zero length if the save state is empty
-
putInt
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
-