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; }
public static Model getLocalModel(String path) { InputStream in = ModelUtil.class.getResourceAsStream(path); Model model = ModelUtil.createDefaultModel(); model.read(in, null, FileUtils.guessLang(path, FileUtils.langXML)); return model; }