Example usage for java.util StringTokenizer StringTokenizer

List of usage examples for java.util StringTokenizer StringTokenizer

Introduction

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

Prototype

public StringTokenizer(String str) 

Source Link

Document

Constructs a string tokenizer for the specified string.

Usage

From source file:com.liusoft.dlog4j.util.StringUtils.java

/**
 * //ww w  .  jav a2s  . co  m
 * @param tags
 * @return
 */
public static List stringToList(String tags) {
    if (tags == null)
        return null;
    ArrayList tagList = new ArrayList();
    StringTokenizer st = new StringTokenizer(tags);
    while (st.hasMoreElements()) {
        tagList.add(st.nextToken());
    }
    return tagList;
}

From source file:edu.vt.middleware.servlet.filter.RequestMethodFilter.java

/**
 * Initialize this filter./*  ww  w  .  j a v a2s. c  om*/
 *
 * @param  config  <code>FilterConfig</code>
 */
public void init(final FilterConfig config) {
    final Enumeration<?> e = config.getInitParameterNames();
    while (e.hasMoreElements()) {
        final String name = (String) e.nextElement();
        final String value = config.getInitParameter(name);

        final StringTokenizer st = new StringTokenizer(name);
        final String methodName = st.nextToken();
        Object[] args = null;
        Class<?>[] params = null;
        if (st.countTokens() > 0) {
            args = new Object[st.countTokens()];
            params = new Class[st.countTokens()];

            int i = 0;
            while (st.hasMoreTokens()) {
                final String token = st.nextToken();
                args[i] = token;
                params[i++] = token.getClass();
            }
        }
        try {
            this.servletMethods.put(methodName, ServletRequest.class.getMethod(methodName, params));
            if (LOG.isDebugEnabled()) {
                LOG.debug("Found method " + methodName + " for ServletRequest");
            }
        } catch (NoSuchMethodException ex) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Could not find method " + methodName + " for ServletRequest");
            }
        }
        try {
            this.httpServletMethods.put(methodName, HttpServletRequest.class.getMethod(methodName, params));
            if (LOG.isDebugEnabled()) {
                LOG.debug("Found method " + methodName + " for HttpServletRequest");
            }
        } catch (NoSuchMethodException ex) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Could not find method " + methodName + " for HttpServletRequest");
            }
        }
        this.arguments.put(methodName, args);
        this.patterns.put(methodName, Pattern.compile(value));
        if (LOG.isDebugEnabled()) {
            if (this.arguments.get(methodName) != null) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Stored method name = " + methodName + ", pattern = " + value
                            + " with these arguments "
                            + Arrays.asList((Object[]) this.arguments.get(methodName)));
                }
            } else {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Stored method name = " + methodName + ", pattern = " + value
                            + " with no arguments");
                }
            }
        }
    }
}

From source file:blueprint.sdk.experimental.florist.db.ConnectionListener.java

