Example usage for java.lang Character isLetterOrDigit

List of usage examples for java.lang Character isLetterOrDigit

Introduction

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

Prototype

public static boolean isLetterOrDigit(int codePoint) 

Source Link

Document

Determines if the specified character (Unicode code point) is a letter or digit.

Usage

From source file:com.welfare.common.util.UtilValidate.java

/**
 * Returns true if character c is a letter or digit.
 *//*w ww .  j  a  va 2 s.c  o m*/
public static boolean isLetterOrDigit(char c) {
    return Character.isLetterOrDigit(c);
}

From source file:Util.java

/**
 * Replace all the occurences of HTML escape strings with the
 * respective characters./*from w ww.  ja  va  2 s.  co m*/
 *
 * @param s a <code>String</code> value
 * @return a <code>String</code> value
 */
public static final String unescapeHTML(String s) {
    char[] chars = s.toCharArray();
    char[] escaped = new char[chars.length];

    // Note: escaped[pos] = end of the escaped char array.
    int pos = 0;

    for (int i = 0; i < chars.length;) {
        if (chars[i] != '&') {
            escaped[pos++] = chars[i++];
            continue;
        }

        // Allow e.g. &#123;
        int j = i + 1;
        if (j < chars.length && chars[j] == '#')
            j++;

        // Scan until we find a char that is not letter or digit.
        for (; j < chars.length; j++) {
            if (!Character.isLetterOrDigit(chars[j]))
                break;
        }

        boolean replaced = false;
        if (j < chars.length && chars[j] == ';') {
            if (s.charAt(i + 1) == '#') { // Check for &#D; and &#xD; pattern
                try {
                    long charcode = 0;
                    char ch = s.charAt(i + 2);
                    if (ch == 'x' || ch == 'X') {
                        charcode = Long.parseLong(new String(chars, i + 3, j - i - 3), 16);
                    } else if (Character.isDigit(ch)) {
                        charcode = Long.parseLong(new String(chars, i + 2, j - i - 2));
                    }
                    if (charcode > 0 && charcode < 65536) {
                        escaped[pos++] = (char) charcode;
                        replaced = true;
                    }
                } catch (NumberFormatException ex) {
                    // Failed, not replaced.
                }

            } else {
                String key = new String(chars, i, j - i + 1);
                Character repl = escapeStrings.get(key);
                if (repl != null) {
                    escaped[pos++] = repl.charValue();
                    replaced = true;
                }
            }
            j++; // Skip over ';'
        }

        if (!replaced) {
            // Not a recognized escape sequence, leave as-is
            System.arraycopy(chars, i, escaped, pos, j - i);
            pos += j - i;
        }
        i = j;
    }
    return new String(escaped, 0, pos);
}

From source file:org.infoglue.cms.applications.common.VisualFormatter.java

public final String replaceNonAscii(String s, String character) {
    if (s == null)
        return null;

    StringBuffer sb = new StringBuffer();
    int n = s.length();
    for (int i = 0; i < n; i++) {
        char c = s.charAt(i);
        if (c < 128 && c > 32) {
            if (Character.isLetterOrDigit(c) || c == '-' || c == '_' || c == '.')
                sb.append(c);//  w ww.j a v  a  2 s . c  o  m
            else
                sb.append(character);
        } else {
            sb.append(character);
        }
    }
    return sb.toString();
}

From source file:org.zeroturnaround.jenkins.LiveRebelProxy.java

public String sanitize(String name) {
    StringBuffer sb = new StringBuffer(name);
    for (int i = 0; i < sb.length(); i++)
        if (!Character.isLetterOrDigit(sb.charAt(i)))
            sb.setCharAt(i, '-');
    sb.append("-");
    sb.append(DigestUtils.md5Hex(name));
    return sb.toString();
}

From source file:com.openedit.util.PathUtilities.java

public static String extractId(String inName, boolean inAllowUnderstores) {
    StringBuffer out = new StringBuffer(inName.length());
    for (int i = 0; i < inName.length(); i++) {
        char c = inName.charAt(i);
        if (Character.isLetterOrDigit(c)) {
            out.append(c);//from   ww  w  . j a  v a2 s. c om
        } else if (inAllowUnderstores) {
            if (c == '_') {
                out.append(c);
            } else if (c == ' ') {
                out.append("_");
            }
        }
    }
    String result = out.toString().toLowerCase();
    result = URLUtilities.escapeUtf8(result);
    //CVS fails to save this. Should use the HEX number
    /*      result = result.replace('', 'a');
    result = result.replace('', 'e');
    result = result.replace('', 'i');
    result = result.replace('', 'o');
    result = result.replace('', 'u');
    */
    return result;
}

From source file:edu.umd.cfar.lamp.viper.util.StringHelp.java

/**
 * Checks to see if a String is a legal identifier.
 * Uses java/c++ standards, execept it cannot start with
 * an underscore.//from ww w  .  ja  v a 2  s.c  o  m
 *
 * @param str The String to check.
 * @return <code>true</code> if the String uses valid characters.
 */
