Example usage for java.util Enumeration nextElement

List of usage examples for java.util Enumeration nextElement

Introduction

In this page you can find the example usage for java.util Enumeration nextElement.

Prototype

E nextElement();

Source Link

Document

Returns the next element of this enumeration if this enumeration object has at least one more element to provide.

Usage

From source file:de.hypoport.ep2.support.configuration.properties.PropertiesLoader.java

static void replacePropertyPlaceHolder(Properties properties) {

    Enumeration<?> keyEnum = properties.propertyNames();
    while (keyEnum.hasMoreElements()) {
        String key = (String) keyEnum.nextElement();
        Object value = properties.get(key);
        if (value != null && value instanceof String) {
            String valueString = (String) value;
            valueString = replacePropertyPlaceHolder(valueString, properties);
            properties.put(key, valueString);
        }// ww  w  .j  ava 2s .c  o  m
    }
}

From source file:Main.java

/**
 * Given an Object, and a key (index), it will get value associated with
 * that key in the Object. The following checks are made:
 * <ul>// ww w .  java2s. c o  m
 * <li>If obj is a Map, use the index as a key to get a value. If no match
 * continue.
 * <li>Check key is an Integer. If not, return the object passed in.
 * <li>If obj is a Map, get the nth value from the <b>key</b> iterator.
 * <li>If obj is a List or an array, get the nth value.
 * <li>If obj is an iterator, enumeration or Collection, get the nth value
 * from the iterator.
 * <li>Return the original obj.
 * </ul>
 * 
 * @param obj
 *            the object to get an index of
 * @param index
 *            the index to get
 * @return the object at the specified index
 * @throws IndexOutOfBoundsException
 * @throws NoSuchElementException
 */
public static Object index(Object obj, Object index) {
    if (obj instanceof Map) {
        Map map = (Map) obj;
        if (map.containsKey(index)) {
            return map.get(index);
        }
    }
    int idx = -1;
    if (index instanceof Integer) {
        idx = ((Integer) index).intValue();
    }
    if (idx < 0) {
        return obj;
    } else if (obj instanceof Map) {
        Map map = (Map) obj;
        Iterator iterator = map.keySet().iterator();
        return index(iterator, idx);
    } else if (obj instanceof List) {
        return ((List) obj).get(idx);
    } else if (obj instanceof Object[]) {
        return ((Object[]) obj)[idx];
    } else if (obj instanceof Enumeration) {
        Enumeration enumeration = (Enumeration) obj;
        while (enumeration.hasMoreElements()) {
            idx--;
            if (idx == -1) {
                return enumeration.nextElement();
            } else {
                enumeration.nextElement();
            }
        }
    } else if (obj instanceof Iterator) {
        return index((Iterator) obj, idx);
    } else if (obj instanceof Collection) {
        Iterator iterator = ((Collection) obj).iterator();
        return index(iterator, idx);
    }
    return obj;
}

From source file:proxy.ElementalHttpGet.java

static List<InetAddress> getAllIp(String networkInterface) {
    try {//from w w w . j ava 2s .  c o m
        NetworkInterface interfaces = NetworkInterface.getByName(networkInterface);
        Enumeration<InetAddress> inetAddresses = interfaces.getInetAddresses();
        List<InetAddress> result = new ArrayList<InetAddress>();
        while (inetAddresses.hasMoreElements()) {
            InetAddress inetAddress = inetAddresses.nextElement();
            if (inetAddress instanceof Inet4Address) {
                result.add(inetAddress);
            }
        }
        return result;
    } catch (SocketException e) {
        throw new RuntimeException(e);
    }
}

From source file:de.knowwe.core.user.UserContextUtil.java

/**
 * Returns a Map<String, String> with all parameters of a http request. This
 * is necessary because the parameter map of the http request is locked.
 * //  ww  w .jav a 2  s.  c o  m
 * @created Mar 9, 2011
 * @param request the http request
 * @return map containing the parameters of the http request.
 */
