org.apache.jena.riot.RDFLanguages Java Examples

The following examples show how to use org.apache.jena.riot.RDFLanguages. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FormatService.java    From RDFUnit with Apache License 2.0 6 votes vote down vote up
public static String getFormatFromExtension(String filename) {
    String format = "TURTLE";
    try {
        // try to get if from Jena first
        String extension;
        Lang jenaLang = RDFLanguages.filenameToLang(filename);
        if (jenaLang != null) {
            extension = jenaLang.getFileExtensions().get(0);
        } else {
            int index = filename.lastIndexOf('.');
            extension = filename.substring(index + 1, filename.length());
        }
        SerializationFormat f = FormatService.getInputFormat(extension);
        if (f != null) {
            format = f.getName();
        }
    } catch (Exception e) {
        log.debug("No format found, using the default one", e);
        return "TURTLE";
    }
    return format;
}
 
Example #2
Source File: S_Data.java    From rdf-delta with Apache License 2.0 6 votes vote down vote up
@Override
protected void executeAction(DeltaAction action) throws IOException {
    LOG.info("GET "+action.getURL());
    Id dsRef = Id.fromString(action.httpArgs.datasourceName);
    String filenameIRI = determineData(action, dsRef);
    ContentType ct = RDFLanguages.guessContentType(filenameIRI) ;
    String fn = IRILib.IRIToFilename(filenameIRI);
    Path path = Paths.get(fn);
    try ( InputStream in = Files.newInputStream(path) ) {
        action.response.setStatus(HttpSC.OK_200);
        action.response.setContentType(ct.getContentTypeStr());
        IOUtils.copy(in, action.response.getOutputStream());
    } catch (NoSuchFileException | FileNotFoundException ex) {
        throw new DeltaNotFoundException(action.getURL());
    }
}
 
Example #3
Source File: ProcessEventObjectsStream.java    From EventCoreference with Apache License 2.0 6 votes vote down vote up
private static void readTrigFromStream(Dataset ds, Dataset dsnew) {
    InputStream is = null;
    try {
        is = System.in;

        if (is==null){
            throw new IllegalArgumentException(
                    "No stream input!");
        }

        ByteArrayOutputStream b = cloneInputStream(is);
        InputStream is1 = new ByteArrayInputStream(b.toByteArray());
        InputStream is2 = new ByteArrayInputStream(b.toByteArray());

        RDFDataMgr.read(ds, is1, RDFLanguages.TRIG);
        RDFDataMgr.read(dsnew, is2, RDFLanguages.TRIG);

    }
    finally {
        // close the streams using close method

    }

}
 
Example #4
Source File: ContextUtils.java    From sparql-generate with Apache License 2.0 6 votes vote down vote up
public static void loadGraph(Context context, String sourceURI, String baseURI, StreamRDF dest) {
	if(getDataset(context).containsNamedModel(sourceURI)) {
		final Model model = getDataset(context).getNamedModel(sourceURI);
		StreamRDFOps.sendGraphToStream(model.getGraph(), dest);
		return;
	}
	if(!isRootContext(context)) {
		Context parentContext = (Context) context.get(PARENT_CONTEXT);
		loadGraph(parentContext, sourceURI, baseURI, dest);
		return;
	}
	final SPARQLExtStreamManager sm = (SPARQLExtStreamManager) context.get(SysRIOT.sysStreamManager);
	final String acceptHeader = "text/turtle;q=1.0,application/rdf+xml;q=0.9,*/*;q=0.1";
	final LookUpRequest request = new LookUpRequest(sourceURI, acceptHeader);
	try (TypedInputStream tin = sm.open(request);) {
		if(tin == null) {
			LOG.warn("Could not locate graph " + request);
			return;
		}
		Lang lang = RDFLanguages.contentTypeToLang(tin.getMediaType());
		RDFParser.create().source(tin).base(baseURI).context(context).lang(lang).parse(dest);
	} catch (RiotException ex) {
		LOG.warn("Error while loading graph " + sourceURI, ex);
	}
}
 
