Skip to content

Unique identifier for the Node instances #403

@geirolz

Description

@geirolz

Hi, i'm writing a Scala library to add a functional layer to this library.
In my library i've defined a rule to edit the xml document.
This rule contains:

  • a function NodeSeq => NodeSeq called Zoom
  • an action to apply to the target(target = Zoom applied to the whole document)

With this rule instance i create a standard RewriteRule that will be applied to the whole document using a RuleTransformer.

The code is something like this:

val document : NodeSeq = ???
val rule : XmlRule = ???
val target : NodeSeq = rule.zoom(document)

val rwr = new RewriteRule {
  override def transform(ns: Seq[Node]): Seq[Node] = {
    if(ns == target)
      rule.action(target)
    else 
      ns 
  }
}

Problem
The problem is: when the document has nodes with the same values ns == target returns true multiple times, also if that node doesn't descends from the same parent.

Question
There is a way to obtain an unique id for each node ? If not, it should be a valid proposal generating an unique id during the node instantiation and keep it as node field ?

abstract class Node {
  private lazy val uuid : UUID = UUID.randomUUID() //or something else

  def sameInstance(that: Node) : Boolean = {
    this.equals(that) && this.uuid.equals(that.uuid)
  }
} 

val n1 : Node = <Node />
val n2 : Node = <Node />
n1.sameInstance(n2) //false

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions