Example usage for org.apache.commons.lang StringUtils isAlphanumeric

List of usage examples for org.apache.commons.lang StringUtils isAlphanumeric

Introduction

In this page you can find the example usage for org.apache.commons.lang StringUtils isAlphanumeric.

Prototype

public static boolean isAlphanumeric(String str) 

Source Link

Document

Checks if the String contains only unicode letters or digits.

Usage

From source file:org.apdplat.superword.tools.WordSources.java

/**
 * ?????/*ww  w . j a v a  2  s.  c  o  m*/
 * @param index ???0
 * @param files ??/
 * @return ??????
 */
public static Set<Word> get(int index, String... files) {
    Set<Word> set = new HashSet<>();
    for (String file : files) {
        URL url = null;
        if (file.startsWith("/")) {
            url = WordSources.class.getResource(file);
        } else {
            try {
                url = Paths.get(file).toUri().toURL();
            } catch (Exception e) {
                LOGGER.error("URL", e);
            }
        }
        if (url == null) {
            LOGGER.error("??" + file);
            continue;
        }
        System.out.println("parse word file: " + url);
        List<String> words = getExistWords(url);
        Set<Word> wordSet = words.parallelStream()
                .filter(line -> !line.trim().startsWith("#") && !"".equals(line.trim()))
                .filter(line -> line.trim().split("\\s+").length >= index + 1)
                .map(line -> new Word(line.trim().split("\\s+")[index], ""))
                .filter(word -> StringUtils.isAlphanumeric(word.getWord())).collect(Collectors.toSet());
        set.addAll(wordSet);
    }
    System.out.println("unique words count: " + set.size());
    return set;
}

From source file:org.carewebframework.ui.command.CommandUtil.java

/**
 * Returns true if the shortcut is a valid symbolic representation of a supported key press.
 * /*from  www  .j  a va2  s.  co m*/
 * @param shortcut Symbolic representation of a shortcut.
 * @return True if the shortcut is valid.
 */
/*package*/static boolean validateShortcut(String shortcut) {
    if (shortcut.startsWith("$")) {
        if (shortcut.startsWith("$#")) {
            shortcut = shortcut.substring(1);
        } else {
            return false;
        }
    }

    if (shortcut.startsWith("^") || shortcut.startsWith("@")) {
        shortcut = shortcut.substring(1);

        if (shortcut.length() == 1 && StringUtils.isAlphanumeric(shortcut)) {
            return true;
        }
    }

    if (!shortcut.startsWith("#")) {
        return false;
    }

    shortcut = shortcut.substring(1);
    return specialKeys.values().contains(shortcut);
}

From source file:org.cesecore.util.query.QueryGenerator.java

/**
 * Validates the provided name against our naming strategy ... columns with alphanumeric chars only.
 * //from   w w  w  .  j  ava2s. c  o m
 * @param name to be validated
 */
private void validate(final String name) {
    if (!StringUtils.isAlphanumeric(name)) {
        throw new QueryParameterException("parameter is not alphanumeric " + name);
    }
    if (!availableFields.contains(name)) {
        throw new QueryParameterException("parameter is not valid field" + name);
    }
}

From source file:org.cmis.portlets.news.web.validator.AttachmentOptionsValidator.java

/**
 * Validate the authorized extensions.//from w ww . j a v  a  2 s.  c  o  m
 * @param form
 * @param errors
 */
public void validateAuthorizedExts(final AttachmentOptionsForm form, final Errors errors) {
    String[] authorizedList = form.getAuthorizedExts().split(";");
    List<String> displayed = new ArrayList<String>();
    boolean valid = true;
    if (authorizedList != null) {
        for (String ext : authorizedList) {
            if (StringUtils.isNotEmpty(ext)) {
                if (!StringUtils.isAlphanumeric(ext)) {
                    valid = false;
                }
                displayed.add(ext);
            }
        }
    }
    if (!valid) {
        form.setAuthorizedList(displayed);
        errors.rejectValue("authorizedExts", "ATTACHMENT_EXT_WRONG_CHAR",
                "Files Extensions must only contain alpha numeric characters");
    }
}

