Example usage for java.lang Character isISOControl

List of usage examples for java.lang Character isISOControl

Introduction

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

Prototype

public static boolean isISOControl(int codePoint) 

Source Link

Document

Determines if the referenced character (Unicode code point) is an ISO control character.

Usage

From source file:com.sec.ose.osi.ui.frm.main.manage.dialog.JDlgProjectCreate.java

/**
 * This method initializes jComboBoxClonedFrom   
 *    //from   w w  w .  j a  v a  2s .  c  o m
 * @return javax.swing.JComboBox   
 */
private JComboBox<String> getJComboBoxClonedFrom() {
    if (jComboBoxClonedFrom == null) {
        jComboBoxClonedFrom = new JComboBox<String>();
        jComboBoxClonedFrom.setRenderer(new ComboToopTip());
        jComboBoxClonedFrom.setEditable(true);
        jComboBoxClonedFrom.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent e) {
                log.debug("jComboBoxClonedFrom.actionPerformed()");
                eventHandler.handleEvent(EventHandler.COMBO_CLONED_FROM);
            }
        });
        refresh(jComboBoxClonedFrom);

        final JTextField editor;
        editor = (JTextField) jComboBoxClonedFrom.getEditor().getEditorComponent();
        editor.addKeyListener(new KeyAdapter() {
            public void keyReleased(KeyEvent e) {
                char ch = e.getKeyChar();

                if (ch != KeyEvent.VK_ENTER && ch != KeyEvent.VK_BACK_SPACE
                        && (ch == KeyEvent.CHAR_UNDEFINED || Character.isISOControl(ch)))
                    return;
                if (ch == KeyEvent.VK_ENTER) {
                    jComboBoxClonedFrom.hidePopup();
                    return;
                }

                String str = editor.getText();

                if (jComboBoxClonedFrom.getComponentCount() > 0) {
                    jComboBoxClonedFrom.removeAllItems();
                }

                jComboBoxClonedFrom.addItem(str);
                try {
                    String tmpProjectName = null;
                    if (str.length() > 0) {
                        for (int i = 0; i < names.size(); i++) {
                            tmpProjectName = names.get(i);
                            if (tmpProjectName.toLowerCase().startsWith(str.toLowerCase()))
                                jComboBoxClonedFrom.addItem(tmpProjectName);
                        }
                    } else {
                        for (int i = 0; i < names.size(); i++) {
                            jComboBoxClonedFrom.addItem(names.get(i));
                        }
                    }
                } catch (Exception e1) {
                    log.warn(e1.getMessage());
                }

                jComboBoxClonedFrom.hidePopup();
                if (str.length() > 0)
                    jComboBoxClonedFrom.showPopup();
            }
        });

        editor.addFocusListener(new FocusAdapter() {
            public void focusGained(FocusEvent e) {
                if (editor.getText().length() > 0)
                    jComboBoxClonedFrom.showPopup();
            }

            public void focusLost(FocusEvent e) {
                jComboBoxClonedFrom.hidePopup();
            }
        });
    }
    return jComboBoxClonedFrom;
}

From source file:org.opengeoportal.proxy.controllers.DynamicOgcController.java

/**
* <p>Encodes characters in the query or fragment part of the URI.
*
* <p>Unfortunately, an incoming URI sometimes has characters disallowed by the spec. HttpClient
* insists that the outgoing proxied request has a valid URI because it uses Java's {@link URI}. To be more
* forgiving, we must escape the problematic characters. See the URI class for the spec.
*
* @param in example: name=value&foo=bar#fragment
*///from  w  w  w. j  av  a2s. c o  m
static CharSequence encodeUriQuery(CharSequence in) {
    //Note that I can't simply use URI.java to encode because it will escape pre-existing escaped things.
    StringBuilder outBuf = null;
    Formatter formatter = null;
    for (int i = 0; i < in.length(); i++) {
        char c = in.charAt(i);
        boolean escape = true;
        if (c < 128) {
            if (asciiQueryChars.get((int) c)) {
                escape = false;
            }
        } else if (!Character.isISOControl(c) && !Character.isSpaceChar(c)) {//not-ascii
            escape = false;
        }
        if (!escape) {
            if (outBuf != null)
                outBuf.append(c);
        } else {
            //escape
            if (outBuf == null) {
                outBuf = new StringBuilder(in.length() + 5 * 3);
                outBuf.append(in, 0, i);
                formatter = new Formatter(outBuf);
            }
            //leading %, 0 padded, width 2, capital hex
            formatter.format("%%%02X", (int) c);//TODO
            formatter.close();
        }
    }
    return outBuf != null ? outBuf : in;
}

