Example usage for java.util Scanner hasNext

List of usage examples for java.util Scanner hasNext

Introduction

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

Prototype

public boolean hasNext() 

Source Link

Document

Returns true if this scanner has another token in its input.

Usage

From source file:jp.go.nict.langrid.wrapper.ws_1_2.translation.AbstractTranslationService.java

/**
 * /*  w  w  w. j a v  a2s. com*/
 * 
 */
public final String multistatementTranslate(String sourceLang, String targetLang, String source,
        String delimiterRegx)
        throws AccessLimitExceededException, InvalidParameterException, LanguagePairNotUniquelyDecidedException,
        NoAccessPermissionException, NoValidEndpointsException, ProcessFailedException, ServerBusyException,
        ServiceNotActiveException, ServiceNotFoundException, UnsupportedLanguagePairException {
    checkStartupException();
    if (StringUtils.isBlank(delimiterRegx)) {
        throw new InvalidParameterException("delimiterRegx", "is Blank.");
    }
    StringBuilder sb = new StringBuilder();
    Scanner s = new Scanner(source).useDelimiter(delimiterRegx);
    int i = 0;
    while (s.hasNext()) {
        String text = s.next();
        MatchResult m = s.match();
        if (i != m.start()) {
            String tag = source.substring(i, m.start());
            sb.append(tag);
        }
        i = m.end();
        sb.append(invokeDoTranslation(sourceLang, targetLang, text));
    }
    if (source.length() != i) {
        String tag = source.substring(i);
        sb.append(tag);
    }

    return sb.toString();
}

From source file:no.sintef.jarfter.Jarfter.java

private String streamToString(InputStream input) {
    String encoding = "UTF-8";
    java.util.Scanner scanner = new java.util.Scanner(input).useDelimiter("\\A");
    String inputAsString = scanner.hasNext() ? scanner.next() : "";
    return inputAsString.replaceAll("[\uFEFF-\uFFFF]", "");
}

From source file:ddf.catalog.transformer.csv.common.CsvTransformerTest.java

@Test
public void writeSearchResultsToCsvWithAliasMap() throws CatalogTransformerException {
    List<AttributeDescriptor> requestedAttributes = new ArrayList<>();
    requestedAttributes.add(buildAttributeDescriptor("attribute1", BasicTypes.STRING_TYPE));

    Map<String, String> aliasMap = ImmutableMap.of("attribute1", "column1");

    Appendable csvText = CsvTransformer.writeMetacardsToCsv(metacardList, requestedAttributes, aliasMap);

    Scanner scanner = new Scanner(csvText.toString());
    scanner.useDelimiter(CSV_ITEM_SEPARATOR_REGEX);

    String[] expectedHeaders = { "column1" };
    validate(scanner, expectedHeaders);//from w w  w.  j  a  v  a  2s .  c  o m

    String[] expectedValues = { "", "value1" };

    for (int i = 0; i < METACARD_COUNT; i++) {
        validate(scanner, expectedValues);
    }

    // final new line causes an extra "" value at end of file
    assertThat(scanner.hasNext(), is(true));
    assertThat(scanner.next(), is(""));
    assertThat(scanner.hasNext(), is(false));
}

From source file:files.populate.java

