Example usage for java.util StringTokenizer countTokens

List of usage examples for java.util StringTokenizer countTokens

Introduction

In this page you can find the example usage for java.util StringTokenizer countTokens.

Prototype

public int countTokens() 

Source Link

Document

Calculates the number of times that this tokenizer's nextToken method can be called before it generates an exception.

Usage

From source file:org.sonar.server.charts.deprecated.CustomBarChart.java

private void addMeasures(String values) {
    if (values != null && values.length() > 0) {
        // Values
        StringTokenizer stValues = new StringTokenizer(values, ",");
        int nbValues = stValues.countTokens();

        // Categories
        String categoriesParam = params.get(CHART_PARAM_CATEGORIES);
        String[] categoriesSplit = null;
        if (categoriesParam != null && categoriesParam.length() > 0) {
            categoriesSplit = categoriesParam.split(",");
        } else {// www.ja  v  a 2s .co  m
            categoriesSplit = new String[nbValues];
            for (int i = 0; i < nbValues; i++) {
                categoriesSplit[i] = DEFAULT_NAME_CATEGORY + i;
            }
        }

        // Series
        String[] seriesSplit = { DEFAULT_NAME_SERIE };
        int nbSeries = 1;

        for (String currentCategory : categoriesSplit) {
            for (int iSeries = 0; iSeries < nbSeries; iSeries++) {
                String currentSerie = seriesSplit[iSeries];
                double currentValue = 0.0;
                if (stValues.hasMoreTokens()) {
                    try {
                        currentValue = Double.parseDouble(stValues.nextToken());
                    } catch (NumberFormatException e) {
                        // ignore
                    }
                }
                dataset.addValue(currentValue, currentSerie, currentCategory);
            }
        }
    }
}

From source file:com.wso2telco.core.authfilter.impl.authorization.BasicAuthenticationFilter.java

@Override
public boolean isAuthenticated(ContainerRequestContext requestContext, Method method,
        String authorizationHeader) {

    String password = null;/*from ww  w .j  a va2s . c  o m*/
    boolean isAuthenticated = false;

    // get base 64 encoded username and password
    final String encodedUserPassword = authorizationHeader
            .replaceFirst(AuthFilterParam.AUTHENTICATION_SCHEME_BASIC.getTObject() + " ", "");

    log.debug("base64 encoded username and password : " + encodedUserPassword);

    if (encodedUserPassword != null && encodedUserPassword.trim().length() > 0) {

        // decode username and password
        String usernameAndPassword = new String(Base64.decodeBase64(encodedUserPassword.getBytes()));

        // split username and password by :
        final StringTokenizer tokenizer = new StringTokenizer(usernameAndPassword, ":");

        if (tokenizer.countTokens() > 1) {

            userName = tokenizer.nextToken();
            password = tokenizer.nextToken();

            log.debug("username : " + userName);
            log.debug("password : " + password);

            // validate user authentication
            isAuthenticated = userAuthentication.isAuthenticatedUser(userName, password);

            if (!isAuthenticated) {

                requestContext.abortWith(accessDenied);
                return false;
            }
        } else {

            requestContext.abortWith(accessDenied);
            return false;
        }
    } else {

        requestContext.abortWith(accessDenied);
        return false;
    }

    return true;
}

From source file:com.sfs.whichdoctor.beans.IsbPayloadBean.java

/**
 * Gets the formatted xml payload header.
 *
 * @return the formatted xml payload header
 *//*from   w  w w .  j av  a 2  s.c om*/
public final String getFormattedXmlPayloadHeader() {
    final String htmlPayload = getTabbedXmlPayload();

    StringBuffer htmlHeader = new StringBuffer();

    StringTokenizer tokenizer = new StringTokenizer(htmlPayload, "\n");
    if (tokenizer.countTokens() < this.headerCount) {
        this.headerCount = tokenizer.countTokens();
    }
    for (int i = 0; i < this.headerCount; i++) {
        htmlHeader.append(tokenizer.nextToken());
        htmlHeader.append("<br/>");
    }
    return htmlHeader.toString();
}

From source file:com.canoo.webtest.plugins.emailtest.EmailMessageStructureFilter.java

private void prepareHeaders() {
    final StringTokenizer tokens = new StringTokenizer(getHeaders(), " ,");
    fTokenizedHeaders = new String[tokens.countTokens()];
    for (int i = 0; i < fTokenizedHeaders.length; i++) {
        fTokenizedHeaders[i] = tokens.nextToken();
    }/*ww  w .j  a  v a  2  s.  c  o m*/
}

From source file:com.quui.chat.Preprocessor.java

/**
 * Tokenizes the user input, then every word thats not in the stopwords-list
 * is stemmed and these are returned./* w  w  w . j a  va 2s  .co  m*/
 * @param s The input to answer to.
 * @return Returns those words (stemmed) that will be processed
 */