From source file:net.sf.jabref.gui.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 w  w w. j ava2  s.c  om*/
    }

    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.);
                    //System.out.println(cp-toSetIn.length()+" - "+cp);
                    comp.select((cp + 1) - toSetIn.length(), cp);
                    lastBeginning = lastBeginning + ch;

                    e.consume();
                    lastCaretPosition = comp.getCaretPosition();

                    //System.out.println("Added char: '"+toSetIn+"'");
                    //System.out.println("LastBeginning: '"+lastBeginning+"'");

                    lastCompletions = findCompletions(lastBeginning, comp);
                    lastShownCompletion = 0;
                    for (int i = 0; i < lastCompletions.length; i++) {
                        String lastCompletion = lastCompletions[i];
                        //System.out.println("Completion["+i+"] = "+lastCompletion);
                        if (lastCompletion.endsWith(toSetIn)) {
                            lastShownCompletion = i;
                            break;
                        }

                    }
                    //System.out.println("Index now: "+lastShownCompletion);
                    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 + '<');

                String[] completed = findCompletions(lastBeginning, comp);
                if ((completed != null) && (completed.length > 0)) {
                    lastShownCompletion = 0;
                    lastCompletions = completed;
                    String sno = completed[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();
                    //Util.pr(""+lastLen);
                    //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);
            if (currentword == null) {
                currentword = new StringBuffer();
            }

            // 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: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;/*w  w w.  j  a  va  2  s . com*/
    }

    // 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.gtdfree.ApplicationHelper.java

/**
 * Escape in Java style in readable friendly way common control characters and other control characters.
 * @param in input string//from  w  w w. j  a v a 2 s. c  o  m
 * @return escaped string in Java style
 */
public static String escapeControls(String in) {
    if (in == null) {
        return null;
    }
    StringBuilder sb = new StringBuilder(in.length() + 10);

    for (int i = 0; i < in.length(); i++) {
        char ch = in.charAt(i);

        switch (ch) {
        case '\b':
            sb.append('\\');
            sb.append('b');
            break;
        case '\n':
            sb.append('\\');
            sb.append('n');
            break;
        case '\t':
            sb.append('\\');
            sb.append('t');
            break;
        case '\f':
            sb.append('\\');
            sb.append('f');
            break;
        case '\r':
            sb.append('\\');
            sb.append('r');
            break;
        /*case '\'':
            if (escapeSingleQuote) {
              out.write('\\');
            }
            out.write('\'');
            break;*/
        case '\\':
            sb.append('\\');
            sb.append('\\');
            break;
        default:
            // TODO: this does not escape properly split characters. Should I care?  
            if (Character.isISOControl(ch)) {
                sb.append(CharUtils.unicodeEscaped(ch));
            } else {
                sb.append(ch);
            }
            break;
        }

    }

    return sb.toString();
}

From source file:com.microsoft.windowsazure.mobileservices.MobileServiceTableBase.java

/**
 * Validates if a given string contains a control character.
 * @param s/*from w  w  w.  j av  a 2 s.  com*/
 * @return
 */
protected boolean containsControlCharacter(String s) {
    boolean result = false;

    final int length = s.length();

    for (int offset = 0; offset < length;) {
        final int codepoint = s.codePointAt(offset);

        if (Character.isISOControl(codepoint)) {
            result = true;
            break;
        }

        offset += Character.charCount(codepoint);
    }

    return result;
}

From source file:cn.knet.showcase.demos.servletproxy.ProxyServlet.java

/**
 * Encodes characters in the query or fragment part of the URI.
 *
 * <p>Unfortunately, an incoming URI sometimes has characters disallowed by the spec.  HttpClient
 * insists that the outgoing proxied request has a valid URI because it uses Java's {@link URI}.
 * To be more forgiving, we must escape the problematic characters.  See the URI class for the
 * spec./* w w w .ja va  2 s  .co  m*/
 *
 * @param in example: name=value&foo=bar#fragment
 */
protected static CharSequence encodeUriQuery(CharSequence in) {
    //Note that I can't simply use URI.java to encode because it will escape pre-existing escaped things.
    StringBuilder outBuf = null;
    Formatter formatter = null;
    for (int i = 0; i < in.length(); i++) {
        char c = in.charAt(i);
        boolean escape = true;
        if (c < 128) {
            if (asciiQueryChars.get((int) c)) {
                escape = false;
            }
        } else if (!Character.isISOControl(c) && !Character.isSpaceChar(c)) {//not-ascii
            escape = false;
        }
        if (!escape) {
            if (outBuf != null)
                outBuf.append(c);
        } else {
            //escape
            if (outBuf == null) {
                outBuf = new StringBuilder(in.length() + 5 * 3);
                outBuf.append(in, 0, i);
                formatter = new Formatter(outBuf);
            }
            //leading %, 0 padded, width 2, capital hex
            formatter.format("%%%02X", (int) c);//TODO
        }
    }
    return outBuf != null ? outBuf : in;
}

From source file:it.geosdi.era.server.servlet.HTTPProxy.java

public static int escapeHtmlFull(int ch) {
    if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' || ch >= '0' && ch <= '9') {
        // safe//from  www .  jav a 2s  . c  o m
        return ch;
    } else if (Character.isWhitespace(ch)) {
        if (ch != '\n' && ch != '\r' && ch != '\t')
            // safe
            return ch;
    } else if (Character.isDefined(ch)) {
        // safe
        return ch;
    } else if (Character.isISOControl(ch)) {
        // paranoid version:isISOControl which are not isWhitespace
        // removed !
        // do nothing do not include in output !
        return -1;
    } else if (Character.isHighSurrogate((char) ch)) {
        // do nothing do not include in output !
        return -1;
    } else if (Character.isLowSurrogate((char) ch)) {
        // wrong char[] sequence, //TODO: LOG !!!
        return -1;
    }

    return -1;
}

