public static String filename2baseIRI(String filename) { if ( filename == null || filename.equals("-") ) return "http://localhost/stdin/" ; String x = IRILib.filenameToIRI(filename) ; return x ; }
private static String encodeURIComponent(String s) { return IRILib.encodeUriComponent(s); }
public static String encodeNonASCII(String string) { if ( ! containsNonASCII(string) ) return string ; byte[] bytes = StrUtils.asUTF8bytes(string) ; StringBuilder sw = new StringBuilder() ; for ( byte b : bytes ) { // Signed bytes ... if ( b > 0 ) { sw.append( (char) b ); continue; } int hi = ( b & 0xF0 ) >> 4; int lo = b & 0xF; sw.append( '%' ); sw.append( Chars.hexDigitsUC[hi] ); sw.append( Chars.hexDigitsUC[lo] ); } return sw.toString() ; }
/** Convert a file: IRI to a filename */ public static String IRIToFilename(String iri) { if ( ! iri.startsWith("file:") ) throw new AtlasException("Not a file: URI: "+iri) ; String fn ; if ( iri.startsWith("file:///") ) fn = iri.substring("file://".length()) ; else fn = iri.substring("file:".length()) ; // MS Windows: we can have // file:///C:/path or file:/C:/path // At this point, we have a filename of /C:/ // so need strip the leading "/" fn = fixupWindows(fn); return decode(fn) ; }
/** Convert a plain file name (no file:) to a file: URL */ private static String plainFilenameToURL(String fn) { // No "file:" // Make Absolute filename. boolean trailingSlash = fn.endsWith("/") ; // To get Path.toAbsolutePath to work, we need to convert /C:/ to C:/ // then back again. fn = fixupWindows(fn) ; fn = Paths.get(fn).toAbsolutePath().normalize().toString() ; if ( trailingSlash && ! fn.endsWith("/") ) fn = fn + "/" ; if ( Sys.isWindows ) { // C:\ => file:///C:/... if ( windowsDrive(fn, 0) ) // Windows drive letter - already absolute path. // Make "URI" absolute path fn = "/"+fn ; // Convert \ to / // Maybe should do this on all platforms? i.e consistency. fn = fn.replace('\\', '/' ) ; } fn = encodeFileURL(fn) ; return "file://"+fn ; }
fn = IRILib.IRIToFilename(filenameIRI) ;
public static NodeValue strEncodeForURI(NodeValue v) { Node n = v.asNode() ; if ( !n.isLiteral() ) throw new ExprEvalException("Not a literal") ; if ( ! Util.isSimpleString(n) && ! Util.isLangString(n) ) throw new ExprEvalException("Not a string literal") ; String str = n.getLiteralLexicalForm() ; String encStr = IRILib.encodeUriComponent(str) ; encStr = IRILib.encodeNonASCII(encStr) ; return NodeValue.makeString(encStr) ; }
/** Open an input stream to a file; do not mask IOExceptions. * If the filename ends in .gz, wrap in GZIPOutputStream * @param filename * @throws FileNotFoundException If the output can't be opened. * @throws IOException for bad gzip encoded data */ static public OutputStream openOutputFileEx(String filename) throws FileNotFoundException,IOException { if ( filename == null || filename.equals("-") ) return System.out ; if ( filename.startsWith("file:") ) { filename = filename.substring("file:".length()) ; filename = IRILib.decode(filename) ; } OutputStream out = new FileOutputStream(filename) ; String ext = FilenameUtils.getExtension(filename); switch ( ext ) { case "": return out; case "gz": return new GZIPOutputStream(out) ; case "bz2": return new BZip2CompressorOutputStream(out); case "sz": throw new UnsupportedOperationException("Snappy output"); } return out ; }
String path = IRILib.IRIToFilename(x.getURI()) ; File dir = new File(path) ; directory = FSDirectory.open(dir.toPath()) ;
/** Convert a plain file name (no file:) to a file: URL */ private static String plainFilenameToURL(String fn) { // No "file:" // Make Absolute filename. boolean trailingSlash = fn.endsWith("/") ; // To get Path.toAbsolutePath to work, we need to convert /C:/ to C:/ // then back again. fn = fixupWindows(fn) ; fn = Paths.get(fn).toAbsolutePath().normalize().toString() ; if ( trailingSlash && ! fn.endsWith("/") ) fn = fn + "/" ; if ( Sys.isWindows ) { // C:\ => file:///C:/... if ( windowsDrive(fn, 0) ) // Windows drive letter - already absolute path. // Make "URI" absolute path fn = "/"+fn ; // Convert \ to / // Maybe should do this on all platforms? i.e consistency. fn = fn.replace('\\', '/' ) ; } fn = encodeFileURL(fn) ; return "file://"+fn ; }
/** Convert a file: IRI to a filename */ public static String IRIToFilename(String iri) { if ( ! iri.startsWith("file:") ) throw new AtlasException("Not a file: URI: "+iri) ; String fn ; if ( iri.startsWith("file:///") ) fn = iri.substring("file://".length()) ; else fn = iri.substring("file:".length()) ; // MS Windows: we can have // file:///C:/path or file:/C:/path // At this point, we have a filename of /C:/ // so need strip the leading "/" fn = fixupWindows(fn); return decode(fn) ; }
/** Open an input stream to a file; do not mask IOExceptions. * If the filename ends in .gz, wrap in GZIPOutputStream * @param filename * @throws FileNotFoundException If the output can't be opened. * @throws IOException for bad gzip encoded data */ static public OutputStream openOutputFileEx(String filename) throws FileNotFoundException,IOException { if ( filename == null || filename.equals("-") ) return System.out ; if ( filename.startsWith("file:") ) { filename = filename.substring("file:".length()) ; filename = IRILib.decode(filename) ; } OutputStream out = new FileOutputStream(filename) ; String ext = FilenameUtils.getExtension(filename); switch ( ext ) { case "": return out; case "gz": return new GZIPOutputStream(out) ; case "bz2": return new BZip2CompressorOutputStream(out); case "sz": throw new UnsupportedOperationException("Snappy output"); } return out ; }
/** Return a string that is an IRI for the filename.*/ public static String fileToIRI(File f) { return filenameToIRI(f.getAbsolutePath()) ; }
String path = IRILib.IRIToFilename(x.getURI()) ; File dir = new File(path) ; directory = FSDirectory.open(dir.toPath()) ;
protected final String target(Node name) { if ( !name.isURI() ) throw new JenaException("Not a URI: " + name) ; String guri = name.getURI() ; // Encode guri = IRILib.encodeUriComponent(guri) ; return remote + "?" + HttpNames.paramGraph + "=" + guri ; }
/** Open an input stream to a file; do not mask IOExceptions. * If the filename is null or "-", return System.in * If the filename ends in .gz, wrap in GZIPInputStream * @param filename * @throws FileNotFoundException * @throws IOException */ static public InputStream openFileEx(String filename) throws IOException, FileNotFoundException { if ( filename == null || filename.equals("-") ) return System.in ; if ( filename.startsWith("file:") ) { filename = filename.substring("file:".length()) ; filename = IRILib.decode(filename) ; } InputStream in = new FileInputStream(filename) ; String ext = FilenameUtils.getExtension(filename); switch ( ext ) { case "": return in; case "gz": return new GZIPInputStream(in) ; case "bz2": return new BZip2CompressorInputStream(in); case "sz": return new SnappyCompressorInputStream(in); } return in ; }
public static String encodeNonASCII(String string) { if ( ! containsNonASCII(string) ) return string ; byte[] bytes = StrUtils.asUTF8bytes(string) ; StringBuilder sw = new StringBuilder() ; for ( byte b : bytes ) { // Signed bytes ... if ( b > 0 ) { sw.append( (char) b ); continue; } int hi = ( b & 0xF0 ) >> 4; int lo = b & 0xF; sw.append( '%' ); sw.append( Chars.hexDigitsUC[hi] ); sw.append( Chars.hexDigitsUC[lo] ); } return sw.toString() ; }
/** Return a string that is an IRI for the filename.*/ public static String fileToIRI(File f) { return filenameToIRI(f.getAbsolutePath()) ; }
String path = IRILib.IRIToFilename(x.getURI()) ; File dir = new File(path) ; directory = FSDirectory.open(dir.toPath()) ;
private static void encodeComponent(String string, String result) { String r = IRILib.encodeUriComponent(string) ; assertEquals(result, r) ; }