private static String correct ( String url , final boolean forceCorrection ) { if ( url == null ) { return null ; } final String initialUrl = url ; // If there is a % that means the URL was already corrected. if ( ! forceCorrection && url . contains ( "%" ) ) { return initialUrl ; } // Extract the reference (anchor) part from the URL. The '#' char identifying the anchor // must not be corrected. String reference = null ; if ( ! forceCorrection ) { final int refIndex = url . lastIndexOf ( ' ' ) ; if ( refIndex != - 1 ) { reference = FilePathToURI . filepath2URI ( url . substring ( refIndex + 1 ) ) ; url = url . substring ( 0 , refIndex ) ; } } // Buffer where eventual query string will be processed. StringBuilder queryBuffer = null ; if ( ! forceCorrection ) { final int queryIndex = url . indexOf ( ' ' ) ; if ( queryIndex != - 1 ) { // We have a query final String query = url . substring ( queryIndex + 1 ) ; url = url . substring ( 0 , queryIndex ) ; queryBuffer = new StringBuilder ( query . length ( ) ) ; // Tokenize by & final StringTokenizer st = new StringTokenizer ( query , "&" ) ; while ( st . hasMoreElements ( ) ) { String token = st . nextToken ( ) ; token = FilePathToURI . filepath2URI ( token ) ; // Correct token queryBuffer . append ( token ) ; if ( st . hasMoreElements ( ) ) { queryBuffer . append ( "&" ) ; } } } } String toReturn = FilePathToURI . filepath2URI ( url ) ; if ( queryBuffer != null ) { // Append to the end the corrected query. toReturn += "?" + queryBuffer . toString ( ) ; } if ( reference != null ) { // Append the reference to the end the corrected query. toReturn += "#" + reference ; } return toReturn ; }
Method introduced to correct the URLs in the default machine encoding .