Example usage for javax.naming CompositeName getAll

List of usage examples for javax.naming CompositeName getAll

Introduction

In this page you can find the example usage for javax.naming CompositeName getAll.

Prototype

public Enumeration<String> getAll() 

Source Link

Document

Retrieves the components of this composite name as an enumeration of strings.

Usage

From source file:org.eclim.util.file.FileUtils.java

/**
 * Translates a file name that does not conform to the standard url file
 * format.//from w  w w  . ja v  a 2 s  .c  o  m
 * <p/>
 * Main purpose is to convert paths like:<br/>
 * <code>/opt/sun-jdk-1.5.0.05/src.zip/javax/swing/Spring.java</code><br/>
 * to<br/>
 * <code>zip:file:///opt/sun-jdk-1.5.0.05/src.zip!/javax/swing/Spring.java</code>
 *
 * @param file The file to translate.
 * @return The translated file.
 */
public static String toUrl(String file) {
    file = file.replace('\\', '/');

    // if the path points to a real file, return it.
    if (new File(file).exists()) {
        return file;
    }

    // already an url.
    if (file.startsWith(JAR_PREFIX) || file.startsWith(ZIP_PREFIX)) {
        return file;
    }

    // otherwise do some conversion.
    StringBuffer buffer = new StringBuffer();
    try {
        CompositeName fileName = new CompositeName(file);
        Enumeration<String> names = fileName.getAll();
        while (names.hasMoreElements()) {
            String name = names.nextElement();
            if (name.indexOf("$") != -1) {
                name = name.substring(0, name.indexOf("$")) + '.' + getExtension(name);
            }
            if (name.length() != 0) {
                buffer.append('/').append(name);

                if (!new File(buffer.toString()).exists()) {
                    String path = getFullPath(buffer.toString());
                    if (path.endsWith("/") || path.endsWith("\\")) {
                        path = path.substring(0, path.length() - 1);
                    }
                    if (path.endsWith(JAR_EXT)) {
                        buffer = new StringBuffer(JAR_PREFIX).append(path).append('!').append('/').append(name);
                    } else if (path.endsWith(ZIP_EXT)) {
                        buffer = new StringBuffer(ZIP_PREFIX).append(path).append('!').append('/').append(name);
                    }
                }
            }
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    return buffer.toString();
}