From source file:org.owasp.appsensor.block.proxy.servlet.ProxyServlet.java

/**
 * Encodes characters in the query or fragment part of the URI.
 *
 * <p>Unfortunately, an incoming URI sometimes has characters disallowed by the spec.  HttpClient
 * insists that the outgoing proxied request has a valid URI because it uses Java's {@link URI}.
 * To be more forgiving, we must escape the problematic characters.  See the URI class for the
 * spec.//  w  w w.  ja  v  a2s  .c  om
 *
 * @param in example: name=value&foo=bar#fragment
 */
@SuppressWarnings("resource")
protected static CharSequence encodeUriQuery(CharSequence in) {
    //Note that I can't simply use URI.java to encode because it will escape pre-existing escaped things.
    StringBuilder outBuf = null;
    Formatter formatter = null;
    for (int i = 0; i < in.length(); i++) {
        char c = in.charAt(i);
        boolean escape = true;
        if (c < 128) {
            if (asciiQueryChars.get((int) c)) {
                escape = false;
            }
        } else if (!Character.isISOControl(c) && !Character.isSpaceChar(c)) {//not-ascii
            escape = false;
        }
        if (!escape) {
            if (outBuf != null)
                outBuf.append(c);
        } else {
            //escape
            if (outBuf == null) {
                outBuf = new StringBuilder(in.length() + 5 * 3);
                outBuf.append(in, 0, i);
                formatter = new Formatter(outBuf);
            }
            //leading %, 0 padded, width 2, capital hex
            formatter.format("%%%02X", (int) c);//TODO
        }
    }
    return outBuf != null ? outBuf : in;
}