public String getItem(int i, boolean withIndirect) { if ( i < 0 || i >= items.size() ) return null ; String item = items.get(i) ; if ( withIndirect && item.startsWith(indirectionMarker) ) { item = item.substring(1) ; try { item = FileUtils.readWholeFileAsUTF8(item) ; } catch (Exception ex) { throw new IllegalArgumentException("Failed to read '"+item+"': "+ex.getMessage()) ; } } return item ; }
/** Guess the language/type of model data * * <ul> * <li> If the URI ends ".rdf", it is assumed to be RDF/XML</li> * <li> If the URI ends ".nt", it is assumed to be N-Triples</li> * <li> If the URI ends ".ttl", it is assumed to be Turtle</li> * <li> If the URI ends ".owl", it is assumed to be RDF/XML</li> * </ul> * @param urlStr URL to base the guess on * @return String Guessed syntax - default is RDF/XML */ public static String guessLang(String urlStr) { return guessLang(urlStr, langXML) ; }
/** Check whether a name is an absolute URI (has a scheme name) * * @param name * @return boolean True if there is a scheme name */ public static boolean isURI(String name) { return (getScheme(name) != null) ; }
/** Create a buffered reader that uses UTF-8 encoding */ static public BufferedReader asBufferedUTF8(InputStream in) { BufferedReader r = new BufferedReader(asUTF8(in)) ; return r ; }
@Override final public void read(Model model, InputStream in, String base) { readImpl(model, FileUtils.asBufferedUTF8(in), base) ; }
public PlainFormat(OutputStream outStream, SerializationContext context) { this.out = FileUtils.asPrintWriterUTF8(outStream) ; this.context = context ; }
/** Guess the language/type of model data. * * <ul> * <li> If the URI ends ".rdf", it is assumed to be RDF/XML</li> * <li> If the URI ends ".nt", it is assumed to be N-Triples</li> * <li> If the URI ends ".ttl", it is assumed to be Turtle</li> * <li> If the URI ends ".owl", it is assumed to be RDF/XML</li> * </ul> * @param name URL to base the guess on * @param otherwise Default guess * @return String Guessed syntax - or the default supplied */ public static String guessLang( String name, String otherwise ) { String suffix = getFilenameExt( name ); if (suffix.equals( "n3" )) return langN3; if (suffix.equals( "nt" )) return langNTriple; if (suffix.equals( "ttl" )) return langTurtle ; if (suffix.equals( "rdf" )) return langXML; if (suffix.equals( "owl" )) return langXML; return otherwise; }
/** Create a print writer that uses UTF-8 encoding */ static public PrintWriter asPrintWriterUTF8(OutputStream out) { return new PrintWriter(asUTF8(out)); }
@Override final public void read(Model model, InputStream in, String base) { readImpl(model, FileUtils.asBufferedUTF8(in), base) ; }
@Override public void format(OutputStream out, boolean answer) { PrintWriter pw = FileUtils.asPrintWriterUTF8(out); if ( answer ) pw.write("yes"); else pw.write("no"); pw.flush(); } }
/** Guess the language/type of model data. * * <ul> * <li> If the URI ends ".rdf", it is assumed to be RDF/XML</li> * <li> If the URI ends ".nt", it is assumed to be N-Triples</li> * <li> If the URI ends ".ttl", it is assumed to be Turtle</li> * <li> If the URI ends ".owl", it is assumed to be RDF/XML</li> * </ul> * @param name URL to base the guess on * @param otherwise Default guess * @return String Guessed syntax - or the default supplied */ public static String guessLang( String name, String otherwise ) { String suffix = getFilenameExt( name ); if (suffix.equals( "n3" )) return langN3; if (suffix.equals( "nt" )) return langNTriple; if (suffix.equals( "ttl" )) return langTurtle ; if (suffix.equals( "rdf" )) return langXML; if (suffix.equals( "owl" )) return langXML; return otherwise; }
public String getItem(int i, boolean withIndirect) { if ( i < 0 || i >= items.size() ) return null ; String item = items.get(i) ; if ( withIndirect && item.startsWith(indirectionMarker) ) { item = item.substring(1) ; try { item = FileUtils.readWholeFileAsUTF8(item) ; } catch (Exception ex) { throw new IllegalArgumentException("Failed to read '"+item+"': "+ex.getMessage()) ; } } return item ; }
/** Guess the language/type of model data * * <ul> * <li> If the URI ends ".rdf", it is assumed to be RDF/XML</li> * <li> If the URI ends ".nt", it is assumed to be N-Triples</li> * <li> If the URI ends ".ttl", it is assumed to be Turtle</li> * <li> If the URI ends ".owl", it is assumed to be RDF/XML</li> * </ul> * @param urlStr URL to base the guess on * @return String Guessed syntax - default is RDF/XML */ public static String guessLang(String urlStr) { return guessLang(urlStr, langXML) ; }
/** Check whether a name is an absolute URI (has a scheme name) * * @param name * @return boolean True if there is a scheme name */ public static boolean isURI(String name) { return (getScheme(name) != null) ; }
/** Create a buffered reader that uses UTF-8 encoding */ static public BufferedReader asBufferedUTF8(InputStream in) { BufferedReader r = new BufferedReader(asUTF8(in)) ; return r ; }
public static BufferedReader readerFromURL( String urlStr ) { try { return asBufferedUTF8( new URL(urlStr).openStream() ); } catch (java.net.MalformedURLException e) { // Try as a plain filename. try { return asBufferedUTF8( new FileInputStream( urlStr ) ); } catch (FileNotFoundException f) { throw new WrappedIOException( f ); } } catch (IOException e) { throw new WrappedIOException( e ); } }
/** Write out an RDF model that encodes the result set * * @param outStream Output * @param format Name of RDF format (names as Jena writers) * @param resultSet The result set to encode in RDF */ static public void outputAsRDF(OutputStream outStream, String format, ResultSet resultSet) { PrintWriter out = FileUtils.asPrintWriterUTF8(outStream) ; RDFOutput.outputAsRDF(out, format, resultSet) ; out.flush() ; }
/** Try to map a URI or file name to a {@link Lang}; return null on no registered mapping. */ public static Lang filenameToLang(String filename) { if ( filename == null ) return null; // Remove any URI fragment (there can be only one # in a URI). // Pragmatically, assume any # is URI related. // URIs can be relative. int iHash = filename.indexOf('#'); if ( iHash > 0 ) filename = filename.substring(0, iHash); // Gzip or BZip2 compressed? filename = IO.filenameNoCompression(filename); return fileExtToLang(FileUtils.getFilenameExt(filename)); }
private String oneFile(String filename) { divider() ; try { return FileUtils.readWholeFileAsUTF8(filename) ; } catch (IOException ex) { System.err.println("No such file: "+filename) ; return null ; } }
public void testGuessLangLowerCase() { assertEquals(FileUtils.langN3, FileUtils.guessLang("simple.n3")); assertEquals(FileUtils.langN3, FileUtils.guessLang("hello.there.n3")); assertEquals(FileUtils.langTurtle, FileUtils.guessLang("simple.ttl")); assertEquals(FileUtils.langTurtle, FileUtils .guessLang("hello.there.ttl")); assertEquals(FileUtils.langNTriple, FileUtils.guessLang("simple.nt")); assertEquals(FileUtils.langNTriple, FileUtils.guessLang("whats.up.nt")); assertEquals(FileUtils.langXML, FileUtils.guessLang("poggle.rdf")); assertEquals(FileUtils.langXML, FileUtils.guessLang("wise.owl")); assertEquals(FileUtils.langXML, FileUtils.guessLang("dotless")); }