protected char [ ] normalize ( char [ ] path ) throws URIException { if ( path == null ) { return null ; } String normalized = new String ( path ) ; // If the buffer begins with "./" or "../", the "." or ".." is removed. if ( normalized . startsWith ( "./" ) ) { normalized = normalized . substring ( 1 ) ; } else if ( normalized . startsWith ( "../" ) ) { normalized = normalized . substring ( 2 ) ; } else if ( normalized . startsWith ( ".." ) ) { normalized = normalized . substring ( 2 ) ; } // All occurrences of "/./" in the buffer are replaced with "/" int index = - 1 ; while ( ( index = normalized . indexOf ( "/./" ) ) != - 1 ) { normalized = normalized . substring ( 0 , index ) + normalized . substring ( index + 2 ) ; } // If the buffer ends with "/.", the "." is removed. if ( normalized . endsWith ( "/." ) ) { normalized = normalized . substring ( 0 , normalized . length ( ) - 1 ) ; } int startIndex = 0 ; // All occurrences of "/<segment>/../" in the buffer, where ".." // and <segment> are complete path segments, are iteratively replaced // with "/" in order from left to right until no matching pattern remains. // If the buffer ends with "/<segment>/..", that is also replaced // with "/". Note that <segment> may be empty. while ( ( index = normalized . indexOf ( "/../" , startIndex ) ) != - 1 ) { int slashIndex = normalized . lastIndexOf ( ' ' , index - 1 ) ; if ( slashIndex >= 0 ) { normalized = normalized . substring ( 0 , slashIndex ) + normalized . substring ( index + 3 ) ; } else { startIndex = index + 3 ; } } if ( normalized . endsWith ( "/.." ) ) { int slashIndex = normalized . lastIndexOf ( ' ' , normalized . length ( ) - 4 ) ; if ( slashIndex >= 0 ) { normalized = normalized . substring ( 0 , slashIndex + 1 ) ; } } // All prefixes of "<segment>/../" in the buffer, where ".." // and <segment> are complete path segments, are iteratively replaced // with "/" in order from left to right until no matching pattern remains. // If the buffer ends with "<segment>/..", that is also replaced // with "/". Note that <segment> may be empty. while ( ( index = normalized . indexOf ( "/../" ) ) != - 1 ) { int slashIndex = normalized . lastIndexOf ( ' ' , index - 1 ) ; if ( slashIndex >= 0 ) { break ; } else { normalized = normalized . substring ( index + 3 ) ; } } if ( normalized . endsWith ( "/.." ) ) { int slashIndex = normalized . lastIndexOf ( ' ' , normalized . length ( ) - 4 ) ; if ( slashIndex < 0 ) { normalized = "/" ; } } return normalized . toCharArray ( ) ; }
Normalize the given hier path part .