public Vector<String> preProcess(String s) {
    if (s == null) {
        throw new NullPointerException("Word to process is null.");
    }
    StringTokenizer tokenizer = new StringTokenizer(s, " .!?,;:^\"$%&/\\()[]#'+*<>|\t-");
    String[] tokens = new String[tokenizer.countTokens()];
    int i = 0;
    while (tokenizer.hasMoreElements()) {
        tokens[i] = tokenizer.nextToken();
        i++;
    }
    Vector<String> result = new Vector<String>();
    for (String element : tokens) {
        String firstStem = element;
        if (this.isWordNetEnabled) {
            firstStem = WNLookup.getStaticStem(element);
            if (firstStem.equals("")) {
                firstStem = element;
            }
        }
        if (!this.stopwords.contains(firstStem) && firstStem.trim().length() > 1) {
            if (firstStem.trim().equals("")) {
                throw new NullPointerException("Empty token!");
            }
            result.add(firstStem);
        }
    }
    return result;
}

From source file:hudson.plugins.simpleupdatesite.VersionNumber.java

/**
 * Parses a string like "1.0.2" into the version number.
 * /*from  w  w w  .  j a  v a 2s  . c  o m*/
 * @throws IllegalArgumentException
 *             if the parsing fails.
 */
public VersionNumber(String num) {
    StringTokenizer tokens = new StringTokenizer(num, ".-");
    this.digits = new int[tokens.countTokens()];
    if (this.digits.length < 2) {
        throw new IllegalArgumentException("Failed to parse " + num + " as version number");
    }

    int i = 0;
    while (tokens.hasMoreTokens()) {
        String token = tokens.nextToken().toLowerCase();
        if (token.equals("*")) {
            this.digits[i++] = 1000;
        } else if (token.startsWith("snapshot")) {
            this.digits[i - 1]--;
            this.digits[i++] = 1000;
            break;
        } else if (token.startsWith("ea")) {
            if (token.length() == 2) {
                this.digits[i++] = -1000; // just "ea"
            } else {
                this.digits[i++] = -1000 + Integer.parseInt(token.substring(2)); // "eaNNN"
            }
        } else {
            if (NumberUtils.isNumber(token)) {
                this.digits[i++] = Integer.parseInt(token);
            }
        }
    }
}

From source file:edu.uci.ics.jung.io.MatrixFile.java

private DoubleMatrix2D createMatrixFromFile(BufferedReader reader) throws IOException {
    List<List<Double>> rows = new ArrayList<List<Double>>();
    String currentLine = null;//from w  ww. j av  a  2 s  .co  m
    while ((currentLine = reader.readLine()) != null) {
        StringTokenizer tokenizer = new StringTokenizer(currentLine);
        if (tokenizer.countTokens() == 0) {
            break;
        }
        List<Double> currentRow = new ArrayList<Double>();
        while (tokenizer.hasMoreTokens()) {
            String token = tokenizer.nextToken();
            currentRow.add(Double.parseDouble(token));
        }
        rows.add(currentRow);
    }
    int size = rows.size();
    DoubleMatrix2D matrix = new SparseDoubleMatrix2D(size, size);
    for (int i = 0; i < size; i++) {
        List<Double> currentRow = rows.get(i);
        if (currentRow.size() != size) {
            throw new IllegalArgumentException("Matrix must have the same number of rows as columns");
        }
        for (int j = 0; j < size; j++) {
            double currentVal = currentRow.get(j);
            if (currentVal != 0) {
                matrix.setQuick(i, j, currentVal);
            }
        }
    }
    return matrix;
}

From source file:edu.umass.cs.msocket.proxy.console.commands.ApproveGuid.java

/**
 * @see edu.umass.cs.msocket.proxy.console.commands.ConsoleCommand#parse(java.lang.String)
 *///from   w  ww  . j a va2 s. c  om
