Example usage for java.util Scanner useDelimiter

List of usage examples for java.util Scanner useDelimiter

Introduction

In this page you can find the example usage for java.util Scanner useDelimiter.

Prototype

public Scanner useDelimiter(String pattern) 

Source Link

Document

Sets this scanner's delimiting pattern to a pattern constructed from the specified String .

Usage

From source file:net.emotivecloud.scheduler.drp4one.DRP4OVF.java

private String VMDescriptionNetwork(EmotiveOVF ovf) {

    StringBuilder buf = new StringBuilder();
    String tmp;//from  ww w . ja va2 s  .  c om

    // check for network attributes, if not available check the properties
    // file

    System.out.println("DRP4ONE - VMDescriptionNetwork()> ovf.getNetworks(): " + ovf.getNetworks());

    if (!ovf.getNetworks().isEmpty()) {

        //            added by smendoza
        //            String netSection = String.format("NIC = [ NETWORK_ID = %s ] \n",props.getProperty("net.id"));
        //            buf.append(netSection);

        // Network attributes
        netConnectNames = ovf4oneProperties(NET_CONNECTION_NAMES, false);
        for (OVFNetwork ovfNetwork : ovf.getNetworks().values()) {
            buf.append("NIC = [\n");
            String nicName = ovfNetwork.getConnectionName();

            // I am working on a new array, so I need to reset the separator
            String separator = "";

            if (nicName == null || "".equals(nicName)) {
                // We supply IP and MAC for this NIC
                tmp = ovfNetwork.getIp();
                if (tmp != null) {
                    buf.append("IP = \"");
                    buf.append(tmp);
                    buf.append("\"");
                    separator = ",\n";
                }

                tmp = ovfNetwork.getMac();
                if (tmp != null) {
                    buf.append(separator);
                    buf.append("MAC = \"");
                    buf.append(tmp);
                    buf.append("\"");
                }

            } else {
                // We ask OpenNebula to assign us IP and MAC
                System.out.println("DRP4ONE - VMDescriptionNetwork()> nicName: " + nicName);

                buf.append("NETWORK = \"");
                buf.append(nicName);
                buf.append("\" ");
            }
            buf.append("\n");
            nicNames.add(nicName);
            buf.append("]\n");
            System.out.println("DRP4ONE - VMDescriptionNetwork()> buf: " + buf);

            nicName = "";

        }

        log.debug("adding the networks configured in props..");
        log.debug("networks configured:" + netConnectNames);
        Scanner commaSperated = new Scanner(netConnectNames);
        commaSperated.useDelimiter(",");

        while (commaSperated.hasNext()) {
            String connectionName = commaSperated.next().trim();
            log.debug("connection name " + connectionName);
            Iterator<String> nicNamesIter = nicNames.iterator();
            while (nicNamesIter.hasNext()) {
                log.debug("nic names config in input:" + nicNamesIter.next());
            }
            if (!nicNames.contains(connectionName)) {
                //addNic(buf, connectionName);
            }
        }
    } else {
        log.debug("no network configured in ovf input, hence checking the network config from properties file");
        log.debug("networks configured:" + netConnectNames);
        Scanner commaSperated = new Scanner(netConnectNames);
        commaSperated.useDelimiter(",");

        while (commaSperated.hasNext()) {
            String connectionName = commaSperated.next().trim();
            // addNic(buf, connectionName);
            // buf.append("NIC = [\n");buf.append("NETWORK = \"");
            // buf.append(connectionName); buf.append("\"");
            // buf.append("]\n\n\n");
        }
    }

    return buf.toString();
}

From source file:net.emotivecloud.scheduler.drp4one.DRP4OVF.java

