/** * Canonicalize the path, i.e. remove ".." and "." occurences. * * @param path the path to be canonicalized * @return the canonicalized path */ public static String canonicalizePath(String path) { int dirIndex; while ((dirIndex = path.indexOf("/./")) >= 0) { //$NON-NLS-1$ path = path.substring(0, dirIndex + 1) + path.substring(dirIndex + 3); } if (path.endsWith("/.")) { //$NON-NLS-1$ path = path.substring(0, path.length() - 1); } while ((dirIndex = path.indexOf("/../")) >= 0) { //$NON-NLS-1$ if (dirIndex != 0) { path = path.substring(0, path .lastIndexOf('/', dirIndex - 1)) + path.substring(dirIndex + 3); } else { path = path.substring(dirIndex + 3); } } if (path.endsWith("/..") && path.length() > 3) { //$NON-NLS-1$ path = path.substring(0, path.lastIndexOf('/', path.length() - 4) + 1); } return path; }
/** * Creates a new URL instance using the given arguments. The URL uses the * specified port instead of the default port for the given protocol. * * @param protocol * the protocol of the new URL. * @param host * the host name or IP address of the new URL. * @param port * the specific port number of the URL. {@code -1} represents the * default port of the protocol. * @param file * the name of the resource. * @param handler * the stream handler to be used by this URL. * @throws MalformedURLException * if the combination of all arguments do not represent a valid * URL or the protocol is invalid. * @throws SecurityException * if {@code handler} is non-{@code null}, and a security * manager is installed that disallows user-defined protocol * handlers. */ public URL(String protocol, String host, int port, String file, URLStreamHandler handler) { // throws MalformedURLException { if (port < -1) { throw new MalformedURLException("Port out of range: " + port); //$NON-NLS-1$ } if (host != null && host.indexOf(":") != -1 && host.charAt(0) != '[') { //$NON-NLS-1$ host = "[" + host + "]"; //$NON-NLS-1$ //$NON-NLS-2$ } if (protocol == null) { throw new java.lang.NullPointerException("Unknown protocol: " + "null"); //$NON-NLS-1$ //$NON-NLS-2$ } this.protocol = protocol; this.host = host; this.port = port; // Set the fields from the arguments. Handle the case where the // passed in "file" includes both a file and a reference part. int index = -1; index = file.indexOf("#", file.lastIndexOf("/")); //$NON-NLS-1$ //$NON-NLS-2$ if (index >= 0) { this.file = file.substring(0, index); refJ = file.substring(index + 1); } else { this.file = file; } fixURL(false); // Set the stream handler for the URL either to the handler // argument if it was specified, or to the default for the // receiver's protocol if the handler was null. if (handler == null) { setupStreamHandler(); if (strmHandler == null) { throw new MalformedURLException("Unknown protocol: " + protocol); //$NON-NLS-1$ } } else { java.lang.SecurityManager sm = java.lang.SystemJ.getSecurityManager(); if (sm != null) { sm.checkPermission(specifyStreamHandlerPermission); } strmHandler = handler; } }
/*static{ TEST(); } public static void TEST() { //ParsedCookie cookie = new ParsedCookie(); String strClientCookie = ""; strClientCookie = URI.parseCookie("auth_token=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT, auth_token=887b2ffd30a7b97be9a0986d7746a934421eec7d; path=/; expires=Sat, 24 Oct 2009 20:56:55 GMT, rhosync_session=BAh7BzoMdXNlcl9pZGkIIgpmbGFzaElDOidBY3Rpb25Db250cm9sbGVyOjpGbGFzaDo6Rmxhc2hIYXNoewAGOgpAdXNlZHsA--f9b67d99397fc534107fb3b7483ccdae23b4a761; path=/; expires=Sun, 10 Oct 2010 19:10:58 GMT; HttpOnly"); strClientCookie = URI.parseCookie("auth_token=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT"); strClientCookie = URI.parseCookie("rhosync_session=BAh7CToNcGFzc3dvcmQiFTiMYru1W11zuoAlN%2FPtgjc6CmxvZ2luIhU4jGK7tVtdc7qAJTfz7YI3Ogx1c2VyX2lkaQYiCmZsYXNoSUM6J0FjdGlvbkNvbnRyb2xsZXI6OkZsYXNoOjpGbGFzaEhhc2h7AAY6CkB1c2VkewA%3D--a7829a70171203d72cd4e83d07b18e8fcf5e2f78; path=/; expires=Thu, 02 Sep 2010 23:51:31 GMT; HttpOnly"); }*/ public static String readFully(Stream stream, String strContType) { String strRes = ""; byte[] byteBuffer = new byte[1024*4]; boolean bUTF8 = false; StreamReader reader = null; if ( strContType != null ) { int nCharsetPos = strContType.lastIndexOf('='); if ( nCharsetPos > 0 ) { String strEnc = strContType.substring(nCharsetPos+1); bUTF8 = strEnc.equalsIgnoreCase("UTF-8"); } } try { if(bUTF8) reader = new StreamReader(stream, Encoding.UTF8); else reader = new StreamReader(stream); strRes = reader.ReadToEnd(); } finally { if(reader != null) reader.Close(); } return strRes; }
/* RubyString[] getOrigColNames(IDBResult rows) { RubyString[] colNames = new RubyString[rows.getColCount()]; for ( int nCol = 0; nCol < rows.getColCount(); nCol ++ ) colNames[nCol] = ObjectFactory.createString(rows.getOrigColName(nCol)); return colNames; } */ private String getNameNoExt(String strPath){ int nDot = strPath.lastIndexOf('.'); String strDbName = ""; if ( nDot > 0 ) strDbName = strPath.substring(0, nDot); else strDbName = strPath; return strDbName; }
public static String getLastNamePart(String strUrl) { int nQuest = strUrl.lastIndexOf('?'); String strRes = strUrl; if (nQuest>=0) strRes = strUrl.substring(0, nQuest); int nSlash = strRes.lastIndexOf('/'); if ( nSlash < 0 ) nSlash = strRes.lastIndexOf('\\'); if ( nSlash >= 0 ) strRes = strRes.substring(nSlash+1); return strRes; }