Example usage for java.lang Character isWhitespace

List of usage examples for java.lang Character isWhitespace

Introduction

In this page you can find the example usage for java.lang Character isWhitespace.

Prototype

public static boolean isWhitespace(int codePoint) 

Source Link

Document

Determines if the specified character (Unicode code point) is white space according to Java.

Usage

From source file:com.ultrapower.eoms.common.plugin.ecside.tag.form.ECSideFormTagUtil.java

 public static String getTagName(String body, int i) {
   if (body == null) {
      return null;
   }//  w  ww .java2  s . c  o  m
   if (body.charAt(i) != '<') {
      return null;      // no tag
   }
   int start = i + 1;      // skip '<'
   int len = body.length();
   boolean isEndTag = false;
   // skip all non-letters
   while (start < len) {
      char c = body.charAt(start);
      if (c == '>') {
         return null;   // tag end found => name not found
      }
      if (c == '/') {      // this is an end tag
         start++;
         isEndTag = true;
         continue;
      }
      if (!Character.isWhitespace(c)) {
         break;
      }
      start++;
   }
   if (start == len) {
      return null;      // tag name not found
   }
   int end = start;
   // skip all letters
   while (end < len) {
      char c = body.charAt(end);
      if ((Character.isWhitespace(c)) || (c == '>')) {
         break;
      }
      end++;
   }
   if (end == len) {
      return null;      // tag end not found
   }
   
   String tagName = body.substring(start, end);
   if (isEndTag) {
      tagName = "/" + tagName;
   }
   return tagName;
}

From source file:com.homeadvisor.kafdrop.config.ini.IniFileReader.java

private String parseValue(String val, BufferedReader reader) throws IOException {
    StringBuilder propertyValue = new StringBuilder();
    boolean lineContinues;
    String value = val.trim();

    do {/*  w  w w .  j a  v a  2s  .  c  o  m*/
        boolean quoted = value.startsWith("\"") || value.startsWith("'");
        boolean stop = false;
        boolean escape = false;

        char quote = quoted ? value.charAt(0) : 0;

        int i = quoted ? 1 : 0;

        StringBuilder result = new StringBuilder();
        char lastChar = 0;
        while (i < value.length() && !stop) {
            char c = value.charAt(i);

            if (quoted) {
                if ('\\' == c && !escape) {
                    escape = true;
                } else if (!escape && quote == c) {
                    stop = true;
                } else if (escape && quote == c) {
                    escape = false;
                    result.append(c);
                } else {
                    if (escape) {
                        escape = false;
                        result.append('\\');
                    }

                    result.append(c);
                }
            } else {
                if (isCommentChar(c) && Character.isWhitespace(lastChar)) {
                    stop = true;
                } else {
                    result.append(c);
                }
            }

            i++;
            lastChar = c;
        }

        String v = result.toString();
        if (!quoted) {
            v = v.trim();
            lineContinues = lineContinues(v);
            if (lineContinues) {
                // remove trailing "\"
                v = v.substring(0, v.length() - 1).trim();
            }
        } else {
            lineContinues = lineContinues(value, i);
        }
        propertyValue.append(v);

        if (lineContinues) {
            propertyValue.append(LINE_SEPARATOR);
            value = reader.readLine();
        }
    } while (lineContinues && value != null);

    return propertyValue.toString();
}

From source file:com.cloudera.sqoop.orm.ClassWriter.java

/**
 * Given some character that can't be in an identifier,
 * try to map it to a string that can.//  w  w  w  .  j  a  va 2s .co  m
 *
 * @param c a character that can't be in a Java identifier
 * @return a string of characters that can, or null if there's
 * no good translation.
 */
static String getIdentifierStrForChar(char c) {
    if (Character.isJavaIdentifierPart(c)) {
        return "" + c;
    } else if (Character.isWhitespace(c)) {
        // Eliminate whitespace.
        return null;
    } else {
        // All other characters map to underscore.
        return "_";
    }
}

From source file:de.undercouch.citeproc.tool.shell.ShellCommandCompleter.java

