List of usage examples for java.lang Character isLetter
public static boolean isLetter(int codePoint)
From source file:ch.bfh.evoting.alljoyn.MessageEncrypter.java
/** * Compute a truncated digest on the salt * We only take the three first letters (not chars!) of the Base64 encoded digest, because * this truncated digest must be transmitted with the group password (only letters) * @param salt the salt from on we want to compute the digest * @return the three first letters of the Base64 encoded digest *///w w w . jav a2s . c o m public String getSaltShortDigest(byte[] salt) { if (salt == null) return ""; //Compute the digest MessageDigest md; String saltHash; Log.d(TAG, "Computing salt digest"); try { md = MessageDigest.getInstance("SHA-1"); md.update(salt, 0, salt.length); saltHash = Base64.encodeToString(md.digest(), Base64.DEFAULT); } catch (NoSuchAlgorithmException e) { Log.e(TAG, "Digest of salt could not be computed"); e.printStackTrace(); return null; } //Truncate the digest String shortDigest = ""; int i = 0; while (shortDigest.length() < 3) { char c = saltHash.charAt(i); if (Character.isLetter(c)) { shortDigest = shortDigest.concat(String.valueOf(Character.toLowerCase(c))); } i++; if (i >= saltHash.length()) { break; } } Log.d(TAG, "Short digest is " + shortDigest); return shortDigest; }
From source file:org.novoj.utils.datePattern.DatePatternConverter.java
/** * This method normalizes entered date to the least valid form: * - white spaces are removed unless they delimit two valid values * - more consequential whitespaces are compressed to one character * - whitespace characters are converted to space character (32) * - special characters swallow border whitespaces * - more consequential special characters are compressed to one character * - string is trimmed//from w w w . j ava2 s.co m * - special characters at the beginning of the string are ignored * - string is lowercased * - all special characters are converted to dot character * * @param value * @return */ @SuppressWarnings({ "OverlyComplexMethod" }) public String getNormalizedDateValue(String value, Locale locale) { String firstNormalization = value.trim().toLowerCase(locale); StringBuilder result = new StringBuilder(); boolean whitespace = false; boolean specialCharacter = false; for (int i = 0; i < firstNormalization.length(); i++) { char letter = firstNormalization.charAt(i); if (specialCharacters.contains(letter)) { specialCharacter = true; } else if (Character.isDigit(letter) || Character.isLetter(letter)) { if (result.length() > 0) { if (specialCharacter || whitespace) { result.append('.'); } } whitespace = false; specialCharacter = false; result.append(letter); } else if (Character.isWhitespace(letter)) { whitespace = true; } } if (specialCharacter) { result.append("."); } return result.toString(); }
From source file:org.eclipsetrader.yahoo.internal.core.connector.StreamingConnector.java
protected Map<String, String> parseScript(String script) { Map<String, String> map = new HashMap<String, String>(); int e = 0;/* www. j a v a 2 s . c om*/ int s = script.indexOf("unixtime"); if (s != -1) { s += 10; e = script.indexOf(',', s); if (e == -1) { e = script.indexOf('}', s); } map.put("unixtime", script.substring(s, e)); } s = script.indexOf("open"); if (s != -1) { s += 6; e = script.indexOf(',', s); if (e == -1) { e = script.indexOf('}', s); } map.put("open", script.substring(s, e)); } s = script.indexOf("close"); if (s != -1) { s += 7; e = script.indexOf(',', s); if (e == -1) { e = script.indexOf('}', s); } map.put("close", script.substring(s, e)); } s = script.indexOf('"', e); if (s != -1) { s++; e = script.indexOf('"', s); String symbol = script.substring(s, e); map.put(K_SYMBOL, symbol); boolean inExpression = false; boolean inValue = false; int vs = -1; int ve = -1; for (int i = e + 1; i < script.length(); i++) { char ch = script.charAt(i); if (inExpression) { if (ch == ':') { e = i; } if (ch == '"') { inValue = !inValue; if (inValue) { vs = i + 1; } else { ve = i; try { String key = script.substring(s, e); String value = script.substring(vs, ve); map.put(key, value); } catch (RuntimeException e1) { System.err.println(script); e1.printStackTrace(); } } } if ((ch == ',' || ch == '}') && !inValue) { inExpression = false; } } else { if (Character.isLetter(ch)) { inExpression = true; s = i; } } } } return map; }
From source file:au.org.ala.delta.util.Utils.java
public static char GetNextChar(String RTFString, int[] startPos, int[] endPos) { char result = 0; int skipLevel = 0; endPos[0] = RTFString.length();/*from w w w .j a v a2 s .c om*/ while (result == 0 && startPos[0] < endPos[0]) { char ch = RTFString.charAt(startPos[0]); if (ch == '{' || ch == '}') { ++startPos[0]; if (skipLevel != 0) { if (ch == '{') { ++skipLevel; } else { --skipLevel; } } } else if (skipLevel != 0) { ++startPos[0]; } else if (ch == '\\') { int cmdStart = startPos[0] + 1; if (cmdStart >= endPos[0]) { // A pathological case - not actually good RTF result = ch; } else { ch = RTFString.charAt(cmdStart); if (Character.isLetter(ch)) { int[] curPos = new int[] { cmdStart }; while (++curPos[0] < endPos[0] && Character.isLetter(RTFString.charAt(curPos[0]))) { } String test = RTFString.substring(cmdStart, cmdStart + curPos[0] - cmdStart); int numStart = curPos[0]; boolean hasParam = false; if (curPos[0] < endPos[0] && (RTFString.charAt(curPos[0]) == '-' || Character.isDigit(RTFString.charAt(curPos[0])))) { hasParam = true; while (++curPos[0] < endPos[0] && Character.isDigit(RTFString.charAt(curPos[0]))) { } } if (curPos[0] < endPos[0] && RTFString.charAt(curPos[0]) == ' ') { ++curPos[0]; } for (int i = 0; i < nSkipWords; ++i) { if (skipWords[i] == test) { skipLevel = 1; break; } } if (skipLevel != 0) { } else if (test == "u") { // Actually had RTF unicode... result = (char) Integer.parseInt(RTFString.substring(numStart, curPos[0] - numStart)); char ansiVal = GetNextChar(RTFString, curPos, endPos); curPos[0] = endPos[0]; result |= ansiVal << 16; } else if (!hasParam) { // Currently match only parameter-less commands for (int i = 0; i < nRTFCmds; ++i) { if (RTFreps[i].cmdString == test) { result = RTFreps[i].unicodeValue; if (result > 0x100) result |= (char) RTFreps[i].repString.charAt(0) << 16; } } } if (result != 0) { // && endPos == RTFString.size()) endPos[0] = curPos[0]; } else { startPos[0] = curPos[0]; } } else if (ch == '{' || ch == '}' || ch == '\\') { result = ch; endPos[0] = cmdStart + 1; } else if (ch == '~') { result = 0xa0; endPos[0] = cmdStart + 1; } else if (ch == '-') { result = 0xad; endPos[0] = cmdStart + 1; } else if (ch == '\'' && cmdStart + 2 < endPos[0]) { char[] buff = new char[2]; buff[0] = RTFString.charAt(cmdStart + 1); buff[1] = RTFString.charAt(cmdStart + 2); result = (char) Integer.parseInt(new String(buff), 16); endPos[0] = cmdStart + 1 + 2; } else { result = ch; endPos[0] = cmdStart + 1; } } } else if (!Character.isISOControl(ch) || ch >= 0x80) { if (ch >= 0x80 && ch < 0xa0) { result = (char) (winANSIChars[ch - 0x80] | ch << 16); } else { result = ch; } endPos[0] = startPos[0] + 1; } else ++startPos[0]; } if ((result >> 16) == 0) result |= (result << 16); return result; }
From source file:importer.filters.PlayFilter.java
/** * Work out if we have a poetic line by counting syllables * @param line the line or paragraph to test * @return true if it is, else false//from ww w .ja v a 2 s .c o m */ boolean itsALine(String line) { int state = 0; int numSyllables = 0; for (int i = 0; i < line.length(); i++) { char token = Character.toLowerCase(line.charAt(i)); switch (state) { case 0: // word start if (vowels.indexOf(token) != -1) state = 1; else if (Character.isLetter(token)) state = 2; break; case 1: // general vowel state if (!Character.isLetter(token)) { numSyllables++; state = 0; } else if (vowels.indexOf(token) == -1) { state = 2; numSyllables++; } break; case 2:// consonants if (trailing.indexOf(token) != -1) state = 3; else if (vowels.indexOf(token) != -1) state = 1; else if (!Character.isLetter(token)) state = 0; break; case 3:// trailing vowel after consonant if (!Character.isLetter(token)) state = 0; else if (vowels.indexOf(token) != -1) state = 1; else { numSyllables++; state = 2; } break; } } if (state == 1) numSyllables++; //System.out.println("detected "+numSyllables+" syllables"); return numSyllables < maxLineSyllables; }
From source file:org.kuali.rice.krad.datadictionary.validator.Validator.java
/** * Checks the property for a valid name. * * @param name - The property name.//from w w w . ja v a 2 s . c om * @return True if the validation passes, false if not */ private static boolean checkPropertyName(String name) { if (!Character.isLetter(name.charAt(0))) { return false; } return true; }
From source file:org.renjin.parser.RLexer.java
private int consumeNextToken() { int c;/* w w w . ja va 2s.c o m*/ if (savedToken != 0) { c = savedToken; yylval = savedLVal; savedLVal = Null.INSTANCE; savedToken = 0; tokenBegin = savedTokenPos; return c; } //xxcharsave = xxcharcount; /* want to be able to go back one token */ c = skipSpace(); if (c == '\r') { c = xxgetc(); if (c != '\n') { xxungetc(c); c = '\r'; } } if (c == '#') c = skipComment(); tokenBegin.line = srcRef.xxlineno; tokenBegin.column = srcRef.xxcolno; tokenBegin.charIndex = srcRef.charIndex; if (c == R_EOF) return END_OF_INPUT; /* Either digits or symbols can start with a "." */ /* so we need to decide which it is and jump to */ /* the correct spot. */ if (c == '.' && typeofnext() >= 2) { return consumeSymbolValue(c); } /* literal numbers */ if (c == '.') { return consumeNumericValue(c); } /* We don't care about other than ASCII digits */ if (isDigit(c)) { return consumeNumericValue(c); } /* literal strings */ if (c == '\"' || c == '\'') { return consumeStringValue(c, false); } /* special functions */ if (c == '%') return consumeSpecialValue(c); /* functions, constants and variables */ if (c == '`') { return consumeStringValue(c, true); } if (Character.isLetter(c)) { return consumeSymbolValue(c); } /* compound tokens */ switch (c) { case '<': if (isNextChar('=')) { yylval = Symbol.get("<="); return LE; } if (isNextChar('-')) { yylval = Symbol.get("<-"); return LEFT_ASSIGN; } if (isNextChar('<')) { if (isNextChar('-')) { yylval = Symbol.get("<<-"); return LEFT_ASSIGN; } else return ERROR; } yylval = Symbol.get("<"); return LT; case '-': if (isNextChar('>')) { if (isNextChar('>')) { yylval = Symbol.get("<<-"); return RIGHT_ASSIGN; } else { yylval = Symbol.get("<-"); return RIGHT_ASSIGN; } } yylval = Symbol.get("-"); return '-'; case '>': if (isNextChar('=')) { yylval = Symbol.get(">="); return GE; } yylval = Symbol.get(">"); return GT; case '!': if (isNextChar('=')) { yylval = Symbol.get("!="); return NE; } yylval = Symbol.get("!"); return '!'; case '=': if (isNextChar('=')) { yylval = Symbol.get("=="); return EQ; } yylval = Symbol.get("="); return EQ_ASSIGN; case ':': if (isNextChar(':')) { if (isNextChar(':')) { yylval = Symbol.get(":::"); return NS_GET_INT; } else { yylval = Symbol.get("::"); return NS_GET; } } if (isNextChar('=')) { yylval = Symbol.get(":="); return LEFT_ASSIGN; } yylval = Symbol.get(":"); return ':'; case '&': if (isNextChar('&')) { yylval = Symbol.get("&&"); return AND2; } yylval = Symbol.get("&"); return AND; case '|': if (isNextChar('|')) { yylval = Symbol.get("||"); return OR2; } yylval = Symbol.get("|"); return OR; case LBRACE: yylval = Symbol.get("{"); return c; case RBRACE: return c; case '(': yylval = Symbol.get("("); return c; case ')': return c; case '[': if (isNextChar('[')) { yylval = Symbol.get("[["); return LBB; } yylval = Symbol.get("["); return c; case ']': return c; case '?': yylval = Symbol.get("?"); return c; case '*': /* Replace ** by ^. This has been here since 1998, but is undocumented (at least in the obvious places). It is in the index of the Blue Book with a reference to p. 431, the help for 'Deprecated'. S-PLUS 6.2 still allowed this, so presumably it was for compatibility with S. */ if (isNextChar('*')) { c = '^'; } yylval = Symbol.get(codePointToString(c)); return c; case '+': case '/': case '^': case '~': case '$': case '@': yylval = Symbol.get(codePointToString(c)); return c; default: return c; } }
From source file:net.sf.jabref.gui.autocompleter.AutoCompleteListener.java
@Override public void keyTyped(KeyEvent e) { LOGGER.debug("key typed event caught " + e.getKeyCode()); char ch = e.getKeyChar(); if (ch == '\n') { // this case is handled at keyPressed(e) return;//from www . j ava 2 s .co m } // don't do auto completion inside words if (!atEndOfWord((JTextComponent) e.getSource())) { return; } if ((e.getModifiers() | InputEvent.SHIFT_MASK) == InputEvent.SHIFT_MASK) { // plain key or SHIFT + key is pressed, no handling of CTRL+key, META+key, ... if (Character.isLetter(ch) || Character.isDigit(ch) || (Character.isWhitespace(ch) && completer.isSingleUnitField())) { JTextComponent comp = (JTextComponent) e.getSource(); if (toSetIn == null) { LOGGER.debug("toSetIn is null"); } else { LOGGER.debug("toSetIn: >" + toSetIn + '<'); } // The case-insensitive system is a bit tricky here // If keyword is "TODO" and user types "tO", then this is treated as "continue" as the "O" matches the "O" // If keyword is "TODO" and user types "To", then this is treated as "discont" as the "o" does NOT match the "O". if ((toSetIn != null) && (toSetIn.length() > 1) && (ch == toSetIn.charAt(1))) { // User continues on the word that was suggested. LOGGER.debug("cont"); toSetIn = toSetIn.substring(1); if (!toSetIn.isEmpty()) { int cp = comp.getCaretPosition(); //comp.setCaretPosition(cp+1-toSetIn.); comp.select((cp + 1) - toSetIn.length(), cp); lastBeginning = lastBeginning + ch; e.consume(); lastCaretPosition = comp.getCaretPosition(); lastCompletions = findCompletions(lastBeginning); lastShownCompletion = 0; for (int i = 0; i < lastCompletions.size(); i++) { String lastCompletion = lastCompletions.get(i); if (lastCompletion.endsWith(toSetIn)) { lastShownCompletion = i; break; } } if (toSetIn.length() < 2) { // User typed the last character of the autocompleted word // We have to replace the automcompletion word by the typed word. // This helps if the user presses "space" after the completion // "space" indicates that the user does NOT want the autocompletion, // but the typed word String text = comp.getText(); comp.setText(text.substring(0, lastCaretPosition - lastBeginning.length()) + lastBeginning + text.substring(lastCaretPosition)); // there is no selected text, therefore we are not updating the selection toSetIn = null; } return; } } if ((toSetIn != null) && ((toSetIn.length() <= 1) || (ch != toSetIn.charAt(1)))) { // User discontinues the word that was suggested. lastBeginning = lastBeginning + ch; LOGGER.debug("discont toSetIn: >" + toSetIn + "'<' lastBeginning: >" + lastBeginning + '<'); List<String> completed = findCompletions(lastBeginning); if ((completed != null) && (!completed.isEmpty())) { lastShownCompletion = 0; lastCompletions = completed; String sno = completed.get(0); // toSetIn = string used for autocompletion last time // this string has to be removed // lastCaretPosition is the position of the caret after toSetIn. int lastLen = toSetIn.length() - 1; toSetIn = sno.substring(lastBeginning.length() - 1); String text = comp.getText(); //we do not use toSetIn as we want to obey the casing of "sno" comp.setText(text.substring(0, (lastCaretPosition - lastLen - lastBeginning.length()) + 1) + sno + text.substring(lastCaretPosition)); int startSelect = (lastCaretPosition + 1) - lastLen; int endSelect = (lastCaretPosition + toSetIn.length()) - lastLen; comp.select(startSelect, endSelect); lastCaretPosition = comp.getCaretPosition(); e.consume(); return; } else { setUnmodifiedTypedLetters(comp, true, false); e.consume(); toSetIn = null; return; } } LOGGER.debug("case else"); comp.replaceSelection(""); StringBuffer currentword = getCurrentWord(comp); // only "real characters" end up here assert (!Character.isISOControl(ch)); currentword.append(ch); startCompletion(currentword, e); return; } else { if (Character.isWhitespace(ch)) { assert (!completer.isSingleUnitField()); LOGGER.debug("whitespace && !singleUnitField"); // start a new search if end-of-field is reached // replace displayed letters with typed letters setUnmodifiedTypedLetters((JTextComponent) e.getSource(), false, true); resetAutoCompletion(); return; } LOGGER.debug("No letter/digit/whitespace or CHAR_UNDEFINED"); // replace displayed letters with typed letters setUnmodifiedTypedLetters((JTextComponent) e.getSource(), false, !Character.isISOControl(ch)); resetAutoCompletion(); return; } } resetAutoCompletion(); }
From source file:org.marketcetera.util.misc.RandomStringsTest.java
@Test public void genStrId() { String sFirst = RandomStrings.genStrId(); boolean foundDifferent = false; for (int i = 0; i < STR_ITERATION_COUNT; i++) { String s = RandomStrings.genStrId(); int[] ucps = StringUtils.toUCPArray(s); assertTrue("Length is " + ucps.length, ((ucps.length >= 2) && (ucps.length <= RandomStrings.DEFAULT_LEN_STR_ID))); assertTrue("Value was " + Integer.toHexString(ucps[0]), Character.isLetter(ucps[0])); assertTrue("Value was " + Integer.toHexString(ucps[1]), Character.isDigit(ucps[1])); for (int j = 2; j < ucps.length; j++) { assertTrue("Value was " + Integer.toHexString(ucps[j]), Character.isLetterOrDigit(ucps[j])); }//from w w w.j av a 2 s .c o m if (!s.equals(sFirst)) { foundDifferent = true; } } assertTrue(foundDifferent); }
From source file:pyromaniac.IO.MMFastaImporter.java
/** * _read qualities./*from w w w . j a v a 2 s. c o m*/ * * @param pyrotagBlock the pyrotag block * @param pos the pos * @return the array list * @throws SeqFormattingException the seq formatting exception */ public ArrayList<Integer> _readQualities(char[] pyrotagBlock, MutableInteger pos) throws SeqFormattingException { ArrayList<Integer> qualities = new ArrayList<Integer>(); String currInt = ""; char curr; int index = pos.value(); try { while (index < pyrotagBlock.length) { curr = pyrotagBlock[index]; if (Character.isLetterOrDigit(curr) || Character.isWhitespace(curr)) { if (Character.isLetter(curr)) { throw new SeqFormattingException("Non-numeric quality score encountered: " + curr, this.qualFile); } else if (Character.isWhitespace(curr) && currInt.length() > 0) { qualities.add(Integer.parseInt(currInt)); currInt = ""; } else if (Character.isDigit(curr)) { currInt = currInt + curr; } } else if (currInt.length() > 0) { qualities.add(Integer.parseInt(currInt)); currInt = ""; } index++; } if (currInt.length() > 0) { qualities.add(Integer.parseInt(currInt)); } return qualities; } catch (NumberFormatException nfe) { throw new SeqFormattingException("Quality score: " + currInt + " is not an integer ", this.qualFile); } }