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

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

Introduction

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

Prototype

public static boolean isAsciiPrintable(String str) 

Source Link

Document

Checks if the string contains only ASCII printable characters.

Usage

From source file:com.skype.bot.event.AdminSetAlias.java

@Override
public void EventFunction(ChatMessage msg) throws SkypeException {
    String[] s = msg.getContent().split(" ");

    if (s.length != 3) {
        return;//from w  w w.j  a va  2s. c om
    }

    if (!SkypeBot.UserRecords().ContainsUser(msg, s[1])) {
        msg.getChat().send(s[1] + " isn't in this chat");
    }

    for (UserRecord u : SkypeBot.UserRecords()) {
        if (u.getAlias().equalsIgnoreCase(s[2])) {
            msg.getChat().send(s[2] + " is already registered to " + u.getSkypeID());
            return;
        }
    }

    if (!StringUtils.isAsciiPrintable(s[2])) {
        msg.getChat().send("Alphanumeric characters only please");
        return;
    }

    if (s[2].length() > 25) {
        msg.getChat().send("Alias must be less than 25 characters");
        return;
    }

    UserRecord user = SkypeBot.UserRecords().GetUser(s[1]);
    user.setAlias(s[2]);
    msg.getChat().send(user.getSkypeID() + "'s alias registered as " + user.getAlias());
    Skype.getUser(user.getSkypeID()).setDisplayName(user.getDisplayName());

    SkypeBot.UserRecords().SaveAlias();
}

From source file:com.skype.bot.event.EventRegisterAlias.java

@Override
public void EventFunction(ChatMessage msg) throws SkypeException {
    String[] s = msg.getContent().split(" ");

    if (s.length != 2) {
        return;/*  w  w  w .  j a v  a 2  s .  c  o  m*/
    }

    for (UserRecord u : SkypeBot.UserRecords()) {
        if (u.getAlias().equalsIgnoreCase(s[1])) {
            msg.getChat().send(s[1] + " is already registered to " + u.getSkypeID());
            return;
        }
    }

    UserRecord user = SkypeBot.UserRecords().GetUser(msg.getSenderId());

    if (!StringUtils.isAsciiPrintable(s[1])) {
        msg.getChat().send("Alphanumeric characters only please");
        return;
    }

    if (s[1].length() > 25) {
        msg.getChat().send("Alias must be less than 25 characters");
        return;
    }

    user.setAlias(s[1]);
    msg.getChat().send(user.getSkypeID() + "'s alias registered as " + user.getAlias());
    msg.getSender().setDisplayName(user.getDisplayName());

    SkypeBot.UserRecords().SaveAlias();

}

From source file:com.mmd.mssp.util.WebUtil.java

public static String getHttpQueryParam(HttpServletRequest request, String key, String charset)
        throws UnsupportedEncodingException {
    String queryString = request.getQueryString();
    if (!StringUtils.isAsciiPrintable(queryString)) {// ? ASCII ??
        byte[] bytes = queryString.getBytes("iso-8859-1");
        queryString = new String(bytes, charset);
    }//from   ww  w.  j a va2s . com
    String val = StringUtil.searchKeyValue(queryString, key);
    if (val != null) {
        val = URLDecoder.decode(val, charset);
    }
    request.setAttribute("QueryDecodeCharset", val);
    return val;
}

From source file:com.googlecode.fascinator.common.MimeTypeUtil.java

/**
 * Print Exception in readable format//from w ww.j av a  2  s.  com
 * 
 * @param e Exception
 * @return Readable version of Exception
 */
private static String toPrintable(Exception e) {
    String msg = e.getMessage();
    if (StringUtils.isAsciiPrintable(msg)) {
        return msg;
    }
    return StringUtils.left(msg.replaceAll("[^\\p{ASCII}\\n]", "."), 32);
}

From source file:com.blackducksoftware.tools.commonframework.core.encoding.Ascii85EncoderTest.java