public void contextInitialized(final ServletContextEvent arg0) {
    Properties poolProp = new Properties();
    try {//from   w w w .jav  a 2s . c o  m
        Context initContext = new InitialContext();

        // load pool properties file (from class path)
        poolProp.load(ConnectionListener.class.getResourceAsStream("/jdbc_pools.properties"));
        StringTokenizer stk = new StringTokenizer(poolProp.getProperty("PROP_LIST"));

        // process all properties files list in pool properties (from class
        // path)
        while (stk.hasMoreTokens()) {
            try {
                String propName = stk.nextToken();
                LOGGER.info(this, "loading jdbc properties - " + propName);

                Properties prop = new Properties();
                prop.load(ConnectionListener.class.getResourceAsStream(propName));

                DataSource dsr;
                if (prop.containsKey("JNDI_NAME")) {
                    // lookup DataSource from JNDI
                    // FIXME JNDI support is not tested yet. SPI or Factory
                    // is needed here.
                    LOGGER.warn(this, "JNDI DataSource support needs more hands on!");
                    dsr = (DataSource) initContext.lookup(prop.getProperty("JNDI_NAME"));
                } else {
                    // create new BasicDataSource
                    BasicDataSource bds = new BasicDataSource();
                    bds.setMaxActive(Integer.parseInt(prop.getProperty("MAX_ACTIVE")));
                    bds.setMaxIdle(Integer.parseInt(prop.getProperty("MAX_IDLE")));
                    bds.setMaxWait(Integer.parseInt(prop.getProperty("MAX_WAIT")));
                    bds.setInitialSize(Integer.parseInt(prop.getProperty("INITIAL")));
                    bds.setDriverClassName(prop.getProperty("CLASS_NAME"));
                    bds.setUrl(prop.getProperty("URL"));
                    bds.setUsername(prop.getProperty("USER"));
                    bds.setPassword(prop.getProperty("PASSWORD"));
                    bds.setValidationQuery(prop.getProperty("VALIDATION"));
                    dsr = bds;
                }
                boundDsrs.put(prop.getProperty("POOL_NAME"), dsr);
                initContext.bind(prop.getProperty("POOL_NAME"), dsr);
            } catch (RuntimeException e) {
                LOGGER.trace(e);
            }
        }
    } catch (IOException | NamingException e) {
        LOGGER.trace(e);
    }
}

From source file:de.tudarmstadt.ukp.clarin.webanno.tcf.WebAnnoSemanticGraphReader.java

public void convertToCas(JCas aJCas, InputStream aIs, String aEncoding) throws IOException

{
    StringBuilder text = new StringBuilder();
    LineIterator lineIterator = IOUtils.lineIterator(aIs, aEncoding);
    int tokenBeginPosition = 0;
    while (lineIterator.hasNext()) {
        String line = lineIterator.next();
        String[] contents = line.split("\t>\t|\tX\t");
        int sentenceBegin = tokenBeginPosition;
        int chainBegin = tokenBeginPosition;
        int chainEnd = 0;
        StringTokenizer st = new StringTokenizer(contents[0]);
        while (st.hasMoreTokens()) {
            String content = st.nextToken();
            Token outToken = new Token(aJCas, tokenBeginPosition, tokenBeginPosition + content.length());
            outToken.addToIndexes();/*from  w w w  .  jav a2  s  .c  om*/
            tokenBeginPosition = outToken.getEnd() + 1;
            chainEnd = tokenBeginPosition;
            text.append(content + " ");
        }

        CoreferenceChain chain = new CoreferenceChain(aJCas);
        CoreferenceLink link = new CoreferenceLink(aJCas, chainBegin, chainEnd - 1);
        link.setReferenceType("text");
        link.addToIndexes();
        chain.setFirst(link);

        if (line.contains("\t>\t")) {
            link.setReferenceRelation("entails");
            Token outToken = new Token(aJCas, tokenBeginPosition, tokenBeginPosition + 1);
            outToken.addToIndexes();
            tokenBeginPosition = outToken.getEnd() + 1;
            text.append("> ");
        } else {
            link.setReferenceRelation("do not entails");
            Token outToken = new Token(aJCas, tokenBeginPosition, tokenBeginPosition + 1);
            outToken.addToIndexes();
            tokenBeginPosition = outToken.getEnd() + 1;
            text.append("X ");
        }

        chainBegin = tokenBeginPosition;
        st = new StringTokenizer(contents[0]);
        while (st.hasMoreTokens()) {
            String content = st.nextToken();
            Token outToken = new Token(aJCas, tokenBeginPosition, tokenBeginPosition + content.length());
            outToken.addToIndexes();
            tokenBeginPosition = outToken.getEnd() + 1;
            chainEnd = tokenBeginPosition;
            text.append(content + " ");

        }
        CoreferenceLink nextLink = new CoreferenceLink(aJCas, chainBegin, chainEnd - 1);
        nextLink.setReferenceType("hypothesis");
        nextLink.addToIndexes();
        link.setNext(nextLink);
        chain.addToIndexes();
        text.append("\n");

        Sentence outSentence = new Sentence(aJCas);
        outSentence.setBegin(sentenceBegin);
        outSentence.setEnd(tokenBeginPosition);
        outSentence.addToIndexes();
        tokenBeginPosition = tokenBeginPosition + 1;
        sentenceBegin = tokenBeginPosition;
    }
    aJCas.setDocumentText(text.toString());
}