public static boolean isLegalIdentifier(String str) {
    if (str == null)
        return (false);

    char c = str.charAt(0);
    if (!Character.isLetter(c)) {
        return (false);
    }
    int size = str.length();
    for (int i = 1; i < size; i++) {
        c = str.charAt(i);
        if (!(Character.isLetterOrDigit(c) || c == '_' || c == '-'))
            return (false);
    }
    return (true);
}

From source file:hydrograph.ui.graph.editor.ELTGraphicalEditor.java

/**
 * Add mouse listener on canvas//from w ww  .  j  a  va  2  s. c o  m
 * 
 */
public void attachCanvasMouseListeners() {

    //TODO - please do not remove this code for now. This code will be useful in resolving stop button bugs 
    /*viewer.getControl().addControlListener(new ControlListener() {
               
       @Override
       public void controlResized(ControlEvent e) {
            
    String consoleName;
    consoleName = (getActiveProject() + "." + getTitle().replace(".job", ""));
            
    Job job = JobManager.INSTANCE.getRunningJob(consoleName);
            
    if(job!=null){
       if(JobStatus.KILLED.equals(job.getJobStatus())){
          ((RunJobHandler)RunStopButtonCommunicator.RunJob.getHandler()).setRunJobEnabled(false);
          ((StopJobHandler)RunStopButtonCommunicator.StopJob.getHandler()).setStopJobEnabled(false);
       }else{
          if(job.isRemoteMode()){
             enableRunJob(false);
          }else{
             ((RunJobHandler)RunStopButtonCommunicator.RunJob.getHandler()).setRunJobEnabled(false);
             ((StopJobHandler)RunStopButtonCommunicator.StopJob.getHandler()).setStopJobEnabled(false);
          }                  
       }
               
    }else{
       if(RunStopButtonCommunicator.StopJob.getHandler()!=null)
       enableRunJob(true);
    }
               
       };
               
       @Override
       public void controlMoved(ControlEvent e) {
    // TODO Auto-generated method stub
            
       }
    });*/

    viewer.getControl().addFocusListener(new FocusListener() {

        @Override
        public void focusLost(FocusEvent e) {
            if (componentTooltip != null && !componentTooltip.isToolBarToolTip()) {
                hideToolTip();
            }

        }

        @Override
        public void focusGained(FocusEvent e) {
            for (Iterator<EditPart> ite = viewer.getEditPartRegistry().values().iterator(); ite.hasNext();) {
                EditPart editPart = (EditPart) ite.next();
                if (editPart instanceof ComponentEditPart) {
                    hydrograph.ui.graph.model.Component component = ((ComponentEditPart) editPart)
                            .getCastedModel();
                    if (component instanceof SubjobComponent) {
                        ((ComponentEditPart) editPart).updateSubjobComponent(
                                (ComponentFigure) ((ComponentEditPart) editPart).getFigure());
                    }
                }
            }
            viewer.getEditDomain().loadDefaultTool();
        }
    });

    viewer.getControl().addKeyListener(new KeyListener() {

        @Override
        public void keyReleased(KeyEvent e) {
            setCustomToolUndoRedoStatus();

        }

        @Override
        public void keyPressed(KeyEvent event) {
            if (((event.stateMask & (SWT.CTRL | SWT.COMMAND)) != 0
                    && (event.keyCode == SWT.ARROW_DOWN || event.keyCode == SWT.ARROW_LEFT
                            || event.keyCode == SWT.ARROW_RIGHT || event.keyCode == SWT.ARROW_UP))) {

                moveComponentWithArrowKey(event);
            } else {
                setCustomToolUndoRedoStatus();
                hideToolTip();
                if (event.stateMask == 0) {

                    if (Character.isLetterOrDigit(event.character)) {
                        new ComponentSearchUtility().showComponentCreationOnCanvas(event, viewer, paletteRoot);
                        setDirty(true);
                    } else if (((event.stateMask & (SWT.CTRL | SWT.COMMAND)) != 0 && (event.keyCode == SWT.SHIFT
                            || event.keyCode == SWT.ALT || event.keyCode == SWT.BS))) {
                        return;
                    }
                }

            }
        }
    });

    viewer.getControl().addMouseListener(new MouseListener() {

        @Override
        public void mouseUp(MouseEvent e) {
            if (toolTipComponentBounds != null && componentTooltip != null) {
                org.eclipse.swt.graphics.Point point = new org.eclipse.swt.graphics.Point(e.x, e.y);
                if (!toolTipComponentBounds.contains(point)) {
                    hideToolTip();
                }
            }
            setCustomToolUndoRedoStatus();
        }

        @Override
        public void mouseDown(MouseEvent e) {
            setCustomToolUndoRedoStatus();
            org.eclipse.swt.graphics.Point p = new org.eclipse.swt.graphics.Point(e.x, e.y);
            location = new Point(p);
        }

        @Override
        public void mouseDoubleClick(MouseEvent e) {
            setCustomToolUndoRedoStatus();
        }
    });

    viewer.getControl().addMouseMoveListener(new MouseMoveListener() {

        @Override
        public void mouseMove(MouseEvent e) {
            setCustomToolUndoRedoStatus();
            if (toolTipComponentBounds != null && componentTooltip != null) {
                if (!componentTooltip.hasToolBarManager()) {
                    org.eclipse.swt.graphics.Point point = new org.eclipse.swt.graphics.Point(e.x, e.y);
                    if (!toolTipComponentBounds.contains(point)) {
                        hideToolTip();
                    }
                }
            }
        }
    });
}

