Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package org.apache.marmotta.platform.core.services.importer.rdf;
 
 
 import java.io.Reader;
 import java.net.URL;
 import java.util.List;
 import java.util.Set;

An io for importing RDF sources in RDF/XML or other RDF formats. Currently uses the Sesame parser for parsing the RDF content.

For each triple found in the imported data, an appropriate KiWi triple is added. For each resource in the imported data, the io creates a ContentItem using rdfs:label or dc:title as title and rdfs:comment or dc:description as content.

Author(s):
Sebastian Schaffert
 
 public class RDFImporterImpl implements Importer {
 
     @Inject
     private Logger log;
 
     @Inject
 
     @Inject
     private TaskManagerService   taskManagerService;
 
     @Inject
     private SesameService sesameService;
 
     private static long taskCounter = 0;
 
     private List<StringacceptTypes;

    
Get a collection of all mime types accepted by this io. Used for automatically selecting the appropriate io in ImportService.

Returns:
a set of strings representing the mime types accepted by this io
 
     @Override
     public Set<StringgetAcceptTypes() {
         return new HashSet<String>();
     }

    
Get a description of this io for presentation to the user.

Returns:
a string describing this io for the user
 
     @Override
     public String getDescription() {
         return "Importer for various RDF formats (RDF/XML, N3, TURTLE); also supports OWL and RDFS files";
     }

    
Get the name of this io. Used for presentation to the user and for internal identification.

Returns:
a string uniquely identifying this io
    @Override
    public String getName() {
        return "RDF";
    }

    
Import data from the input stream provided as argument into the KiWi database.

Parameters:
url the url from which to read the data
user the user to use as author of all imported data
    @Override
    //@RaiseEvent("ontologyChanged")
    public int importData(URL urlString formatResource userURI contextthrows MarmottaImportException {
        try {
            return importData(url.openStream(), formatuser,contexturl.toString());
        } catch (IOException ex) {
            .error("I/O error while importing data from URL {}: {}"urlex.getMessage());
            return 0;
        }
    }


    
Import data from the input stream provided as argument into the KiWi database.

Import function for formats supported by Sesame; imports the data first into a separate memory repository, and then iterates over all statements, adding them to the current knowledge space. This method also checks for resources that have a rdfs:label, dc:title, or skos:prefLabel and uses it as the title for newly created ContentItems.

Parameters:
is the input stream from which to read the data
user the user to use as author of all imported data
    @Override
    //@RaiseEvent("ontologyChanged")
    public int importData(InputStream isString formatResource userURI contextthrows MarmottaImportException {
        String baseUri = .getBaseUri() + "resource/";
        return importData(is,format,user,context,baseUri);
    }

    
Import data from the input stream provided as argument into the KiWi database.

Import function for formats supported by Sesame; imports the data first into a separate memory repository, and then iterates over all statements, adding them to the current knowledge space. This method also checks for resources that have a rdfs:label, dc:title, or skos:prefLabel and uses it as the title for newly created ContentItems.

Parameters:
is the input stream from which to read the data
user the user to use as author of all imported data
    //@RaiseEvent("ontologyChanged")
    private int importData(InputStream isString formatResource userURI contextString baseUrithrows MarmottaImportException {
        // TODO: need to figure out format automatically!
        RDFFormat f = getFormat(format);
        final String taskName = String.format("RDF Importer Task %d (%s)", ++f.getName());
        Task task = .createSubTask(taskName,"Importer");
        task.updateMessage("importing data into Apache Marmotta repository");
        task.updateDetailMessage("format"f.getDefaultMIMEType());
        task.updateDetailMessage("baseUri"baseUri);
        int count = 0;
        try {
            if (is != null) {
                long timer = System.currentTimeMillis();
                RepositoryConnection c_import = .getConnection();
                try {
                    c_import.add(isbaseUrifcontext );
                } catch (RepositoryException ex) {
                    .error("error while importing Sesame data:"ex);
                    c_import.rollback();
                    throw ex;
                } catch (RDFParseException ex) {
                    .error("parse error while importing Sesame data:"ex);
                    c_import.rollback();
                    throw ex;
                } catch (IOException ex) {
                    .error("I/O error while importing Sesame data:"ex);
                    c_import.rollback();
                    throw ex;
                } finally {
                    c_import.commit();
                    c_import.close();
                }
                .debug("imported data into Apache Marmotta repository ({} ms)", System.currentTimeMillis() - timer);
            } else {
                .error("could not load ontology; InputStream was null");
            }
        } catch (Exception ex) {
            .error("error while importing Sesame data:"ex);
            throw new MarmottaImportException(ex);
        } finally {
            .endTask(task);
        }
        return count;
    }

    
Import data from the reader provided as argument into the KiWi database.

Import function for formats supported by Sesame; imports the data first into a separate memory repository, and then iterates over all statements, adding them to the current knowledge space. This method also checks for resources that have a rdfs:label, dc:title, or skos:prefLabel and uses it as the title for newly created ContentItems.

Parameters:
reader the reader from which to read the data
user the user to use as author of all imported data
    @Override
    public int importData(Reader readerString formatResource userURI contextthrows MarmottaImportException {
        // TODO: need to figure out format automatically!
        RDFFormat f = getFormat(format);
        String baseUri = .getBaseUri() + "resource/";
        final String taskName = String.format("RDF Importer Task %d (%s)", ++f.getName());
        Task task = .createSubTask(taskName"Importer");
        task.updateMessage("importing data into Apache Marmotta repository");
        task.updateDetailMessage("format"f.getDefaultMIMEType());
        task.updateDetailMessage("baseURI"baseUri);
        int count = 0;
        try {
            if (reader != null) {
                long timer = System.currentTimeMillis();
                RepositoryConnection c_import = .getConnection();
                try {
                    c_import.add(readerbaseUrifcontext);
                } catch (RepositoryException ex) {
                    .error("error while importing Sesame data:"ex);
                    c_import.rollback();
                    throw ex;
                } catch (RDFParseException ex) {
                    .error("parse error while importing Sesame data:"ex);
                    c_import.rollback();
                    throw ex;
                } catch (IOException ex) {
                    .error("I/O error while importing Sesame data:"ex);
                    c_import.rollback();
                    throw ex;
                } finally {
                    c_import.commit();
                    c_import.close();
                }
                .info("imported data into Apache Marmotta repository ({} ms)", System.currentTimeMillis() - timer);
            } else {
                .error("could not load ontology; InputStream was null");
            }
        } catch (Exception ex) {
            .error("error while importing Sesame data:"ex);
            throw new MarmottaImportException(ex);
        } finally {
            .endTask(task);
        }
        return count;
    }
    public void initialise() {
        .info("registering RDF importer ...");
        RDFParserRegistry parserRegistry = RDFParserRegistry.getInstance();
         = new ArrayList<String>();
        for(RDFFormat format : parserRegistry.getKeys()) {
            .addAll(format.getMIMETypes());
        }
        .info(" - available parsers: {}", Arrays.toString(.toArray()));
    }
    private static RDFFormat getFormat(String format) {
        return RDFParserRegistry.getInstance().getFileFormatForMIMEType(format);
    }
New to GrepCode? Check out our FAQ X