From source file:edu.cmu.cs.lti.ark.fn.data.prep.ParsePreparation.java

/**
 * Converts a POS tagged file into conll format
 * @param posFile//from w w w  .j  a  v  a  2s .  co  m
 * @param conllInputFile
 */
public static void printCoNLLTypeInput(String posFile, String conllInputFile) throws IOException {
    List<String> posSentences = readLines(posFile);
    BufferedWriter bWriter = new BufferedWriter(new FileWriter(conllInputFile));
    try {
        for (String posSentence : posSentences) {
            posSentence = replaceSentenceWithPTBWords(posSentence);
            ArrayList<String> words = new ArrayList<String>();
            ArrayList<String> pos = new ArrayList<String>();
            ArrayList<String> parents = new ArrayList<String>();
            ArrayList<String> labels = new ArrayList<String>();
            StringTokenizer st = new StringTokenizer(posSentence.trim());
            while (st.hasMoreTokens()) {
                String token = st.nextToken();
                int lastIndex = token.lastIndexOf('_');
                String word = token.substring(0, lastIndex);
                String POS = token.substring(lastIndex + 1);
                words.add(word);
                pos.add(POS);
                parents.add("0");
                labels.add("SUB");
            }
            writeStuff(bWriter, words, pos, parents, labels);
        }
    } finally {
        IOUtils.closeQuietly(bWriter);
    }
}

From source file:edu.stanford.muse.util.JSONUtils.java

public static String jsonForNewNewAlgResults(AddressBook ab, String resultsFile)
        throws IOException, JSONException {
    LineNumberReader lnr = new LineNumberReader(new InputStreamReader(new FileInputStream(resultsFile)));
    JSONObject result = new JSONObject();

    JSONArray groups = new JSONArray();
    int groupNum = 0;
    while (true) {
        String line = lnr.readLine();
        if (line == null)
            break;
        line = line.trim();/*from  ww w .  ja  v a 2  s  .c om*/
        // line: group 8, freq 49: seojiwon@gmail.com sseong@stanford.edu debangsu@cs.stanford.edu

        // ignore lines without a ':'
        int idx = line.indexOf(":");
        if (idx == -1)
            continue;

        String vars = line.substring(0, idx);
        // vars: freq=5 foo bar somevar=someval
        // we'll pick up all tokens with a = in them and assume they mean key=value
        StringTokenizer varsSt = new StringTokenizer(vars);

        JSONObject group = new JSONObject();
        while (varsSt.hasMoreTokens()) {
            String str = varsSt.nextToken();
            // str: freq=5
            int x = str.indexOf("=");
            if (x >= 0) {
                String key = str.substring(0, x);
                String value = "";
                // we should handle the case of key= (empty string)
                if (x < str.length() - 1)
                    value = str.substring(x + 1);
                group.put(key, value);
            }
        }

        String groupsStr = line.substring(idx + 1);
        // groupsStr: seojiwon@gmail.com sseong@stanford.edu debangsu@cs.stanford.edu

        StringTokenizer st = new StringTokenizer(groupsStr);

        JSONArray groupMembers = new JSONArray();
        int i = 0;

        while (st.hasMoreTokens()) {
            String s = st.nextToken();
            Contact ci = ab.lookupByEmail(s);
            if (ci == null) {
                System.out.println("WARNING: no contact info for email address: " + s);
                continue;
            }
            groupMembers.put(i++, ci.toJson());
        }
        group.put("members", groupMembers);
        groups.put(groupNum, group);
        groupNum++;
    }

    result.put("groups", groups);
    return result.toString();
}