void populate_users(Connection connection, String filename) {
    String fileName = "/Users/yash/Documents/workspace/HW 3/data/yelp_user.json";
    Path path = Paths.get(fileName);
    Scanner scanner = null;
    try {/* w w w.  ja v  a2  s  . c  o m*/
        scanner = new Scanner(path);

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

    //read file line by line
    scanner.useDelimiter("\n");
    int i = 0;
    while (scanner.hasNext()) {
        if (i < 500) {
            JSONParser parser = new JSONParser();
            String s = (String) scanner.next();
            JSONObject obj;
            try {
                obj = (JSONObject) parser.parse(s);
                Map votes = (Map) obj.get("votes");
                ArrayList friends;
                friends = (ArrayList) obj.get("friends");
                Map compliments = (Map) obj.get("compliments");
                //                    
                String query = "INSERT INTO YELP_USERS (user_id,yelping_since,name,fans,average_stars,type,review_count,VOTES_FUNNY,VOTES_COOL,VOTES_USEFUL) VALUES('"
                        + obj.get("user_id") + "','" + obj.get("yelping_since") + "','" + obj.get("name") + "',"
                        + obj.get("fans") + "," + obj.get("average_stars") + ",'" + obj.get("type") + "',"
                        + obj.get("review_count") + "," + votes.get("funny") + "," + votes.get("cool") + ","
                        + votes.get("useful") + ")";
                System.out.println(query);
                Statement statement = connection.createStatement();
                statement.executeUpdate(query);
                for (int j = 0; j < friends.size(); ++j) {
                    String q2 = "insert into users_friends values ('" + obj.get("user_id") + "','"
                            + friends.get(j) + "')";
                    System.out.println(q2);
                    statement.executeUpdate(q2);
                }
                Set keys = compliments.keySet();
                Object[] keys1 = keys.toArray();
                for (int j = 0; j < keys1.length; ++j) {
                    //                             String q2 = "insert into users_friends values ("+obj.get("user_id")+","+compliments.get(j)+")";
                    String thiskey = keys1[j].toString();
                    long val = (long) compliments.get(thiskey);
                    String q3 = "insert into users_compliments values ('" + obj.get("user_id") + "','" + thiskey
                            + "'," + val + ")";
                    System.out.println(q3);
                    statement.executeUpdate(q3);
                }
                statement.close();
            } catch (ParseException ex) {
                Logger.getLogger(populate.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SQLException ex) {
                Logger.getLogger(populate.class.getName()).log(Level.SEVERE, null, ex);
            }

            i++;
        } else
            break;
    }

    scanner.close();

}

From source file:com.inmobi.conduit.distcp.tools.TestIntegration.java

@Test
public void testJobConters() {
    try {//from   ww w.ja  v  a  2 s  .c  o m
        Path listFile = new Path("target/tmp1/listing").makeQualified(fs);
        addEntries(listFile, "*");
        createFileForAudit("/conduit/streams/test1/2013/10/10/10/10/file1.gz");
        runTest(listFile, target, true);
        int numberOfCountersPerFile = 0;
        long sumOfCounterValues = 0;
        FileStatus[] statuses = fs.listStatus(counterOutputPath, new PathFilter() {
            public boolean accept(Path path) {
                return path.toString().contains("part");
            }
        });
        for (FileStatus status : statuses) {
            Scanner scanner = new Scanner(fs.open(status.getPath()));
            while (scanner.hasNext()) {
                String counterNameValue = null;
                try {
                    counterNameValue = scanner.next();
                    String tmp[] = counterNameValue.split(ConduitConstants.AUDIT_COUNTER_NAME_DELIMITER);
                    Assert.assertEquals(4, tmp.length);
                    Long numOfMsgs = Long.parseLong(tmp[3]);
                    numberOfCountersPerFile++;
                    sumOfCounterValues += numOfMsgs;
                } catch (Exception e) {
                    LOG.error("Counters file has malformed line with counter name = " + counterNameValue
                            + " ..skipping the line ", e);
                }
            }
        }
        // should have 2 conters per file
        Assert.assertEquals(2, numberOfCountersPerFile);
        // sum of all counter values should equal to total number of messages
        Assert.assertEquals(3, sumOfCounterValues);
        checkResult(target, 1);
    } catch (IOException e) {
        LOG.error("Exception encountered while testing distcp", e);
        Assert.fail("distcp failure");
    } finally {
        TestDistCpUtils.delete(fs, root);
    }
}

From source file:com.laex.j2objc.ToObjectiveCDelegate.java

@Override
public boolean visit(final IResource resource) throws CoreException {
    // cancel the job
    if (monitor.isCanceled()) {
        onCancelled();/*from  w  w w.  j av a 2  s. co m*/
        resource.getProject().refreshLocal(IResource.DEPTH_INFINITE, monitor);
        monitor.done();
        return false;
    }

    if (!(resource.getType() == IResource.FILE)) {
        return true;
    }

    if (!JavaCore.isJavaLikeFileName(resource.getName())) {
        return true;
    }

    String sourcePath = resource.getLocation().makeAbsolute().toOSString();
    // As per the discussion with Tom Ball, the output of compilation is
    // stored in the project's root source folder
    // See
    // https://groups.google.com/forum/?fromgroups=#!topic/j2objc-discuss/lJGzN-pxmkQ
    String outputPath = resource.getProject().getFolder("src").getLocation().makeAbsolute().toOSString();

    try {
        String cmd = buildCommand(this.display, prefs, resource.getProject(), resource, sourcePath, outputPath);

        monitor.subTask(resource.getName());

        Process p = Runtime.getRuntime().exec(cmd);

        Scanner scanInput = new Scanner(p.getInputStream());
        Scanner scanErr = new Scanner(p.getErrorStream());

        MessageConsoleStream mct = MessageUtil.findConsole(MessageUtil.J2OBJC_CONSOLE).newMessageStream();

        mct.write(cmd);
        mct.write(MessageUtil.NEW_LINE_CONSTANT);

        while (scanInput.hasNext()) {
            MessageUtil.resetConsoleColor(display, mct);
            mct.write(scanInput.nextLine());
            mct.write(MessageUtil.NEW_LINE_CONSTANT);
        }

        while (scanErr.hasNext()) {
            MessageUtil.setConsoleColor(display, mct, SWT.COLOR_RED);
            mct.write(scanErr.nextLine());
            mct.write(MessageUtil.NEW_LINE_CONSTANT);
        }

        mct.write(MessageUtil.NEW_LINE_CONSTANT);

    } catch (IOException e) {
        LogUtil.logException(e);
    }

    monitor.worked(1);

    return true;
}

From source file:ddf.catalog.transformer.csv.common.CsvTransformerTest.java

@Test
public void writeSearchResultsToCsv() throws CatalogTransformerException {
    List<AttributeDescriptor> requestedAttributes = new ArrayList<>();
    requestedAttributes.add(buildAttributeDescriptor("attribute1", BasicTypes.STRING_TYPE));

    Appendable csvText = CsvTransformer.writeMetacardsToCsv(metacardList, requestedAttributes,
            Collections.emptyMap());

    Scanner scanner = new Scanner(csvText.toString());
    scanner.useDelimiter(CSV_ITEM_SEPARATOR_REGEX);

    String[] expectedHeaders = { "attribute1" };
    validate(scanner, expectedHeaders);//from w  w  w  . j  a va  2 s  . com

    String[] expectedValues = { "", "value1" };

    for (int i = 0; i < METACARD_COUNT; i++) {
        validate(scanner, expectedValues);
    }

    // final new line causes an extra "" value at end of file
    assertThat(scanner.hasNext(), is(true));
    assertThat(scanner.next(), is(""));
    assertThat(scanner.hasNext(), is(false));
}

From source file:openscim.restful.server.resources.group.ldap.LdapGroupResource.java

@Override
public Response createGroup(UriInfo uriInfo, Group group) {
    // check the ldap template has been setup correctly
    if (ldapTemplate != null) {
        // create the mapper if it doesn't already exists
        if (mapper == null)
            mapper = new GroupAttributesMapper(properties);

        // build the group dn
        String dn = group.getId();
        if (properties
                .getProperty(GroupAttributesMapper.CONCEAL_GROUP_DNS,
                        GroupAttributesMapper.DEFAULT_CONCEAL_GROUP_DNS)
                .equalsIgnoreCase(GroupAttributesMapper.DEFAULT_CONCEAL_GROUP_DNS)) {
            // utilise ldap formated dn
            dn = properties.getProperty(GroupAttributesMapper.GID_ATTRIBUTE,
                    GroupAttributesMapper.DEFAULT_GID_ATTRIBUTE) + "=" + group.getId() + ","
                    + properties.getProperty(GroupAttributesMapper.GROUP_BASEDN,
                            GroupAttributesMapper.DEFAULT_GROUP_BASEDN);
        }//from w  w  w .  j  a  v a 2 s.  co  m

        try {
            try {
                // retrieve the group
                Group lookedGroup = (Group) ldapTemplate.lookup(dn, mapper);

                // check if the group was found
                if (lookedGroup != null) {
                    // user already exists            
                    return ResourceUtilities.buildErrorResponse(HttpStatus.CONFLICT,
                            HttpStatus.CONFLICT.getMessage() + ": Resource " + dn + " already exists");
                }
            } catch (Exception nException) {
                // group not found, do nothing
            }

            Attributes groupAttributes = new BasicAttributes();

            // get the objectclasses
            String objectclasses = properties.getProperty(GroupAttributesMapper.GROUP_OBJECTCLASS_ATTRIBUTE,
                    GroupAttributesMapper.DEFAULT_GROUP_OBJECTCLASS_ATTRIBUTE);

            // set the objectclass of the group
            Scanner scanner = new Scanner(objectclasses);
            scanner.useDelimiter(",");
            while (scanner.hasNext()) {
                groupAttributes.put("objectclass", scanner.next());
            }

            // set the gid
            String gidAtttributeName = properties.getProperty(GroupAttributesMapper.GID_ATTRIBUTE,
                    GroupAttributesMapper.DEFAULT_GID_ATTRIBUTE);
            groupAttributes.put(gidAtttributeName, group.getId());

            // get the member attribute name
            String memberAtttributeName = properties.getProperty(GroupAttributesMapper.MEMBER_ATTRIBUTE,
                    GroupAttributesMapper.DEFAULT_MEMBER_ATTRIBUTE);

            // set the members
            Attribute memberAttribute = new BasicAttribute(memberAtttributeName);
            if (group.getAny() instanceof List) {
                List members = (List) group.getAny();
                for (Object object : members) {
                    if (object instanceof PluralAttribute) {
                        PluralAttribute member = (PluralAttribute) object;
                        String uid = member.getValue();

                        // build the user dn
                        String userdn = uid;
                        if (properties.getProperty(UserAttributesMapper.CONCEAL_ACCOUNT_DNS, "true")
                                .equalsIgnoreCase("true")) {
                            // utilise ldap formated dn
                            userdn = properties.getProperty(UserAttributesMapper.UID_ATTRIBUTE,
                                    UserAttributesMapper.DEFAULT_UID_ATTRIBUTE) + "=" + uid + ","
                                    + properties.getProperty(UserAttributesMapper.ACCOUNT_BASEDN,
                                            UserAttributesMapper.DEFAULT_ACCOUNT_BASEDN);
                        }

                        memberAttribute.add(userdn);
                    }
                }
            }
            groupAttributes.put(memberAttribute);

            // create the group
            ldapTemplate.bind(dn, null, groupAttributes);

            // determine the url of the new resource
            URI location = new URI("/Group/" + dn);
            if (properties
                    .getProperty(GroupAttributesMapper.CONCEAL_GROUP_DNS,
                            GroupAttributesMapper.DEFAULT_CONCEAL_GROUP_DNS)
                    .equalsIgnoreCase(GroupAttributesMapper.DEFAULT_CONCEAL_GROUP_DNS)) {
                location = new URI("/User/" + group.getId());
            }

            // group stored successfully, return the group            
            return Response.created(location).entity(group).build();
        } catch (URISyntaxException usException) {
            // problem generating entity location
            logger.error("problem generating entity location");

            // return a server error
            return ResourceUtilities.buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR,
                    HttpStatus.NOT_IMPLEMENTED.getMessage()
                            + ": Service Provider problem generating entity location");
        } catch (Exception nException) {
            // problem creating group
            logger.error("problem creating group");

            // return a server error
            return ResourceUtilities.buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR,
                    HttpStatus.NOT_IMPLEMENTED.getMessage() + ": Service Provider problem creating group");
        }
    } else {
        // ldap not configured
        logger.error("ldap not configured");

        // return a server error
        return ResourceUtilities.buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR,
                HttpStatus.NOT_IMPLEMENTED.getMessage()
                        + ": Service Provider group ldap repository not configured");
    }
}

