Class TreePath

java.lang.Object
com.streamconverter.path.TreePath
All Implemented Interfaces:
IPath<List<String>>

public class TreePath extends Object implements IPath<List<String>>
Represents a hierarchical path for tree-like data structures (JSON, XML).

This class handles parsing and matching of path expressions in both JSON-style ("$.user.name") and XML-style ("user/name") formats. It converts path expressions into hierarchical segments for efficient matching during data processing.

  • Method Details

    • fromXml

      public static TreePath fromXml(String xmlPath)
      Creates a TreePath from an XML path expression.
      Parameters:
      xmlPath - the XML path expression (e.g., "user/name")
      Returns:
      TreePath instance
      Throws:
      IllegalArgumentException - if xmlPath is null or invalid
    • fromJson

      public static TreePath fromJson(String jsonPath)
      Creates a TreePath from a JSON path expression.
      Parameters:
      jsonPath - the JSON path expression (e.g., "$.user.name")
      Returns:
      TreePath instance
      Throws:
      IllegalArgumentException - if jsonPath is null or invalid
    • matches

      public boolean matches(List<String> currentPath)
      Checks if current path matches the target path segments
      Specified by:
      matches in interface IPath<List<String>>
      Parameters:
      currentPath - current path segments to match against
      Returns:
      true if paths match exactly
    • matchesIgnoringArraySyntax

      public boolean matchesIgnoringArraySyntax(List<String> currentPath)
      Checks if current path matches after stripping array-index syntax from this path's segments.

      A segment like "orders[*]" or "orders[0]" is normalized to "orders" before comparison. This allows paths such as "$.orders[*].product_code" to match the streaming currentPath list ["orders", "product_code"].

      Parameters:
      currentPath - current path segments built by the streaming traversal
      Returns:
      true if the normalized segments equal currentPath
    • toString

      public String toString()
      Returns the original path expression.
      Overrides:
      toString in class Object
      Returns:
      the original path expression
    • equals

      public boolean equals(Object obj)
      2つのTreePathが等しいかどうかをセグメントリストで比較する。

      注意: 元のパス文字列(toString() の値)が異なっていても、セグメントに展開した結果が同じであれば等しいとみなす。 例えば JSONスタイルの "$.user.name" と XMLスタイルの "user/name" がパース後に同じセグメントになる場合、 等しいと判定される。

      Overrides:
      equals in class Object
      Parameters:
      obj - 比較対象のオブジェクト
      Returns:
      セグメントリストが等しい場合true
    • hashCode

      public int hashCode()
      セグメントリストに基づくハッシュコードを返す。
      Overrides:
      hashCode in class Object
      Returns:
      ハッシュコード