From source file:net.jradius.webservice.WebServiceListener.java

public JRadiusEvent parseRequest(ListenerRequest listenerRequest, ByteBuffer byteBuffer,
        InputStream inputStream) throws IOException, WebServiceException {
    DataInputStream reader = new DataInputStream(inputStream);
    WebServiceRequest request = new WebServiceRequest();

    String line = null;//w w  w  .ja v  a  2 s. c o  m

    try {
        line = reader.readLine();
    } catch (SocketException e) {
        return null;
    }

    if (line == null)
        throw new WebServiceException("Invalid relay request");

    StringTokenizer tokens = new StringTokenizer(line);
    String method = tokens.nextToken();
    String uri = tokens.nextToken();
    String httpVersion = tokens.nextToken();

    if ("GET".equals(method))
        request.setMethod(WebServiceRequest.GET);
    else if ("POST".equals(method))
        request.setMethod(WebServiceRequest.POST);
    else if ("PUT".equals(method))
        request.setMethod(WebServiceRequest.PUT);
    else
        throw new WebServiceException("Does not handle HTTP request method: " + method);

    request.setHttpVersion(httpVersion);

    try {
        request.setUri(new URI(uri));
    } catch (URISyntaxException e) {
        throw new WebServiceException(e.getMessage());
    }

    Map<String, String> headers = getHeaders(reader);
    request.setHeaderMap(headers);

    String clen = headers.get("content-length");
    if (clen != null) {
        request.setContent(getContent(reader, Integer.parseInt(clen)));
    }

    return request;
}

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 w  w.jav a  2 s.  c o m*/
@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:info.halo9pan.word2vec.hadoop.mr.ReadWordsMapper.java

@Override
public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
    String line = value.toString();
    for (String pattern : skipPatterns) {
        if (line.contains(pattern)) {
            Counter counter = context.getCounter(WordsCounter.SkipWords.toString(), pattern);
            counter.increment(1);//w  w  w .j av a  2s . c o  m
        }
        line = line.replaceAll(pattern, StringUtils.EMPTY);
    }
    StringTokenizer itr = new StringTokenizer(line);
    while (itr.hasMoreTokens()) {
        word.set(itr.nextToken());
        context.write(word, ONE);
        Counter counter = context.getCounter(WordsCounter.class.getName(), WordsCounter.InputWords.toString());
        counter.increment(1);
    }
}

From source file:edu.vt.middleware.servlet.filter.SessionAttributeFilter.java

/**
 * Initialize this filter./*from w  w w .j  a va  2 s.co m*/
 *
 * @param  config  <code>FilterConfig</code>
 */
public void init(final FilterConfig config) {
    this.context = config.getServletContext();
    this.requireAttribute = Boolean.valueOf(config.getInitParameter(REQUIRE_ATTRIBUTE)).booleanValue();
    if (LOG.isDebugEnabled()) {
        LOG.debug("requireAttribute = " + this.requireAttribute);
    }

    final Enumeration<?> e = config.getInitParameterNames();
    while (e.hasMoreElements()) {
        final String name = (String) e.nextElement();
        if (!name.equals(REQUIRE_ATTRIBUTE)) {
            final String value = config.getInitParameter(name);
            if (LOG.isDebugEnabled()) {
                LOG.debug("Loaded attribute name:value " + name + ":" + value);
            }

            final StringTokenizer st = new StringTokenizer(name);
            final String attrName = st.nextToken();
            final String attrValue = st.nextToken();

            this.attributes.put(attrName, Pattern.compile(attrValue));
            this.redirects.put(attrName, value);
            if (LOG.isDebugEnabled()) {
                LOG.debug("Stored attribute " + attrName + " for pattern " + attrValue + " with redirect of "
                        + value);
            }
        }
    }
}