Exemplo n.º 1
0
  private Dataset setUpDataset(Query query, TestItem testItem) {
    try {
      // testItem.requiresTextIndex()

      if (doesQueryHaveDataset(query) && doesTestItemHaveDataset(testItem)) {
        // Only warn if there are results to test
        // Syntax tests may have FROM etc and a manifest data file.
        if (testItem.getResultFile() != null)
          Log.warn(this, testItem.getName() + " : query data source and also in test file");
      }

      // In test file?
      if (doesTestItemHaveDataset(testItem))
        // Not specified in the query - get from test item and load
        return createDataset(testItem.getDefaultGraphURIs(), testItem.getNamedGraphURIs());

      // Check 3 - were there any at all?

      if (!doesQueryHaveDataset(query)) fail("No dataset");

      // Left to query
      return null;

    } catch (JenaException jEx) {
      fail("JenaException creating data source: " + jEx.getMessage());
      return null;
    }
  }
  // All throwable handling.
  private static void perform(Query query, String string, Action action) {
    Reader in = new StringReader(string);
    ARQParser parser = new ARQParser(in);

    try {
      query.setStrict(true);
      parser.setQuery(query);
      action.exec(parser);
    } catch (com.hp.hpl.jena.sparql.lang.arq.ParseException ex) {
      throw new QueryParseException(
          ex.getMessage(), ex.currentToken.beginLine, ex.currentToken.beginColumn);
    } catch (com.hp.hpl.jena.sparql.lang.arq.TokenMgrError tErr) {
      // Last valid token : not the same as token error message - but this should not happen
      int col = parser.token.endColumn;
      int line = parser.token.endLine;
      throw new QueryParseException(tErr.getMessage(), line, col);
    } catch (QueryException ex) {
      throw ex;
    } catch (JenaException ex) {
      throw new QueryException(ex.getMessage(), ex);
    } catch (Error err) {
      // The token stream can throw errors.
      throw new QueryParseException(err.getMessage(), err, -1, -1);
    } catch (Throwable th) {
      Log.warn(ParserSPARQL11.class, "Unexpected throwable: ", th);
      throw new QueryException(th.getMessage(), th);
    }
  }
 public void testConnectionDescriptionFailsOnMissingType() {
   ConnectionDescription c = new ConnectionDescription("eh:/subject", "URL", null, null, null);
   try {
     c.getConnection();
     fail("should trap null type");
   } catch (JenaException e) {
     assertTrue(
         e.getMessage().endsWith("cannot be opened because no dbURL or dbType was specified"));
   }
 }
  public Model getMappingModel() {
    if (mappingModel == null && jdbcURL == null && mappingFile == null && input_mapping == null) {
      throw new D2RQException("no mapping file or JDBC URL specified");
    }
    if (mappingModel == null && mappingFile == null) {
      if (input_mapping != null) {
        // gui staff
        mappingModel = ModelFactory.createDefaultModel();
        mappingModel.read(new StringReader(input_mapping), getResourceBaseURI(), "TURTLE");
      } else {

        if (d2rqMapping == null && r2rmlMapping == null) {
          generateMapping();
        }
        StringWriter out = new StringWriter();
        getWriter().write(out);
        mappingModel = ModelFactory.createDefaultModel();
        mappingModel.read(new StringReader(out.toString()), getResourceBaseURI(), "TURTLE");
      }
    }
    if (mappingModel == null) {
      log.info("Reading mapping file from " + mappingFile);
      // Guess the language/type of mapping file based on file extension. If it is not among the
      // known types then assume that the file has TURTLE syntax and force to use TURTLE parser
      String lang = FileUtils.guessLang(mappingFile, "unknown");
      try {
        if (lang.equals("unknown")) {
          mappingModel = FileManager.get().loadModel(mappingFile, getResourceBaseURI(), "TURTLE");
        } else {
          // if the type is known then let Jena auto-detect it and load the appropriate parser
          mappingModel = FileManager.get().loadModel(mappingFile, getResourceBaseURI(), null);
        }
      } catch (TurtleParseException ex) {
        // We have wired RIOT into Jena in the static initializer above,
        // so this should never happen (it's for the old Jena Turtle/N3 parser)
        throw new D2RQException("Error parsing " + mappingFile + ": " + ex.getMessage(), ex, 77);
      } catch (JenaException ex) {
        if (ex.getCause() != null && ex.getCause() instanceof RiotException) {
          throw new D2RQException(
              "Error parsing " + mappingFile + ": " + ex.getCause().getMessage(), ex, 77);
        }
        throw ex;
      } catch (AtlasException ex) {
        // Detect the specific case of non-UTF-8 encoded input files
        // and do a custom error message
        if (FileUtils.langTurtle.equals(lang)
            && ex.getCause() != null
            && (ex.getCause() instanceof MalformedInputException)) {
          throw new D2RQException(
              "Error parsing "
                  + mappingFile
                  + ": Turtle files must be in UTF-8 encoding; "
                  + "bad encoding found at byte "
                  + ((MalformedInputException) ex.getCause()).getInputLength(),
              ex,
              77);
        }
        // Generic error message for other parse errors
        throw new D2RQException("Error parsing " + mappingFile + ": " + ex.getMessage(), ex, 77);
      }
    }
    return mappingModel;
  }