Example usage for org.apache.commons.lang CharEncoding UTF_8

List of usage examples for org.apache.commons.lang CharEncoding UTF_8

Introduction

In this page you can find the example usage for org.apache.commons.lang CharEncoding UTF_8.

Prototype

String UTF_8

To view the source code for org.apache.commons.lang CharEncoding UTF_8.

Click Source Link

Document

Eight-bit Unicode Transformation Format.

Usage

From source file:org.sonar.server.plugins.ClassLoaderUtils.java

/**
 * Finds directories and files within a given directory and its subdirectories.
 *
 * @param classLoader/*from w w w.ja v a2  s .c  om*/
 * @param rootPath    the root directory, for example org/sonar/sqale, or a file in this root directory, for example org/sonar/sqale/index.txt
 * @param
 * @return a list of relative paths, for example {"org/sonar/sqale", "org/sonar/sqale/foo", "org/sonar/sqale/foo/bar.txt}. Never null.
 */
public static Collection<String> listResources(ClassLoader classLoader, String rootPath,
        Predicate<String> predicate) {
    String jarPath = null;
    JarFile jar = null;
    try {
        Collection<String> paths = Lists.newArrayList();
        rootPath = StringUtils.removeStart(rootPath, "/");

        URL root = classLoader.getResource(rootPath);
        if (root != null) {
            checkJarFile(root);

            // Path of the root directory
            // Examples :
            // org/sonar/sqale/index.txt  -> rootDirectory is org/sonar/sqale
            // org/sonar/sqale/  -> rootDirectory is org/sonar/sqale
            // org/sonar/sqale  -> rootDirectory is org/sonar/sqale
            String rootDirectory = rootPath;
            if (StringUtils.substringAfterLast(rootPath, "/").indexOf('.') >= 0) {
                rootDirectory = StringUtils.substringBeforeLast(rootPath, "/");
            }
            jarPath = root.getPath().substring(5, root.getPath().indexOf("!")); //strip out only the JAR file
            jar = new JarFile(URLDecoder.decode(jarPath, CharEncoding.UTF_8));
            Enumeration<JarEntry> entries = jar.entries();
            while (entries.hasMoreElements()) {
                String name = entries.nextElement().getName();
                if (name.startsWith(rootDirectory) && predicate.apply(name)) {
                    paths.add(name);
                }
            }
        }
        return paths;
    } catch (Exception e) {
        throw Throwables.propagate(e);
    } finally {
        closeJar(jar, jarPath);
    }
}

From source file:org.sonar.server.rules.DeprecatedRuleRepositories.java

private void registerRuleExtensions(List<Rule> rules) {
    for (File file : fileSystem.getPluginExtensionXml(getKey())) {
        try {//from w  w  w. ja va  2s  . co m
            String fileContent = FileUtils.readFileToString(file, CharEncoding.UTF_8);
            List<Rule> deprecatedRules = deprecatedRepository.parseReferential(fileContent);
            if (deprecatedRules != null) {
                for (Rule deprecatedRule : deprecatedRules) {
                    rules.add(cloneRule(deprecatedRule));
                }
            }
        } catch (IOException e) {
            throw new SonarException("Can not read the file: " + file.getPath(), e);
        }
    }
}

From source file:org.sonar.server.util.ClassLoaderUtils.java

/**
 * Finds directories and files within a given directory and its subdirectories.
 *
 * @param classLoader/*from ww  w  .ja v  a  2s. c o m*/
 * @param rootPath    the root directory, for example org/sonar/sqale, or a file in this root directory, for example org/sonar/sqale/index.txt
 * @param predicate
 * @return a list of relative paths, for example {"org/sonar/sqale", "org/sonar/sqale/foo", "org/sonar/sqale/foo/bar.txt}. Never null.
 */
public static Collection<String> listResources(ClassLoader classLoader, String rootPath,
        Predicate<String> predicate) {
    String jarPath = null;
    JarFile jar = null;
    try {
        Collection<String> paths = Lists.newArrayList();
        URL root = classLoader.getResource(rootPath);
        if (root != null) {
            checkJarFile(root);

            // Path of the root directory
            // Examples :
            // org/sonar/sqale/index.txt -> rootDirectory is org/sonar/sqale
            // org/sonar/sqale/ -> rootDirectory is org/sonar/sqale
            // org/sonar/sqale -> rootDirectory is org/sonar/sqale
            String rootDirectory = rootPath;
            if (StringUtils.substringAfterLast(rootPath, "/").indexOf('.') >= 0) {
                rootDirectory = StringUtils.substringBeforeLast(rootPath, "/");
            }
            // strip out only the JAR file
            jarPath = root.getPath().substring(5, root.getPath().indexOf('!'));
            jar = new JarFile(URLDecoder.decode(jarPath, CharEncoding.UTF_8));
            Enumeration<JarEntry> entries = jar.entries();
            while (entries.hasMoreElements()) {
                String name = entries.nextElement().getName();
                if (name.startsWith(rootDirectory) && predicate.test(name)) {
                    paths.add(name);
                }
            }
        }
        return paths;
    } catch (Exception e) {
        throw Throwables.propagate(e);
    } finally {
        closeJar(jar, jarPath);
    }
}

From source file:org.terentich.pram.GUIPRAM.java

private void showFileContent(File... files) {
    for (File file : files) {

        JPanel filePanel = new JPanel(new BorderLayout());
        JTextArea textbox = new JTextArea();
        filePanel.add(new JScrollPane(textbox));
        tabPane.add(file.getName(), filePanel);
        tabPane.setSelectedComponent(filePanel);

        try {//from   ww w  .  j ava 2 s .c  o  m
            @SuppressWarnings("unchecked")
            List<String> fileContent = FileUtils.readLines(file, CharEncoding.UTF_8);

            for (String line : fileContent) {
                textbox.append(line + "\n");
            }

            textbox.setCaretPosition(0);

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

From source file:org.terentich.pram.tookit.MetaReader.java

private static Manifest getCurrentJarManifest() {

    Manifest manifest = null;/*from   w w  w. j av a 2s .  c o  m*/
    CodeSource code = MetaReader.class.getProtectionDomain().getCodeSource();

    try {
        String currentFile = URLDecoder.decode(code.getLocation().getFile(), CharEncoding.UTF_8);

        if (currentFile.endsWith(".jar")) {
            JarFile jar = new JarFile(currentFile);
            manifest = jar.getManifest();
        }
    } catch (IOException e) {
        System.out.println("An exception occurs: " + e);
    }

    return manifest;
}

From source file:org.wso2.appcloud.integration.test.utils.clients.BaseClient.java

/**
 * Returns a String that is suitable for use as an application/x-www-form-urlencoded list of parameters in an HTTP PUT or HTTP POST.
 *
 * @param keyVal parameter map/*from  w w  w. ja  v  a2 s .  c  o  m*/
 * @return message body
 */
public String generateMsgBody(Map<String, String> keyVal) {
    List<NameValuePair> qparams = new ArrayList<NameValuePair>();
    for (Map.Entry<String, String> keyValEntry : keyVal.entrySet()) {
        qparams.add(new BasicNameValuePair(keyValEntry.getKey(), keyValEntry.getValue()));
    }
    return URLEncodedUtils.format(qparams, CharEncoding.UTF_8);
}