전체 페이지뷰

2017년 2월 26일 일요일

sparql ResultSetFormatter.outputAsJSON encoding

http://www.programcreek.com/java-api-examples/index.php?class=com.hp.hpl.jena.query.ResultSetFormatter&method=outputAsJSON

Example 1
Project: iswc2012metadata-master View SourceVote up8 votes
private Object gerRet(Config.RDFSYNTAX rdfSyntax){
 Object ret = null;
 if (query.isDescribeType()){
  ret = qexec.execDescribe();
 }else if (query.isConstructType()){
  ret = qexec.execConstruct() ;
 }else if (query.isSelectType()){
  ResultSet results = qexec.execSelect() ;
 
  //System.out.println(ResultSetFormatter.asText(results));    

  ByteArrayOutputStream sw = new ByteArrayOutputStream();
  if (Config.RDFSYNTAX.SPARQL_XML.equals(rdfSyntax)){
   ResultSetFormatter.outputAsXML(sw, results);
  }else if (Config.RDFSYNTAX.SPARQL_JSON.equals(rdfSyntax)){
   ResultSetFormatter.outputAsJSON(sw, results);
  }else if (Config.RDFSYNTAX.CSV.equals(rdfSyntax)){
   ResultSetFormatter.outputAsCSV(sw, results);
  }else if (Config.RDFSYNTAX.TSV.equals(rdfSyntax)){
   ResultSetFormatter.outputAsTSV(sw, results);
  }else if (Config.RDFSYNTAX.SPARQL_TXT.equals(rdfSyntax)){
   return ResultSetFormatter.asText(results);
  }else{
   ResultSetFormatter.out(sw,results, query);    
  }

  try {

   ret = sw.toString("UTF-8");
  } catch (UnsupportedEncodingException e) {
   e.printStackTrace();
  }
  
 }else if (query.isAskType()){
  ret = qexec.execAsk() ;
 }
 
 return ret;
}
 
Example 2
Project: humfrey-java-master View SourceVote up7 votes
@Override
public void serializeResultSet(ResultSet resultset, Query query,
  HttpServletRequest req, HttpServletResponse resp)
  throws IOException {

 resp.setContentType(getContentType());
 
 ResultSetFormatter.outputAsJSON(resp.getOutputStream(), resultset);
}
 
Example 3
Project: humfrey-java-master View SourceVote up6 votes
@Override
public void serializeBoolean(boolean value, Query query,
  HttpServletRequest req, HttpServletResponse resp)
  throws IOException {
 resp.setContentType(getContentType());
 ResultSetFormatter.outputAsJSON(resp.getOutputStream(), value);
}
 
Example 4
Project: Edge-Node-master View SourceVote up6 votes
private static String buildRDF_JSON(Model RDFmodel) {

  // From http://www.w3.org/wiki/TriplesInJSON
  String queryString = "SELECT ?s ?p ?o  " + "WHERE { ?s ?p ?o }";

  Query q = QueryFactory.create(queryString);

  // Create a SPARQL-DL query execution for the given query and
  // ontology model
  QueryExecution qe = SparqlDLExecutionFactory.create(q, RDFmodel);

  // Print the query for better understanding
  System.out.println(q.toString());

  // We want to execute a SELECT query, do it, and return the result set
  ResultSet resultSet = qe.execSelect();

  ByteArrayOutputStream b = new ByteArrayOutputStream();
  ResultSetFormatter.outputAsJSON(b, resultSet);
  String json = b.toString();

  System.out.println(json);

  return json;
 }
 
Example 5
Project: Edge-Node-master View SourceVote up6 votes
private static String buildRDF_JSON(Model RDFmodel) {

  // From http://www.w3.org/wiki/TriplesInJSON
  String queryString = "SELECT ?s ?p ?o  " + "WHERE { ?s ?p ?o }";

  Query q = QueryFactory.create(queryString);

  // Create a SPARQL-DL query execution for the given query and
  // ontology model
  QueryExecution qe = SparqlDLExecutionFactory.create(q, RDFmodel);

  // Print the query for better understanding
  System.out.println(q.toString());

  // We want to execute a SELECT query, do it, and return the result set
  ResultSet resultSet = qe.execSelect();

  ByteArrayOutputStream b = new ByteArrayOutputStream();
  ResultSetFormatter.outputAsJSON(b, resultSet);
  String json = b.toString();

  System.out.println(json);

  return json;
 }
 
