@ Pure @ SuppressWarnings ( "checkstyle:npathcomplexity" ) public static File convertURLToFile ( URL url ) { URL theUrl = url ; if ( theUrl == null ) { return null ; } if ( URISchemeType . RESOURCE . isURL ( theUrl ) ) { theUrl = Resources . getResource ( decodeHTMLEntities ( theUrl . getFile ( ) ) ) ; if ( theUrl == null ) { theUrl = url ; } } URI uri ; try { // this is the step that can fail, and so // it should be this step that should be fixed uri = theUrl . toURI ( ) ; } catch ( URISyntaxException e ) { // OK if we are here, then obviously the URL did // not comply with RFC 2396. This can only // happen if we have illegal unescaped characters. // If we have one unescaped character, then // the only automated fix we can apply, is to assume // all characters are unescaped. // If we want to construct a URI from unescaped // characters, then we have to use the component // constructors: try { uri = new URI ( theUrl . getProtocol ( ) , theUrl . getUserInfo ( ) , theUrl . getHost ( ) , theUrl . getPort ( ) , decodeHTMLEntities ( theUrl . getPath ( ) ) , decodeHTMLEntities ( theUrl . getQuery ( ) ) , theUrl . getRef ( ) ) ; } catch ( URISyntaxException e1 ) { // The URL is broken beyond automatic repair throw new IllegalArgumentException ( Locale . getString ( "E1" , theUrl ) ) ; //$NON-NLS-1$ } } if ( uri != null && URISchemeType . FILE . isURI ( uri ) ) { final String auth = uri . getAuthority ( ) ; String path = uri . getPath ( ) ; if ( path == null ) { path = uri . getRawPath ( ) ; } if ( path == null ) { path = uri . getSchemeSpecificPart ( ) ; } if ( path == null ) { path = uri . getRawSchemeSpecificPart ( ) ; } if ( path != null ) { if ( auth == null || "" . equals ( auth ) ) { //$NON-NLS-1$ // absolute filename in URI path = decodeHTMLEntities ( path ) ; } else { // relative filename in URI, extract it directly path = decodeHTMLEntities ( auth + path ) ; } if ( Pattern . matches ( "^" + Pattern . quote ( URL_PATH_SEPARATOR ) //$NON-NLS-1$ + "[a-zA-Z][:|].*$" , path ) ) { //$NON-NLS-1$ path = path . substring ( URL_PATH_SEPARATOR . length ( ) ) ; } return new File ( path ) ; } } throw new IllegalArgumentException ( Locale . getString ( "E2" , theUrl ) ) ; //$NON-NLS-1$ }
Convert an URL which represents a local file or a resource into a File .