private boolean isAscii(final byte[] bytes) {
    final String stringToTest = new String(bytes, UTF8);
    return StringUtils.isAsciiPrintable(stringToTest);
}

From source file:de.xirp.ui.widgets.panels.virtual.MazeComposite.java

/**
 * //from w  w  w  .ja v  a2s .c o  m
 */
private void init() {
    listener = new ATEAdapter() {

        @Override
        public void classListChanged() {
            classes.removeAll();
            for (String s : ATEManager.getMazeJavaClasses()) {
                classes.add(s);
            }
        }

    };
    ATEManager.addATEListener(listener);

    addDisposeListener(new DisposeListener() {

        public void widgetDisposed(DisposeEvent e) {
            ATEManager.removeATEListener(listener);
        }

    });

    SWTUtil.setGridLayout(this, 5, true);

    mc = new MazeCanvas(this, 10, 10);
    SWTUtil.setGridData(mc, false, false, SWT.FILL, SWT.FILL, 4, 3);

    XGroup constantsGroup = new XGroup(this, SWT.NONE);
    constantsGroup.setText("Constants"); //$NON-NLS-1$
    SWTUtil.setGridData(constantsGroup, true, false, SWT.FILL, SWT.FILL, 1, 1);
    SWTUtil.setGridLayout(constantsGroup, 2, true);

    for (MazeField mf : MazeField.values()) {
        XLabel reward = new XLabel(constantsGroup, SWT.NONE);
        reward.setText(mf.name() + " - Reward:"); //$NON-NLS-1$
        SWTUtil.setGridData(reward, true, false, SWT.FILL, SWT.FILL, 1, 1);

        XStyledSpinner spinner = new XStyledSpinner(constantsGroup, SWT.BORDER, SpinnerStyle.ALL);
        SWTUtil.setGridData(spinner, true, false, SWT.FILL, SWT.FILL, 1, 1);
        spinner.setData("name", mf.name()); //$NON-NLS-1$

        ATEManager.setConstant(mf.name(), 0.0);

        spinner.addValueChangedListener(new ValueChangedListener() {

            public void valueChanged(ValueChangedEvent event) {
                XStyledSpinner sp = (XStyledSpinner) event.getSource();
                ATEManager.setConstant((String) sp.getData("name"), event.getPreciseValue()); //$NON-NLS-1$
            }
        });
    }

    XGroup variablesGroup = new XGroup(this, SWT.NONE);
    variablesGroup.setText("Variables"); //$NON-NLS-1$
    SWTUtil.setGridData(variablesGroup, true, true, SWT.FILL, SWT.FILL, 1, 1);
    SWTUtil.setGridLayout(variablesGroup, 2, true);

    varName = new XTextField(variablesGroup, SWT.NONE);
    SWTUtil.setGridData(varName, true, false, SWT.FILL, SWT.FILL, 2, 1);
    varName.addModifyListener(new ModifyListener() {

        public void modifyText(ModifyEvent e) {
            XTextField tf = (XTextField) e.widget;
            String text = tf.getText();
            if (text.length() > 0 && StringUtils.isAsciiPrintable(text) && StringUtils.isAlpha(text)
                    && !variables.contains(text)) {
                add.setEnabled(true);
            } else {
                add.setEnabled(false);
            }
        }

    });

    add = new XButton(variablesGroup);
    add.setText("Add"); //$NON-NLS-1$
    add.setEnabled(false);
    SWTUtil.setGridData(add, true, false, SWT.FILL, SWT.FILL, 2, 1);
    add.addSelectionListener(new SelectionAdapter() {

        /* (non-Javadoc)
         * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
         */
        @Override
        public void widgetSelected(SelectionEvent e) {
            XTableItem itm = new XTableItem(table, SWT.NONE);
            String text = varName.getText();

            variables.add(text);
            itm.setText(0, text);
            //TODO
            itm.setText(1, "23.0"); //$NON-NLS-1$

            SWTUtil.packTable(table);
            add.setEnabled(false);

            ATEManager.setVariable(text, 23.0);
        }

    });

    table = new XTable(variablesGroup, SWT.FULL_SELECTION | SWT.SINGLE | SWT.BORDER);
    table.setLinesVisible(true);
    table.setHeaderVisible(true);

    String[] titles = { "Variable", "Value" }; //$NON-NLS-1$ //$NON-NLS-2$
    for (int i = 0; i < titles.length; i++) {
        XTableColumn column = new XTableColumn(table, SWT.NONE);
        column.setText(titles[i]);
    }

    SWTUtil.packTable(table);
    SWTUtil.setGridData(table, true, true, SWT.FILL, SWT.FILL, 2, 1);

    remove = new XButton(variablesGroup);
    remove.setText("Remove selected"); //$NON-NLS-1$
    SWTUtil.setGridData(remove, true, false, SWT.FILL, SWT.FILL, 2, 1);
    remove.addSelectionListener(new SelectionAdapter() {

        /* (non-Javadoc)
         * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
         */
        @Override
        public void widgetSelected(SelectionEvent e) {
            try {
                int idx = table.getSelectionIndex();
                String text = table.getItem(idx).getText(0);
                table.remove(idx);
                variables.remove(text);
            } catch (IllegalArgumentException ex) {
            }
        }

    });

    XGroup exeGroup = new XGroup(this, SWT.NONE);
    exeGroup.setText("Execute"); //$NON-NLS-1$
    SWTUtil.setGridData(exeGroup, true, false, SWT.FILL, SWT.FILL, 1, 1);
    SWTUtil.setGridLayout(exeGroup, 2, true);

    classes = new XCombo(exeGroup, SWT.BORDER | SWT.READ_ONLY);
    for (String s : ATEManager.getMazeJavaClasses()) {
        classes.add(s);
    }
    classes.addSelectionListener(new SelectionAdapter() {

        /* (non-Javadoc)
         * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
         */
        @Override
        public void widgetSelected(SelectionEvent e) {
            XCombo c = (XCombo) e.widget;
            if (!Util.isEmpty(c.getText())) {
                execute.setEnabled(true);
            }
        }

    });
    SWTUtil.setGridData(classes, true, false, SWT.FILL, SWT.FILL, 2, 1);

    execute = new XButton(exeGroup);
    execute.setText("Execute code"); //$NON-NLS-1$
    execute.setEnabled(false);
    SWTUtil.setGridData(execute, true, false, SWT.FILL, SWT.FILL, 2, 1);
    execute.addSelectionListener(new SelectionAdapter() {

        /* (non-Javadoc)
         * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
         */
        @Override
        public void widgetSelected(SelectionEvent e) {
            ATEManager.execute(classes.getText());
        }

    });
}