private String VMDescriptionContext(EmotiveOVF ovf) {
    StringBuilder buf = new StringBuilder();
    // Context - requires again network attributes
    ///*  www.  j a v a2 s . c om*/
    // We list devices in order eth0, eth1, eth2... The context script will
    // get
    // them out and prepare the network configuration files.

    buf.append("CONTEXT = [\n");
    String separator = "";
    //        buf.append("hostname = \"optimis2.leeds\",");

    int ethNumber = 0;
    if (!ovf.getNetworks().isEmpty()) {
        for (OVFNetwork ovfNetwork : ovf.getNetworks().values()) {
            String nicName = ovfNetwork.getConnectionName();

            if (nicName == null || "".equals(nicName)) {
                String tmp = ovfNetwork.getIp();
                if (tmp != null) {
                    buf.append(separator);
                    buf.append("IP_");
                    buf.append(ethNumber);
                    buf.append(" = \"");
                    buf.append(tmp);
                    buf.append("\"\n");
                    separator = ",\n";
                }

                tmp = ovfNetwork.getMac();
                if (tmp != null) {
                    buf.append(separator);
                    buf.append("MAC_");
                    buf.append(ethNumber);
                    buf.append(" = \"");
                    buf.append(tmp);
                    buf.append("\"\n");
                    separator = ",\n";

                }

            } else {
                addNetworkConfigToContext(buf, separator, ethNumber, nicName);
            }
            ethNumber++;
        }

        log.debug("adding the networks configured in props..");
        log.debug("networks configured:" + netConnectNames);
        Scanner commaSperated = new Scanner(netConnectNames);
        commaSperated.useDelimiter(",");

        while (commaSperated.hasNext()) {
            String connectionName = commaSperated.next().trim();
            if (!nicNames.contains(connectionName)) {
                addNetworkConfigToContext(buf, separator, ethNumber, connectionName);
                log.debug("adding network config:" + connectionName);
                ethNumber++;
            }
        }
    } else {

        log.debug("no network configured in ovf input, hence checking the network config from properties file");
        log.debug("networks configured:" + netConnectNames);
        Scanner commaSperated = new Scanner(netConnectNames);
        commaSperated.useDelimiter(",");

        while (commaSperated.hasNext()) {
            String connectionName = commaSperated.next().trim();
            addNetworkConfigToContext(buf, separator, ethNumber, connectionName);
            ethNumber++;
            // buf.append("NIC = [\n");buf.append("NETWORK = \"");
            // buf.append(connectionName); buf.append("\"");
            // buf.append("]\n\n\n");
        }

    }

    Iterator<String> home_iter = arg1.iterator();
    Iterator<String> swap_iter = arg2.iterator();
    buf.append("HOME = \"");
    while (home_iter.hasNext()) {

        buf.append(home_iter.next());
        buf.append(" ");

    }

    buf.append("\",\n");
    buf.append("SWAP = \"");
    while (swap_iter.hasNext()) {

        buf.append(swap_iter.next());
        buf.append(" ");
    }
    buf.append("\"\n");
    //        buf.append("FILES=" + "\"" + scriptlocation + "/init.sh \"");
    buf.append("]\n");
    return buf.toString();
}

From source file:Model.MultiPlatformLDA.java

