Class GProperties

java.lang.Object
ghidra.framework.options.GProperties
Direct Known Subclasses:
JSonProperties, XmlProperties

public class GProperties extends 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 GProperies object. The idea is that each state variable in the class is first saved into a GProperties object via a String key. Then the GProperties object is written out as XML or Json. When the GProperties object is restored, the GProperties object is constructed with an XML Element or JsonObject that contains all of the name/value pairs. There are convenience subclasses for reading these from a file (XmlProperties and JsonProperties). 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 property names are assumed to be unique. When a putXXX() method is called, if a value already exists for a name, it will be overwritten.

The GProperties supports the following types:

      java primitives
      arrays of java primitives
      String
      Color
      Font
      KeyStroke
      File
      Date
      Enum
      GProperties (values can be nested GProperties)
  
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static DateFormat
     
    protected TreeMap<String,Object>
     
    protected static final String
     
    protected static final String
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    GProperties(com.google.gson.JsonObject root)
    Construct a new GProperties object using the given JSonObject.
    Creates a new GProperties object with a non-default name.
    GProperties(org.jdom.Element root)
    Construct a new GProperties object using the given XML element.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Clear all objects from this GProperties
    protected org.jdom.Element
     
    protected org.jdom.Element
    createElementFromElement(String internalKey, org.jdom.Element internalElement)
     
    protected <T> T
    getAsType(String name, T defaultValue, Class<T> clazz)
     
    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
    getByte(String name, byte defaultValue)
    Gets the byte value for the given name.
    byte[]
    getBytes(String name, byte[] defaultValue)
    Gets the byte array for the given name.
    getColor(String name, Color defaultValue)
    Gets the Color value for the given name.
    getDate(String name, Date defaultValue)
    Gets the Date value for the given name.
    double
    getDouble(String name, double defaultValue)
    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>>
    T
    getEnum(String name, T defaultValue)
    Gets the Enum value for the given name.
    getFile(String name, File defaultValue)
    Gets the File value for the given name.
    float
    getFloat(String name, float defaultValue)
    Gets the float value for the given name.
    float[]
    getFloats(String name, float[] defaultValue)
    Gets the float array for the given name.
    getFont(String name, Font defaultValue)
    Gets the Font value for the given name.
     
    int
    getInt(String name, int defaultValue)
    Gets the int value for the given name.
    int[]
    getInts(String name, int[] defaultValue)
    Gets the int array for the given name.
    getKeyStroke(String name, KeyStroke defaultValue)
    Gets the KeyStroke value for the given name.
    long
    getLong(String name, long defaultValue)
    Gets the long value for the given name.
    long[]
    getLongs(String name, long[] defaultValue)
    Gets the long array for the given name.
    Return the names of the properties in this GProperties.
    short
    getShort(String name, short defaultValue)
    Gets the short value for the given name.
    short[]
    getShorts(String name, short[] defaultValue)
    Gets the short array for the given name.
    getString(String name, String defaultValue)
    Gets the String value for the given name.
    getStrings(String name, String[] defaultValue)
    Gets the String array for the given name.
    org.jdom.Element
    Returns the root of an XML sub-tree associated with the given name.
    boolean
    Returns true if there is a value for the given name
    boolean
    Returns true if this GProperties contains no elements
    protected void
    processElement(org.jdom.Element elem)
     
    void
    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
    putByte(String name, byte value)
    Associates a byte value with the given name.
    void
    putBytes(String name, byte[] value)
    Associates a byte array with the given name.
    void
    putColor(String name, Color value)
    Associates a Color value with the given name.
    void
    putDate(String name, Date value)
    Associates a Date value with the given name.
    void
    putDouble(String name, double value)
    Associates a double value with the given name.
    void
    putDoubles(String name, double[] value)
    Associates a double value with the given name.
    void
    putEnum(String name, Enum<?> value)
    Associates an Enum with the given name.
    void
    putFile(String name, File value)
    Associates a File value with the given name.
    void
    putFloat(String name, float value)
    Associates a float value with the given name.
    void
    putFloats(String name, float[] value)
    Associates a float array with the given name.
    void
    putFont(String name, Font value)
    Associates a Font value with the given name.
    void
    Associates a sub SaveState value with the given name.
    void
    putInt(String name, int value)
    Associates an integer value with the given name.
    void
    putInts(String name, int[] value)
    Associates an integer array with the given name.
    void
    Associates a KeyStroke value with the given name.
    void
    putLong(String name, long value)
    Associates a long value with the given name.
    void
    putLongs(String name, long[] value)
    Associates a long array with the given name.
    void
    putShort(String name, short value)
    Associates a short value with the given name.
    void
    putShorts(String name, short[] value)
    Associates a short array with the given name.
    void
    putString(String name, String value)
    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 this GProperties.
    void
    remove(String name)
    Remove the object identified by the given name
    com.google.gson.JsonObject
    Save this object to an JsonObject
    void
    Outputs this GProperties to a file using Json
    org.jdom.Element
    Save this object to an XML element.
    void
    Write the properties to a file as XML
    int
    Return the number of properties in this GProperties
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

  • Constructor Details

    • GProperties

      public GProperties(String name)
      Creates a new GProperties object with a non-default name. The name serves no real purpose other than as a hint as to what the GProperties represents
      Parameters:
      name - of this GProperties object
    • GProperties

      public GProperties(org.jdom.Element root)
      Construct a new GProperties object using the given XML element.
      Parameters:
      root - XML contents of GProperties
    • GProperties

      public GProperties(com.google.gson.JsonObject root)
      Construct a new GProperties object using the given JSonObject.
      Parameters:
      root - JSonObject representing a GProperties
  • Method Details

    • processElement

      protected void processElement(org.jdom.Element elem)
    • saveToJsonFile

      public void saveToJsonFile(File file) throws IOException
      Outputs this GProperties to a file using Json

      For example, a GProperties that is created with:

        ss = new GProperties("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
      
       {
        "GPROPERTIES 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
    • saveToXmlFile

      public void saveToXmlFile(File file) throws IOException
      Write the properties to a file as XML
      Parameters:
      file - the file to write to.
      Throws:
      IOException - if the file could not be written
    • saveToXml

      public org.jdom.Element saveToXml()
      Save this object to an XML element.
      Returns:
      Element XML element containing the properties
    • createElement

      protected org.jdom.Element createElement(String key, Object value)
    • saveToJson

      public com.google.gson.JsonObject saveToJson()
      Save this object to an JsonObject
      Returns:
      JsonObject containing the properties
    • createElementFromElement

      protected org.jdom.Element createElementFromElement(String internalKey, org.jdom.Element internalElement)
    • isEmpty

      public boolean isEmpty()
      Returns true if this GProperties contains no elements
      Returns:
      true if there are no properties in this GProperties
    • remove

      public void remove(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 this GProperties
    • size

      public int size()
      Return the number of properties in this GProperties
      Returns:
      The number of properties in this GProperties
    • getNames

      public String[] getNames()
      Return the names of the properties in this GProperties.
      Returns:
      the names of the properties in this GProperties.
    • putInt

      public void putInt(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(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(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(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(String name, 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(String name, 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(String name, 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(String name, 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(String name, 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(String name, 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(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(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(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.
    • putGProperties

      public void putGProperties(String name, GProperties 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(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(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(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(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(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 String getString(String name, 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 Color getColor(String name, 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 Date getDate(String name, 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 File getFile(String name, 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 KeyStroke getKeyStroke(String name, 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 Font getFont(String name, 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(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(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(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(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(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(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(String name, 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(String name, 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(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(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(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(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(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(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(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(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 String[] getStrings(String name, 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 Enum<T>> T getEnum(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(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(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(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 this GProperties has a value for the given name
    • putXmlElement

      public void putXmlElement(String name, org.jdom.Element element)
      Adds an XML element to the this GProperties. Used by plugins that have more complicated state information that needs to be saved.
      Parameters:
      name - the name to associate with the element
      element - XML element which is the root of an XML sub-tree.
    • getXmlElement

      public org.jdom.Element getXmlElement(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.
    • getGProperties

      public GProperties getGProperties(String name)
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • getAsType

      protected <T> T getAsType(String name, T defaultValue, Class<T> clazz)