Class PropertyFile

java.lang.Object
ortus.boxlang.runtime.util.PropertyFile

public class PropertyFile extends Object
A fluent API for managing property files in BoxLang.
  • Constructor Details

    • PropertyFile

      public PropertyFile()
      Constructor - initializes a new PropertyFile instance Sets default values for maxLineWidth (150), empty lines list, and uses system line separator
  • Method Details

    • fromMap

      public static PropertyFile fromMap(Map<String,String> properties)
      Creates a new PropertyFile from a Map
      Parameters:
      properties - The properties as a Map
      Returns:
      A new PropertyFile instance
    • fromIStruct

      public static PropertyFile fromIStruct(IStruct properties)
      Creates a new PropertyFile from an IStruct
      Parameters:
      properties - The properties as an IStruct
      Returns:
      A new PropertyFile instance
    • load

      public PropertyFile load(String path)
      Loads a property file from the specified path using BoxLang FileSystemUtil
      Parameters:
      path - A fully qualified path to a property file
      Returns:
      This PropertyFile instance for method chaining
      Throws:
      BoxRuntimeException - If the file does not exist or cannot be read
    • store

      public PropertyFile store(String path)
      Stores the property file to the specified path, if it doesn't exist, it will create the directory structure.
      Parameters:
      path - The path to store the file (optional, uses loaded path if not provided)
      Returns:
      This PropertyFile instance for method chaining
    • store

      public PropertyFile store()
      Stores the property file to the currently loaded path
      Returns:
      This PropertyFile instance for method chaining
    • get

      public String get(String name)
      Gets a property value by name
      Parameters:
      name - The property name
      Returns:
      The property value
      Throws:
      IllegalArgumentException - If the key doesn't exist
    • get

      public String get(String name, String defaultValue)
      Gets a property value by name with a default value
      Parameters:
      name - The property name
      defaultValue - The default value if the key doesn't exist
      Returns:
      The property value or default value
    • addComment

      public PropertyFile addComment(String comment)
      Adds a comment line to the property file
      Parameters:
      comment - The comment text (without # prefix)
      Returns:
      This PropertyFile instance for method chaining
    • addBlankLine

      public PropertyFile addBlankLine()
      Adds a blank line to the property file
      Returns:
      This PropertyFile instance for method chaining
    • set

      public PropertyFile set(String name, String value)
      Sets a property value
      Parameters:
      name - The property name
      value - The property value
      Returns:
      This PropertyFile instance for method chaining
    • setMulti

      public PropertyFile setMulti(IStruct properties)
      Sets multiple property values at once
      Parameters:
      properties - An IStruct containing key-value pairs to set
      Returns:
      This PropertyFile instance for method chaining
    • setMulti

      public PropertyFile setMulti(Map<String,String> properties)
      Sets multiple property values at once from a Map
      Parameters:
      properties - A Map containing key-value pairs to set
      Returns:
      This PropertyFile instance for method chaining
    • remove

      public PropertyFile remove(String name)
      Removes a property
      Parameters:
      name - The property name to remove
      Returns:
      This PropertyFile instance for method chaining
    • removeMulti

      public PropertyFile removeMulti(Array names)
      Removes multiple properties at once
      Parameters:
      names - The property names to remove
      Returns:
      This PropertyFile instance for method chaining
    • exists

      public boolean exists(String name)
      Checks if a property exists
      Parameters:
      name - The property name
      Returns:
      true if the property exists, false otherwise
    • hasProperties

      public boolean hasProperties()
      Checks if the property file has any properties
      Returns:
      true if the property file contains at least one property, false otherwise
    • size

      public int size()
      Gets the number of properties in the file
      Returns:
      The count of properties
    • clearProperties

      public PropertyFile clearProperties()
      Clears all properties while preserving comments and whitespace
      Returns:
      This PropertyFile instance for method chaining
    • clear

      public PropertyFile clear()
      Clears everything including comments and whitespace
      Returns:
      This PropertyFile instance for method chaining
    • getAsStruct

      public IStruct getAsStruct()
      Gets all properties as a BoxLang IStruct Uses LinkedStruct to preserve property order as they appear in the file
      Returns:
      An IStruct containing all properties with insertion order preserved
    • getAsMap

      public Map<String,String> getAsMap()
      Gets all properties as a standard Java Map Properties are returned in the order they appear in the file
      Returns:
      A LinkedHashMap containing all properties with insertion order preserved
    • mergeStruct

      public PropertyFile mergeStruct(IStruct incomingStruct)
      Merges properties from an IStruct into this PropertyFile
      Parameters:
      incomingStruct - The IStruct containing properties to merge
      Returns:
      This PropertyFile instance for method chaining
    • mergeMap

      public PropertyFile mergeMap(Map<String,String> incomingMap)
      Merges properties from a Map into this PropertyFile
      Parameters:
      incomingMap - The Map containing properties to merge
      Returns:
      This PropertyFile instance for method chaining
    • getPropertyNames

      public Array getPropertyNames()
      Gets all property names
      Returns:
      An array of property names
    • toString

      public String toString()
      To String representation of the property file as a struct
      Overrides:
      toString in class Object
    • toJSON

      public String toJSON()
      JSON representation of the property file
    • getPath

      public String getPath()
      Gets the file path of this property file
      Returns:
      The fully qualified path to the property file
    • setPath

      public PropertyFile setPath(String path)
      Sets the file path of this property file
      Parameters:
      path - The fully qualified path to the property file
      Returns:
      This PropertyFile instance for method chaining
    • getLines

      public List<IStruct> getLines()
      Gets the list of all lines (comments, whitespace, and properties) in the file
      Returns:
      List of IStruct objects representing the file structure
    • setLines

      public PropertyFile setLines(List<IStruct> lines)
      Sets the list of all lines in the file
      Parameters:
      lines - List of IStruct objects representing the file structure
      Returns:
      This PropertyFile instance for method chaining
    • getMaxLineWidth

      public int getMaxLineWidth()
      Gets the maximum line width for formatting long property values
      Returns:
      The maximum line width in characters
    • setMaxLineWidth

      public PropertyFile setMaxLineWidth(int maxLineWidth)
      Sets the maximum line width for formatting long property values
      Parameters:
      maxLineWidth - The maximum line width in characters
      Returns:
      This PropertyFile instance for method chaining