From source file:com.spidertracks.datanucleus.query.runtime.EqualityOperand.java

@Override
public void toString(final StringBuilder sb) {
    final List<IndexExpression> expList = this.clause.getExpressions();
    int i;//from  w  w w. java  2 s  .c  om
    for (i = 0; i < expList.size(); i++) {
        if (i > 0) {
            sb.append(" AND ");
        }
        final IndexExpression exp = expList.get(i);
        sb.append(new String(exp.column_name.array()));
        switch (exp.op) {
        case EQ:
            sb.append(" = ");
            break;
        case GTE:
            sb.append(" >= ");
            break;
        case GT:
            sb.append(" > ");
            break;
        case LTE:
            sb.append(" <= ");
            break;
        case LT:
            sb.append(" < ");
            break;
        default:
            throw new RuntimeException("Unhandled operand [" + exp.op + "]");
        }
        ;

        final String val = new String(exp.value.array());

        if (!StringUtils.isAsciiPrintable(val)) {
            sb.append("hex('");
            sb.append(new String(Hex.encodeHex(exp.value.array())));
            sb.append("')");
        } else {
            sb.append('\'').append(val.replace("\\", "\\\\").replace("'", "\\'")).append('\'');
        }
    }
    sb.append(" ");
}

From source file:de.tudarmstadt.ukp.dkpro.wsd.wsi.io.AMBIENTReader.java