From source file:no.sintef.jarfter.Jarfter.java

private boolean fileContains(String filename, String searchString) {
    File file = new File(filename);
    try {/*from w w w.j av a 2  s.c om*/
        FileInputStream fileStream = new FileInputStream(file);
        java.util.Scanner scanner = new java.util.Scanner(fileStream).useDelimiter("\\A");
        String fileAsString = scanner.hasNext() ? scanner.next() : "";
        return fileAsString.contains(searchString);
    } catch (FileNotFoundException fnfe) {
        throw new JarfterException(JarfterException.Error.IO_NO_TEMP_FILE);
    }
}

From source file:com.aurel.track.DBScriptTest.java

/**
 * Run an SQL script//from w  ww.j a v  a 2  s .c  o m
 * @param script
 */
private void runSQLScript(String scriptToRunWithPath, Connection cono) {
    int line = 0;
    try {
        cono.setAutoCommit(false);
        Statement ostmt = cono.createStatement();
        InputStream in = new FileInputStream(scriptToRunWithPath);//populateURL.openStream();
        java.util.Scanner s = new java.util.Scanner(in, "UTF-8").useDelimiter(";");
        String st = null;
        StringBuffer stb = new StringBuffer();
        System.out.println("Running SQL script " + scriptToRunWithPath);
        while (s.hasNext()) {
            stb.append(s.nextLine().trim());
            st = stb.toString();
            ++line;
            if (!st.isEmpty() && !st.startsWith("--") && !st.startsWith("/*") && !st.startsWith("#")) {
                if (st.trim().equalsIgnoreCase("go")) {
                    try {
                        cono.commit();
                    } catch (Exception ex) {
                        System.err.println(org.apache.commons.lang3.exception.ExceptionUtils.getStackTrace(ex));
                    }
                    stb = new StringBuffer();
                } else {
                    if (st.endsWith(";")) {
                        stb = new StringBuffer(); // clear buffer
                        st = st.substring(0, st.length() - 1); // remove the semicolon
                        try {
                            if ("commit".equals(st.trim().toLowerCase())
                                    || "go".equals(st.trim().toLowerCase())) {
                                cono.commit();
                            } else {
                                ostmt.executeUpdate(st);
                                // LOGGER.info(st);
                            }
                        } catch (Exception exc) {
                            if (!(scriptToRunWithPath.contains("Derby")
                                    && exc.getMessage().contains("DROP TABLE")
                                    && exc.getMessage().contains("not exist"))) {
                                System.err.println("Problem executing DDL statements: " + exc.getMessage());
                                System.err.println("Line " + line + ": " + st);
                            }
                        }
                    } else {
                        stb.append(" ");
                    }
                }
            } else {
                stb = new StringBuffer();
            }
        }
        in.close();
        cono.commit();
        cono.setAutoCommit(true);

    } catch (Exception e) {
        System.err.println("Problem upgrading database schema in line " + line + " of file "
                + scriptToRunWithPath + "  " + e);
    }
}