Example #5
Source File: ExRIOT_1.java    From xcurator with Apache License 2.0 6 votes vote down vote up
public static void main(String...argv)
{
    Model m = ModelFactory.createDefaultModel() ;
    // read into the model.
    m.read("data.ttl") ;
    
    // Alternatively, use the RDFDataMgr, which reads from the web,
    // with content negotiation.  Plain names are assumed to be 
    // local files where file extension indicates the syntax.  
    
    Model m2 = RDFDataMgr.loadModel("data.ttl") ;
    
    // read in more data, the remote server serves up the data
    // with the right MIME type.
    RDFDataMgr.read(m2, "http://host/some-published-data") ;
    
    
    // Read some data but also give a hint for the synatx if it is not
    // discovered by inspectying the file or by HTTP content negotiation.  
    RDFDataMgr.read(m2, "some-more-data.out", RDFLanguages.TURTLE) ;
}
 
Example #6
Source File: BasedModelProvider.java    From Processor with Apache License 2.0 6 votes vote down vote up
@Override
public Model readFrom(Class<Model> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException
{
    if (log.isTraceEnabled()) log.trace("Reading Model with HTTP headers: {} MediaType: {}", httpHeaders, mediaType);
    
    Model model = ModelFactory.createDefaultModel();

    MediaType formatType = new MediaType(mediaType.getType(), mediaType.getSubtype()); // discard charset param
    Lang lang = RDFLanguages.contentTypeToLang(formatType.toString());
    if (lang == null)
    {
        if (log.isErrorEnabled()) log.error("MediaType '{}' not supported by Jena", formatType);
        throw new NoReaderForLangException("MediaType not supported: " + formatType);
    }
    if (log.isDebugEnabled()) log.debug("RDF language used to read Model: {}", lang);
    
    return read(model, entityStream, lang, getUriInfo().getBaseUri().toString());
}
 
Example #7
Source File: BasedModelProvider.java    From Processor with Apache License 2.0 6 votes vote down vote up
@Override
public void writeTo(Model model, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException
{
    if (log.isTraceEnabled()) log.trace("Writing Model with HTTP headers: {} MediaType: {}", httpHeaders, mediaType);

    MediaType formatType = new MediaType(mediaType.getType(), mediaType.getSubtype()); // discard charset param
    Lang lang = RDFLanguages.contentTypeToLang(formatType.toString());
    if (lang == null)
    {
        if (log.isErrorEnabled()) log.error("MediaType '{}' not supported by Jena", formatType);
        throw new NoWriterForLangException("MediaType not supported: " + formatType);
    }
    if (log.isDebugEnabled()) log.debug("RDF language used to read Model: {}", lang);
    
    write(model, entityStream, lang, getUriInfo().getBaseUri().toString());
}
 
Example #8
Source File: RdfBulkUpdateRequestHandler.java    From SolRDF with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings("rawtypes")
protected Map<String, ContentStreamLoader> createDefaultLoaders(final NamedList parameters) {
	final Map<String, ContentStreamLoader> registry = new HashMap<String, ContentStreamLoader>();
	final ContentStreamLoader loader = new RdfDataLoader();
	for (final Lang language : RDFLanguages.getRegisteredLanguages()) {
		registry.put(language.getContentType().toHeaderString(), loader);
	}
	registry.put(WebContent.contentTypeSPARQLUpdate, new Sparql11UpdateRdfDataLoader());

	if (log.isDebugEnabled()) {
		prettyPrint(registry);
	}
	
	return registry;
}
 
Example #9
Source File: ModelImplJena.java    From semweb4j with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Resolves an RDF2Go {@Link Syntax} to a Jena {@link Lang}.
 * 
 * @param syntax
 *            The RDF2Go Syntax to resolve.
 * @return A {@link Lang} for the specified syntax.
 * @throws SyntaxNotSupportedException
 *             When the Syntax could not be resolved to a {@link Lang}.
 */
public static Lang getJenaLang(Syntax syntax) throws SyntaxNotSupportedException {
    for (String mimeType : syntax.getMimeTypes()) {
        Lang lang = RDFLanguages.contentTypeToLang(mimeType);
        if (lang != null) {
            return lang;
        }
    }
    throw new SyntaxNotSupportedException("This version of Jena seems to have no "
            + "support for " + syntax);
}
 
Example #10
Source File: RdfStreamReader.java    From RDFUnit with Apache License 2.0 5 votes vote down vote up
@Override
public void readDataset(Dataset dataset) throws RdfReaderException {
    try {
        RDFDataMgr.read(dataset, inputStream, null, RDFLanguages.nameToLang(format));
    } catch (Exception e) {
        throw new RdfReaderException(e.getMessage(), e);
    }

}
 
Example #11
Source File: RdfStreamReader.java    From RDFUnit with Apache License 2.0 5 votes vote down vote up
@Override
public void read(Model model) throws RdfReaderException {
    try {
        RDFDataMgr.read(model, inputStream, null, RDFLanguages.nameToLang(format));
    } catch (Exception e) {
        throw new RdfReaderException(e.getMessage(), e);
    }

}
 
Example #12
Source File: RdfBulkUpdateRequestHandler.java    From SolRDF with Apache License 2.0 5 votes vote down vote up
@Override
public void load(
		final SolrQueryRequest request, 
		final SolrQueryResponse response,
		final ContentStream stream, 
		final UpdateRequestProcessor processor) throws Exception {
	
	final PipedRDFIterator<Triple> iterator = new PipedRDFIterator<Triple>();
	final StreamRDF inputStream = new PipedTriplesStream(iterator);
	
	executor.submit(new Runnable() {
		@Override
		public void run() {
			try {
				RDFDataMgr.parse(
						inputStream, 
						stream.getStream(), 
						RDFLanguages.contentTypeToLang(stream.getContentType()));
			} catch (final IOException exception) {
				throw new SolrException(ErrorCode.SERVER_ERROR, exception);
			}					
		}
	});
		
	// Graph Store Protocol indicates the target graph URI separately.
	// So the incoming Content-type here is one that maps "Triples Loader" but
	// the indexed tuple could be a Quad.
	final String graphUri = request.getParams().get(Names.GRAPH_URI_ATTRIBUTE_NAME);
	
	final DatasetGraph dataset = new LocalDatasetGraph(request, response, null, null);
	final Graph defaultGraph = graphUri == null 
			? dataset.getDefaultGraph() 
			: dataset.getGraph(NodeFactory.createURI(graphUri));
	while (iterator.hasNext()) {
		defaultGraph.add(iterator.next());
	}		
}
 
Example #13
Source File: RdfBulkUpdateRequestHandler.java    From SolRDF with Apache License 2.0 5 votes vote down vote up
@Override
public void load( 
		final SolrQueryRequest request, 
		final SolrQueryResponse response,
		final ContentStream stream, 
		final UpdateRequestProcessor processor) throws Exception {
	
	final PipedRDFIterator<Quad> iterator = new PipedRDFIterator<Quad>();
	final StreamRDF inputStream = new PipedQuadsStream(iterator);
	
	executor.submit(new Runnable() {
		@Override
		public void run() {
			try {
				RDFDataMgr.parse(
						inputStream, 
						stream.getStream(), 
						RDFLanguages.contentTypeToLang(stream.getContentType()));
			} catch (final IOException exception) {
				throw new SolrException(ErrorCode.SERVER_ERROR, exception);
			}					
		}
	});
	
	final DatasetGraph dataset = new LocalDatasetGraph(request, response);
	while (iterator.hasNext()) {
		dataset.add(iterator.next());
	}									
}
 
Example #14
Source File: FileBasedNIFDataset.java    From gerbil with GNU Affero General Public License v3.0 5 votes vote down vote up
protected static Lang fileExtToLang(String filePath) {
    File file = new File(filePath);
    String ext = file.getName();
    int pos = ext.lastIndexOf('.');
    if (pos < 0) {
        return null;
    }
    return RDFLanguages.fileExtToLang(ext.substring(pos));
}
 
Example #15
Source File: FileBasedNIFDataset.java    From gerbil with GNU Affero General Public License v3.0 5 votes vote down vote up
public FileBasedNIFDataset(String filePath, String name, String language) {
    super(name);
    this.filePath = filePath;
    this.language = RDFLanguages.nameToLang(language);
    if (this.language == null) {
        this.language = fileExtToLang(filePath);
    }
    if (this.language == null) {
        throw new IllegalArgumentException("Couldn't determine language of dataset.");
    }
}
 
Example #16
Source File: SHACLC.java    From shacl with Apache License 2.0 5 votes vote down vote up
public static void install() {
	RDFLanguages.register(SHACLC.lang);
	RDFParserRegistry.registerLangTriples(SHACLC.lang, (language, profile) -> new SHACLCReader());
	RDFFormat format = new RDFFormat(SHACLC.lang);
	RDFWriterRegistry.register(SHACLC.lang, format);
	RDFWriterRegistry.register(format, new WriterGraphRIOTFactory() {
		
		@Override
		public WriterGraphRIOT create(RDFFormat syntaxForm) {
			return new SHACLCWriter();
		}
	});
	
	// new SHACLCTestRunner().run();
}
 
Example #17
Source File: RDFFileTypeDetector.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
public String probeContentType(Path path) throws IOException {
    if(path == null) {
        throw new IOException();
    }
    String fileName = path.getFileName().toString();
    if(fileName.endsWith(SPARQLExt.EXT)) {
        return SPARQLExt.MEDIA_TYPE;
    }
    ContentType ct = RDFLanguages.guessContentType(fileName);
    if(ct==null) {
        throw new IOException();
    }
    return ct.getContentType();
}
 
Example #18
Source File: append.java    From rdf-delta with Apache License 2.0 5 votes vote down vote up
protected RDFPatch toPatch(String fn) {
        // .gz??
        Lang lang = RDFLanguages.filenameToLang(fn);
        if ( lang != null && ( RDFLanguages.isTriples(lang) || RDFLanguages.isQuads(lang) ) ) {
            RDFChangesCollector x = new RDFChangesCollector();
            StreamRDF dest  = new RDF2Patch(x);
            // dest will do the start-finish on the RDFChangesCollector via parsing.
            RDFDataMgr.parse(dest, fn);
            return x.getRDFPatch();
        }

        // Not RDF - assume a text patch.
//        String ext = FileOps.extension(fn);
//        switch(ext) {
//            case RDFPatchConst.EXT:
//                break;
//            case RDFPatchConst.EXT_B:
//                break;
//            default:
//                Log.warn(addpatch.class, "Conventionally, patches have file extension ."+RDFPatchConst.EXT);
//        }

        Path path = Paths.get(fn);
        try(InputStream in = Files.newInputStream(path) ) {
            return RDFPatchOps.read(in);
        } catch (IOException ex ) { throw IOX.exception(ex); }
    }
 
Example #19
Source File: LangTurtleStarTest.java    From RDFstarTools with Apache License 2.0 5 votes vote down vote up
@Test
public void registrationOK() {
	assertTrue( RDFLanguages.isRegistered(LangTurtleStar.TURTLESTAR) );
	assertTrue( RDFLanguages.isTriples(LangTurtleStar.TURTLESTAR) );
	assertTrue( RDFParserRegistry.isRegistered(LangTurtleStar.TURTLESTAR) );
	assertTrue( RDFParserRegistry.isTriples(LangTurtleStar.TURTLESTAR) );
	assertEquals( LangTurtleStar.TURTLESTAR, RDFLanguages.fileExtToLang("ttls") );
	assertEquals( LangTurtleStar.TURTLESTAR, RDFLanguages.filenameToLang("OneNestedTriple1.ttls") );		
}
 
Example #20
Source File: LangTurtleStar.java    From RDFstarTools with Apache License 2.0 4 votes vote down vote up
private static synchronized void init$() {
	RDFLanguages.register(TURTLESTAR);
	RDFParserRegistry.registerLangTriples( TURTLESTAR, new TurtleStarReaderFactory() );
}
 
Example #21
Source File: AnnotationTools.java    From incubator-taverna-language with Apache License 2.0 4 votes vote down vote up
public Dataset annotationDatasetFor(Child<?> workflowBean) {
	Dataset dataset = DatasetFactory.createMem();
	for (Annotation ann : scufl2Tools.annotationsFor(workflowBean)) {
		WorkflowBundle bundle = ann.getParent();
		URI annUri = uritools.uriForBean(ann);
		String bodyUri = bundle.getGlobalBaseURI().resolve(ann.getBody())
				.toASCIIString();

		if (ann.getBody().isAbsolute()) {
			logger.info("Skipping absolute annotation body URI: "
					+ ann.getBody());
			// TODO: Optional loading of external annotation bodies
			continue;
		}
		String path = ann.getBody().getPath();

		ResourceEntry resourceEntry = bundle.getResources()
				.getResourceEntry(path);
		if (resourceEntry == null) {
			logger.warning("Can't find annotation body: " + path);
			continue;
		}
		String contentType = resourceEntry.getMediaType();
		Lang lang = RDFLanguages.contentTypeToLang(contentType);
		if (lang == null) {
			lang = RDFLanguages.filenameToLang(path);
		}
		if (lang == null) {
			logger.warning("Can't find media type of annotation body: "
					+ ann.getBody());
			continue;
		}
		Model model = ModelFactory.createDefaultModel();
		try (InputStream inStream = bundle.getResources()
				.getResourceAsInputStream(path)) {
			RDFDataMgr.read(model, inStream, bodyUri, lang);
		} catch (IOException e) {
			logger.warning("Can't read annotation body: " + path);
			continue;
		}
		dataset.addNamedModel(annUri.toString(), model);
	}

	return dataset;
}
 
Example #22
Source File: RdfBulkUpdateRequestHandler.java    From SolRDF with Apache License 2.0 4 votes vote down vote up
@Override
public void load(
		final SolrQueryRequest request, 
		final SolrQueryResponse response,
		final ContentStream stream, 
		final UpdateRequestProcessor processor) throws Exception {
	
	// Default ContentStream implementation starts reading the stream and
	// if it starts with '<' then it assumes a content type of "application/xml", 
	// if it starts with '{' then it assumes a content type of "application/json" 			
	// This behaviour is wrong is SolRDF and maybe we need a custom ContentStream here
	// At the moment this is just a workaround:
	final String contentType = stream.getContentType() != null 
			&& !"application/xml".equals(stream.getContentType())
			&& !"application/json".equals(stream.getContentType()) 
				? stream.getContentType() 
				: request.getParams().get(UpdateParams.ASSUME_CONTENT_TYPE);
	
	log.debug(MessageCatalog._00094_BULK_LOADER_CT, contentType);			
				
	final Lang lang = RDFLanguages.contentTypeToLang(contentType);
	if (lang == null) {
		final String message = MessageFactory.createMessage(MessageCatalog._00095_INVALID_CT, contentType);
		log.error(message);							
		throw new SolrException(ErrorCode.BAD_REQUEST, message);
	}
	
	final ContentStreamLoader delegate = 
			(lang == Lang.NQ || lang == Lang.NQUADS || lang == Lang.TRIG)
				? quadsLoader
				: triplesLoader;
	
	log.debug(MessageCatalog._00096_SELECTED_BULK_LOADER, contentType, delegate);
	
	delegate.load(
			request, 
			response, 
			new ContentStream() {	
				@Override
				public InputStream getStream() throws IOException {
					return stream.getStream();
				}
				
				@Override
				public String getSourceInfo() {
					return stream.getSourceInfo();
				}
				
				@Override
				public Long getSize() {
					return stream.getSize();
				}
				
				@Override
				public Reader getReader() throws IOException {
					return stream.getReader();
				}
				
				@Override
				public String getName() {
					return stream.getName();
				}
				
				@Override
				public String getContentType() {
					return contentType;
				}
			}, 
		processor);
}
 
Example #23
Source File: WorkflowBundleParser.java    From incubator-taverna-language with Apache License 2.0 4 votes vote down vote up
/**
 * Workaround for TAVERNA-71 to find annotations in WorkflowBundle
 * <p>
 * FIXME: The annotation links should instead be stored in the 
 * {@link Manifest} using taverna-robundle - see TAVERNA-71
 * 
 * @param wb
 * @throws IOException
 */
private void parseAnnotations(final WorkflowBundle wb) throws IOException {
	if (! wb.getAnnotations().isEmpty()) {
		// Assume already parsed
		return;
	}
	URITools uriTools = new URITools();		
	for (ResourceEntry resource : wb.getResources().listResources("annotation").values()) {
		Lang lang = RDFLanguages.contentTypeToLang(resource.getMediaType());
		if (lang == null) {
			// Not a semantic annotation
			continue;
		}
		//System.out.println(resource.getPath());
		//System.out.println(resource.getMediaType());
		Annotation ann = new Annotation();
		// Hackish way to generate a name from the annotation filename
		// as these typically are UUIDs
		String name = resource.getPath().replace("annotation/", "").replaceAll("\\..*", ""); // strip extension
		ann.setName(name);
		ann.setParent(wb);
		
		String path = resource.getPath();
		ann.setBody(URI.create("/" + path));		
		URI base = wb.getGlobalBaseURI().resolve(path);
		Model model = ModelFactory.createDefaultModel();			
		InputStream inputStream = resource.getUcfPackage().getResourceAsInputStream(path);
		if (inputStream == null) {
			// Not found!
			continue;
		}
		RDFDataMgr.read(model, inputStream, 
				base.toASCIIString(), lang);
		ResIterator subjs = model.listSubjects();			
		while (subjs.hasNext()) { 
			Resource r = subjs.next();
			//System.out.println(r);
			WorkflowBean b = uriTools.resolveUri(URI.create(r.getURI()), wb);
			//System.out.println(b);
			if (b != null) {
				ann.setTarget(b);
			}
			break;
		}
	}
}
 
Example #24
Source File: RdfWriterImpl.java    From Server.Java with MIT License 4 votes vote down vote up
public RdfWriterImpl(Map<String, String> prefixes, HashMap<String, IDataSource> datasources, String mimeType) {
    super(prefixes, datasources);
    this.contentType = RDFLanguages.contentTypeToLang(mimeType);
    ARQ.init();
}
 
Example #25
Source File: LangTurtleStarTest.java    From RDFstarTools with Apache License 2.0 3 votes vote down vote up
protected Graph loadGraphFromTurtleStarFile( String filename ) {

		final String fullFilename = getClass().getResource("/TurtleStar/"+filename).getFile();

        final Graph g = ModelFactory.createDefaultModel().getGraph();
        final StreamRDF dest = StreamRDFLib.graph(g);

		assertEquals( LangTurtleStar.TURTLESTAR, RDFLanguages.filenameToLang(fullFilename) );

        RDFParser.create()
                 .source( "file://" + fullFilename )
                 .parse(dest);

		return g;
	}