List of usage examples for java.util StringTokenizer countTokens
public int countTokens()
From source file:edu.stanford.muse.util.SloppyDates.java
private static Triple<Integer, Integer, Integer> parseDate(String s) { // separate into month and date // "jan10", "10jan", "jan 10" "10 jan" should all work s = s.toLowerCase();//from w w w . j av a2s. c o m s = s.trim(); StringBuilder sb = new StringBuilder(); // detect when string changes from alpha to num or vice versa and ensure a whitespace there boolean prevCharDigit = false, prevCharLetter = false; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (Character.isWhitespace(c)) { sb.append(c); prevCharDigit = prevCharLetter = false; continue; } // treat apostrophe like a space if (c == '\'') { sb.append(' '); prevCharDigit = prevCharLetter = false; continue; } if (Character.isLetter(c)) { if (prevCharDigit) sb.append(' '); sb.append(c); prevCharLetter = true; prevCharDigit = false; } else if (Character.isDigit(c)) { if (prevCharLetter) sb.append(' '); sb.append(c); prevCharDigit = true; prevCharLetter = false; } else throw new RuntimeException(); } String newS = sb.toString(); log.info("string " + s + " parsed to " + newS); StringTokenizer st = new StringTokenizer(newS); int nTokens = st.countTokens(); if (nTokens == 0 || nTokens > 3) return new Triple<Integer, Integer, Integer>(-1, -1, -1); int mm = -1, dd = -1, yy = -1; while (st.hasMoreTokens()) { String token = st.nextToken(); boolean isNumber = true; int num = -1; try { num = Integer.parseInt(token); } catch (NumberFormatException nfe) { isNumber = false; } if (isNumber && num < 0) return new Triple<Integer, Integer, Integer>(-1, -1, -1); if (isNumber) { if (dd == -1 && num > 0 && num <= 31) dd = num; else if (yy == -1) { yy = num; if (yy < 100) { yy = (yy > 12) ? (1900 + yy) : (2000 + yy); } if (yy < 1900 || yy > 2015) return new Triple<Integer, Integer, Integer>(-1, -1, -1); } else return new Triple<Integer, Integer, Integer>(-1, -1, -1); } else { int x = SloppyDates.uniquePrefixIdx(token, monthNames); if (x >= 0 && mm == -1) mm = x; else return new Triple<Integer, Integer, Integer>(-1, -1, -1); } } return new Triple<Integer, Integer, Integer>(dd, mm, yy); }
From source file:TextUtilities.java
/** * Parses the specified list of integers delimited by the specified * delimiters.//from ww w . ja v a 2 s.c o m */ public static int[] parseIntList(String text, String delimiters) { StringTokenizer tokenizer = new StringTokenizer(text, delimiters); int[] tokens = new int[tokenizer.countTokens()]; for (int i = 0; i < tokens.length; i++) tokens[i] = Integer.parseInt(tokenizer.nextToken()); return tokens; }
From source file:PackageUtils.java
public static String getNamespace(String packageName) { if (packageName == null || packageName.length() == 0) { return null; }/*from ww w . jav a 2s.c om*/ StringTokenizer tokenizer = new StringTokenizer(packageName, "."); String[] tokens; if (tokenizer.countTokens() == 0) { tokens = new String[0]; } else { tokens = new String[tokenizer.countTokens()]; for (int i = tokenizer.countTokens() - 1; i >= 0; i--) { tokens[i] = tokenizer.nextToken(); } } StringBuffer namespace = new StringBuffer("http://"); String dot = ""; for (int i = 0; i < tokens.length; i++) { if (i == 1) { dot = "."; } namespace.append(dot + tokens[i]); } namespace.append('/'); return namespace.toString(); }
From source file:TextUtilities.java
/** * Returns an array of the tokens produced by the specified string with the * specified delimiter characters.//from w w w .j a va 2 s . c o m */ public static String[] getTokens(String text, String delimiters) { StringTokenizer tokenizer = new StringTokenizer(text, delimiters); String[] tokens = new String[tokenizer.countTokens()]; for (int i = 0; i < tokens.length; i++) tokens[i] = tokenizer.nextToken(); return tokens; }
From source file:net.servicefixture.PluginManager.java
private static List<String[]> parsePluginValue(String value, int numOfTokens) { List<String[]> list = new ArrayList<String[]>(); StringTokenizer pluginTokenizer = new StringTokenizer(value, PLUGIN_DELIMITER); while (pluginTokenizer.hasMoreTokens()) { String token = pluginTokenizer.nextToken(); StringTokenizer tokenizer = new StringTokenizer(token, TOKEN_DELIMITER); String[] tokens = new String[tokenizer.countTokens()]; if (tokens.length != numOfTokens) { throw new ServiceFixtureException("Invalid plugin value: " + value); }/*from w ww . j av a2 s . co m*/ for (int i = 0; i < tokens.length; i++) { tokens[i] = tokenizer.nextToken().trim(); } list.add(tokens); } return list; }
From source file:net.securnetwork.itebooks.downloader.EbookDownloader.java
private static int getLastEbookIndex() { try {//from www. j a va 2 s .com 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:Utils.java
/** * Wrap multi-line strings (and get the individual lines). * //w w w . j a va2 s.com * @param original * the original string to wrap * @param width * the maximum width of lines * @param breakIterator * breaks original to chars, words, sentences, depending on what * instance you provide. * @param removeNewLines * if <code>true</code>, any newlines in the original string are * ignored * @return the lines after wrapping */ public static String[] wrapStringToArray(String original, int width, BreakIterator breakIterator, boolean removeNewLines) { if (original.length() == 0) { return new String[] { original }; } String[] workingSet; // substitute original newlines with spaces, // remove newlines from head and tail if (removeNewLines) { original = trimString(original); original = original.replace('\n', ' '); workingSet = new String[] { original }; } else { StringTokenizer tokens = new StringTokenizer(original, "\n"); // NOI18N int len = tokens.countTokens(); workingSet = new String[len]; for (int i = 0; i < len; i++) { workingSet[i] = tokens.nextToken(); } } if (width < 1) { width = 1; } if (original.length() <= width) { return workingSet; } widthcheck: { boolean ok = true; for (int i = 0; i < workingSet.length; i++) { ok = ok && (workingSet[i].length() < width); if (!ok) { break widthcheck; } } return workingSet; } java.util.ArrayList<String> lines = new java.util.ArrayList<String>(); int lineStart = 0; // the position of start of currently processed line in // the original string for (int i = 0; i < workingSet.length; i++) { if (workingSet[i].length() < width) { lines.add(workingSet[i]); } else { breakIterator.setText(workingSet[i]); int nextStart = breakIterator.next(); int prevStart = 0; do { while (((nextStart - lineStart) < width) && (nextStart != BreakIterator.DONE)) { prevStart = nextStart; nextStart = breakIterator.next(); } if (nextStart == BreakIterator.DONE) { nextStart = prevStart = workingSet[i].length(); } if (prevStart == 0) { prevStart = nextStart; } lines.add(workingSet[i].substring(lineStart, prevStart)); lineStart = prevStart; prevStart = 0; } while (lineStart < workingSet[i].length()); lineStart = 0; } } String[] s = new String[lines.size()]; return (String[]) lines.toArray(s); }
From source file:Main.java
static byte[] decryptJWE(String jwe, Key privRsaKey) { // Log.d("","decryptJWE"); try {/* w w w .j a v a2 s . c o m*/ // split jwe string StringTokenizer tokens = new StringTokenizer(jwe, "."); int count = tokens.countTokens(); // Log.d("","parts.length: "+count); if (count != 5) return null; String jweProtectedHeader64 = tokens.nextToken(); String jweEncrypted64 = tokens.nextToken(); String jweInitVector64 = tokens.nextToken(); String cryptedBytes64 = tokens.nextToken(); String auth_tag64 = tokens.nextToken(); // decrypt cek using private rsa key byte[] cek = decryptRsaB64(jweEncrypted64, privRsaKey); // check cek result byte array if (cek == null || cek.length == 0 || (cek.length % 2) != 0) return null; int keySize = cek.length / 2; Log.d("", "Decryption AES: " + keySize * 8); // build aes_key and hmac_key byte aes_key[] = new byte[keySize]; byte hmac_key[] = new byte[keySize]; System.arraycopy(cek, 0, hmac_key, 0, keySize); System.arraycopy(cek, keySize, aes_key, 0, keySize); // decode initialization vector byte[] iv_key = decodeB64(jweInitVector64); Log.d("", "hmac_key: " + bytesToHex(hmac_key)); Log.d("", "aes_key: " + bytesToHex(aes_key)); Log.d("", "iv_key: " + bytesToHex(iv_key)); // decrypt content using aes_key and iv_key byte[] cryptedBytes = decodeB64(cryptedBytes64); Cipher decrypt = Cipher.getInstance("AES/CBC/PKCS5Padding", "SC"); decrypt.init(Cipher.DECRYPT_MODE, new SecretKeySpec(aes_key, "AES"), new IvParameterSpec(iv_key)); byte[] decryptedBytes = decrypt.doFinal(cryptedBytes); Log.d("", "decryptedBytes:"); Log.d("", bytesToHex(decryptedBytes)); // validation verification byte[] aad = jweProtectedHeader64.getBytes(); long al = aad.length * 8; // concatenate aad, iv_key, cryptedBytes and al byte[] hmacData = new byte[aad.length + iv_key.length + cryptedBytes.length + 8]; int offset = 0; System.arraycopy(aad, offset, hmacData, 0, aad.length); offset += aad.length; System.arraycopy(iv_key, 0, hmacData, offset, iv_key.length); offset += iv_key.length; System.arraycopy(cryptedBytes, 0, hmacData, offset, cryptedBytes.length); offset += cryptedBytes.length; ByteBuffer buffer = ByteBuffer.allocate(8); buffer.putLong(al); System.arraycopy(buffer.array(), 0, hmacData, offset, 8); // compute hmac Mac hmac = Mac.getInstance("HmacSHA256", "SC"); hmac.init(new SecretKeySpec(hmac_key, "HmacSHA256")); byte[] hmacValue = hmac.doFinal(hmacData); // pick authentication tag byte[] authTag = Arrays.copyOf(hmacValue, 16); // validate authentication tag byte[] authTagRead = decodeB64(auth_tag64); for (int i = 0; i < 16; i++) { if (authTag[i] != authTagRead[i]) { Log.d("", "validation failed"); return decryptedBytes; } } Log.d("", "validation success"); // validation success return decryptedBytes; } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:de.innovationgate.webgate.api.WGContentKey.java
/** * Parses a content key string and returns the according content key object. * The database, whose content is meant needs to be provided to parse the struct key part. * If the database is ommitted the struct key will be left in it's string representation.. * An additional parameter determines, if the unique mode key or the URL mode key is to be created. * @param key Content key string representation * @param db Content database, whose content is adressed by the content key. Omit to keep struct key in its string representation. * @param unique When true, creates unique content key (always use real version), else creates url mode key (if content is released, 0 is taken for version) * @return WGContentKey// w ww . j a va 2 s .co m * @throws WGAPIException */ public static WGContentKey parse(String key, WGDatabase db, boolean unique) throws WGAPIException { java.util.StringTokenizer tokens = new java.util.StringTokenizer(key, WGContentKey.TOKEN_DIVIDER); if (tokens.countTokens() != 3) { return null; } Object structKey = tokens.nextToken(); if (db != null) { structKey = db.parseStructKey((String) structKey); } String strLanguage = tokens.nextToken(); String strVersion = tokens.nextToken(); int version = 0; try { version = Integer.parseInt(strVersion); } catch (NumberFormatException e) { // Everything ok. All non-numeric versions (p, mediakey) will get parsed as version 0 } return new WGContentKey(structKey, strLanguage, version); }
From source file:org.alfresco.web.bean.wcm.AVMWorkflowUtil.java
/** * @return the list of WorkflowDefinition objects as configured in the wcm/workflows client config. *///w w w . ja v a 2s. co m public static List<WorkflowDefinition> getConfiguredWorkflows() { if ((configuredWorkflowDefs == null) || (Application.isDynamicConfig(FacesContext.getCurrentInstance()))) { FacesContext fc = FacesContext.getCurrentInstance(); List<WorkflowDefinition> defs = Collections.<WorkflowDefinition>emptyList(); ConfigElement config = Application.getConfigService(fc).getGlobalConfig().getConfigElement("wcm"); if (config == null) { logger.warn("WARNING: Unable to find 'wcm' config element definition."); } else { ConfigElement workflowConfig = config.getChild("workflows"); if (workflowConfig == null) { logger.warn("WARNING: Unable to find WCM 'workflows' config element definition."); } else { WorkflowService service = Repository.getServiceRegistry(fc).getWorkflowService(); StringTokenizer t = new StringTokenizer(workflowConfig.getValue().trim(), ", "); defs = new ArrayList<WorkflowDefinition>(t.countTokens()); while (t.hasMoreTokens()) { String wfName = t.nextToken(); WorkflowDefinition def = service.getDefinitionByName("jbpm$" + wfName); if (def != null) { defs.add(def); } else { logger.warn("WARNING: Cannot find WCM workflow def for configured definition name: " + wfName); } } } } configuredWorkflowDefs = defs; } return configuredWorkflowDefs; }