private void downloadHTMLPage(JCas jCas, final Result result) {
    try {/*from  w w w.  j ava2 s  .c om*/
        URL inputURL = new URL(URLDecoder.decode(result.url));
        URLConnection conn = inputURL.openConnection();
        conn.setReadTimeout(30000);
        InputStream is = conn.getInputStream();

        String text;

        CharsetDetector detector = new CharsetDetector();
        detector.enableInputFilter(true);

        try {
            text = IOUtils.toString(detector.getReader(is, null));

        } catch (Exception e) {
            text = IOUtils.toString(is);
        }
        StringBuffer cleanedText = new StringBuffer(Jsoup.parse(text).text());

        int index = XMLUtils.checkForNonXmlCharacters(cleanedText.toString(), false);
        while (index > -1) {
            cleanedText.delete(index, index + 1);
            index = XMLUtils.checkForNonXmlCharacters(cleanedText.toString(), false);
        }
        if (StringUtils.isAsciiPrintable(cleanedText.toString())) {
            jCas.setDocumentText(result.text + " " + cleanedText.toString());
        } else {
            jCas.setDocumentText(result.text);
        }
    } catch (Exception e) {
        getLogger().warn("Connection to " + result.url + " timed out/failed, using snippet only");
        e.printStackTrace();
        getLogger().warn(e);
        jCas.setDocumentText(result.text);
    }
}

From source file:com.flexive.ejb.beans.PhraseEngineBean.java

/**
 * Check if a phrase key is valid, will throw an exception if invalid
 *
 * @param key phrase key to check/*from ww w . j a  v  a 2  s.c o m*/
 */
private void checkPhraseKey(String key) {
    if (StringUtils.isBlank(key))
        throw new FxApplicationException("ex.phrase.key.empty").asRuntimeException();
    if (!key.equals(key.trim()))
        throw new FxApplicationException("ex.phrase.key.trim", key).asRuntimeException();
    if (key.length() > 250)
        throw new FxApplicationException("ex.phrase.key.tooLong", key).asRuntimeException();
    if (!StringUtils.isAsciiPrintable(key) || key.contains("%"))
        throw new FxApplicationException("ex.phrase.key.nonAscii", key).asRuntimeException();
}

From source file:com.flexive.ejb.beans.configuration.DivisionConfigurationEngineBean.java

/**
 * {@inheritDoc}//  w  ww  . j a  v a 2 s.co m
 */
@Override
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void setResourceValue(String key, FxString value) throws FxApplicationException {
    if (StringUtils.isBlank(key))
        return;
    key = key.trim();
    if (key.length() > 250)
        throw new FxApplicationException("ex.configuration.resource.key.tooLong", key);
    if (!StringUtils.isAsciiPrintable(key))
        throw new FxApplicationException("ex.configuration.resource.key.nonAscii", key);
    Connection con = null;
    PreparedStatement ps = null;
    try {
        con = Database.getDbConnection();
        ps = con.prepareStatement("DELETE FROM " + TBL_RESOURCES + " WHERE RKEY=?");
        ps.setString(1, key);
        ps.executeUpdate();
        if (value != null && !value.isEmpty()) {
            ps.close();
            ps = con.prepareStatement("INSERT INTO " + TBL_RESOURCES + " (RKEY,LANG,RVAL)VALUES(?,?,?)");
            ps.setString(1, key);
            for (long lang : value.getTranslatedLanguages()) {
                ps.setLong(2, lang);
                ps.setString(3, value.getTranslation(lang));
                ps.addBatch();
            }
            ps.executeBatch();
        }
    } catch (SQLException e) {
        throw new FxApplicationException(e, "ex.db.sqlError", e.getMessage());
    } finally {
        Database.closeObjects(DivisionConfigurationEngine.class, con, ps);
    }
}