Example usage for java.util StringTokenizer nextToken

List of usage examples for java.util StringTokenizer nextToken

Introduction

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

Prototype

public String nextToken() 

Source Link

Document

Returns the next token from this string tokenizer.

Usage

From source file:io.datenwelt.cargo.rest.utils.Rfc2047.java

public static String decodeHeader(String input) {
    StringTokenizer tokenizer = new StringTokenizer(input, " \t", true);
    StringBuilder decoded = new StringBuilder();
    while (tokenizer.hasMoreTokens()) {
        String token = tokenizer.nextToken();
        if (!token.startsWith("=?") || !token.endsWith("?=")) {
            decoded.append(token);/* ww  w. j  a  va 2 s .c  om*/
            continue;
        }
        String encodedWord = token;
        String[] parts = encodedWord.substring(2, encodedWord.length() - 2).split("\\?", 3);
        if (parts.length != 3) {
            decoded.append(token);
            continue;
        }
        Charset charset;
        try {
            charset = Charset.forName(parts[0]);
        } catch (Exception ex) {
            LOG.debug("Skipping the decoding of a header value due to an unknown charset \"{}\" found in: ",
                    parts[1], input, ex);
            decoded.append(token);
            continue;
        }
        String encoding = parts[1].toUpperCase();
        switch (encoding) {
        case "B":
            BCodec bcodec = new BCodec(charset);
            try {
                decoded.append(bcodec.decode(encodedWord));
            } catch (Exception ex) {
                LOG.debug("Skipping the decoding of BASE64 value from string \"{}\" found in: ", encodedWord,
                        input, ex);
                decoded.append(token);
            }
            break;
        case "Q":
            QCodec qcodec = new QCodec(charset);
            try {
                decoded.append(qcodec.decode(encodedWord));
            } catch (Exception ex) {
                LOG.debug("Skipping the decoding of Q encoded value from string \"{}\" found in: ", encodedWord,
                        input, ex);
                decoded.append(token);
            }
            break;
        default:
            LOG.debug("Skipping the decoding of value from unknown encoding \"{}\" found in: ", encodedWord,
                    input);
            decoded.append(token);
        }
    }
    return decoded.toString();
}

From source file:com.bibisco.manager.TextEditorManager.java

private static void parseTextNode(HtmlParsingResult pHtmlParsingResult, Node pNode) {

    List<String> lListWords = new ArrayList<String>();

    mLog.debug("Start parseTextNode(HtmlParsingResult, Node): ", pNode.toString());

    // character count
    String lStrNodeText = StringUtils.replace(pNode.toString(), "&nbsp;", " ");
    lStrNodeText = StringUtils.replace(lStrNodeText, "\n", "");
    lStrNodeText = StringEscapeUtils.unescapeHtml(lStrNodeText);
    pHtmlParsingResult.characterCount += lStrNodeText.length();

    // extract words
    lStrNodeText = pNode.toString();/*from  ww  w. ja v a2  s.com*/
    lStrNodeText = StringUtils.replace(lStrNodeText, "&nbsp;", "");
    lStrNodeText = StringUtils.replace(lStrNodeText, "&laquo;", "");
    lStrNodeText = StringUtils.replace(lStrNodeText, "&raquo;", "");
    lStrNodeText = StringUtils.replace(lStrNodeText, "&mdash;", "");
    lStrNodeText = StringEscapeUtils.unescapeHtml(lStrNodeText);
    lStrNodeText = replaceCharIntervalWithWhiteSpace(lStrNodeText, 33, 38);
    lStrNodeText = replaceCharIntervalWithWhiteSpace(lStrNodeText, 40, 47);
    lStrNodeText = replaceCharIntervalWithWhiteSpace(lStrNodeText, 58, 64);
    lStrNodeText = replaceCharIntervalWithWhiteSpace(lStrNodeText, 91, 96);
    lStrNodeText = replaceCharIntervalWithWhiteSpace(lStrNodeText, 123, 126);
    lStrNodeText = replaceCharIntervalWithWhiteSpace(lStrNodeText, 161, 191);
    lStrNodeText = StringUtils.replaceChars(lStrNodeText, '', ' ');
    lStrNodeText = StringUtils.replaceChars(lStrNodeText, '', ' ');
    lStrNodeText = StringUtils.replaceChars(lStrNodeText, '', ' ');
    lStrNodeText = lStrNodeText.trim();

    if (StringUtils.isNotBlank(lStrNodeText)) {
        StringTokenizer lStringTokenizer = new StringTokenizer(lStrNodeText);
        while (lStringTokenizer.hasMoreTokens()) {
            lListWords.add(lStringTokenizer.nextToken());
        }
    }
    pHtmlParsingResult.words.addAll(lListWords);

    mLog.debug("End parseTextNode(HtmlParsingResult, Node)");
}