@Override
public int complete(String buffer, int cursor, List<CharSequence> candidates) {
    boolean allparsed;
    Set<String> result = new HashSet<String>();

    try {//from   w  ww.j av a  2 s. com
        Result pr = ShellCommandParser.parse(buffer, excludedCommands);
        if (pr.getFirstCommand() == HelpCommand.class || pr.getFirstCommand() == ShellHelpCommand.class) {
            //parse again, but skip 'help'
            pr = ShellCommandParser.parse(pr.getRemainingArgs(), excludedCommands);
        }

        if (pr.getRemainingArgs().length > 1) {
            //command line could not be parsed completely. we cannot
            //provide suggestions for more than one unrecognized argument.
            allparsed = false;
        } else {
            OptionGroup<ID> options;
            if (pr.getLastCommand() == null) {
                options = OptionIntrospector.introspect(CSLTool.class, AdditionalShellCommands.class);
            } else {
                options = OptionIntrospector.introspect(pr.getLastCommand());
            }

            String[] ra = pr.getRemainingArgs();
            allparsed = (ra == null || ra.length == 0);

            //add completions
            for (Option<ID> o : options.getCommands()) {
                Class<? extends Command> cmd = OptionIntrospector.getCommand(o.getId());
                if (excludedCommands.contains(cmd)) {
                    continue;
                }

                if (allparsed || o.getLongName().startsWith(ra[0])) {
                    result.add(o.getLongName());
                }
            }
        }

        if (pr.getLastCommand() != null && Completer.class.isAssignableFrom(pr.getLastCommand())) {
            Completer cc;
            try {
                cc = (Completer) pr.getLastCommand().newInstance();
            } catch (Exception e) {
                //should never happen
                throw new RuntimeException(e);
            }
            List<CharSequence> ccl = new ArrayList<CharSequence>();
            String jra = StringUtils.join(pr.getRemainingArgs(), " ");
            cc.complete(jra, jra.length(), ccl);
            for (CharSequence cs : ccl) {
                result.add(cs.toString());
            }
        }
    } catch (InvalidOptionException e) {
        //there's an option, we cannot calculate completions anymore
        //because options are not allowed in the interactive shell
        allparsed = false;
    } catch (IntrospectionException e) {
        throw new RuntimeException("Could not inspect command", e);
    }

    //sort completions
    List<String> resultList = new ArrayList<String>(result);
    Collections.sort(resultList);
    candidates.addAll(resultList);

    //determine place to insert completion
    int pos = buffer.length();
    if (!allparsed && pos > 0) {
        while (pos > 0 && Character.isWhitespace(buffer.charAt(pos - 1)))
            --pos;
        if (pos == 0) {
            //buffer consists of whitespaces only
            pos = buffer.length();
        }
        while (pos > 0 && !Character.isWhitespace(buffer.charAt(pos - 1)))
            --pos;
    } else if (allparsed && buffer.length() > 0
            && !Character.isWhitespace(buffer.charAt(buffer.length() - 1))) {
        ++pos;
    }

    return candidates.isEmpty() ? -1 : pos;
}

From source file:com.xie.javacase.json.XMLTokener.java

/**
 * Returns the next XML meta token. This is used for skipping over <!...>
 * and <?...?> structures./*from   w  w w  .j  ava  2s.co m*/
 * @return Syntax characters (<code>< > / = ! ?</code>) are returned as
 *  Character, and strings and names are returned as Boolean. We don't care
 *  what the values actually are.
 * @throws org.json.JSONException If a string is not properly closed or if the XML
 *  is badly structured.
 */
public Object nextMeta() throws JSONException {
    char c;
    char q;
    do {
        c = next();
    } while (Character.isWhitespace(c));
    switch (c) {
    case 0:
        throw syntaxError("Misshaped meta tag");
    case '<':
        return XML.LT;
    case '>':
        return XML.GT;
    case '/':
        return XML.SLASH;
    case '=':
        return XML.EQ;
    case '!':
        return XML.BANG;
    case '?':
        return XML.QUEST;
    case '"':
    case '\'':
        q = c;
        for (;;) {
            c = next();
            if (c == 0) {
                throw syntaxError("Unterminated string");
            }
            if (c == q) {
                return Boolean.TRUE;
            }
        }
    default:
        for (;;) {
            c = next();
            if (Character.isWhitespace(c)) {
                return Boolean.TRUE;
            }
            switch (c) {
            case 0:
            case '<':
            case '>':
            case '/':
            case '=':
            case '!':
            case '?':
            case '"':
            case '\'':
                back();
                return Boolean.TRUE;
            }
        }
    }
}

From source file:importer.filters.Filter.java

/**
 * Get the last word of a line excluding punctuation etc
 * @param line the line in question/*  w w w.j a  v a2 s  .  c o m*/
 * @return the word
 */
protected String getLastWord(String text) {
    int len = text.length();
    if (len > 0) {
        int start = 0;
        int size = 0, i = len - 1;
        // point beyond trailing hyphen
        if (len > 1 && text.endsWith("--")) {
            lastEndsInHyphen = true;
            return "--";
        } else if (text.charAt(len - 1) == '-') {
            lastEndsInHyphen = true;
            len--;
            i--;
        } else {
            lastEndsInHyphen = false;
            // point to last non-space
            for (; i > 0; i--) {
                if (!Character.isWhitespace(text.charAt(i)))
                    break;
            }
        }
        int j = i;
        for (; i > 0; i--) {
            if (!Character.isLetter(text.charAt(i))) {
                start = i + 1;
                size = j - i;
                break;
            }
        }
        if (i == 0)
            size = (j - i) + 1;
        return text.substring(start, start + size);
    } else
        return "";
}

From source file:com.googlecode.jtiger.modules.ecside.tag.form.ECSideFormTagUtil.java

