/** * Decodes URL elements. This method may be used for all * parts of URL, except for the query parts, since it does * not decode the '+' character. * @see #decodeQuery(String, String) */ public static String decode(final String source, final String encoding) { return decode(source, encoding, false); }
@Test void testDecode() { assertEquals("", decode("")); assertEquals("foobar", decode("foobar")); assertEquals("foo bar", decode("foo%20bar")); assertEquals("foo+bar", decode("foo%2bbar")); assertEquals("T\u014dky\u014d", decode("T%C5%8Dky%C5%8D")); assertEquals("/Z\u00fcrich", decode("/Z%C3%BCrich")); assertEquals("T\u014dky\u014d", decode("T\u014dky\u014d")); assertEquals("foo+bar", decode("foo+bar")); assertEquals("foo bar", decodeQuery("foo+bar")); }
/** * Decodes query name or value. */ public static String decodeQuery(final String source, final String encoding) { return decode(source, encoding, true); }
/** * Decodes URL elements. */ public static String decode(final String url) { return decode(url, JoddCore.encoding, false); }
/** * Decodes query name or value. */ public static String decodeQuery(final String source) { return decode(source, JoddCore.encoding, true); }
/** * Converts {@link File} {@link URL}s to file name. Accepts only {@link URL}s with 'file' protocol. * Otherwise, for other schemes returns {@code null}. * * @param url {@link URL} to convert * @return file name */ public static String toFileName(final URL url) { if ((url == null) || !(url.getProtocol().equals(FILE_PROTOCOL))) { return null; } String filename = url.getFile().replace('/', File.separatorChar); return URLDecoder.decode(filename, encoding()); }
/** * Returns the jar file from which the given class is loaded; or null * if no such jar file can be located. */ public static JarFile jarFileOf(final Class<?> klass) { URL url = klass.getResource( "/" + klass.getName().replace('.', '/') + ".class"); if (url == null) { return null; } String s = url.getFile(); int beginIndex = s.indexOf("file:") + "file:".length(); int endIndex = s.indexOf(".jar!"); if (endIndex == -1) { return null; } endIndex += ".jar".length(); String f = s.substring(beginIndex, endIndex); // decode URL string - it may contain encoded chars (e.g. whitespaces) which are not supported for file-instances f = URLDecoder.decode(f, "UTF-8"); File file = new File(f); try { return file.exists() ? new JarFile(file) : null; } catch (IOException e) { throw new IllegalStateException(e); } }
/** * Decodes query name or value. */ public static String decodeQuery(final String source, final String encoding) { return decode(source, encoding, true); }
/** * Decodes URL elements. */ public static String decode(final String url) { return decode(url, JoddCore.encoding, false); }
/** * Decodes URL elements. This method may be used for all * parts of URL, except for the query parts, since it does * not decode the '+' character. * @see #decodeQuery(String, String) */ public static String decode(final String source, final String encoding) { return decode(source, encoding, false); }
/** * Decodes query name or value. */ public static String decodeQuery(final String source) { return decode(source, JoddCore.encoding, true); }
/** * Converts {@link File} {@link URL}s to file name. Accepts only {@link URL}s with 'file' protocol. * Otherwise, for other schemes returns {@code null}. * * @param url {@link URL} to convert * @return file name */ public static String toFileName(final URL url) { if ((url == null) || !(url.getProtocol().equals(FILE_PROTOCOL))) { return null; } String filename = url.getFile().replace('/', File.separatorChar); return URLDecoder.decode(filename, encoding()); }
/** * Returns the jar file from which the given class is loaded; or null * if no such jar file can be located. */ public static JarFile jarFileOf(final Class<?> klass) { URL url = klass.getResource( "/" + klass.getName().replace('.', '/') + ".class"); if (url == null) { return null; } String s = url.getFile(); int beginIndex = s.indexOf("file:") + "file:".length(); int endIndex = s.indexOf(".jar!"); if (endIndex == -1) { return null; } endIndex += ".jar".length(); String f = s.substring(beginIndex, endIndex); // decode URL string - it may contain encoded chars (e.g. whitespaces) which are not supported for file-instances f = URLDecoder.decode(f, "UTF-8"); File file = new File(f); try { return file.exists() ? new JarFile(file) : null; } catch (IOException e) { throw new IllegalStateException(e); } }