示例#1
0
 /* (non-Javadoc)
  * @see com.semagia.mio.rdf.IMappingReader#read()
  */
 @Override
 public void read(final Source source) throws IOException, MIOException {
   // TODO: check for characterstream / bytestream
   _parser.setRDFHandler(new RDFStatementHandler());
   final InputStream stream = new BufferedInputStream(new URL(source.getIRI()).openStream());
   _handler.start();
   try {
     _parser.parse(stream, source.getBaseIRI());
   } catch (OpenRDFException ex) {
     if (ex.getCause() instanceof MIOException) {
       throw (MIOException) ex.getCause();
     }
     if (ex instanceof RDFParseException) {
       throw new MIOParseException(
           ex,
           ((RDFParseException) ex).getLineNumber(),
           ((RDFParseException) ex).getColumnNumber());
     }
     throw new MIOException(ex);
   } finally {
     if (stream != null) {
       stream.close();
     }
   }
   _buildMapping();
   _handler.end();
   _mappings.clear();
 }
 private void run() throws IOException {
   failed = false;
   for (String s : nanopubs) {
     count = 0;
     try {
       if (sparqlEndpointUrl != null) {
         if (sparqlRepo == null) {
           sparqlRepo = new SPARQLRepository(sparqlEndpointUrl);
           sparqlRepo.initialize();
         }
         processNanopub(new NanopubImpl(sparqlRepo, new URIImpl(s)));
       } else {
         if (verbose) {
           System.out.println("Reading file: " + s);
         }
         MultiNanopubRdfHandler.process(
             new File(s),
             new NanopubHandler() {
               @Override
               public void handleNanopub(Nanopub np) {
                 if (failed) return;
                 processNanopub(np);
               }
             });
         if (count == 0) {
           System.out.println("NO NANOPUB FOUND: " + s);
           break;
         }
       }
     } catch (OpenRDFException ex) {
       System.out.println("RDF ERROR: " + s);
       ex.printStackTrace(System.err);
       break;
     } catch (MalformedNanopubException ex) {
       System.out.println("INVALID NANOPUB: " + s);
       ex.printStackTrace(System.err);
       break;
     }
     if (failed) {
       System.out.println("FAILED TO PUBLISH NANOPUBS");
       break;
     }
   }
   for (String s : usedServers.keySet()) {
     int c = usedServers.get(s);
     System.out.println(c + " nanopub" + (c == 1 ? "" : "s") + " published at " + s);
   }
   if (sparqlRepo != null) {
     try {
       sparqlRepo.shutDown();
     } catch (RepositoryException ex) {
       ex.printStackTrace();
     }
   }
 }
 public void publishNanopub(Nanopub nanopub) throws IOException {
   if (serverInfo == null) {
     if (serverUrls == null || serverUrls.isEmpty()) {
       serverIterator = new ServerIterator();
     } else {
       serverIterator = new ServerIterator(serverUrls);
     }
     serverInfo = serverIterator.next();
   }
   artifactCode = TrustyUriUtils.getArtifactCode(nanopub.getUri().toString());
   if (verbose) {
     System.out.println("---");
     System.out.println("Trying to publish nanopub: " + artifactCode);
   }
   while (serverInfo != null) {
     String serverUrl = serverInfo.getPublicUrl();
     try {
       if (!ServerInfo.load(serverUrl).isPostNanopubsEnabled()) {
         serverInfo = serverIterator.next();
         continue;
       }
     } catch (ServerInfoException ex) {
       serverInfo = serverIterator.next();
       continue;
     }
     if (verbose) {
       System.out.println("Trying server: " + serverUrl);
     }
     try {
       HttpPost post = new HttpPost(serverUrl);
       String nanopubString = NanopubUtils.writeToString(nanopub, RDFFormat.TRIG);
       post.setEntity(new StringEntity(nanopubString, "UTF-8"));
       post.setHeader("Content-Type", RDFFormat.TRIG.getDefaultMIMEType());
       HttpResponse response = HttpClientBuilder.create().build().execute(post);
       int code = response.getStatusLine().getStatusCode();
       if (code >= 200 && code < 300) {
         if (usedServers.containsKey(serverUrl)) {
           usedServers.put(serverUrl, usedServers.get(serverUrl) + 1);
         } else {
           usedServers.put(serverUrl, 1);
         }
         if (verbose) {
           System.out.println("Published: " + artifactCode);
         }
         return;
       } else {
         if (verbose) {
           System.out.println(
               "Response: " + code + " " + response.getStatusLine().getReasonPhrase());
         }
       }
     } catch (IOException ex) {
       if (verbose) {
         System.out.println(ex.getClass().getName() + ": " + ex.getMessage());
       }
     } catch (OpenRDFException ex) {
       if (verbose) {
         System.out.println(ex.getClass().getName() + ": " + ex.getMessage());
       }
     }
     serverInfo = serverIterator.next();
   }
   serverInfo = null;
   throw new IOException("Failed to publish the nanopub");
 }