public void testLocalRoots() throws Exception { Graph graph = MemoryGraphFactory.getInstance().openGraph(); assertEquals(XdiLocalRoot.findLocalRoot(graph).getContextNode(), graph.getRootContextNode()); assertEquals( XdiLocalRoot.findLocalRoot(graph).getContextNode().getXri(), XDIConstants.XRI_S_CONTEXT); graph.close(); }
public void testSubGraph() throws Exception { Graph graph = MemoryGraphFactory.getInstance().openGraph(); XdiCommonRoot localRoot = XdiCommonRoot.findCommonRoot(graph); XdiPeerRoot peerRoot = localRoot.getPeerRoot( XDIAddress.create("=!:uuid:91f28153-f600-ae24-91f2-8153f600ae24"), true); XdiInnerRoot innerRoot = peerRoot.getInnerRoot(XDIAddress.create("=!1111"), XDIAddress.create("$add"), true); assertTrue( XdiAbstractContext.fromContextNode(localRoot.getContextNode()) instanceof XdiCommonRoot); assertTrue( XdiAbstractContext.fromContextNode(peerRoot.getContextNode()) instanceof XdiPeerRoot); assertTrue( XdiAbstractContext.fromContextNode(innerRoot.getContextNode()) instanceof XdiInnerRoot); graph.close(); }
private static String makeJSON(Graph graph) { StringBuffer buffer = new StringBuffer(); buffer.append("var treeData = \n"); addContextNodeToBuffer(buffer, graph.getRootContextNode(true)); buffer.append(";\n"); buffer.append("var relData = \n"); addRelationsToBuffer(buffer, graph.getRootContextNode(true).getAllRelations()); buffer.append(";\n"); return buffer.toString(); /* StringWriter writer = new StringWriter(); new XDIJSONWriter(null).write(graph, writer); return writer.getBuffer().toString();*/ }
@Test public void testDuplicateDatatypes() throws Exception { StringBuilder buffer = new StringBuilder(); buffer.append("=markus<#age>/&/\"33\"\n"); buffer.append("=markus<#age>/$is#/+$xsd$int\n"); buffer.append("=markus<#age>/$is#/+$json$number\n"); String xdiString = buffer.toString(); try { Graph graph = (new MemoryGraphFactory()).parseGraph(xdiString); DataTypes.setDataType( graph.getDeepContextNode(XDIAddress.create("=markus<#age>")), XDIAddress.create("+$json$number")); fail(); graph.close(); } catch (Exception ex) { } }
public static void removeAccessToken(Graph graph, XDI3Segment userXri) { XDI3Segment contextNodeXri = new XDI3Segment( "" + GoogleCalendarMapping.XRI_S_GOOGLE_CALENDAR_CONTEXT + userXri + XDIMessagingConstants.XRI_S_OAUTH_TOKEN); ContextNode contextNode = graph.findContextNode(contextNodeXri, false); if (contextNode == null) return; contextNode.delete(); }
public static void storeAccessToken(Graph graph, XDI3Segment userXri, String accessToken) { XDI3Segment contextNodeXri = new XDI3Segment( "" + GoogleCalendarMapping.XRI_S_GOOGLE_CALENDAR_CONTEXT + userXri + XDIMessagingConstants.XRI_S_OAUTH_TOKEN); ContextNode contextNode = graph.findContextNode(contextNodeXri, true); if (contextNode.containsLiteral()) contextNode.getLiteral().setLiteralData(accessToken); else contextNode.createLiteral(accessToken); }
public void testDatatypes() throws Exception { StringBuilder buffer = new StringBuilder(); buffer.append("=markus<#age>/&/\"33\"\n"); buffer.append("=markus<#age>/$is#/$xsd$int\n"); buffer.append("=markus<#age>/$is#/$json$number\n"); String xdiString = buffer.toString(); Graph graph = (new MemoryGraphFactory()).parseGraph(xdiString, "XDI DISPLAY", null); DataTypes.setDataType( graph.getDeepContextNode(XDIAddress.create("=markus<#age>")), XDIAddress.create("$mime$image$png")); List<XDIAddress> dataTypes = DataTypes.getDataTypes(graph.getDeepContextNode(XDIAddress.create("=markus<#age>"))); assertNotNull(dataTypes); for (XDIAddress dataType : dataTypes) { if (dataType.toString().contains("json")) { assertEquals("number", DataTypes.jsonTypeFromDataTypeXDIAddress(dataType).toString()); } else if (dataType.toString().contains("xsd")) { assertEquals("xsd:int", DataTypes.xsdTypeFromDataTypeXDIAddress(dataType).toString()); } else if (dataType.toString().contains("mime")) { assertEquals("image/png", DataTypes.mimeTypeFromDataTypeXDIAddress(dataType).toString()); } } graph.close(); }
public static String retrieveAccessToken(Graph graph, XDI3Segment userXri) { XDI3Segment contextNodeXri = new XDI3Segment( "" + GoogleCalendarMapping.XRI_S_GOOGLE_CALENDAR_CONTEXT + userXri + XDIMessagingConstants.XRI_S_OAUTH_TOKEN); ContextNode contextNode = graph.findContextNode(contextNodeXri, false); if (contextNode == null) return null; Literal literal = contextNode.getLiteral(); if (literal == null) return null; return literal.getLiteralData(); }
private static void read(Graph graph, BufferedReader bufferedReader) throws IOException, Xdi2ParseException { String line; int lineNr = 0; while ((line = bufferedReader.readLine()) != null) { lineNr++; line = line.trim(); if (line.isEmpty()) continue; XDI3Statement statementXri; try { statementXri = XDI3Statement.create(line); } catch (Exception ex) { throw new Xdi2ParseException( "Parser problem at line " + lineNr + ": " + ex.getMessage(), ex); } // add the statement to the graph try { graph.setStatement(statementXri); } catch (Exception ex) { throw new Xdi2ParseException( "Graph problem at line " + lineNr + ": " + ex.getMessage(), ex); } } }
@Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); String input = request.getParameter("input"); String type = request.getParameter("type"); String graphId = null; String stats = "-1"; String error = null; XDIReader xdiReader = XDIReaderRegistry.getAuto(); Graph graph = graphFactory.openGraph(); long start = System.currentTimeMillis(); try { xdiReader.read(graph, new StringReader(input)); graphId = UUID.randomUUID().toString(); ImageCache.put(graphId, makeImage(graph, type)); JSONCache.put(graphId, makeJSON(graph)); } catch (Exception ex) { ex.printStackTrace(System.err); error = ex.getMessage(); if (error == null) error = ex.getClass().getName(); } long stop = System.currentTimeMillis(); stats = ""; stats += Long.toString(stop - start) + " ms time. "; stats += Long.toString(graph.getRootContextNode(true).getAllContextNodeCount() + 1) + " context nodes. "; stats += Long.toString(graph.getRootContextNode(true).getAllRelationCount()) + " relations. "; stats += Long.toString(graph.getRootContextNode(true).getAllLiteralCount()) + " literals. "; stats += Long.toString(graph.getRootContextNode(true).getAllStatementCount()) + " statements. "; if (xdiReader != null) stats += "Input format: " + xdiReader.getFormat() + ((xdiReader instanceof AutoReader && ((AutoReader) xdiReader).getLastSuccessfulReader() != null) ? " (" + ((AutoReader) xdiReader).getLastSuccessfulReader().getFormat() + ")" : "") + ". "; graph.close(); // display results request.setAttribute("sampleInputs", Integer.valueOf(sampleInputs.size())); request.setAttribute("input", input); request.setAttribute("type", type); request.setAttribute("graphId", graphId); request.setAttribute("stats", stats); request.setAttribute("error", error); request.getRequestDispatcher("/XDIGrapher.jsp").forward(request, response); }
@Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String from = request.getParameter("from"); String input = request.getParameter("input"); String output = ""; String stats = "-1"; String error = null; XDIReader xdiReader = XDIReaderRegistry.forFormat(from, null); Graph graph = graphFactory.openGraph(); try { xdiReader.read(graph, new StringReader(input)); output = "Success!\n"; /* output += Integer.toString(Constraints.getAllConstraintCount(graph)) + " constraints found.\n"; output += Integer.toString(Versioning.getAllVersionListCount(graph)) + " version lists, "; output += Integer.toString(Versioning.getAllVersionSnapshotCount(graph)) + " version snapshots and "; output += Integer.toString(Versioning.getAllVersionLogCount(graph)) + " version logs found.\n"; output += Integer.toString(LinkContracts.getAllLinkContractRootCount(graph)) + " link contract roots found. "; output += Integer.toString(LinkContracts.getAllLinkContractCount(graph)) + " link contracts found.\n"; output += Integer.toString(Signatures.getAllSignatureCount(graph)) + " signatures found.";*/ } catch (Exception ex) { log.error(ex.getMessage(), ex); error = ex.getMessage(); if (error == null) error = ex.getClass().getName(); } stats = ""; stats += Long.toString(graph.getRootContextNode(true).getAllContextNodeCount() + 1) + " context nodes. "; stats += Long.toString(graph.getRootContextNode(true).getAllRelationCount()) + " relations. "; stats += Long.toString(graph.getRootContextNode(true).getAllLiteralCount()) + " literals. "; stats += Long.toString(graph.getRootContextNode(true).getAllStatementCount()) + " statements. "; if (xdiReader != null) stats += "Input format: " + xdiReader.getFormat() + ((xdiReader instanceof AutoReader && ((AutoReader) xdiReader).getLastSuccessfulReader() != null) ? " (" + ((AutoReader) xdiReader).getLastSuccessfulReader().getFormat() + ")" : "") + ". "; graph.close(); // display results request.setAttribute("sampleInputs", Integer.valueOf(sampleInputs.size())); request.setAttribute("from", from); request.setAttribute("input", input); request.setAttribute("output", output); request.setAttribute("stats", stats); request.setAttribute("error", error); request.getRequestDispatcher("/XDIValidator.jsp").forward(request, response); }
public void testTypes() throws Exception { Graph graph = MemoryGraphFactory.getInstance().openGraph(); ContextNode contextNode = graph.getRootContextNode().setContextNode(XDIArc.create("=markus")); XDIAddress type1 = XDIAddress.create("#employee"); XDIAddress type2 = XDIAddress.create("#person"); XDIAddress type3 = XDIAddress.create("#developer"); Dictionary.setContextNodeType(contextNode, type1); assertEquals(Dictionary.getContextNodeType(contextNode), type1); assertEquals(new IteratorCounter(Dictionary.getContextNodeTypes(contextNode)).count(), 1); assertTrue( new IteratorContains<XDIAddress>(Dictionary.getContextNodeTypes(contextNode), type1) .contains()); assertTrue(Dictionary.isContextNodeType(contextNode, type1)); Dictionary.setContextNodeType(contextNode, type2); assertEquals(new IteratorCounter(Dictionary.getContextNodeTypes(contextNode)).count(), 2); assertTrue( new IteratorContains<XDIAddress>(Dictionary.getContextNodeTypes(contextNode), type1) .contains()); assertTrue( new IteratorContains<XDIAddress>(Dictionary.getContextNodeTypes(contextNode), type2) .contains()); assertTrue(Dictionary.isContextNodeType(contextNode, type1)); assertTrue(Dictionary.isContextNodeType(contextNode, type2)); Dictionary.setContextNodeType(contextNode, type3); assertEquals(new IteratorCounter(Dictionary.getContextNodeTypes(contextNode)).count(), 3); assertTrue( new IteratorContains<XDIAddress>(Dictionary.getContextNodeTypes(contextNode), type1) .contains()); assertTrue( new IteratorContains<XDIAddress>(Dictionary.getContextNodeTypes(contextNode), type2) .contains()); assertTrue( new IteratorContains<XDIAddress>(Dictionary.getContextNodeTypes(contextNode), type3) .contains()); assertTrue(Dictionary.isContextNodeType(contextNode, type1)); assertTrue(Dictionary.isContextNodeType(contextNode, type2)); assertTrue(Dictionary.isContextNodeType(contextNode, type3)); Dictionary.delContextNodeType(contextNode, type2); assertEquals(new IteratorCounter(Dictionary.getContextNodeTypes(contextNode)).count(), 2); assertTrue( new IteratorContains<XDIAddress>(Dictionary.getContextNodeTypes(contextNode), type1) .contains()); assertFalse( new IteratorContains<XDIAddress>(Dictionary.getContextNodeTypes(contextNode), type2) .contains()); assertTrue( new IteratorContains<XDIAddress>(Dictionary.getContextNodeTypes(contextNode), type3) .contains()); assertTrue(Dictionary.isContextNodeType(contextNode, type1)); assertFalse(Dictionary.isContextNodeType(contextNode, type2)); assertTrue(Dictionary.isContextNodeType(contextNode, type3)); Dictionary.replaceContextNodeType(contextNode, type3); assertEquals(Dictionary.getContextNodeType(contextNode), type3); assertEquals(new IteratorCounter(Dictionary.getContextNodeTypes(contextNode)).count(), 1); assertFalse( new IteratorContains<XDIAddress>(Dictionary.getContextNodeTypes(contextNode), type1) .contains()); assertFalse( new IteratorContains<XDIAddress>(Dictionary.getContextNodeTypes(contextNode), type2) .contains()); assertTrue( new IteratorContains<XDIAddress>(Dictionary.getContextNodeTypes(contextNode), type3) .contains()); assertFalse(Dictionary.isContextNodeType(contextNode, type1)); assertFalse(Dictionary.isContextNodeType(contextNode, type2)); assertTrue(Dictionary.isContextNodeType(contextNode, type3)); graph.close(); }