Design for Xalan-J class org.apache.xpath.compiler.OpMap

Last modified: 2026-05-20

(Apache Xalan-J team)

The org.apache.xpath.compiler.OpMap class in Apache Xalan-J serves as the structural foundation for XPath expression representation. It is designed to act as an optimized, flat data structure (an array of integers) to represent an XPath abstract syntax tree (AST), prioritizing fast execution speeds and low memory overhead.


1. The Core Design Concept: The "OpMap"

Instead of building a traditional tree of objects (which introduces heavy memory and allocation overhead), Xalan-J parses an XPath string into a flat integer array called the Operations Map (OpMap).

1.1) Flat Array Representation: Each node and its parameters (like operations, step operators, and literals) are stored sequentially in an OpMapVector.

1.2) Opcode + Arguments: The array is traversed in chunks. A specific opcode (e.g., OpCodes.OP_LOCATIONPATH) sits at a given position, immediately followed by the length of the operation and the lengths/pointers to its child nodes.

1.3) Helper Methods: OpMap is equipped with protected/utility methods like getArgLength() and getFirstChildPos() to parse and navigate this array structure without writing explicit, repetitive pointer math.


2. Primary Components

The OpMap encapsulates the basic state and memory structures required to store a parsed XPath:

2.1) m_opMap: An OpMapVector (a custom array-based list) that holds the actual tree of integer opcodes and their arguments.

2.2) m_tokenQueue: An ObjectVector containing the literal tokens (e.g., string literals, node names, and numeric constants) referenced by the opcodes in m_opMap.

2.3) m_currentPattern: Stores the raw XPath string, which is mostly kept for diagnostic and debugging purposes.


3. Structural Hierarchy

3.1) Base Class (OpMap): Contains the data structures and basic navigation algorithms for traversing the opcode array.

3.2) Derived Class (Compiler): Extends OpMap to actually iterate over the flat integer map and build a proper executable expression tree (e.g., Expression objects) that evaluates the XPath dynamically against context nodes.

This design, for Xalan-J XPath parser creating a flat array (OpMap) and a compiler traversing it was chosen to balance Xalan-J's need for both interpretative tooling and fast, compiled runtime performance.