Пример #1
0
  protected void addBasicKnowledge(Repository repository) throws RepositoryException {
    String[] bundles = {"net.enilink.vocab.owl", "net.enilink.vocab.rdfs"};

    if (AbstractKommaPlugin.IS_ECLIPSE_RUNNING) {
      RepositoryConnection conn = null;
      try {
        conn = repository.getConnection();
        URI defaultGraph = getDefaultGraph();
        Resource[] contexts =
            defaultGraph == null
                ? new Resource[0]
                : new Resource[] {repository.getValueFactory().createURI(defaultGraph.toString())};
        if (!conn.hasStatement(null, null, null, false, contexts)) {
          for (String name : bundles) {
            URL url = Platform.getBundle(name).getResource("META-INF/org.openrdf.ontologies");
            if (url != null) {
              URL resolvedUrl = FileLocator.resolve(url);

              Properties properties = new Properties();
              InputStream in = resolvedUrl.openStream();
              properties.load(in);
              in.close();

              URI baseUri = URIs.createURI(url.toString()).trimSegments(1);
              for (Map.Entry<Object, Object> entry : properties.entrySet()) {
                String file = entry.getKey().toString();
                if (file.contains("rdfs") && skipRdfsOnImport()) {
                  // skip RDF and RDFS schema
                  continue;
                }

                URI fileUri = URIs.createFileURI(file);
                fileUri = fileUri.resolve(baseUri);

                resolvedUrl = FileLocator.resolve(new URL(fileUri.toString()));
                if (resolvedUrl != null) {
                  in = resolvedUrl.openStream();
                  if (in != null && in.available() > 0) {
                    conn.add(in, "", RDFFormat.RDFXML, contexts);
                  }
                  if (in != null) {
                    in.close();
                  }
                }
              }
            }
          }
        }
      } catch (IOException e) {
        throw new KommaException("Cannot access RDF data", e);
      } catch (RepositoryException e) {
        throw new KommaException("Loading RDF failed", e);
      } catch (RDFParseException e) {
        throw new KommaException("Invalid RDF data", e);
      } finally {
        if (conn != null) {
          try {
            conn.close();
          } catch (RepositoryException e) {
            ModelPlugin.log(e);
          }
        }
      }
    }
  }