public static String getTagName(String body, int i) {
    if (body == null) {
        return null;
    }//  w w  w. j a  v  a2s  .  c o m
    if (body.charAt(i) != '<') {
        return null; // no tag
    }
    int start = i + 1; // skip '<'
    int len = body.length();
    boolean isEndTag = false;
    // skip all non-letters
    while (start < len) {
        char c = body.charAt(start);
        if (c == '>') {
            return null; // tag end found => name not found
        }
        if (c == '/') { // this is an end tag
            start++;
            isEndTag = true;
            continue;
        }
        if (!Character.isWhitespace(c)) {
            break;
        }
        start++;
    }
    if (start == len) {
        return null; // tag name not found
    }
    int end = start;
    // skip all letters
    while (end < len) {
        char c = body.charAt(end);
        if ((Character.isWhitespace(c)) || (c == '>')) {
            break;
        }
        end++;
    }
    if (end == len) {
        return null; // tag end not found
    }

    String tagName = body.substring(start, end);
    if (isEndTag) {
        tagName = "/" + tagName;
    }
    return tagName;
}

From source file:com.aionengine.gameserver.cache.HTMLCache.java

private String compactHtml(StringBuilder sb, String html) {
    sb.setLength(0);/*from  w  w  w .  ja v  a  2 s  .com*/
    sb.append(html);

    for (int i = 0; i < sb.length(); i++)
        if (Character.isWhitespace(sb.charAt(i)))
            sb.setCharAt(i, ' ');

    replaceAll(sb, "  ", " ");

    replaceAll(sb, "< ", "<");
    replaceAll(sb, " >", ">");

    for (int i = 0; i < TAGS_TO_COMPACT.length; i += 3) {
        replaceAll(sb, TAGS_TO_COMPACT[i + 1], TAGS_TO_COMPACT[i]);
        replaceAll(sb, TAGS_TO_COMPACT[i + 2], TAGS_TO_COMPACT[i]);
    }

    replaceAll(sb, "  ", " ");

    // String.trim() without additional garbage
    int fromIndex = 0;
    int toIndex = sb.length();

    while (fromIndex < toIndex && sb.charAt(fromIndex) == ' ')
        fromIndex++;

    while (fromIndex < toIndex && sb.charAt(toIndex - 1) == ' ')
        toIndex--;

    return sb.substring(fromIndex, toIndex);
}

From source file:com.fujitsu.dc.core.odata.DcExpressionParser.java

/**
 * tokenizer./*from  www . j  a  va2s.  c  om*/
 * OData4j?tokenizer???'_'?????????
 * @param value value
 * @return 
 */
public static List<Token> tokenize(String value) {
    List<Token> rt = new ArrayList<Token>();
    int current = 0;
    int end = 0;

    while (true) {
        if (current == value.length()) {
            return rt;
        }
        char c = value.charAt(current);
        if (Character.isWhitespace(c)) {
            end = readWhitespace(value, current);
            rt.add(new Token(TokenType.WHITESPACE, value.substring(current, end)));
            current = end;
        } else if (c == '\'') {
            end = readQuotedString(value, current + 1);
            rt.add(new Token(TokenType.QUOTED_STRING, value.substring(current, end)));
            current = end;
        } else if (Character.isLetter(c)) {
            end = readWord(value, current + 1);
            rt.add(new Token(TokenType.WORD, value.substring(current, end)));
            current = end;
        } else if (c == '_') {
            end = readWord(value, current + 1);
            rt.add(new Token(TokenType.WORD, value.substring(current, end)));
            current = end;
        } else if (Character.isDigit(c)) {
            end = readDigits(value, current + 1);
            rt.add(new Token(TokenType.NUMBER, value.substring(current, end)));
            current = end;
        } else if (c == '(') {
            rt.add(new Token(TokenType.OPENPAREN, Character.toString(c)));
            current++;
        } else if (c == ')') {
            rt.add(new Token(TokenType.CLOSEPAREN, Character.toString(c)));
            current++;
        } else if (c == '-') {
            if (Character.isDigit(value.charAt(current + 1))) {
                end = readDigits(value, current + 1);
                rt.add(new Token(TokenType.NUMBER, value.substring(current, end)));
                current = end;
            } else {
                rt.add(new Token(TokenType.SYMBOL, Character.toString(c)));
                current++;
            }
        } else if (",.+=:".indexOf(c) > -1) {
            rt.add(new Token(TokenType.SYMBOL, Character.toString(c)));
            current++;
        } else {
            dumpTokens(rt);
            throw new RuntimeException("Unable to tokenize: " + value + " current: " + current + " rem: "
                    + value.substring(current));
        }
    }

}

From source file:Ch5CompletionEditor.java

public ICompletionProposal[] computeCompletionProposals(ITextViewer textViewer, int documentOffset) {
    IDocument document = textViewer.getDocument();
    int currOffset = documentOffset - 1;

    try {//from   w  w  w  .j  a v a  2s  . co  m
        String currWord = "";
        char currChar;
        while (currOffset > 0 && !Character.isWhitespace(currChar = document.getChar(currOffset))) {
            currWord = currChar + currWord;
            currOffset--;
        }

        List suggestions = wordTracker.suggest(currWord);
        ICompletionProposal[] proposals = null;
        if (suggestions.size() > 0) {
            proposals = buildProposals(suggestions, currWord, documentOffset - currWord.length());
            lastError = null;
        }
        return proposals;
    } catch (BadLocationException e) {
        e.printStackTrace();
        lastError = e.getMessage();
        return null;
    }
}