Example 6
Project: graphity-core-master View SourceVote up6 votes
@Override
   public void writeTo(ResultSet results, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException
   {
if (mediaType.isCompatible(org.graphity.core.MediaType.APPLICATION_SPARQL_RESULTS_JSON_TYPE))
    ResultSetFormatter.outputAsJSON(entityStream, results);
else
    ResultSetFormatter.outputAsXML(entityStream, results);
   }
 
Example 7
Project: SemanticWebQueryEngine-master View SourceVote up6 votes
@Test public void test_RS_5()
{
    ResultSetRewindable rs1 = new ResultSetMem() ;
    ByteArrayOutputStream arr = new ByteArrayOutputStream() ;
    ResultSetFormatter.outputAsJSON(arr, rs1) ;
    rs1.reset() ;
    ByteArrayInputStream ins = new ByteArrayInputStream(arr.toByteArray()) ;
    ResultSet rs2 = ResultSetFactory.fromJSON(ins) ;
    assertTrue(ResultSetCompare.equalsByTerm(rs1, rs2)) ;
}
 
Example 8
Project: SemanticWebQueryEngine-master View SourceVote up6 votes
@Test public void test_RS_6()
{
    ResultSetRewindable rs1 = make2Rewindable("x", Node.createURI("tag:local")) ;
    ByteArrayOutputStream arr = new ByteArrayOutputStream() ;
    ResultSetFormatter.outputAsJSON(arr, rs1) ;
    rs1.reset() ;
    ByteArrayInputStream ins = new ByteArrayInputStream(arr.toByteArray()) ;
    ResultSet rs2 = ResultSetFactory.fromJSON(ins) ;    // Test using the DAWG examples
    assertTrue(ResultSetCompare.equalsByTerm(rs1, rs2)) ;
}
 
Example 9
Project: bygle-ldp-master View SourceVote up5 votes
private ResponseEntity<?> formatSelectOutput(ResultSet resultSet, int outputFormat) throws Exception {
 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
 HttpHeaders headers = new HttpHeaders();
 switch (outputFormat) {
 case BygleSystemUtils.OUTPUTFORMAT_BIO:
  ResultSetFormatter.outputAsBIO(byteArrayOutputStream, resultSet);
  headers.add("Content-Type", "text/bio" + "; charset=" + BygleSystemUtils.getStringProperty("default.encoding"));
  headers.add("Content-Disposition", "attachment; filename=query.bio");
  headers.add("Content-Length", Integer.toString(byteArrayOutputStream.toByteArray().length));
  return new ResponseEntity<byte[]>(byteArrayOutputStream.toByteArray(), headers, HttpStatus.OK);
 case BygleSystemUtils.OUTPUTFORMAT_CSV:
  ResultSetFormatter.outputAsCSV(byteArrayOutputStream, resultSet);
  headers.add("Content-Type", "text/csv" + "; charset=" + BygleSystemUtils.getStringProperty("default.encoding"));
  headers.add("Content-Disposition", "attachment; filename=query.csv");
  return new ResponseEntity<byte[]>(byteArrayOutputStream.toByteArray(), headers, HttpStatus.OK);
 case BygleSystemUtils.OUTPUTFORMAT_JSON:
  ResultSetFormatter.outputAsJSON(byteArrayOutputStream, resultSet);
  headers.add("Content-Type", "application/json" + "; charset=" + BygleSystemUtils.getStringProperty("default.encoding"));
  headers.add("Content-Disposition", "attachment; filename=query.json");
  return new ResponseEntity<byte[]>(byteArrayOutputStream.toByteArray(), headers, HttpStatus.OK);
 case BygleSystemUtils.OUTPUTFORMAT_RDF:
  ResultSetFormatter.outputAsRDF(byteArrayOutputStream, "RDF/XML", resultSet);
  headers.add("Content-Type", "application/rdf+xml" + "; charset=" + BygleSystemUtils.getStringProperty("default.encoding"));
  headers.add("Content-Disposition", "attachment; filename=query.rdf");
  return new ResponseEntity<byte[]>(byteArrayOutputStream.toByteArray(), headers, HttpStatus.OK);
 case BygleSystemUtils.OUTPUTFORMAT_RDF_ABBR:
  ResultSetFormatter.outputAsRDF(byteArrayOutputStream, "RDF/XML-ABBREV", resultSet);
  headers.add("Content-Type", "application/rdf+xml" + "; charset=" + BygleSystemUtils.getStringProperty("default.encoding"));
  headers.add("Content-Disposition", "attachment; filename=query.rdf");
  return new ResponseEntity<byte[]>(byteArrayOutputStream.toByteArray(), headers, HttpStatus.OK);
 case BygleSystemUtils.OUTPUTFORMAT_TSV:
  ResultSetFormatter.outputAsTSV(byteArrayOutputStream, resultSet);
  headers.add("Content-Type", "text/tab-separated-values" + "; charset=" + BygleSystemUtils.getStringProperty("default.encoding"));
  headers.add("Content-Disposition", "attachment; filename=query.tsv");
  return new ResponseEntity<byte[]>(byteArrayOutputStream.toByteArray(), headers, HttpStatus.OK);
 case BygleSystemUtils.OUTPUTFORMAT_XML:
  headers.add("Content-Type", "application/xml" + "; charset=" + BygleSystemUtils.getStringProperty("default.encoding"));
  headers.add("Content-Disposition", "attachment; filename=query.xml");
  return new ResponseEntity<byte[]>(ResultSetFormatter.asXMLString(resultSet).getBytes(), headers, HttpStatus.OK);
 case BygleSystemUtils.OUTPUTFORMAT_N_TRIPLE:
  ResultSetFormatter.outputAsRDF(byteArrayOutputStream, "N-TRIPLE", resultSet);
  headers.add("Content-Type", "application/n-triples" + "; charset=" + BygleSystemUtils.getStringProperty("default.encoding"));
  headers.add("Content-Disposition", "attachment; filename=query.n3");
  return new ResponseEntity<byte[]>(byteArrayOutputStream.toByteArray(), headers, HttpStatus.OK);
 case BygleSystemUtils.OUTPUTFORMAT_TURTLE:
  ResultSetFormatter.outputAsRDF(byteArrayOutputStream, "TURTLE", resultSet);
  headers.add("Content-Type", "text/turtle" + "; charset=" + BygleSystemUtils.getStringProperty("default.encoding"));
  headers.add("Content-Disposition", "attachment; filename=query.ttl");
  return new ResponseEntity<byte[]>(byteArrayOutputStream.toByteArray(), headers, HttpStatus.OK);
 case BygleSystemUtils.OUTPUTFORMAT_HTML:
  headers.add("Content-Type", "text/html" + "; charset=" + BygleSystemUtils.getStringProperty("default.encoding"));
  return new ResponseEntity<byte[]>(TrasformXslt.xslt(ResultSetFormatter.asXMLString(resultSet), BygleSystemUtils.getXSLHTMLTController()).getBytes(), headers, HttpStatus.OK);
 default:
  headers.add("Content-Type", "text/html" + "; charset=" + BygleSystemUtils.getStringProperty("default.encoding"));
  return new ResponseEntity<byte[]>(TrasformXslt.xslt(ResultSetFormatter.asXMLString(resultSet), BygleSystemUtils.getXSLHTMLTController()).getBytes(), headers, HttpStatus.OK);
 }
}
 
Example 10
Project: bygle-ldp-master View SourceVote up5 votes
private ResponseEntity<?> formatAskOutput(boolean ask, int outputFormat) throws Exception {
 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
 HttpHeaders headers = new HttpHeaders();
 switch (outputFormat) {
 case BygleSystemUtils.OUTPUTFORMAT_CSV:
  ResultSetFormatter.outputAsCSV(byteArrayOutputStream, ask);
  headers.add("Content-Type", "text/csv" + "; charset=" + BygleSystemUtils.getStringProperty("default.encoding"));
  headers.add("Content-Disposition", "attachment; filename=query.csv");
  return new ResponseEntity<byte[]>(byteArrayOutputStream.toByteArray(), headers, HttpStatus.OK);
 case BygleSystemUtils.OUTPUTFORMAT_JSON:
  ResultSetFormatter.outputAsJSON(byteArrayOutputStream, ask);
  headers.add("Content-Type", "application/json" + "; charset=" + BygleSystemUtils.getStringProperty("default.encoding"));
  headers.add("Content-Disposition", "attachment; filename=query.json");
  return new ResponseEntity<byte[]>(byteArrayOutputStream.toByteArray(), headers, HttpStatus.OK);
 case BygleSystemUtils.OUTPUTFORMAT_RDF:
  ResultSetFormatter.outputAsRDF(byteArrayOutputStream, "RDF/XML", ask);
  headers.add("Content-Type", "application/rdf+xml" + "; charset=" + BygleSystemUtils.getStringProperty("default.encoding"));
  headers.add("Content-Disposition", "attachment; filename=query.rdf");
  return new ResponseEntity<byte[]>(byteArrayOutputStream.toByteArray(), headers, HttpStatus.OK);
 case BygleSystemUtils.OUTPUTFORMAT_RDF_ABBR:
  ResultSetFormatter.outputAsRDF(byteArrayOutputStream, "RDF/XML-ABBREV", ask);
  headers.add("Content-Type", "application/rdf+xml" + "; charset=" + BygleSystemUtils.getStringProperty("default.encoding"));
  headers.add("Content-Disposition", "attachment; filename=query.rdf");
  return new ResponseEntity<byte[]>(byteArrayOutputStream.toByteArray(), headers, HttpStatus.OK);
 case BygleSystemUtils.OUTPUTFORMAT_TSV:
  ResultSetFormatter.outputAsTSV(byteArrayOutputStream, ask);
  headers.add("Content-Type", "text/tab-separated-values" + "; charset=" + BygleSystemUtils.getStringProperty("default.encoding"));
  headers.add("Content-Disposition", "attachment; filename=query.tsv");
  return new ResponseEntity<byte[]>(byteArrayOutputStream.toByteArray(), headers, HttpStatus.OK);
 case BygleSystemUtils.OUTPUTFORMAT_XML:
  headers.add("Content-Type", "application/xml" + "; charset=" + BygleSystemUtils.getStringProperty("default.encoding"));
  headers.add("Content-Disposition", "attachment; filename=query.xml");
  return new ResponseEntity<byte[]>(ResultSetFormatter.asXMLString(ask).getBytes(), headers, HttpStatus.OK);
 case BygleSystemUtils.OUTPUTFORMAT_N_TRIPLE:
  ResultSetFormatter.outputAsRDF(byteArrayOutputStream, "N-TRIPLE", ask);
  headers.add("Content-Type", "application/n-triples" + "; charset=" + BygleSystemUtils.getStringProperty("default.encoding"));
  headers.add("Content-Disposition", "attachment; filename=query.n3");
  return new ResponseEntity<byte[]>(byteArrayOutputStream.toByteArray(), headers, HttpStatus.OK);
 case BygleSystemUtils.OUTPUTFORMAT_TURTLE:
  ResultSetFormatter.outputAsRDF(byteArrayOutputStream, "TURTLE", ask);
  headers.add("Content-Type", "text/turtle" + "; charset=" + BygleSystemUtils.getStringProperty("default.encoding"));
  headers.add("Content-Disposition", "attachment; filename=query.ttl");
  return new ResponseEntity<byte[]>(byteArrayOutputStream.toByteArray(), headers, HttpStatus.OK);
 case BygleSystemUtils.OUTPUTFORMAT_HTML:
  headers.add("Content-Type", "text/html" + "; charset=" + BygleSystemUtils.getStringProperty("default.encoding"));
  return new ResponseEntity<byte[]>(TrasformXslt.xslt(ResultSetFormatter.asXMLString(ask), BygleSystemUtils.getXSLHTMLTController()).getBytes(), headers, HttpStatus.OK);
 default:
  headers.add("Content-Type", "text/html" + "; charset=" + BygleSystemUtils.getStringProperty("default.encoding"));
  return new ResponseEntity<byte[]>(TrasformXslt.xslt(ResultSetFormatter.asXMLString(ask), BygleSystemUtils.getXSLHTMLTController()).getBytes(), headers, HttpStatus.OK);
 }
}

댓글 없음:

댓글 쓰기