Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-8206

Groovy clone node with new parent reference in child nodes

    XMLWordPrintableJSON

    Details

    • Type: Improvement
    • Status: Open
    • Priority: Major
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: groovy-jdk
    • Labels:
      None

      Description

      As of now groovy supports clone node feature without storing parent reference(i.e. the new clone nodes) in child nodes.
      We can add following custom methods in some util class to make cloning more advanced :
      1:

      private Node cloneNode(Node node, Node parentNode) {
          Object newValue = node.value()
          Node clonedNode = new Node(null, node.name(), new HashMap(node.attributes()), newValue)
          if (newValue instanceof NodeList) {
              NodeList nodes = (NodeList) newValue
              newValue = cloneNodeList(nodes, clonedNode)
          }
          clonedNode.setValue(newValue)
          clonedNode.setParent(parentNode)
          return clonedNode
      }
      

      2:

      private NodeList cloneNodeList(NodeList nodeList, Node parentNode) {
          NodeList result = new NodeList(nodeList.size())
          for (int i = 0; i < nodeList.size(); i++) {
              Object next = nodeList.get(i)
              if (next instanceof Node) {
                  result.add(cloneNode((Node) next, parentNode))
              } else {
                  result.add(next)
              }
          }
          return result
      }
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                manishyadav149 Manish Yadav
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated: