/** * Guess the language, based on filename, or URL, extenstion. Returns null if there isn't a guess * available */ public static Lang guess(String resourceIRI) { if (resourceIRI == null) return null; String ext = FileUtils.getFilenameExt(resourceIRI).toLowerCase(); if (ext != null && ext.equals("gz")) { resourceIRI = resourceIRI.substring(0, resourceIRI.length() - ".gz".length()); ext = FileUtils.getFilenameExt(resourceIRI).toLowerCase(); } if (isOneOf(ext, extRDFXML)) return RDFXML; if (isOneOf(ext, extNTriples)) return NTRIPLES; if (isOneOf(ext, extNTurtle)) return TURTLE; if (isOneOf(ext, extN3)) return N3; if (isOneOf(ext, extNQuads)) return NQUADS; if (isOneOf(ext, extTrig)) return TRIG; return null; }
private String oneFile(String filename) { divider(); try { return FileUtils.readWholeFileAsUTF8(filename); } catch (IOException ex) { System.err.println("No such file: " + filename); return null; } }
void printFailedModelTest(Query query, Model expected, Model results) { PrintWriter out = FileUtils.asPrintWriterUTF8(System.out); out.println("======================================="); out.println("Failure: " + description()); results.write(out, "TTL"); out.println("---------------------------------------"); expected.write(out, "TTL"); out.println(); }
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; }