From source file:esg.node.core.Resource.java

@SuppressWarnings("unchecked")
private static String[] split(String str, String delim) {
    // Use a Vector to hold the split strings.
    Vector v = new Vector();

    // Use a StringTokenizer to do the splitting.
    StringTokenizer tokenizer = new StringTokenizer(str, delim);
    while (tokenizer.hasMoreTokens()) {
        v.addElement(tokenizer.nextToken());
    }/*from   w  w  w.  j a  v a2  s  . com*/

    String[] ret = new String[v.size()];
    v.copyInto(ret);
    return ret;
}

From source file:arena.mail.MailSender.java

protected static Session makeSession(String smtpServer, String smtpServerDelimiter,
        Properties extraMailProperties) {
    Properties mailProps = new Properties();
    mailProps.put("mail.transport.protocol", "smtp");

    // Support alternate syntax for core properties: "server,user,pass,localhost"
    StringTokenizer st = new StringTokenizer(smtpServer, smtpServerDelimiter);
    String property = null;/* w  w  w  . j ava  2s. c om*/
    for (int i = 0; st.hasMoreElements(); i++) {
        property = st.nextToken();

        if (!property.trim().equals("")) {
            mailProps.put(PROPS_KEYS[i], property);
        }
    }
    if (extraMailProperties != null) {
        mailProps.putAll(extraMailProperties);
    }

    LogFactory.getLog(MailSender.class)
            .debug("mailProps['mail.smtp.host'] ->" + mailProps.getProperty("mail.smtp.host"));
    return Session.getInstance(mailProps, null);
}

From source file:marytts.tools.install.LicenseRegistry.java

private static void loadLocalLicenses() {
    remote2local = new HashMap<URL, String>();
    File downloadDir = new File(System.getProperty("mary.downloadDir", "."));
    File licenseIndexFile = new File(downloadDir, "license-index.txt");
    if (!licenseIndexFile.canRead()) {
        return; // nothing to load
    }//  w  w w.ja  v a2s.  co  m
    try (BufferedReader br = new BufferedReader(
            new InputStreamReader(new FileInputStream(licenseIndexFile), "UTF-8"))) {
        // Each line in licenseIndexFile is expected to be a pair of local file name (relative to downloadDir) and URL string,
        // separated by a |(pipe) character.

        String line;
        while ((line = br.readLine()) != null) {
            line = line.trim();
            StringTokenizer st = new StringTokenizer(line, "|");
            if (!st.hasMoreTokens()) {
                continue; // skip empty lines
            }
            String localFilename = st.nextToken().trim();
            if (!st.hasMoreTokens()) {
                continue; // skip lines that don't contain a |
            }
            String remoteURLString = st.nextToken().trim();
            File localLicenseFile = new File(downloadDir, localFilename);
            if (!localLicenseFile.canRead()) {
                System.err.println("License index file " + licenseIndexFile.getAbsolutePath()
                        + " refers to license file " + localLicenseFile.getAbsolutePath()
                        + ", but that file cannot be read. Skipping.");
                continue;
            }
            URL remoteURL = new URL(remoteURLString);
            remote2local.put(remoteURL, localFilename);
        }
    } catch (IOException e) {
        System.err.println(
                "Problem reading local license index file " + licenseIndexFile.getAbsolutePath() + ":");
        e.printStackTrace();
    }
}

From source file:com.dosport.system.utils.ServletUtils.java

/**
 * ?? If-None-Match Header, Etag?./*from ww w. j  a va  2 s  . com*/
 * 
 * Etag, checkIfNoneMatchfalse, 304 not modify status.
 * 
 * @param etag
 *            ETag.
 */
public static boolean checkIfNoneMatchEtag(HttpServletRequest request, HttpServletResponse response,
        String etag) {
    String headerValue = request.getHeader("If-None-Match");
    if (headerValue != null) {
        boolean conditionSatisfied = false;
        if (!"*".equals(headerValue)) {
            StringTokenizer commaTokenizer = new StringTokenizer(headerValue, ",");

            while (!conditionSatisfied && commaTokenizer.hasMoreTokens()) {
                String currentToken = commaTokenizer.nextToken();
                if (currentToken.trim().equals(etag)) {
                    conditionSatisfied = true;
                }
            }
        } else {
            conditionSatisfied = true;
        }

        if (conditionSatisfied) {
            response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
            response.setHeader("ETag", etag);
            return false;
        }
    }
    return true;
}