public static Map<String, String> getParameters(HttpServletRequest request) {
    Map<String, String> parameters = new HashMap<>();
    if (request != null) {
        Enumeration<?> iter = request.getParameterNames();
        boolean decode = checkForFlowChart(request.getParameter("action"));
        while (iter.hasMoreElements()) {
            String key = (String) iter.nextElement();
            String value = request.getParameter(key);
            parameters.put(key, decode ? Strings.decodeURL(value) : value);
        }
        if (request.getMethod() != null && request.getMethod().equals("POST")) {

            // do not handle file uploads, leave this to the action
            if (!ServletFileUpload.isMultipartContent(request)) {

                try {
                    BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));

                    String line;
                    StringBuilder bob = new StringBuilder();

                    while ((line = br.readLine()) != null) {
                        bob.append(line).append("\n");
                    }

                    parameters.put("data", bob.toString());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    return parameters;
}

From source file:de.lazyzero.kkMulticopterFlashTool.utils.Zip.java

public static File unzipFile(File zipFile, File file) {
    System.out.println("path to zipFile: " + zipFile.getPath());
    System.out.println("file to extract: " + file.getPath());
    String fileName = null;//from  ww w .j  ava2 s. c o m

    try {
        zipFile.mkdir();
        BufferedOutputStream dest = null;
        BufferedInputStream is = null;
        ZipEntry entry;
        ZipFile zipfile = new ZipFile(zipFile);
        Enumeration e = zipfile.entries();
        while (e.hasMoreElements()) {
            entry = (ZipEntry) e.nextElement();
            // System.out.println(entry.getName());
            if (entry.getName().substring(entry.getName().indexOf("/") + 1).equals(file.getName())) {
                //   if (entry.getName().contains(file.getName())){
                System.out.println("firmware to extract found.");

                String tempFolder = System.getProperty("user.dir") + File.separatorChar + "tmp"
                        + File.separatorChar;
                if (System.getProperty("os.name").toLowerCase().contains("mac")) {
                    tempFolder = System.getProperty("user.home")
                            + "/Library/Preferences/kkMulticopterFlashTool/";
                }

                String newDir;
                if (entry.getName().indexOf("/") == -1) {
                    newDir = zipFile.getName().substring(0, zipFile.getName().indexOf("."));
                } else {
                    newDir = entry.getName().substring(0, entry.getName().indexOf("/"));
                }
                String folder = tempFolder + newDir;
                System.out.println("Create folder: " + folder);
                if ((new File(folder).mkdir())) {
                    System.out.println("Done.");
                    ;
                }

                System.out.println("Extracting: " + entry);
                is = new BufferedInputStream(zipfile.getInputStream(entry));
                int count;
                byte data[] = new byte[2048];
                fileName = tempFolder + entry.getName();
                FileOutputStream fos = new FileOutputStream(tempFolder + entry.getName());
                dest = new BufferedOutputStream(fos, 2048);
                while ((count = is.read(data, 0, 2048)) != -1) {
                    dest.write(data, 0, count);
                }
                dest.flush();
                dest.close();
                is.close();
                break;
            }
        }
    } catch (ZipException e) {
        zipFile.delete();
        kk.err(Translatrix._("error.zipfileDamaged"));
        kk.err(Translatrix._("reportProblem"));
    } catch (Exception e) {
        e.printStackTrace();
    }

    return new File(fileName);
}

From source file:eu.leads.processor.planner.ClassUtil.java

private static void findClasses(Set<Class> matchedClassSet, File root, File file, boolean includeJars,
        Class type, String packageFilter) {
    if (file.isDirectory()) {
        for (File child : file.listFiles()) {
            findClasses(matchedClassSet, root, child, includeJars, type, packageFilter);
        }/*w ww.ja  v a  2s.c  om*/
    } else {
        if (file.getName().toLowerCase().endsWith(".jar") && includeJars) {
            JarFile jar = null;
            try {
                jar = new JarFile(file);
            } catch (Exception ex) {
                LOG.error(ex.getMessage(), ex);
                return;
            }
            Enumeration<JarEntry> entries = jar.entries();
            while (entries.hasMoreElements()) {
                JarEntry entry = entries.nextElement();
                String name = entry.getName();
                int extIndex = name.lastIndexOf(".class");
                if (extIndex > 0) {
                    String qualifiedClassName = name.substring(0, extIndex).replace("/", ".");
                    if (qualifiedClassName.indexOf(packageFilter) >= 0 && !isTestClass(qualifiedClassName)) {
                        try {
                            Class clazz = Class.forName(qualifiedClassName);

                            if (!clazz.isInterface() && isMatch(type, clazz)) {
                                matchedClassSet.add(clazz);
                            }
                        } catch (ClassNotFoundException e) {
                            LOG.error(e.getMessage(), e);
                        }
                    }
                }
            }
        } else if (file.getName().toLowerCase().endsWith(".class")) {
            String qualifiedClassName = createClassName(root, file);
            // if (qualifiedClassName.indexOf(packageFilter) >= 0 && !isTestClass(qualifiedClassName)) {
            try {
                Class clazz = Class.forName(qualifiedClassName);
                if (!clazz.isInterface() && isMatch(type, clazz)) {
                    matchedClassSet.add(clazz);
                }
            } catch (ClassNotFoundException e) {
                LOG.error(e.getMessage(), e);
            }
            // }
        }
    }
}

From source file:com.stevpet.sonar.plugins.dotnet.mscover.codecoverage.command.ZipUtils.java

/**
 * Extracts the specified folder from the specified archive, into the supplied output directory.
 * //ww w .ja va  2  s .c  o m
 * @param archivePath
 *          the archive Path
 * @param folderToExtract
 *          the folder to extract
 * @param outputDirectory
 *          the output directory
 * @return the extracted folder path
 * @throws IOException
 *           if a problem occurs while extracting
 */
public static File extractArchiveFolderIntoDirectory(String archivePath, String folderToExtract,
        String outputDirectory) throws IOException {
    File destinationFolder = new File(outputDirectory);
    destinationFolder.mkdirs();

    ZipFile zip = null;
    try {
        zip = new ZipFile(new File(archivePath));
        Enumeration<?> zipFileEntries = zip.entries();
        // Process each entry
        while (zipFileEntries.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
            String currentEntry = entry.getName();
            if (currentEntry.startsWith(folderToExtract)) {
                File destFile = new File(destinationFolder, currentEntry);
                destFile.getParentFile().mkdirs();
                if (!entry.isDirectory()) {
                    BufferedInputStream is = null;
                    BufferedOutputStream dest = null;
                    try {
                        is = new BufferedInputStream(zip.getInputStream(entry));
                        int currentByte;
                        // establish buffer for writing file
                        byte data[] = new byte[BUFFER_SIZE];

                        // write the current file to disk
                        FileOutputStream fos = new FileOutputStream(destFile);
                        dest = new BufferedOutputStream(fos, BUFFER_SIZE);

                        // read and write until last byte is encountered
                        while ((currentByte = is.read(data, 0, BUFFER_SIZE)) != -1) {
                            dest.write(data, 0, currentByte);
                        }
                    } finally {
                        if (dest != null) {
                            dest.flush();
                        }
                        IOUtils.closeQuietly(dest);
                        IOUtils.closeQuietly(is);
                    }
                }
            }
        }
    } finally {
        if (zip != null) {
            zip.close();
        }
    }

    return new File(destinationFolder, folderToExtract);
}

From source file:cz.zeno.miner.Utils.java

public static String[] getAvailableSerialPorts() {
    java.util.Enumeration<CommPortIdentifier> portEnum = CommPortIdentifier.getPortIdentifiers();
    ArrayList<String> serialPorts = new ArrayList<>();
    while (portEnum.hasMoreElements()) {
        CommPortIdentifier portIdentifier = portEnum.nextElement();

        if (CommPortIdentifier.PORT_SERIAL == portIdentifier.getPortType()) {
            serialPorts.add(portIdentifier.getName());
        }/*w w w .ja  v  a  2 s  . c om*/
    }
    return serialPorts.toArray(new String[0]);
}

From source file:de.lazyzero.kkMulticopterFlashTool.utils.Zip.java

public static void unzip2folder(File zipFile, File toFolder) throws FileExistsException {
    if (!toFolder.exists()) {
        toFolder.mkdirs();//from  w  w  w  .  j  av  a2 s  .co  m
    } else if (toFolder.isFile()) {
        throw new FileExistsException(toFolder.getName());
    }

    try {
        ZipEntry entry;
        @SuppressWarnings("resource")
        ZipFile zipfile = new ZipFile(zipFile);
        Enumeration<? extends ZipEntry> e = zipfile.entries();
        while (e.hasMoreElements()) {
            entry = e.nextElement();

            //            String newDir;
            //            if (entry.getName().indexOf("/") == -1) {
            //               newDir = zipFile.getName().substring(0, zipFile.getName().indexOf("."));
            //            } else {
            //               newDir = entry.getName().substring(0, entry.getName().indexOf("/"));
            //            }
            //            String folder = toFolder + (File.separatorChar+"") + newDir;
            //            System.out.println(folder);
            //            if ((new File(folder).mkdir())) {
            //               System.out.println("Done.");
            //            }
            System.out.println("Extracting: " + entry);
            if (entry.isDirectory()) {
                new File(toFolder + (File.separatorChar + "") + entry.getName()).mkdir();
            } else {
                BufferedInputStream is = new BufferedInputStream(zipfile.getInputStream(entry));
                int count;
                byte data[] = new byte[2048];
                String fileName = toFolder + (File.separatorChar + "") + entry.getName();
                FileOutputStream fos = new FileOutputStream(fileName);
                BufferedOutputStream dest = new BufferedOutputStream(fos, 2048);
                while ((count = is.read(data, 0, 2048)) != -1) {
                    dest.write(data, 0, count);
                }
                dest.flush();
                dest.close();
                is.close();
                System.out.println("extracted to: " + fileName);
            }

        }
    } catch (ZipException e1) {
        zipFile.delete();
        e1.printStackTrace();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } finally {

    }

}

From source file:com.google.enterprise.connector.otex.LivelinkConnectorFactory.java

public static LivelinkConnector getConnector(String prefix) throws RepositoryException {
    Map<String, String> p = new HashMap<String, String>();
    p.putAll(emptyProperties);// ww w  .  j a  v  a 2 s .  c om

    Properties system = System.getProperties();
    Enumeration<?> names = system.propertyNames();
    boolean prefixFound = false;

    while (names.hasMoreElements()) {
        String name = (String) names.nextElement();
        if (name.startsWith(prefix)) {
            prefixFound = true;
            LOGGER.config("PROPERTY: " + name);
            p.put(name.substring(prefix.length()), system.getProperty(name));
        }
    }

    // If there is no connector configured by this name, bail early.
    if (!prefixFound) {
        throw new RepositoryException("No javatest." + prefix + "* properties specified for connector.");
    }

    return (LivelinkConnector) instance.makeConnector(p);
}