@Override public void write( OutputStream out ) { if( null == jenaModel ) { jenaModel = ModelFactory.createDefaultModel(); jenaModel.setNsPrefix( "rdfs", "http://www.w3.org/2000/01/rdf-schema#" ); jenaModel.setNsPrefix( "foaf", "http://xmlns.com/foaf/0.1/" ); jenaModel.setNsPrefix( "dc", "http://purl.org/dc/elements/1.1/" ); } for( FoafPerson person : persons ) { Resource jenaPerson = jenaModel.createResource( FOAF.Person ); populatePersonProperties( person, jenaPerson ); } RDFWriter w = jenaModel.getWriter(); w.setProperty( "showXmlDeclaration", "true" ); w.write( jenaModel, out, "RDF/XML" ); }
public void write(Model model, HttpServletResponse response) throws IOException { RDFWriter writer = model.getWriter("RDF/XML-ABBREV"); writer.setProperty("showXmlDeclaration", "true"); // From Joseki -- workaround for the j.cook.up bug. writer.setProperty("blockRules", "propertyAttr"); writer.write(model, new OutputStreamWriter(response.getOutputStream(), "utf-8"), null); }
public static void store(Model model, OutputStream out, String sFormat) throws IOException { try { RDFWriter w = model.getWriter(sFormat); w.setProperty("allowBadURIs", "true"); w.write(model, out, null); out.flush(); } finally { out.close(); } }
public static void store(Model model, File dest, String sFormat , Map<String,String> props) throws IOException { FileOutputStream out = new FileOutputStream(dest); try { RDFWriter w = model.getWriter(sFormat); for ( String k : props.keySet() ) { w.setProperty(k, props.get(k)); } //w.setProperty("allowBadURIs", "true"); w.write(model, out, null); out.flush(); } finally { out.close(); } }
public static void store(Model model, File dest, String sFormat) throws IOException { FileOutputStream out = new FileOutputStream(dest); try { RDFWriter w = model.getWriter(sFormat); w.setProperty("allowBadURIs", "true"); w.write(model, out, null); out.flush(); } finally { out.close(); } }
public boolean saveInferredModel(String filename, String modelname, boolean deductionsOnly) throws FileNotFoundException { try { prepareInfModel(); } catch (ConfigurationException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } if (infModel != null) { OntModel m; if (deductionsOnly && infModel instanceof InfModel) { m = ModelFactory.createOntologyModel(configurationMgr.getOntModelSpec(null), ((InfModel) infModel).getDeductionsModel()); } else { m = ModelFactory.createOntologyModel(configurationMgr.getOntModelSpec(null), infModel); } String format = "RDF/XML-ABBREV"; FileOutputStream fps = new FileOutputStream(filename); RDFWriter rdfw = m.getWriter(format); rdfw.write(m, fps, modelname); try { fps.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return false; }
public boolean saveInferredModel(String filename, String modelname, boolean deductionsOnly) throws FileNotFoundException { try { prepareInfModel(); } catch (ConfigurationException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } if (infModel != null) { OntModel m; if (deductionsOnly && infModel instanceof InfModel) { m = ModelFactory.createOntologyModel(configurationMgr.getOntModelSpec(null), ((InfModel) infModel).getDeductionsModel()); } else { m = ModelFactory.createOntologyModel(configurationMgr.getOntModelSpec(null), infModel); } String format = ConfigurationManager.RDF_XML_ABBREV_FORMAT; FileOutputStream fps = new FileOutputStream(filename); RDFWriter rdfw = m.getWriter(format); rdfw.write(m, fps, modelname); try { fps.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return false; }
@Override public boolean saveMetrics(String format) throws IOException, ConfigurationException, URISyntaxException { RDFWriter w = getTheJenaModel().getWriter(format); w.setProperty("xmlbase", baseUri); FileOutputStream out = new FileOutputStream(filename); w.write(getTheJenaModel().getBaseModel(), out, baseUri); out.close(); SadlUtils su = new SadlUtils(); configMgr.addMapping(su.fileNameToFileUrl(filename), baseUri, null, false, "SRL_Metrics"); return true; }
private void showQueryResults(String queryString, HttpServletRequest request, HttpServletResponse response) throws IOException { Query query = QueryFactory.create(queryString); if(query.isSelectType() || query.isDescribeType()) { Config config = new Config(request); ResultsFormat fmt = ResultsFormat.lookup(request.getParameter("format")); Dataset tdbstore = TDBFactory.createDataset(config.getTripleStoreDir()); QueryExecution qexec = QueryExecutionFactory.create(query, tdbstore); qexec.getContext().set(TDB.symUnionDefaultGraph, true); if(query.isSelectType()) { ResultSet results = qexec.execSelect(); if(fmt == null) { out.print(queryString+"\n"); ResultSetFormatter.out(out, results, query); } else ResultSetFormatter.output(out, results, fmt); } else { Model model = qexec.execDescribe(); RDFWriter rdfWriter = model.getWriter("RDF/XML-ABBREV"); rdfWriter.setProperty("showXmlDeclaration", "true"); rdfWriter.setProperty("tab", "6"); rdfWriter.write(model, out, null); } } else { out.print("Only select or describe queries allowed"); } }
private void setFromWriterSystemProperties( Map<String, String> ns, Set<String> prefixesUsed ) { Iterator<String> it = namespacesNeeded.iterator(); while (it.hasNext()) { String uri = it.next(); String val = JenaRuntime.getSystemProperty( RDFWriter.NSPREFIXPROPBASE + uri ); if (val != null && checkLegalPrefix( val ) && !prefixesUsed.contains( val )) { ns.put(uri, val); prefixesUsed.add(val); } } }
/** * JE: starting an attempt to do the relocation through String reading and writing: * The ideal case is that in the generated files, the namespace appears exactly twice: * in xmlns and in xml:base. * But it depends on the actual seed. * Two versions would be possible: * OntModel.write( OutputStream out ) / OntModel.write( Writer writer ) * new Model().read( InputStream in, String base ) / new Model().read(Reader reader, String base) * So far this is less efficient (and less elegant) than the modifications above */ public OntModel changeNamespace2( String base2 ) { OntModel model = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM ); try { //Charset defaultCharset = Charset.forName("UTF8"); ByteArrayOutputStream out = new ByteArrayOutputStream(); //StringWriter sout = new StringWriter(); RDFWriter writer = modifiedModel.getWriter("RDF/XML-ABBREV"); writer.setProperty( "showXmlDeclaration","true" ); writer.setProperty( "relativeURIs", "" );//faster 26.6 -> 26.03 (?) // -------- critical section // I am curious: isn't it possible to set new ns here? Apparently NO writer.setProperty( "xmlbase", modifiedOntologyNS ); modifiedModel.setNsPrefix( "", modifiedOntologyNS ); renameOntology( modifiedModel, modifiedOntologyNS, modifiedOntologyNS ); // -------- writer.write( modifiedModel.getBaseModel(), out, ""); //writer.write( modifiedModel.getBaseModel(), sout, ""); String serial = out.toString( "UTF8" ); //StringBuffer serial = sout.getBuffer(); // Can be printed right away String sout = serial.replace( modifiedOntologyNS, base2 ); // Does not work well with StringBuffer // JE: This part below is unnecessary if we just want to print the result and close the story // But suppressing it makes problems even in random... // Using the read allocate as much as before //InputStream in = null; from the string ByteArrayInputStream in = new ByteArrayInputStream( sout.getBytes("UTF8") ); model.read( in, null ); } catch ( Exception ex ) { //UnsupportedEncodingException; ex.printStackTrace(); } return model; }
/** * Imports a private dataset in Open Data format (DCAT). * * @param authorization authorization string. * @param dcatInputStream * @return * @throws java.text.ParseException * @throws com.fasterxml.jackson.core.JsonProcessingException */ @POST @Path("/importOpenData") @ApiOperation( value = "Import Open Data." ) @ApiResponses(value = { @ApiResponse(code = 401, message = "Unauthorized"), @ApiResponse(code = 500, message = "Internal server error") }) @Consumes(MediaType.APPLICATION_OCTET_STREAM) @Produces(MediaType.APPLICATION_JSON) public Response importOpenData(@HeaderParam("Authorization") String authorization, InputStream dcatInputStream) throws ParseException, JsonProcessingException { log.info(MGS_IMPORT_OPENDATA); model = ModelFactory.createDefaultModel(); // new allocation of the model, not sure if really needed model.read(dcatInputStream, null); StringWriter res = new StringWriter(); RDFWriter writer = model.getWriter("RDF/XML"); writer.setProperty("showXmlDeclaration", true); writer.write(model, res, null); log.debug("Serialized RDF result:\n" + res); ResIterator iterator = model.listSubjects(); List<Resource> resourcesList = iterator.toList(); String datasetId = ""; for (Resource resource : resourcesList) { if (resource.getURI() != null && resource.getURI().startsWith(URI_DATASET)) { Dataset dataset = new Dataset(); dataset.setId(resource.getProperty(dctIdentifier).getString()); dataset.setName(resource.getProperty(dctTitle).getString()); dataset.setDescription(resource.getProperty(dctDescription).getString()); dataset.setType("Default type"); Date dt = format.parse(resource.getProperty(dctIssued).getString()); dataset.setCreationDate(dt.getTime() / 1000); dt = format.parse(resource.getProperty(dctModified).getString()); dataset.setLastModifiedDate(dt.getTime() / 1000); dataset.setPermissions(new ArrayList<Permission>()); // get owner String str = resource.getProperty(dctPublisher).getObject().toString(); String id[] = str.split("#"); dataset.setOwner(id[id.length - 1]); // get status dataset.setStatus("public"); // get readonly dataset.setReadOnly(false); // get structure dataset.setStructure(new DatasetStructure()); log.debug("here's the dataset: " + dataset.toString()); datasetId = INSTANCE.getDatasetService().createDataset(dataset); break; // exit while the dataset is completed } } return Response.ok(datasetId, MediaType.APPLICATION_JSON).build(); }