public void readData() {
    Scanner sc = null;
    BufferedReader br = null;//from  w ww . j  a  va  2s.c  o  m
    String line = null;
    HashMap<String, Integer> userId2Index = null;
    HashMap<Integer, String> userIndex2Id = null;

    try {
        String folderName = dataPath + "/users";
        File postFolder = new File(folderName);

        // Read number of users
        int nUser = postFolder.listFiles().length;
        users = new User[nUser];
        userId2Index = new HashMap<String, Integer>(nUser);
        userIndex2Id = new HashMap<Integer, String>(nUser);
        int u = -1;

        // Read the posts from each user file
        for (File postFile : postFolder.listFiles()) {
            u++;
            users[u] = new User();

            // Read index of the user
            String userId = FilenameUtils.removeExtension(postFile.getName());
            userId2Index.put(userId, u);
            userIndex2Id.put(u, userId);
            users[u].userID = userId;

            // Read the number of posts from user
            int nPost = 0;
            br = new BufferedReader(new FileReader(postFile.getAbsolutePath()));
            while (br.readLine() != null) {
                nPost++;
            }
            br.close();

            // Declare the number of posts from user
            users[u].posts = new Post[nPost];

            // Read each of the post
            br = new BufferedReader(new FileReader(postFile.getAbsolutePath()));
            int j = -1;
            while ((line = br.readLine()) != null) {
                j++;
                users[u].posts[j] = new Post();

                sc = new Scanner(line.toString());
                sc.useDelimiter(",");
                while (sc.hasNext()) {
                    users[u].posts[j].postID = sc.next();
                    users[u].posts[j].platform = sc.nextInt();
                    users[u].posts[j].batch = sc.nextInt();

                    // Read the words in each post
                    String[] tokens = sc.next().toString().split(" ");
                    users[u].posts[j].words = new int[tokens.length];
                    for (int i = 0; i < tokens.length; i++) {
                        users[u].posts[j].words[i] = Integer.parseInt(tokens[i]);
                    }
                }
            }
            br.close();
        }

        // Read post vocabulary
        String vocabularyFileName = dataPath + "/vocabulary.csv";

        br = new BufferedReader(new FileReader(vocabularyFileName));
        int nPostWord = 0;
        while (br.readLine() != null) {
            nPostWord++;
        }
        br.close();
        vocabulary = new String[nPostWord];

        br = new BufferedReader(new FileReader(vocabularyFileName));
        while ((line = br.readLine()) != null) {
            String[] tokens = line.split(",");
            int index = Integer.parseInt(tokens[0]);
            vocabulary[index] = tokens[1];
        }
        br.close();

    } catch (Exception e) {
        System.out.println("Error in reading post from file!");
        e.printStackTrace();
        System.exit(0);
    }
}

From source file:com.ikanow.infinit.e.harvest.enrichment.custom.UnstructuredAnalysisHarvester.java