From source file:org.yamj.core.tools.MetadataTools.java

/**
 * Set the sort title.//from   w  w w .ja va  2  s .  co m
 *
 * @param metadata the scanned metadata
 * @param prefixes a list with prefixed to strip
 */
public static void setSortTitle(AbstractMetadata metadata, List<String> prefixes) {
    String sortTitle;
    if (StringUtils.isBlank(metadata.getTitleSort())) {
        sortTitle = StringUtils.stripAccents(metadata.getTitle());

        // strip prefix
        for (String prefix : prefixes) {
            String check = prefix.trim() + " ";
            if (StringUtils.startsWithIgnoreCase(sortTitle, check)) {
                sortTitle = sortTitle.substring(check.length());
                break;
            }
        }
    } else {
        sortTitle = StringUtils.stripAccents(metadata.getTitleSort());
    }

    // first char must be a letter or digit
    int idx = 0;
    while (idx < sortTitle.length() && !Character.isLetterOrDigit(sortTitle.charAt(idx))) {
        idx++;
    }

    // replace all non-standard characters in the title sort
    sortTitle = MetadataTools.stringMapReplacement(sortTitle.substring(idx));
    metadata.setTitleSort(sortTitle);
}

From source file:eionet.cr.web.action.admin.harvestscripts.HarvestScriptParser.java

/**
 *
 * @param str//from  www  . j a  v  a2  s.c om
 * @param tokenToReplace
 * @param replacement
 * @return
 */
private static String replaceToken(String str, String tokenToReplace, String replacement) {

    if (str == null || str.trim().length() == 0 || tokenToReplace == null
            || tokenToReplace.trim().length() == 0) {
        return str;
    }

    StringTokenizer st = new StringTokenizer(str, " \t\n\r\f", true);
    ArrayList<String> originalTokens = new ArrayList<String>();
    ArrayList<String> upperCaseTokens = new ArrayList<String>();
    while (st.hasMoreTokens()) {
        String nextToken = st.nextToken();
        originalTokens.add(nextToken);
        upperCaseTokens.add(nextToken.toUpperCase());
    }

    StringBuilder buf = new StringBuilder();
    for (String token : originalTokens) {

        if (token.equalsIgnoreCase(tokenToReplace)) {
            buf.append(replacement);
        } else if (StringUtils.startsWithIgnoreCase(token, tokenToReplace)) {
            if (!Character.isLetterOrDigit(token.charAt(tokenToReplace.length()))) {
                buf.append(replacement).append(StringUtils.substringAfter(token, tokenToReplace));
            } else {
                buf.append(token);
            }
        } else {
            buf.append(token);
        }
    }
    return buf.toString();
}

From source file:org.infoglue.cms.applications.common.VisualFormatter.java

/**
 * This method replaces all non-ascii-characters with a corresponding one defined in the system properties-object. 
 * If not defined there it replaces the char with the default character.
 * @param s/*from  ww w  .  j  a v a2  s. co  m*/
 * @param defaultCharacter
 * @return
 */

public final String replaceNiceURINonAsciiWithSpecifiedChars(String s, String defaultCharacter) {
    if (s == null)
        return null;

    boolean toLowerCase = CmsPropertyHandler.getNiceURIUseLowerCase();
    Properties properties = CmsPropertyHandler.getCharacterReplacingMapping();

    StringBuffer sb = new StringBuffer();
    int n = s.length();
    for (int i = 0; i < n; i++) {
        char c = s.charAt(i);
        if (c < 128 && c > 32) {
            if (Character.isLetterOrDigit(c) || c == '-' || c == '_' || c == '.')
                sb.append(c);
            else {
                String replaceChar = properties.getProperty("" + c);
                if (replaceChar != null && !replaceChar.equals(""))
                    sb.append(replaceChar);
                else if (defaultCharacter != null && !defaultCharacter.equalsIgnoreCase("none"))
                    sb.append(defaultCharacter);
            }
        } else {
            String replaceChar = properties.getProperty("" + c);
            if (replaceChar != null && !replaceChar.equals(""))
                sb.append(replaceChar);
            else if (defaultCharacter != null && !defaultCharacter.equalsIgnoreCase("none"))
                sb.append(defaultCharacter);
        }
    }

    return (toLowerCase ? sb.toString().toLowerCase() : sb.toString());
}