List of usage examples for java.lang Character isLetterOrDigit
public static boolean isLetterOrDigit(int codePoint)
From source file:nl.strohalm.cyclos.utils.StringHelper.java
private static String removeMarkupTagsAndUnescapeEntities(String string, final boolean unescapeEntities) { if (string == null) { return null; }// ww w . j a v a 2 s .c o m final StringBuilder sb = new StringBuilder(); final int length = string.length(); boolean inTag = false; boolean inComments = false; boolean hasText = false; for (int i = 0; i < length; i++) { final char c = string.charAt(i); if (c == '<' && i < length - 1 && (Character.isLetterOrDigit(string.charAt(i + 1)) || string.charAt(i + 1) == '/')) { inTag = true; } else if (i < length - 4 && "<!--".equals(string.substring(i, i + 4))) { inComments = true; i += 3; } else if (inComments && i < length - 3 && "-->".equals(string.substring(i, i + 3))) { inComments = false; hasText = false; i += 2; } else if (c == '>') { inTag = false; hasText = false; } else if (!inTag && !inComments) { if (!hasText) { // Append an space if this is the first text right after a tag hasText = true; if (sb.length() > 0) { // But not before the first output character sb.append(' '); } } sb.append(c); } } string = sb.toString(); if (unescapeEntities) { string = StringEscapeUtils.unescapeHtml(string); } return string; }
From source file:org.jboss.confluence.plugin.docbook_tools.web.DocbookImportAction.java
protected boolean validTitlePrefixBase() { if (titlePrefixBase == null) return true; if (titlePrefixBase.length() < 2 || titlePrefixBase.length() > 3) { addActionError("'Unique page title prefix base' must be two or three characters long"); return false; } else {/*from ww w.jav a 2s . c om*/ for (char ch : titlePrefixBase.toCharArray()) { if (!Character.isLetterOrDigit(ch)) { addActionError("'Unique page title prefix base' must contain only letters or digits"); return false; } } } return true; }
From source file:eu.fbk.dh.tint.tokenizer.ItalianTokenizer.java
public TokenGroup tokenArray(String text) { if (text.length() == 0) { return new TokenGroup(); }// w w w . j a va 2 s . c o m // List<Token> list = new ArrayList<Token>(); TokenGroup tokenGroup = new TokenGroup(); Character currentChar; Character previousChar = null; int start = 0; Boolean isCurrentCharLetterOrDigit; Boolean isPreviousCharLetterOrDigit; MutableBoolean isNewLine = new MutableBoolean(false); Token lastToken = new Token(0, 0, ""); //logger.debug("0\t" + (int) previousChar + "\t<" + previousChar + ">"); for (int i = 0; i < text.length(); i++) { currentChar = text.charAt(i); isCurrentCharLetterOrDigit = Character.isLetterOrDigit(currentChar); isPreviousCharLetterOrDigit = previousChar != null && Character.isLetterOrDigit(previousChar); if (isCurrentCharLetterOrDigit) { if (!isPreviousCharLetterOrDigit) { start = i; } } else { if (isPreviousCharLetterOrDigit) { String substring = text.substring(start, i); addToken(tokenGroup, start, i, substring, isNewLine, lastToken); if (!isSeparatorChar(currentChar)) { String charString = new String(new char[] { currentChar }); addToken(tokenGroup, i, i + 1, charString, isNewLine, lastToken); } } else { if (!isSeparatorChar(currentChar)) { String charString = new String(new char[] { currentChar }); addToken(tokenGroup, i, i + 1, charString, isNewLine, lastToken); } } } if (currentChar == CharacterTable.LINE_FEED) { isNewLine.setValue(true); } previousChar = currentChar; } if (Character.isLetterOrDigit(previousChar)) { String substring = text.substring(start, text.length()); addToken(tokenGroup, start, text.length(), substring, isNewLine, lastToken); } return tokenGroup; }
From source file:ca.uhn.hl7v2.sourcegen.SourceGenerator.java
public static String makeAlternateAccessorName(String fieldDesc, String parentName, int index) { StringBuffer aName = new StringBuffer(); aName.append(StringUtils.capitalize(parentName.toLowerCase())).append(index).append("_"); char[] chars = fieldDesc.toCharArray(); boolean lastCharWasNotLetter = true; int inBrackets = 0; StringBuffer bracketContents = new StringBuffer(); for (int i = 0; i < chars.length; i++) { if (chars[i] == '(') inBrackets++;//ww w . ja va2s . com if (chars[i] == ')') inBrackets--; if (Character.isLetterOrDigit(chars[i])) { if (inBrackets > 0) { //buffer everthing in brackets bracketContents.append(chars[i]); } else { //add capitalized bracketed text if appropriate if (bracketContents.length() > 0) { aName.append(capitalize(filterBracketedText(bracketContents.toString()))); bracketContents = new StringBuffer(); } if (lastCharWasNotLetter) { //first letter of each word is upper-case aName.append(Character.toUpperCase(chars[i])); } else { aName.append(chars[i]); } lastCharWasNotLetter = false; } } else { lastCharWasNotLetter = true; } } aName.append(capitalize(filterBracketedText(bracketContents.toString()))); String retVal = aName.toString(); return retVal; }
From source file:edu.ku.brc.af.ui.db.JEditComboBox.java
protected KeyAdapter createKeyAdapter() { return new KeyAdapter() { @Override// ww w .j a v a2 s . c o m public void keyReleased(KeyEvent ev) { char key = ev.getKeyChar(); if (ev.getKeyCode() == clearKeyStroke.getKeyCode()) { int selectedIndex = getSelectedIndex(); if (selectedIndex > -1 && dbAdapter != null && textField != null && textField.getText().length() == 0 && !dbAdapter.isReadOnly()) { // delete item PickListItem item = (PickListItem) getSelectedItem(); dbAdapter.getList().remove(item); } } else if (!(Character.isLetterOrDigit(key) || Character.isSpaceChar(key))) { if (ev.getKeyCode() == KeyEvent.VK_ENTER) { addNewItemFromTextField(); } } else { if (textField != null) { if (getSelectedIndex() > -1) { int pos = textField.getCaretPosition(); String currentText = textField.getText(); setSelectedIndex(-1); textField.setText(currentText); textField.moveCaretPosition(pos); textField.setSelectionStart(pos); textField.setSelectionEnd(pos); } } else { setSelectedIndex(-1); } } } }; }
From source file:de.micromata.genome.gwiki.page.impl.wiki.parser.GWikiWikiParser.java
protected boolean isDecorateEnd(GWikiWikiTokens tks) { char pk = tks.peekToken(-1); if (pk == 0 || Character.isWhitespace(pk) == true) { return false; }/* w w w.ja v a 2 s .c o m*/ pk = tks.peekToken(1); if (pk == 0) { return true; } if (Character.isLetterOrDigit(pk) == true) { return false; } return true; }
From source file:util.misc.TextLineBreakerStringWidth.java
/** * Read the Next Line in TextReader/*from w w w. j a va 2 s . co m*/ * @param textReader get next Line from this Reader * @param maxWidth Max width of each Line * @return one Line * @throws IOException */ private String readNextLine(Reader textReader, int maxWidth) throws IOException { // Clear the current line mCurrLineBuffer.setLength(0); int lineWidth = 0; while (true) { // Check whether there is a word that has to be processed first if (mNextWordWidth == -1) { // There is no unprocessed word any more -> Read to the next word // (A length of -1 means it was processed) // Ignore white space do { mCurrChar = textReader.read(); // Check whether we have to force a line break if (isEndOfLine(mCurrChar)) { // Force line break return mCurrLineBuffer.toString(); } } while (Character.isSpaceChar(((char) mCurrChar))); // Read the next word mNextWord = readNextWord(textReader); mNextWordWidth = getStringWidth(mNextWord); } int newLineWidth = lineWidth + mNextWordWidth; if (lineWidth != 0) { newLineWidth += mSpaceWidth; } int lineLength = mCurrLineBuffer.length(); if (newLineWidth - mSpaceWidth > maxWidth) { // The next word does not fit if (lineWidth == 0 || (maxWidth - lineWidth > 20)) { // The line is empty -> Break the word int breakPos = findBreakPos(mNextWord, maxWidth - lineWidth, lineWidth == 0); if (breakPos <= 0) { if (mCurrLineBuffer.length() > 0) { // avoid returning empty lines, leading to endless loops return mCurrLineBuffer.toString(); } else { breakPos = Math.min(2, mNextWordWidth); } } String firstPart = mNextWord.substring(0, breakPos); if (lineLength > 0 && (mCurrLineBuffer.charAt(lineLength - 1) != '-' || (lineLength > 1 && mCurrLineBuffer.charAt(lineLength - 2) == ' '))) { mCurrLineBuffer.append(' '); } mCurrLineBuffer.append(firstPart); // Append a minus if the last character is a letter or digit char lastChar = firstPart.charAt(firstPart.length() - 1); if (Character.isLetterOrDigit(lastChar)) { mCurrLineBuffer.append('-'); } mNextWord = mNextWord.substring(breakPos); mNextWordWidth = getStringWidth(mNextWord); return mCurrLineBuffer.toString(); } else { // Make a line break here (and process the word the next time) return mCurrLineBuffer.toString(); } } else { if (lineWidth != 0) { // Add a space, but not if our current word ends with "-" char lastChar = mCurrLineBuffer.charAt(lineLength - 1); if (lastChar != '/' && (lastChar != '-' || (lineLength >= 2 && mCurrLineBuffer.charAt(lineLength - 2) == ' '))) { mCurrLineBuffer.append(' '); } lineWidth += mSpaceWidth; } // The next word fits -> Add it mCurrLineBuffer.append(mNextWord); lineWidth += mNextWordWidth; mNextWordWidth = -1; // Mark the word as processed // Check whether we have to force a line break if (isEndOfLine(mCurrChar)) { // Force line break return mCurrLineBuffer.toString(); } } } }
From source file:opennlp.tools.textsimilarity.TextProcessor.java
private static boolean isSafePunc(char[] chars, int idx) { if (true) {/*from w w w . jav a 2 s.c om*/ return false; } boolean retVal = false; int c = chars[idx]; // are we dealing with a safe character if (c == 39 || c == 45 || c == 8211 || c == 8212 || c == 145 || c == 146 || c == 8216 || c == 8217) { // if we are at end or start of array, then character is not good if (idx == chars.length - 1 || idx == 0) { return false; } // check to see if previous and next character are acceptable if (Character.isLetterOrDigit(chars[idx + 1]) && Character.isLetterOrDigit(chars[idx - 1])) { return true; } } return retVal; }
From source file:feedsplugin.FeedsPlugin.java
private boolean matchesTitle(final String feedTitle, final String programTitle, String quotedTitle) { if (programTitle.isEmpty()) { return false; }// ww w. j av a2 s . c o m if (feedTitle.equalsIgnoreCase(programTitle)) { return true; } if (StringUtils.containsIgnoreCase(feedTitle, programTitle)) { if (quotedTitle == null) { quotedTitle = quoteTitle(programTitle); } if (feedTitle.matches(quotedTitle)) { return true; } } if (programTitle.contains(" - ")) { String[] parts = StringUtils.splitByWholeSeparator(programTitle, " - "); if (parts[0].length() >= MIN_MATCH_LENGTH) { return matchesTitle(feedTitle, parts[0], null); } } else if (programTitle.endsWith(")")) { int index = programTitle.lastIndexOf("("); if (index > 0) { // try without the suffix in brackets, which might be a part number or // the like return matchesTitle(feedTitle, programTitle.substring(0, index).trim(), null); } } else if (!Character.isLetterOrDigit(programTitle.charAt(programTitle.length() - 1))) { String shortProgramTitle = removePunctuation(programTitle); String shortFeedTitle = removePunctuation(feedTitle); return matchesTitle(shortFeedTitle, shortProgramTitle, null); } return false; }
From source file:com.wellsandwhistles.android.redditsp.reddit.prepared.markdown.MarkdownTokenizer.java
private static void linkify(final IntArrayLengthPair input, final IntArrayLengthPair output) { if (input.data.length > output.data.length * 3) throw new RuntimeException(); output.clear();/*from w w w .ja va2 s .c om*/ int inBrackets = 0; boolean lastCharOk = true; for (int i = 0; i < input.pos; i++) { final int token = input.data[i]; switch (token) { case TOKEN_BRACKET_SQUARE_OPEN: case TOKEN_PAREN_OPEN: output.data[output.pos++] = token; inBrackets++; lastCharOk = true; break; case TOKEN_BRACKET_SQUARE_CLOSE: case TOKEN_PAREN_CLOSE: output.data[output.pos++] = token; inBrackets--; lastCharOk = true; break; case ' ': output.data[output.pos++] = ' '; lastCharOk = true; break; case 'h': case 'w': if (inBrackets == 0 && lastCharOk) { final int linkStartType = getLinkStartType(input.data, i, input.pos); if (linkStartType >= 0) { // Greedily read to space, or <>, or etc final int linkStartPos = i; final int linkPrefixEndPos = linkPrefixes[linkStartType].length + linkStartPos; int linkEndPos = linkPrefixEndPos; while (linkEndPos < input.pos) { final int lToken = input.data[linkEndPos]; final boolean isValidChar = lToken != ' ' && lToken != '<' && lToken != '>' && lToken != TOKEN_GRAVE && lToken != TOKEN_BRACKET_SQUARE_OPEN && lToken != TOKEN_BRACKET_SQUARE_CLOSE; if (isValidChar) { linkEndPos++; } else { break; } } // discard many final chars if they are '.', ',', '?', ';' etc // THEN, discard single final char if it is '\'', '"', etc while (input.data[linkEndPos - 1] == '.' || input.data[linkEndPos - 1] == ',' || input.data[linkEndPos - 1] == '?' || input.data[linkEndPos - 1] == ';') { linkEndPos--; } if (input.data[linkEndPos - 1] == '"') { linkEndPos--; } if (input.data[linkEndPos - 1] == '\'') { linkEndPos--; } if (input.data[linkEndPos - 1] == ')') { linkEndPos--; } if (linkEndPos - linkPrefixEndPos >= 2) { final int[] reverted = revert(input.data, linkStartPos, linkEndPos); output.data[output.pos++] = TOKEN_BRACKET_SQUARE_OPEN; output.append(reverted); output.data[output.pos++] = TOKEN_BRACKET_SQUARE_CLOSE; output.data[output.pos++] = TOKEN_PAREN_OPEN; output.append(reverted); output.data[output.pos++] = TOKEN_PAREN_CLOSE; i = linkEndPos - 1; } else { output.data[output.pos++] = token; } } else { output.data[output.pos++] = token; } } else { output.data[output.pos++] = token; } lastCharOk = false; break; case 'r': case 'u': case '/': if (inBrackets == 0 && lastCharOk) { final int linkStartType = getRedditLinkStartType(input.data, i, input.pos); if (linkStartType >= 0) { final int linkStartPos = i; final int linkPrefixEndPos = linkPrefixes_reddit[linkStartType].length + linkStartPos; int linkEndPos = linkPrefixEndPos; while (linkEndPos < input.pos) { final int lToken = input.data[linkEndPos]; final boolean isValidChar = (lToken >= 'a' && lToken <= 'z') || (lToken >= 'A' && lToken <= 'Z') || (lToken >= '0' && lToken <= '9') || lToken == '_' || lToken == TOKEN_UNDERSCORE || lToken == TOKEN_UNDERSCORE_DOUBLE || lToken == '+' || lToken == '-'; if (isValidChar) { linkEndPos++; } else { break; } } if (linkEndPos - linkPrefixEndPos > 2) { final int[] reverted = revert(input.data, linkStartPos, linkEndPos); output.data[output.pos++] = TOKEN_BRACKET_SQUARE_OPEN; output.append(reverted); output.data[output.pos++] = TOKEN_BRACKET_SQUARE_CLOSE; output.data[output.pos++] = TOKEN_PAREN_OPEN; output.append(reverted); output.data[output.pos++] = TOKEN_PAREN_CLOSE; i = linkEndPos - 1; } else { output.data[output.pos++] = token; } } else { output.data[output.pos++] = token; } } else { output.data[output.pos++] = token; } lastCharOk = false; break; default: // TODO test this against reddits impl lastCharOk = token < 0 || (!Character.isLetterOrDigit(token)); output.data[output.pos++] = token; break; } } }