From source file:net.securnetwork.itebooks.downloader.EbookDownloader.java

private static int getLastEbookIndex() {
    try {// ww w  . j a va2 s  .  co  m
        Source sourceHTML = new Source(new URL(BASE_URL));
        List<Element> allTDs = sourceHTML.getAllElements(HTMLElementName.TD);
        for (Element td : allTDs) {
            List<Element> innerH2s = td.getAllElements(HTMLElementName.H2);
            if (!innerH2s.isEmpty() && "Last Upload Ebooks".equalsIgnoreCase( //$NON-NLS-1$
                    innerH2s.get(0).getTextExtractor().toString())) {
                // Found interesting section
                List<Element> allTDElements = td.getAllElements(HTMLElementName.TD);
                Element lastEbookLink = allTDElements.get(0).getAllElements(HTMLElementName.A).get(0);
                String lastEbookRelativeHref = lastEbookLink.getAttributeValue("href"); //$NON-NLS-1$
                StringTokenizer tokenizer = new StringTokenizer(lastEbookRelativeHref, "/"); //$NON-NLS-1$
                for (int i = 1; i < tokenizer.countTokens(); i++) {
                    tokenizer.nextToken();
                }
                return Integer.parseInt(tokenizer.nextToken());
            }

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

    return MAX_EBOOK_INDEX;
}

From source file:Main.java

/**
 * /* ww w.  j a  v a2  s  .  c om*/
 * @param value
 * @return double[]
 */
public static double[] toDoubleArray(final String value) {
    if (value == null) {
        return new double[] {};
    }

    final int BRACKET_LENGTH = 1;
    final String strippedValue = value.substring(BRACKET_LENGTH, value.length() - BRACKET_LENGTH);
    final StringTokenizer tokenizer = new StringTokenizer(strippedValue, ELEMENT_SEPARATOR);
    final Collection<Double> doubleCollection = new ArrayList<>();

    while (tokenizer.hasMoreTokens()) {
        doubleCollection.add(Double.valueOf(tokenizer.nextToken().trim()));
    }

    return toDoubleArray(doubleCollection);
}

From source file:com.npower.dm.util.DDFTreeHelper.java

/**
 * Split the nodePath into a List. The "./" prefix in the node path will be
 * ignored. eg: ./a/b/c/d/ results: {a, b, c, d} /a/b/c/d/ results: {a, b, c,
 * d} a/b/c/d results: {a, b, c, d}//from w  w w  .j ava  2 s . c  o  m
 * 
 * @param nodePath
 * @return
 */
public static List<String> getPathVector(String nodePath) {
    String path = nodePath;
    if (path.startsWith("./")) {
        path = nodePath.substring(2, path.length());
    }
    StringTokenizer tokenizer = new StringTokenizer(path, "/");
    List<String> pathVector = new Vector<String>();

    while (tokenizer.hasMoreTokens()) {
        pathVector.add(tokenizer.nextToken());
    }
    return pathVector;
}

From source file:IPAddress.java

/**
 * Check if the specified address is a valid numeric TCP/IP address and return as an integer value
 * //from  w  w w .  j a  v  a2  s  .  c o m
 * @param ipaddr String
 * @return int
 */
public final static int parseNumericAddress(String ipaddr) {

    //   Check if the string is valid

    if (ipaddr == null || ipaddr.length() < 7 || ipaddr.length() > 15)
        return 0;

    //   Check the address string, should be n.n.n.n format

    StringTokenizer token = new StringTokenizer(ipaddr, ".");
    if (token.countTokens() != 4)
        return 0;

    int ipInt = 0;

    while (token.hasMoreTokens()) {

        //   Get the current token and convert to an integer value

        String ipNum = token.nextToken();

        try {

            //   Validate the current address part

            int ipVal = Integer.valueOf(ipNum).intValue();
            if (ipVal < 0 || ipVal > 255)
                return 0;

            //   Add to the integer address

            ipInt = (ipInt << 8) + ipVal;
        } catch (NumberFormatException ex) {
            return 0;
        }
    }

    //   Return the integer address

    return ipInt;
}