From source file:org.cmis.portlets.news.web.validator.AttachmentOptionsValidator.java

/**
 * Validate the forbidden extensions./*from   ww w  .  j ava 2  s .  c  o m*/
 * @param form
 * @param errors
 */
public void validateForbiddenExts(final AttachmentOptionsForm form, final Errors errors) {
    String[] forbiddenList = form.getForbiddenExts().split(";");
    List<String> displayed = new ArrayList<String>();
    boolean valid = true;
    if (forbiddenList != null) {
        for (String ext : forbiddenList) {
            if (StringUtils.isNotEmpty(ext)) {
                if (!StringUtils.isAlphanumeric(ext)) {
                    valid = false;
                }
                displayed.add(ext);
            }
        }
    }
    if (!valid) {
        form.setForbiddenList(displayed);
        errors.rejectValue("forbiddenExts", "ATTACHMENT_EXT_WRONG_CHAR",
                "Files Extensions must only contain alpha numeric characters");
    }
}

From source file:org.codice.ddf.spatial.geocoding.extract.GeoNamesFileExtractor.java

/**
 * Determines the appropriate InputStream to get from the given file resource.  If there is no
 * file extension, the resource is downloaded from a url ( default : http://download.geonames.org/export/dump/ )
 * otherwise it is treated as an absolute path for a file.
 *
 * @param resource           - a String representing the resource.  Could be a data set from GeoNames
 *                           (allCities, cities15000, AU, US etc) or an absolute path.
 * @param extractionCallback - the callback to receive updates about the progress, may be
 *                           null if you don't want any updates
 * @return the InputStream for the given resource
 * @throws GeoEntryExtractionException     if an error occurs during extraction, obtaining the InputStream
 *                                         from a local resource, or if the resource is not a .zip or .txt file
 * @throws GeoNamesRemoteDownloadException if an error occurs when attempting to get an InputStream
 *                                         from a URL
 *//*  ww w. ja  va2 s.c o  m*/

private InputStream getInputStreamFromResource(String resource, ProgressCallback extractionCallback)
        throws GeoEntryExtractionException, GeoNamesRemoteDownloadException {

    if (FilenameUtils.isExtension(resource, "zip")) {
        InputStream inputStream = getFileInputStream(resource);
        return unZipInputStream(resource, inputStream);

    } else if (FilenameUtils.isExtension(resource, "txt")) {
        return getFileInputStream(resource);

    } else if (StringUtils.isAlphanumeric(resource)) {

        // Support the "all" keyword
        if (resource.equalsIgnoreCase("all")) {
            resource = "allCountries";
            // Support case insensitive "cities" keyword
        } else if (resource.matches("((?i)cities[0-9]+)")) {
            resource = resource.toLowerCase();
            // Support case insensitive country codes
        } else if (!resource.equalsIgnoreCase("allCountries")) {
            resource = resource.toUpperCase();
        }

        Response response = createConnection(resource);
        InputStream urlInputStream = getUrlInputStreamFromWebClient();
        InputStream inputStream = getInputStreamFromUrl(resource, response, urlInputStream, extractionCallback);

        return unZipInputStream(resource, inputStream);
    }
    throw new GeoEntryExtractionException("Unable to update the index.  " + resource
            + " is not a .zip or .txt, or an invalid country code was entered.");
}

From source file:org.dkpro.tc.api.features.util.FeatureUtil.java

public static String escapeFeatureName(String name) {

    // TODO Issue 120: improve the escaping
    // the fix was necessary due to Issue 32
    // http://code.google.com/p/dkpro-tc/issues/detail?id=32
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < name.length(); i++) {
        String c = name.substring(i, i + 1);
        if (StringUtils.isAlphanumeric(c) || c.equals("_")) {
            sb.append(c);//from w w w  .  j a  v  a2s  . c  o m
        } else {
            sb.append("u");
            sb.append(c.codePointAt(0));
        }
    }
    return sb.toString();
}

