TXFAFormStructure

HPDFXFAFormStructure provides native preparation and identity mapping for template-backed form instances

The class borrows its TXFADocument, owns its structural graph and detached candidates, and never frees the borrowed document

constructor Create(Document: TXFADocument;
  const Limits: TXFADataStructureLimits;
  const OnTick: TXFADataStructureTick);
procedure BeginOperation(const OnTick: TXFADataStructureTick);
procedure EndOperation;
function PrepareUnboundModel: TXFANode;
procedure AcceptDataReplacements(
  const Replacements: TDictionary<UInt64, TXFANode>);
procedure AcceptMergeDataReplacements(
  const Replacements: TDictionary<UInt64, TXFANode>);
procedure NotifyDataMutation;
property MergeActive: Boolean;
function EnsureInstance(Definition, DataNode: TXFANode): TXFANode;
function PrepareMaterialization(Definition, Instance: TXFANode;
  out Aliases: TXFAFormStructureAliases): TXFANode;
function ReplaceInstance(Instance, Prepared: TXFANode): TXFANode;
procedure DiscardPrepared(Node: TXFANode);
property Graph: TXFADataStructure;

Operation scope

Call BeginOperation before structural work and EndOperation in a finally block

The supplied callback is called throughout bounded traversal, copying and graph preflight, and may raise an exception to cancel the operation

Graph.DetachedRoots exposes the graph's owning collection for observation and coordinated snapshot restoration, and callers must not independently free nodes still owned by that collection

Preparation and adoption

PrepareMaterialization composes a detached form candidate from a template definition and an optional existing instance, returning source-to-candidate pairs in TXFAFormStructureAliases

TXFAFormStructureAlias.Source and Target are borrowed node references for remapping external identities before releasing the previous instance

ReplaceInstance adopts the prepared candidate and returns the previous instance as a detached graph-owned node

Retarget dependent references before calling DiscardPrepared on the returned previous instance

DiscardPrepared releases a detached candidate owned by the same graph and does not remove an attached live instance

Initial and sparse instances

PrepareUnboundModel returns a detached form model for an empty merge, preserving authored template content and expanding supported subform occurrences into independent native nodes

EnsureInstance returns an existing live form node or constructs the required sparse instance path for a template definition and its live data context

EnsureInstance can mutate several live nodes, so compound-operation rollback belongs to the caller's surrounding document transaction

TXFAWidgetRuntime provides that transaction for public script events, including native document and detached identity restoration

Accepted data replacements

AcceptDataReplacements migrates existing automatic scalar, group and iteration binding metadata after a successful live Data XML replacement. The dictionary maps native identities captured before replacement to the corresponding nodes now attached to the same document's datasets packet

Call it inside BeginOperation/EndOperation and the caller's surrounding document transaction, before later field actions or automatic Form reconciliation. The Runtime supplies accepted mappings during native XML import; the helper borrows the dictionary and target nodes, which remain owned by their document, and does not create a new Form tree

Missing or nil replacements leave their prior identity metadata intact for strict removed-node resolution. Explicit record-cursor selection retains its own record identity and missing-record behaviour

Traversal, metadata capacity, path depth and byte accounting use the structure's existing limits and tick callback. Foreign or detached replacement targets are rejected by this live-document helper; ordinary detached Data DOM XML import retains its existing separate path

Predicate merge coordination

Computed binding predicates may change native data while a merge is being planned. MergeActive identifies this existing merge operation; callbacks must borrow its operation scope rather than recursively beginning another graph operation

NotifyDataMutation charges the current operation and invalidates pending merge plans when a merge is active. The coordinator completes the current query, preserves its ordered native-identity receipt and rebuilds pending plans before publishing Form allocations

AcceptMergeDataReplacements requires an active merge and borrows the accepted old-identity-to-live-node map. It migrates both existing Form binding metadata and pending query receipts without starting another operation. One mapping batch is simultaneous rather than transitively chained; a later independent batch can migrate the newly accepted identities again

Notify the coordinator separately after applying an accepted mutation; the replacement helper does not send that notification itself. The Runtime coordinates these calls for native Data XML and structural actions, deferring ordinary global value synchronisation until pending plans have unwound

Receipts, logical instance slots, live identity checks and replanning share the existing operation, byte, node and depth allowances. Completed scalar selection never promotes its second match when its first accepted target disappears, while repeated groups preserve their accepted allocated subset

Helper functions

FunctionPurpose
HPDFXFAEmptyMergeCountValidates an occurrence declaration and returns its empty-merge count, using explicit initial or the minimum
HPDFXFAIsMaterializedFormNodeChecks the namespace-qualified materialisation marker under an explicit traversal budget
HPDFXFAResolveMaterializedDefinitionResolves the effective form node for a definition and data context, reporting whether the materialised model is authoritative
HPDFXFAResolveActualFormScopeResolves the live data scope of an actual repeated form instance without repeating its template declaration again

The saved marker uses urn:hotpdf:xfa:form-structure:1 with a collision-safe prefix, while native form nodes use the XFA form namespace

Standalone native callers must retain the document for the class lifetime and provide rollback around compound graph operations when cancellation or another failure must restore earlier successful steps