public void getRawTextFromUrlIfNeeded(DocumentPojo doc, SourceRssConfigPojo feedConfig) throws IOException {
    if (null != doc.getFullText()) { // Nothing to do
        return;/* ww w .j av a  2  s .co  m*/
    }
    Scanner s = null;
    OutputStreamWriter wr = null;
    try {
        URL url = new URL(doc.getUrl());
        URLConnection urlConnect = null;
        String postContent = null;
        if (null != feedConfig) {
            urlConnect = url.openConnection(ProxyManager.getProxy(url, feedConfig.getProxyOverride()));
            if (null != feedConfig.getUserAgent()) {
                urlConnect.setRequestProperty("User-Agent", feedConfig.getUserAgent());
            } // TESTED (by hand)
            if (null != feedConfig.getHttpFields()) {
                for (Map.Entry<String, String> httpFieldPair : feedConfig.getHttpFields().entrySet()) {
                    if (httpFieldPair.getKey().equalsIgnoreCase("content")) {
                        postContent = httpFieldPair.getValue();
                        urlConnect.setDoInput(true);
                        urlConnect.setDoOutput(true);
                    } else {
                        urlConnect.setRequestProperty(httpFieldPair.getKey(), httpFieldPair.getValue());
                    }
                }
            } //TESTED (by hand)
        } else {
            urlConnect = url.openConnection();
        }
        InputStream urlStream = null;
        try {
            securityManager.setSecureFlag(true); // (disallow file/local URL access)
            if (null != postContent) {
                wr = new OutputStreamWriter(urlConnect.getOutputStream());
                wr.write(postContent.toCharArray());
                wr.flush();
            } //TESTED
            urlStream = urlConnect.getInputStream();
        } catch (SecurityException se) {
            throw se;
        } catch (Exception e) { // Try one more time, this time exception out all the way
            securityManager.setSecureFlag(false); // (some file stuff - so need to re-enable)
            if (null != feedConfig) {
                urlConnect = url.openConnection(ProxyManager.getProxy(url, feedConfig.getProxyOverride()));
                if (null != feedConfig.getUserAgent()) {
                    urlConnect.setRequestProperty("User-Agent", feedConfig.getUserAgent());
                } // TESTED
                if (null != feedConfig.getHttpFields()) {
                    for (Map.Entry<String, String> httpFieldPair : feedConfig.getHttpFields().entrySet()) {
                        if (httpFieldPair.getKey().equalsIgnoreCase("content")) {
                            urlConnect.setDoInput(true); // (need to do this again)
                            urlConnect.setDoOutput(true);
                        } else {
                            urlConnect.setRequestProperty(httpFieldPair.getKey(), httpFieldPair.getValue());
                        }
                    }
                } //TESTED
            } else {
                urlConnect = url.openConnection();
            }
            securityManager.setSecureFlag(true); // (disallow file/local URL access)
            if (null != postContent) {
                wr = new OutputStreamWriter(urlConnect.getOutputStream());
                wr.write(postContent.toCharArray());
                wr.flush();
            } //TESTED
            urlStream = urlConnect.getInputStream();
        } finally {
            securityManager.setSecureFlag(false); // (turn security check for local URL/file access off)
        }
        // Grab any interesting header fields
        Map<String, List<String>> headers = urlConnect.getHeaderFields();
        BasicDBObject metadataHeaderObj = null;
        for (Map.Entry<String, List<String>> it : headers.entrySet()) {
            if (null != it.getKey()) {
                if (it.getKey().startsWith("X-") || it.getKey().startsWith("Set-")
                        || it.getKey().startsWith("Location")) {
                    if (null == metadataHeaderObj) {
                        metadataHeaderObj = new BasicDBObject();
                    }
                    metadataHeaderObj.put(it.getKey(), it.getValue());
                }
            }
        } //TESTED
          // Grab the response code
        try {
            HttpURLConnection httpUrlConnect = (HttpURLConnection) urlConnect;
            int responseCode = httpUrlConnect.getResponseCode();
            if (200 != responseCode) {
                if (null == metadataHeaderObj) {
                    metadataHeaderObj = new BasicDBObject();
                }
                metadataHeaderObj.put("responseCode", String.valueOf(responseCode));
            }
        } //TESTED
        catch (Exception e) {
        } // interesting, not an HTTP connect ... shrug and carry on
        if (null != metadataHeaderObj) {
            doc.addToMetadata("__FEED_METADATA__", metadataHeaderObj);
        } //TESTED
        s = new Scanner(urlStream, "UTF-8");
        doc.setFullText(s.useDelimiter("\\A").next());
    } catch (MalformedURLException me) { // This one is worthy of a more useful error message
        throw new MalformedURLException(me.getMessage()
                + ": Likely because the document has no full text (eg JSON) and you are calling a contentMetadata block without setting flags:'m' or 'd'");
    } finally { //(release resources)
        if (null != s) {
            s.close();
        }
        if (null != wr) {
            wr.close();
        }
    }

}

From source file:net.siveo.virtualization.vmware.Main.java

public String processLine(String aLine) {
    Scanner scanner = new Scanner(aLine);
    scanner.useDelimiter("=");

    String parameter = null;/*from  www  . ja v a2s.  co m*/
    String value = null;
    String txt = "";

    if (scanner.hasNext()) {
        parameter = scanner.next().trim();
        value = scanner.next().trim();

        if (parameter.equalsIgnoreCase("numvcpus") || parameter.equalsIgnoreCase("sched.cpu.min")
                || parameter.equalsIgnoreCase("sched.cpu.max") || parameter.equalsIgnoreCase("memSize")
                || parameter.equalsIgnoreCase("sched.mem.min") || parameter.equalsIgnoreCase("sched.mem.max")) {
            txt = txt + parameter + "=" + value + "\n";
        }

    }

    return txt;

}