From source file:org.ebayopensource.turmeric.eclipse.ui.wizards.pages.typelib.ComplexTypeWizardAttribPage.java

@Override
protected boolean dialogChanged() {
    updateStatus(null);/*from www . j  a  v a 2  s. c om*/

    final Set<String> attribNames = new HashSet<String>();
    for (final AttribTableModel attribModel : attribHolder) {
        final String attribName = attribModel.getAttribName();
        if (!StringUtils.isAlphanumeric(attribName)) {
            updateStatus(this.attribViewer.getTable(), "Attribute name should be alphanumeric");
            return false;
        }
        if (attribNames.contains(attribName)) {
            updateStatus(this.attribViewer.getTable(), "Duplicate attrib name - " + attribName);
            return false;
        } else {
            updateStatus(null);
        }
        attribNames.add(attribName);
    }
    return true;
}

From source file:org.ebayopensource.turmeric.eclipse.ui.wizards.pages.typelib.ComplexTypeWizardElementPage.java

@Override
public boolean dialogChanged() {
    if (super.dialogChanged() == false)
        return false;

    boolean addButtonEnabled = true;
    if (getPreviousPage() instanceof ComplexTypeCCWizardGeneralPage) {
        final ComplexTypeCCWizardGeneralPage ccPage = (ComplexTypeCCWizardGeneralPage) getPreviousPage();
        if (ccPage.getRawBaseType() == null || StringUtils.isBlank(ccPage.getRawBaseType().toString())) {
            updatePageStatus(null, EclipseMessageUtils.createStatus(
                    "The extension type of the previous page need to be selected in order to add elements",
                    IStatus.WARNING));//from   ww  w.  ja v  a  2 s . c o m
            addButtonEnabled = false;
        }
    }

    if (super.typesViewer != null) {
        super.typesViewer.setAddButtonsEnabled(addButtonEnabled);

        if (addButtonEnabled == false)
            return false;
    }

    final Set<String> elementNames = new HashSet<String>();
    for (final IParameterElement elementModel : elements) {
        if (elementModel instanceof ElementTableModel) {
            final String elementValue = ((ElementTableModel) elementModel).getElementName();
            final int minOccursValue = ((ElementTableModel) elementModel).getMinOccurs();
            final int maxOccursValue = ((ElementTableModel) elementModel).getMaxOccurs();
            if (!StringUtils.isAlphanumeric(elementValue)) {
                updateStatus(super.typesViewer.getControl(), "Element name should be alphanumeric");
                return false;
            }
            if (minOccursValue < 0) {
                updateStatus(super.typesViewer.getControl(), "The min occurs must be greater than zero");
                return false;
            }
            if (maxOccursValue != -1) {
                if (maxOccursValue >= 0 && minOccursValue > maxOccursValue) {
                    updateStatus(super.typesViewer.getControl(), "The min occurs " + minOccursValue
                            + " cannot be greater than max occurs " + maxOccursValue);
                    return false;
                }
            }
            if (elementNames.contains(elementValue)) {
                updateStatus(super.typesViewer.getControl(), "Duplicate element name - " + elementValue);
                return false;
            } else {
                updateStatus(null);
            }
            elementNames.add(elementValue);
        }
    }
    return true;
}

From source file:org.exoplatform.ecm.webui.form.UIDialogForm.java

