/** * Copies a whole graph into a target graph. * @param graph A graph. * @param targetGraph The target graph. * @param copyStrategy The strategy to determine what to copy. */ public static void copyGraph(Graph graph, Graph targetGraph, CopyStrategy copyStrategy) { if (graph == null) throw new NullPointerException(); if (targetGraph == null) throw new NullPointerException(); if (copyStrategy == null) copyStrategy = DEFAULT_COPY_STRATEGY; if (graph == targetGraph) throw new Xdi2RuntimeException("Source and target graph cannot be the same."); copyContextNodeContents(graph.getRootContextNode(true), targetGraph, copyStrategy); }
/** * Creates a relative graph in which a given context node is the root context node. * @param contextNode A context node in a graph. * @return The relative graph. */ public static Graph relativeGraph(ContextNode contextNode) { Graph relativeGraph = MemoryGraphFactory.getInstance().openGraph(); CopyUtil.copyContextNodeContents(contextNode, relativeGraph, null); return relativeGraph; } }
@Override public List<ContextNode> replaceContextNode(ContextNode contextNode) { XDIAddress contextNodeXDIAddress = contextNode.getXDIAddress(); XDIAddress replacedContextNodeXDIAddress = replaceXDIAddress(contextNodeXDIAddress); ContextNode replacedContextNode = GraphUtil.contextNodeFromComponents(replacedContextNodeXDIAddress); CopyUtil.copyContextNodeContents(contextNode, replacedContextNode, null); return Collections.singletonList(replacedContextNode); }
/** * Copies a context node into a target graph. * @param contextNode A context node from any graph. * @param targetGraph The target graph. * @param copyStrategy The strategy to determine what to copy. * @return The copied context node in the target graph. */ public static ContextNode copyContextNode(ContextNode contextNode, Graph targetGraph, CopyStrategy copyStrategy) { if (contextNode == null) throw new NullPointerException(); if (targetGraph == null) throw new NullPointerException(); if (copyStrategy == null) copyStrategy = DEFAULT_COPY_STRATEGY; ContextNode targetContextNode; XDIAddress contextNodeXDIAddress = contextNode.getXDIAddress(); if (contextNode.isRootContextNode()) { targetContextNode = targetGraph.getRootContextNode(false); copyContextNodeContents(contextNode, targetContextNode, copyStrategy); } else { targetContextNode = targetGraph.setDeepContextNode(contextNodeXDIAddress); copyContextNodeContents(contextNode, targetContextNode, copyStrategy); } return targetContextNode; }
/** * 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; }
CopyUtil.copyContextNodeContents(contextNode, replacedContextNode, null);
public static FullMessagingResponse fromMessageEnvelope(MessageEnvelope messageEnvelope) { Graph resultGraph = MemoryGraphFactory.getInstance().openGraph(); for (XdiInnerRoot xdiInnerRoot : messageEnvelope.getOperationResults()) { CopyUtil.copyContextNodeContents(xdiInnerRoot.getContextNode(), resultGraph, null); } FullMessagingResponse messageEnvelopeMessagingResponse = new FullMessagingResponse(messageEnvelope, resultGraph); return messageEnvelopeMessagingResponse; }
@Override public List<ContextNode> replaceContextNode(ContextNode contextNode) { if (contextNode.isRootContextNode()) return null; if (! contextNode.getContextNode().isRootContextNode()) return null; XDIAddress contextNodeXDIAddress = contextNode.getXDIAddress(); XDIAddress replacedContextNodeXDIAddress = replaceXDIAddress(contextNodeXDIAddress); if (log.isTraceEnabled()) log.trace("Replaced " + contextNodeXDIAddress + " with " + replacedContextNodeXDIAddress); if (contextNodeXDIAddress.equals(replacedContextNodeXDIAddress)) return super.replaceContextNode(contextNode); ContextNode replacedContextNode = GraphUtil.contextNodeFromComponents(replacedContextNodeXDIAddress); CopyUtil.copyContextNodeContents(contextNode, replacedContextNode, null); int additionalArcs = replacedContextNodeXDIAddress.getNumXDIArcs() - contextNodeXDIAddress.getNumXDIArcs(); replacedContextNode = replacedContextNode.getContextNode(additionalArcs); return Collections.singletonList(replacedContextNode); }
/** * Creates a clone of the given message with the same contents. * @param message The message to clone. * @return The cloned message. */ public static Message cloneMessage(Message message, boolean newId) { MessageCollection clonedMessageCollection = new MessageEnvelope().getMessageCollection(message.getSenderXDIAddress(), true); XdiEntity xdiEntity = message.getXdiEntity(); ContextNode copiedContextNode; if (newId) { if (xdiEntity instanceof XdiEntityInstanceUnordered) { copiedContextNode = clonedMessageCollection.getXdiEntityCollection().setXdiInstanceUnordered(true, false).getContextNode(); CopyUtil.copyContextNodeContents(message.getContextNode(), copiedContextNode, null); } else if (xdiEntity instanceof XdiEntityInstanceOrdered) { copiedContextNode = clonedMessageCollection.getXdiEntityCollection().setXdiInstanceOrdered(false, true).getContextNode(); CopyUtil.copyContextNodeContents(message.getContextNode(), copiedContextNode, null); } else { throw new Xdi2RuntimeException("Unexpected message entity: " + xdiEntity + " (" + xdiEntity.getClass().getSimpleName() + ")"); } } else { copiedContextNode = CopyUtil.copyContextNode(message.getContextNode(), clonedMessageCollection.getContextNode(), null); } XdiEntity clonedXdiEntity = XdiAbstractEntity.fromContextNode(copiedContextNode); Message clonedMessage = Message.fromMessageCollectionAndXdiEntity(clonedMessageCollection, clonedXdiEntity); if (log.isTraceEnabled()) log.trace("Cloned message: " + clonedMessage.getMessageEnvelope()); return clonedMessage; }
new ReplaceLiteralVariablesCopyStrategy(variableValues), new ReplaceEscapedVariablesCopyStrategy()); CopyUtil.copyContextNodeContents(this.getMessageTemplate().getContextNode(), message.getContextNode(), copyStrategy);
new ReplaceLiteralVariablesCopyStrategy(variableValues), new ReplaceEscapedVariablesCopyStrategy()); CopyUtil.copyContextNodeContents(this.getLinkContractTemplate().getContextNode(), linkContract.getContextNode(), copyStrategy);
public void setDID(DID did) { if (did.equals(this.getDID())) return; ContextNode newContextNode = this.getContextNode().getGraph().setDeepContextNode(did.getXDIAddress()); CopyUtil.copyContextNodeContents(this.getContextNode(), newContextNode, null); this.getContextNode().delete(); this.xdiEntity = XdiAbstractEntity.fromContextNode(newContextNode); }
CopyUtil.copyContextNodeContents(refRepTargetContextNode, tempContextNode, null);
copyContextNodeContents(copyRelation.followContextNode(), targetRelation.followContextNode(), copyStrategy); } else {