@Override public List<ContextNode> replaceContextNode(ContextNode contextNode) { List<ContextNode> compoundCopyContextNodes = Collections.singletonList(contextNode); for (CopyStrategy copyStrategy : this.copyStrategies) { List<ContextNode> nextCompoundCopyContextNodes = new ArrayList<ContextNode> (); for (ContextNode compoundCopyContextNode : compoundCopyContextNodes) { List<ContextNode> copyContextNodes = copyStrategy.replaceContextNode(compoundCopyContextNode); if (copyContextNodes != null) nextCompoundCopyContextNodes.addAll(copyContextNodes); else nextCompoundCopyContextNodes.add(compoundCopyContextNode); } compoundCopyContextNodes = nextCompoundCopyContextNodes; } return compoundCopyContextNodes; }
/** * Copies a context node into a target context node. * @param contextNode A context node from any context node. * @param targetContextNode The target context node. * @param copyStrategy The strategy to determine what to copy. * @return The copied context node in the target context node. */ public static ContextNode copyContextNode(ContextNode contextNode, ContextNode targetContextNode, CopyStrategy copyStrategy) { if (contextNode == null) throw new NullPointerException(); if (targetContextNode == null) throw new NullPointerException(); if (copyStrategy == null) copyStrategy = DEFAULT_COPY_STRATEGY; List<ContextNode> copyContextNodes = copyStrategy.replaceContextNode(contextNode); if (copyContextNodes == null) copyContextNodes = Collections.singletonList(contextNode); // TODO: maybe avoid creating a list object here ContextNode targetCopiedContextNode = null; for (ContextNode copyContextNode : copyContextNodes) { XDIArc copyContextNodeXDIArc = copyContextNode.getXDIArc(); if (copyContextNodeXDIArc != null) targetCopiedContextNode = targetContextNode.setContextNode(copyContextNodeXDIArc); else targetCopiedContextNode = targetContextNode; copyContextNodeContents(copyContextNode, targetCopiedContextNode, copyStrategy); } return copyContextNodes.size() > 1 ? null : targetCopiedContextNode; }