@Override
public void parse(String commandText) throws Exception {
    String guid = null;
    try {
        StringTokenizer st = new StringTokenizer(commandText);
        if (st.countTokens() != 1) {
            console.printString("Bad number of arguments (expected 1 instead of " + st.countTokens() + ")\n");
            return;
        }

        final GuidEntry groupGuid = module.getProxyGroupGuid();
        if (groupGuid == null) {
            console.printString(
                    "Not connected to a proxy group. Use proxy_group_connect or help for instructions.\n");
            return;
        }

        guid = st.nextToken();

        UniversalGnsClient gnsClient = module.getGnsClient();
        gnsClient.groupAddGuid(groupGuid.getGuid(), guid, groupGuid);

        /*
         * Check what kind of service this GUID represents and put it in the
         * appropriate list
         */
        String service = gnsClient.fieldRead(guid, GnsConstants.SERVICE_TYPE_FIELD, groupGuid).getString(0);
        if (GnsConstants.PROXY_SERVICE.equals(service)) {
            console.printString("Granting access to proxy " + guid
                    + " and moving it to the inactive proxy list. Make sure a watchdog is running to detect its activity.\n");
            gnsClient.fieldAppend(module.getProxyGroupGuid().getGuid(), GnsConstants.INACTIVE_PROXY_FIELD,
                    new JSONArray().put(guid), groupGuid);
        } else if (GnsConstants.LOCATION_SERVICE.equals(service)) {
            console.printString("Granting access to location service " + guid
                    + " and moving it to the inactive service list. Make sure a watchdog is running to detect its activity.\n");
            gnsClient.fieldAppend(groupGuid.getGuid(), GnsConstants.INACTIVE_LOCATION_FIELD,
                    new JSONArray().put(guid), groupGuid);
            gnsClient.aclAdd(AccessType.READ_WHITELIST, groupGuid, GnsConstants.ACTIVE_PROXY_FIELD, guid);
        } else if (GnsConstants.WATCHDOG_SERVICE.equals(service)) {
            console.printString("Granting access to watchdog service " + guid
                    + " and moving it to the inactive wachdog list. Make sure another watchdog is running to detect its activity.\n");
            gnsClient.fieldAppend(groupGuid.getGuid(), GnsConstants.INACTIVE_WATCHDOG_FIELD,
                    new JSONArray().put(guid), groupGuid);
            // Open lists in read/write for the watchdog so that it can manipulate
            // lists
            setReadWriteAccess(guid, groupGuid, gnsClient, GnsConstants.ACTIVE_PROXY_FIELD);
            setReadWriteAccess(guid, groupGuid, gnsClient, GnsConstants.SUSPICIOUS_PROXY_FIELD);
            setReadWriteAccess(guid, groupGuid, gnsClient, GnsConstants.INACTIVE_PROXY_FIELD);
            setReadWriteAccess(guid, groupGuid, gnsClient, GnsConstants.ACTIVE_LOCATION_FIELD);
            setReadWriteAccess(guid, groupGuid, gnsClient, GnsConstants.SUSPICIOUS_LOCATION_FIELD);
            setReadWriteAccess(guid, groupGuid, gnsClient, GnsConstants.INACTIVE_LOCATION_FIELD);
            setReadWriteAccess(guid, groupGuid, gnsClient, GnsConstants.ACTIVE_WATCHDOG_FIELD);
            setReadWriteAccess(guid, groupGuid, gnsClient, GnsConstants.SUSPICIOUS_WATCHDOG_FIELD);
            setReadWriteAccess(guid, groupGuid, gnsClient, GnsConstants.INACTIVE_WATCHDOG_FIELD);
        }

    } catch (Exception e) {
        console.printString(
                "Failed to grant permission to join the proxy group to GUID" + guid + " ( " + e + ")\n");
        e.printStackTrace();
    }
}

From source file:net.sf.jasperreports.data.hibernate.spring.SpringHibernateDataAdapterService.java

@Override
public void contributeParameters(Map<String, Object> parameters) throws JRException {
    SpringHibernateDataAdapter hbmDA = getHibernateDataAdapter();
    if (hbmDA != null) {
        try {/*  w w w. j  a v a 2s  .  c  o  m*/
            Class<?> clazz = JRClassLoader
                    .loadClassForRealName("org.springframework.context.support.ClassPathXmlApplicationContext");
            if (clazz != null) {
                StringTokenizer parser = new StringTokenizer(hbmDA.getSpringConfig(), ",");
                String[] configs = new String[parser.countTokens()];
                int iCount = 0;
                while (parser.hasMoreTokens()) {
                    configs[iCount++] = parser.nextToken();
                }
                Object configure = clazz.getConstructor(String[].class).newInstance((Object) configs);
                if (configure != null) {
                    Object bsf = clazz.getMethod("getBean", String.class).invoke(configure, hbmDA.getBeanId());

                    session = bsf.getClass().getMethod("openSession", new Class[] {}).invoke(bsf,
                            new Object[] {});
                    session.getClass().getMethod("beginTransaction", new Class[] {}).invoke(session,
                            new Object[] {});
                    parameters.put(JRHibernateQueryExecuterFactory.PARAMETER_HIBERNATE_SESSION, session);
                }
            }
        } catch (ClassNotFoundException e) {
            throw new JRException(e);
        } catch (InstantiationException e) {
            throw new JRException(e);
        } catch (IllegalAccessException e) {
            throw new JRException(e);
        } catch (IllegalArgumentException e) {
            throw new JRException(e);
        } catch (SecurityException e) {
            throw new JRException(e);
        } catch (InvocationTargetException e) {
            throw new JRException(e);
        } catch (NoSuchMethodException e) {
            throw new JRException(e);
        }
    }
}

From source file:com.twinsoft.convertigo.engine.EnginePropertiesManager.java

public static String[] getPropertyAsStringArray(PropertyName property, boolean bSubstitute) {
    if (property.getType() != PropertyType.Array) {
        throw new IllegalArgumentException("The requested property is not of type Array: " + property);
    }/*from  w  w w  . j  a v  a  2 s. com*/

    String array = getProperty(property, bSubstitute);
    StringTokenizer st = new StringTokenizer(array, ";", false);
    String[] propertyAsStringArray = new String[st.countTokens()];
    int i = 0;
    while (st.hasMoreTokens()) {
        String item = st.nextToken();
        item = item.replaceAll("\\[\\[pv\\]\\]", ";");
        propertyAsStringArray[i] = item;
        i++;
    }

    return propertyAsStringArray;
}