public void addSelectBoxField(String name, String label, String[] arguments) throws Exception {
    UIFormSelectBoxField formSelectBoxField = new UIFormSelectBoxField(name, label, arguments);
    String jcrPath = formSelectBoxField.getJcrPath();
    String editable = formSelectBoxField.getEditable();
    String onchange = formSelectBoxField.getOnchange();
    String defaultValue = formSelectBoxField.getDefaultValue();
    String options = formSelectBoxField.getOptions();
    String script = formSelectBoxField.getGroovyScript();
    if (editable == null)
        formSelectBoxField.setEditable("true");
    List<SelectItemOption<String>> optionsList = new ArrayList<SelectItemOption<String>>();
    UIFormSelectBox uiSelectBox = findComponentById(name);
    boolean isFirstTimeRender = false;
    if (uiSelectBox == null || isResetForm) {
        isFirstTimeRender = true;//from w  w w .ja  va2s.  com
        uiSelectBox = new UIFormSelectBox(name, name, null);
        if (script != null) {
            try {
                String[] scriptParams = formSelectBoxField.getScriptParams();
                executeScript(script, uiSelectBox, scriptParams, true);
            } catch (Exception e) {
                if (LOG.isErrorEnabled()) {
                    LOG.error("An unexpected error occurs", e);
                }
                uiSelectBox.setOptions(optionsList);
            }
        } else if (options != null && options.length() > 0) {
            String[] array = options.split(",");
            RequestContext context = RequestContext.getCurrentInstance();
            ResourceBundle res = context.getApplicationResourceBundle();
            String optionLabel;
            for (int i = 0; i < array.length; i++) {
                List<String> listValue = new ArrayList<String>();
                String[] arrayChild = array[i].trim().split(SEPARATOR_VALUE);
                for (int j = 0; j < arrayChild.length; j++) {
                    if (!arrayChild[j].trim().equals("")) {
                        listValue.add(arrayChild[j].trim());
                    }
                }
                try {
                    String tagName = listValue.get(0).replaceAll(" ", "-");
                    optionLabel = res.getString(tagName);
                } catch (MissingResourceException e) {
                    optionLabel = listValue.get(0);
                }
                if (listValue.size() > 1) {
                    optionsList.add(new SelectItemOption<String>(optionLabel, listValue.get(1)));
                } else {
                    optionsList.add(new SelectItemOption<String>(optionLabel, listValue.get(0)));
                }
            }
            uiSelectBox.setOptions(optionsList);
        } else {
            uiSelectBox.setOptions(optionsList);
        }
        if (defaultValue != null)
            uiSelectBox.setValue(defaultValue);
    }

    propertiesName.put(name, getPropertyName(jcrPath));
    fieldNames.put(getPropertyName(jcrPath), name);
    if (formSelectBoxField.validateType != null) {
        DialogFormUtil.addValidators(uiSelectBox, formSelectBoxField.validateType);
    }
    String[] arrNodes = jcrPath.split("/");
    Node childNode = null;
    Node node = getNode();
    String propertyName = getPropertyName(jcrPath);
    if (node != null && arrNodes.length == 4)
        childNode = node.getNode(arrNodes[2]);
    if (formSelectBoxField.isMultiValues()) {
        if (formSelectBoxField.getSize() != null && StringUtils.isAlphanumeric(formSelectBoxField.getSize())) {
            uiSelectBox.setSize(Integer.parseInt(formSelectBoxField.getSize()));
        }
        uiSelectBox.setMultiple(true);
        StringBuffer buffer = new StringBuffer();
        if ((childNode != null) && isFirstTimeRender && childNode.hasProperty(propertyName)) {
            List<String> valueList = new ArrayList<String>();
            Value[] values = childNode.getProperty(propertyName).getValues();
            for (Value value : values) {
                buffer.append(value.getString()).append(",");
            }
            uiSelectBox.setSelectedValues(StringUtils.split(buffer.toString(), ","));
        } else {
            if (node != null && isFirstTimeRender && node.hasProperty(propertyName)) {
                List<String> valueList = new ArrayList<String>();
                if (node.getProperty(propertyName).getDefinition().isMultiple()
                        && (!"true".equals(onchange) || !isOnchange)) {
                    Value[] values = node.getProperty(propertyName).getValues();
                    for (Value value : values) {
                        buffer.append(value.getString()).append(",");
                    }
                } else if ("true".equals(onchange) && isOnchange) {
                    if (uiSelectBox.isMultiple()) {
                        String[] values = uiSelectBox.getSelectedValues();
                        for (String value : values) {
                            buffer.append(value).append(",");
                        }
                    } else {
                        String values = uiSelectBox.getValue();
                        buffer.append(values).append(",");
                    }
                } else {
                    Value[] values = node.getProperty(propertyName).getValues();
                    for (Value value : values) {
                        buffer.append(value.getString()).append(",");
                    }
                }
                uiSelectBox.setSelectedValues(StringUtils.split(buffer.toString(), ","));
            }
        }
    } else {
        if ((childNode != null) && isFirstTimeRender && childNode.hasProperty(propertyName)) {
            uiSelectBox.setValue(childNode.getProperty(propertyName).getValue().getString());
        } else {
            if (node != null && node.hasProperty(propertyName)) {
                if (node.getProperty(propertyName).getDefinition().isMultiple()) {
                    if (findComponentById(name) == null)
                        uiSelectBox.setValue(node.getProperty(propertyName).getValues().toString());
                } else if (formSelectBoxField.isOnchange() && isOnchange) {
                    uiSelectBox.setValue(uiSelectBox.getValue());
                } else {
                    if (findComponentById(name) == null)
                        uiSelectBox.setValue(node.getProperty(propertyName).getValue().getString());
                }
            }
        }
    }
    uiSelectBox.setEditable(formSelectBoxField.isEditable());
    //    addUIFormInput(uiSelectBox);
    if (isNotEditNode) {
        Node child = getChildNode();
        if (child != null && child.hasProperty(propertyName)) {
            if (child.getProperty(propertyName).getDefinition().isMultiple()) {
                Value[] values = child.getProperty(propertyName).getValues();
                List<String> listValues = new ArrayList<String>();
                for (Value value : values) {
                    listValues.add(value.getString());
                }
                uiSelectBox.setSelectedValues(listValues.toArray(new String[listValues.size()]));
            } else {
                uiSelectBox.setValue(DialogFormUtil.getPropertyValueAsString(child, propertyName));
            }
        }
    }
    if (formSelectBoxField.isOnchange())
        uiSelectBox.setOnChange("Onchange");
    if (findComponentById(name) == null)
        addUIFormInput(uiSelectBox);

    StringBuilder newValues = new StringBuilder();
    int count = 0;
    for (String v : ((UIFormSelectBox) findComponentById(name)).getSelectedValues()) {
        if (count++ > 0)
            newValues.append(",");
        newValues.append(v);
    }
    String newValue = newValues.toString();
    JcrInputProperty inputProperty = properties.get(name);
    if (inputProperty == null) {
        inputProperty = new JcrInputProperty();
        inputProperty.setJcrPath(jcrPath);
        inputProperty.setChangeInJcrPathParam(formSelectBoxField.getChangeInJcrPathParam());
        setInputProperty(name, inputProperty);
    } else {
        if (inputProperty.getValue() != null) {
            String oldValue = inputProperty.getValue().toString();
            if ((oldValue != null) && (!oldValue.equals(newValue))) {
                Iterator componentSelector = componentSelectors.keySet().iterator();
                Map<String, String> obj = null;
                while (componentSelector.hasNext()) {
                    String componentName = (String) componentSelector.next();
                    obj = (Map<String, String>) componentSelectors.get(componentName);
                    Set<String> set = obj.keySet();
                    for (String key : set) {
                        if (name.equals(obj.get(key))) {
                            UIComponent uiInput = findComponentById(componentName);
                            ((UIFormStringInput) uiInput).reset();
                        }
                    }
                }
            }
        }
    }
    inputProperty.setValue(newValue);
    if (isUpdateSelect && newValue != null) {
        String[] values1 = newValue.split(",");
        uiSelectBox.setSelectedValues(values1);
    }
    renderField(name);
}