[
https://issues.apache.org/jira/browse/GROOVY-8206?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16025831#comment-16025831
]
Manish Yadav commented on GROOVY-8206:
--------------------------------------
[~paulk],
Here are the replication of issue because of current clone implementation(no
parent for child too):
class Test {
public static void main(String[] args){
// create a node with a child
Node node = new Node(null, 'parent1', new NodeList())
node.appendNode(new Node(null, 'child1', 'childVal1'))
// here children have reference to parent
println node.value().get(0).parent()
// clone node
Node clonedNode = node.clone()
// prints null here because after cloning children doesn't have
reference to parent
println clonedNode.value().get(0).parent()
}
}
Scenario which had was that we have framework where we get the parent from
node(after cloning) which fails in current implementation since it does not
store parent info in child nodes though parant have its list of children which
is half cloning only.
Hope I explained it well this time.
> Groovy clone node with parent
> -----------------------------
>
> Key: GROOVY-8206
> URL: https://issues.apache.org/jira/browse/GROOVY-8206
> Project: Groovy
> Issue Type: Improvement
> Components: groovy-jdk
> Reporter: Manish Yadav
>
> As of now groovy supports clone node feature without cloning parents.
> We can add following custom methods in some util class to make cloning more
> advanced :
> 1:
> {code}
> 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
> }
> {code}
> 2:
> {code}
> 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
> }
> {code}
--
This message was sent by Atlassian JIRA
(v6.3.15#6346)