/** * Copies all context nodes of a context node into a target context node. * @param contextNode A context node from any graph. * @param targetContextNode The target context node. * @param copyStrategy The strategy to determine what to copy. * @return The copied context nodes in the target graph. */ public static Iterator<ContextNode> copyContextNodes(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; for (Iterator<ContextNode> innerContextNodes = contextNode.getContextNodes(); innerContextNodes.hasNext(); ) { ContextNode innerContextNode = innerContextNodes.next(); copyContextNode(innerContextNode, targetContextNode, copyStrategy); } return targetContextNode.getContextNodes(); }
@Override public ReadOnlyIterator<XdiPeerRoot> getPeerRoots() { return new MappingContextNodePeerRootIterator(this.getContextNode().getContextNodes()); }
@Override public ReadOnlyIterator<XdiInnerRoot> getInnerRoots() { return new MappingContextNodeInnerRootIterator(this.getContextNode().getContextNodes()); }
private void writeInternal(Graph graph, JsonArray jsonArray) throws IOException { // start with the common root node for (ContextNode childContextNode : graph.getRootContextNode().getContextNodes()) { // create JSON object for this context node JsonObject jsonObject = new JsonObject(); // start mapping JXDMapping JXDmapping = this.startMapping(jsonObject); // process context node this.putContextNodeIntoJsonObject(childContextNode, jsonObject, JXDmapping, true); // finish mapping this.finishMapping(JXDmapping, jsonObject); // finish JSON object for this context node if (! jsonObject.entrySet().isEmpty()) jsonArray.add(jsonObject); } }
private void putInnerRootIntoJsonObject(Relation relation, JsonObject jsonObject, JXDMapping mapping) { // include it? if (! includeInnerRoot(relation)) return; // determine key String key = mapInnerRoot(relation.getXDIAddress(), mapping); // determine child JSON object JsonElement childJsonElement = jsonObject.get(key); JsonObject childJsonObject; if (childJsonElement == null) { childJsonObject = new JsonObject(); jsonObject.add(key, childJsonObject); } else if (childJsonElement instanceof JsonArray) { childJsonObject = new JsonObject(); ((JsonArray) childJsonElement).add(childJsonElement); } else { throw new Xdi2RuntimeException("Unexpected JSON element for inner root at key " + key + ": " + childJsonElement); } // type information for child JSON object childJsonObject.add(JXDConstants.JXD_TYPE, new JsonPrimitive(JXDConstants.JXD_GRAPH)); // fill child JSON object for (ContextNode childContextNode : relation.followContextNode().getContextNodes()) { this.putContextNodeIntoJsonObject(childContextNode, childJsonObject, mapping, false); } }
public void clearAfterDecrypt() { for (ContextNode contextNode : this.getBaseContextNode().getContextNodes()) { XdiAttribute xdiAttribute = XdiAbstractAttribute.fromContextNode(contextNode); if (! Encryption.isValid(xdiAttribute)) continue; contextNode.delete(); } }
public static Map<XDIArc, Object> getVariableValues(XdiEntity variableValuesXdiEntity) { if (variableValuesXdiEntity == null) return Collections.emptyMap(); Map<XDIArc, Object> variableValues = new HashMap<XDIArc, Object> (); MappingContextNodeXdiVariableIterator xdiVariablesIterator = new MappingContextNodeXdiVariableIterator(variableValuesXdiEntity.getContextNode().getContextNodes()); for (XdiVariable<?> xdiVariable : xdiVariablesIterator) { XDIArc variableValueXDIArc = xdiVariable.getXDIArc(); ReadOnlyIterator<ContextNode> variableValueContextNodes = Equivalence.getIdentityContextNodes(xdiVariable.getContextNode()); Object variableLiteralDataValue = xdiVariable.getContextNode().getLiteralData(); if (variableValueContextNodes.hasNext() && variableLiteralDataValue != null) throw new Xdi2RuntimeException("Variable has both an XDI address and a literal data value."); if ((! variableValueContextNodes.hasNext()) && variableLiteralDataValue == null) throw new Xdi2RuntimeException("Variable has neither XDI address nor literal data value."); if (variableValueContextNodes.hasNext()) { List<XDIAddress> variableXDIAddressValues = new ArrayList<XDIAddress> (); variableValues.put(variableValueXDIArc, variableXDIAddressValues); for (ContextNode variableValueContextNode : variableValueContextNodes) { XDIAddress variableXDIAddressValue = variableValueContextNode.getXDIAddress(); if (log.isDebugEnabled()) log.debug("Variable XDI address value: " + variableValueXDIArc + " --> " + variableXDIAddressValue); variableXDIAddressValues.add(variableXDIAddressValue); } } if (variableLiteralDataValue != null) { if (log.isDebugEnabled()) log.debug("Variable literal data value: " + variableValueXDIArc + " --> " + variableLiteralDataValue); variableValues.put(variableValueXDIArc, variableLiteralDataValue); } } return variableValues; }
for (ContextNode innerContextNode : contextNode.getContextNodes()) {
public void clearAfterEncrypt() { for (ContextNode contextNode : this.getBaseContextNode().getContextNodes()) { XdiAttribute xdiAttribute = XdiAbstractAttribute.fromContextNode(contextNode); if (Encryption.isValid(xdiAttribute)) continue; contextNode.delete(); } this.getBaseContextNode().delRelations(); this.getBaseContextNode().delLiteralNode(); }
private void putAttributeIntoEntityJsonObject(ContextNode attributeContextNode, JsonObject entityJsonObject, XDIAddress parentXDIAddress) throws IOException { XDIAddress XDIaddress = attributeContextNode.getXDIAddress(); XDIAddress localXDIAddress = XDIAddressUtil.localXDIAddress(XDIaddress, - parentXDIAddress.getNumXDIArcs()); // set up attribute JsonObject attributeJsonObject = new JsonObject(); entityJsonObject.add(localXDIAddress.toString(), attributeJsonObject); // context nodes for (ContextNode contextNode : attributeContextNode.getContextNodes()) { if (XdiAbstractAttribute.isValid(contextNode) || XdiAttributeCollection.isValid(contextNode)) { this.putAttributeIntoEntityJsonObject(contextNode, entityJsonObject, parentXDIAddress); continue; } throw new Xdi2RuntimeException("Unexpected context node: " + contextNode + " on attribute context node: " + attributeContextNode); } // relations for (Relation relation : attributeContextNode.getRelations()) { this.putRelationIntoJsonObject(relation, attributeJsonObject); } // literal node if (attributeContextNode.containsLiteralNode()) { LiteralNode literalNode = attributeContextNode.getLiteralNode(); this.putLiteralNodeIntoAttributeJsonObject(literalNode, attributeJsonObject, XDIaddress); } // finish attribute if (attributeJsonObject.entrySet().isEmpty() && ! attributeContextNode.isEmpty()) { if (! this.isWriteImplied()) entityJsonObject.remove(localXDIAddress.toString()); } }
for (ContextNode contextNode : entityContextNode.getContextNodes()) {
for (ContextNode contextNode : rootContextNode.getContextNodes()) {
private static ContextNode collapseContextNode(ContextNode contextNode) { if (contextNode.getContextNodeCount() != 1) return contextNode; if (contextNode.containsRelations()) return contextNode; if (contextNode.containsLiteralNode()) return contextNode; ContextNode childContextNode = contextNode.getContextNodes().next(); if (! (XdiAbstractEntity.isValid(contextNode) || XdiEntityCollection.isValid(contextNode)) && (XdiAbstractEntity.isValid(childContextNode) || XdiEntityCollection.isValid(childContextNode))) return contextNode; if (! (XdiAbstractAttribute.isValid(contextNode) || XdiAttributeCollection.isValid(contextNode)) && (XdiAbstractAttribute.isValid(childContextNode) || XdiAttributeCollection.isValid(childContextNode))) return contextNode; return collapseContextNode(childContextNode); }
for (ContextNode contextNode : attributeContextNode.getContextNodes()) {
for (ContextNode childContextNode : contextNode.getContextNodes()) {
for (ContextNode contextNode : entityContextNode.getContextNodes()) {
for (ContextNode contextNode : rootContextNode.getContextNodes()) {
assertFalse(root.containsRelations()); assertFalse(root.containsLiteralNode()); assertFalse(root.getContextNodes().hasNext()); assertFalse(root.getRelations().hasNext()); assertNull(root.getLiteralNode()); assertTrue(root.containsRelations()); assertFalse(root.containsLiteralNode()); assertTrue(root.getContextNodes().hasNext()); assertTrue(root.getRelations().hasNext()); assertNotNull(root.getRelations().next().follow()); assertTrue(root.containsRelations()); assertFalse(root.containsLiteralNode()); assertTrue(root.getContextNodes().hasNext()); assertTrue(root.getRelations().hasNext()); assertNull(root.getLiteralNode()); root.getContextNodes().next().delete(); root.getContextNodes().next().delete(); root.getContextNodes().next().delete(); assertFalse(root.containsRelations()); assertFalse(root.containsLiteralNode()); assertFalse(root.getContextNodes().hasNext()); assertFalse(root.getRelations().hasNext()); assertNull(root.getLiteralNode());
assertFalse(markus.getContextNodes().hasNext()); try { markus.getContextNodes().next(); fail(); } catch (NoSuchElementException ex) { } assertNull(markus.getContextNode(XDIArc.create("*not"))); assertNull(markus.getDeepContextNode(XDIAddress.create("*not")));
assertEquals(arcs.size(), new IteratorCounter(contextNodesArray[i].getContextNodes()).count()); for (Iterator<ContextNode> it = contextNodesArray[i].getContextNodes(); it.hasNext(); ) assertTrue(arcs.remove(it.next().getXDIArc())); assertTrue(arcs.isEmpty());