Example usage for org.apache.commons.lang3 StringUtils length

List of usage examples for org.apache.commons.lang3 StringUtils length

Introduction

In this page you can find the example usage for org.apache.commons.lang3 StringUtils length.

Prototype

public static int length(final CharSequence cs) 

Source Link

Document

Gets a CharSequence length or 0 if the CharSequence is null .

Usage

From source file:org.goko.grbl.controller.GrblStreamingRunnable.java

protected void runExecutionToken() throws GkException {
    try {/*from   w w  w  .j  a  v a  2  s .  c  o  m*/
        while (executionQueue.getCurrentToken() != null && executionQueue.getCurrentToken().hasMoreCommand()) {
            GrblGCodeExecutionToken token = executionQueue.getCurrentToken();
            waitTokenUnpaused();
            if (token.hasMoreCommand()) {
                GCodeCommand command = token.getNextCommand();
                waitBufferSpace(StringUtils.length(command.getStringCommand()));
                command = token.takeNextCommand();
                if (command.getType() == EnumGCodeCommandType.COMMENT) {
                    // Skip comments
                    token.markAsExecuted(command.getId());
                } else {
                    grblService.sendCommand(command);
                    token.markAsSent(command.getId());
                }
                GokoEventBus.getInstance().post(new GCodeCommandSelectionEvent(command));
            }
        }
        LOG.info("runExecutionToken ends");
    } catch (GkException e) {
        LOG.error(e);
    }
}

From source file:org.goko.serial.bindings.SerialConsoleController.java

protected void appendTextToConsoleWidget(final String text, final int color) {
    if (consoleWidget != null) {
        Display.getDefault().asyncExec(new Runnable() {
            @Override/*  w  w  w.  jav a2 s  .  c  o m*/
            public void run() {
                int initialCharCount = consoleWidget.getCharCount();
                consoleWidget.append(text);

                StyleRange[] ranges = new StyleRange[1];

                ranges[0] = new StyleRange();
                ranges[0].start = initialCharCount;
                ranges[0].length = StringUtils.length(text);
                ranges[0].foreground = SWTResourceManager.getColor(color);
                consoleWidget.replaceStyleRanges(ranges[0].start, ranges[0].length, ranges);

                if (SerialActivator.getPreferenceStore()
                        .getBoolean(SerialConsolePreferences.SERIAL_CONSOLE_LIMIT_CHARACTER)) {
                    int maxChar = SerialActivator.getPreferenceStore()
                            .getInt(SerialConsolePreferences.SERIAL_CONSOLE_MAX_CHARACTER);

                    if (consoleWidget.getCharCount() > maxChar) {
                        int delta = consoleWidget.getCharCount() - maxChar;
                        //consoleWidget.setText( StringUtils.substring(consoleWidget.getText(), delta));
                        getDataModel().setConsole(StringUtils.substring(consoleWidget.getText(), delta));
                        updateCarretPosition();
                    }
                } else {
                    getDataModel().setConsole(consoleWidget.getText());
                }
            }
        });

    }
}

From source file:org.goko.tools.serial.jssc.console.internal.JsscSerialConsoleController.java

@Override
public void onDataSent(List<Byte> data) throws GkException {
    if (getDataModel().isConsoleEnabled()) {
        outputBuffer.addAll(data);//from   w ww  . j  a  va2 s  .  co  m
        if (outputBuffer.hasNext()) {
            final String text = GkUtils.toString(outputBuffer.unstackNextCommand());
            Display.getDefault().asyncExec(new Runnable() {
                @Override
                public void run() {
                    if (!textDisplay.isDisposed()) {
                        StyleRange style = new StyleRange();
                        style.start = textDisplay.getCharCount();
                        style.length = StringUtils.length(text);
                        style.foreground = textDisplay.getDisplay().getSystemColor(SWT.COLOR_DARK_GREEN);
                        textDisplay.append(text);
                        textDisplay.setStyleRange(style);
                        if (!getDataModel().isScrollLock()) {
                            textDisplay.setTopIndex(textDisplay.getLineCount() - 1);
                        }
                    }
                }
            });
        }
    }

}

From source file:org.jamwiki.db.AnsiDataHandler.java

/**
 * Update a special page used on the wiki, such as the left menu or
 * default stylesheet./*from  ww  w . j av  a  2  s .co m*/
 *
 * @param locale The locale to be used when updating a special page such
 *  as the left menu and default stylesheet.  This parameter will affect
 *  the language used when updating up the page.
 * @param virtualWiki The VirtualWiki for which the special page are being
 *  updated.
 * @param topicName The name of the special page topic that is being
 *  updated.
 * @param userDisplay A display name for the user updating special pages,
 *  typically the IP address.
 * @throws DataAccessException Thrown if any error occurs during method execution.
 * @throws WikiException Thrown if the topic information is invalid.
 */
public void updateSpecialPage(Locale locale, String virtualWiki, String topicName, String userDisplay)
        throws DataAccessException, WikiException {
    logger.info("Updating special page " + virtualWiki + " / " + topicName);
    TransactionStatus status = null;
    try {
        status = DatabaseConnection.startTransaction();
        Connection conn = DatabaseConnection.getConnection();
        String contents = WikiDatabase.readSpecialPage(locale, topicName);
        Topic topic = this.lookupTopic(virtualWiki, topicName, false, conn);
        int charactersChanged = StringUtils.length(contents) - StringUtils.length(topic.getTopicContent());
        topic.setTopicContent(contents);
        // FIXME - hard coding
        TopicVersion topicVersion = new TopicVersion(null, userDisplay,
                "Automatically updated by system upgrade", contents, charactersChanged);
        ParserOutput parserOutput = ParserUtil.parserOutput(topic.getTopicContent(), virtualWiki, topicName);
        writeTopic(topic, topicVersion, parserOutput.getCategories(), parserOutput.getLinks());
    } catch (DataAccessException e) {
        DatabaseConnection.rollbackOnException(status, e);
        throw e;
    } catch (ParserException e) {
        DatabaseConnection.rollbackOnException(status, e);
        throw new DataAccessException(e);
    } catch (IOException e) {
        DatabaseConnection.rollbackOnException(status, e);
        throw new DataAccessException(e);
    } catch (SQLException e) {
        DatabaseConnection.rollbackOnException(status, e);
        throw new DataAccessException(e);
    } catch (WikiException e) {
        DatabaseConnection.rollbackOnException(status, e);
        throw e;
    }
    DatabaseConnection.commit(status);
}

From source file:org.jamwiki.migrate.MediaWikiXmlImporter.java

/**
 * end of xml-tag//from ww  w .j av a2 s  .  c o  m
 *
 * @param uri The Namespace URI, or the empty string if the element has no Namespace URI or
 *  if Namespace processing is not being performed.
 * @param localName The local name (without prefix), or the empty string if Namespace processing
 *  is not being performed.
 * @param qName The qualified name (with prefix), or the empty string if qualified names are not available.
 */
public void endElement(String uri, String localName, String qName) throws SAXException {
    if (StringUtils.equals(MediaWikiConstants.MEDIAWIKI_ELEMENT_NAMESPACE, qName)) {
        int key = NumberUtils.toInt(this.currentAttributeMap.get("key"));
        try {
            Namespace jamwikiNamespace = WikiBase.getDataHandler().lookupNamespaceById(key);
            if (jamwikiNamespace != null) {
                String mediawikiNamespace = currentElementBuffer.toString().trim();
                mediawikiNamespaceMap.put(mediawikiNamespace, jamwikiNamespace.getLabel(this.virtualWiki));
            }
        } catch (DataAccessException e) {
            throw new SAXException("Failure while processing namespace with ID: " + key, e);
        }
    } else if (MediaWikiConstants.MEDIAWIKI_ELEMENT_TOPIC_NAME.equals(qName)) {
        String topicName = currentElementBuffer.toString().trim();
        this.initCurrentTopic(topicName);
    } else if (MediaWikiConstants.MEDIAWIKI_ELEMENT_TOPIC_CONTENT.equals(qName)) {
        String topicContent = this.convertToJAMWikiNamespaces(currentElementBuffer.toString().trim());
        currentTopicVersion.setVersionContent(topicContent);
        currentTopicVersion.setCharactersChanged(StringUtils.length(topicContent) - previousTopicContentLength);
        previousTopicContentLength = StringUtils.length(topicContent);
    } else if (MediaWikiConstants.MEDIAWIKI_ELEMENT_TOPIC_VERSION_COMMENT.equals(qName)) {
        this.currentTopicVersion.setEditComment(currentElementBuffer.toString().trim());
    } else if (MediaWikiConstants.MEDIAWIKI_ELEMENT_TOPIC_VERSION_MINOR_EDIT.equals(qName)) {
        this.currentTopicVersion.setEditType(TopicVersion.EDIT_MINOR);
    } else if (MediaWikiConstants.MEDIAWIKI_ELEMENT_TOPIC_VERSION_EDIT_DATE.equals(qName)) {
        this.currentTopicVersion
                .setEditDate(this.parseMediaWikiTimestamp(currentElementBuffer.toString().trim()));
    } else if (MediaWikiConstants.MEDIAWIKI_ELEMENT_TOPIC_VERSION_IP.equals(qName)
            || MediaWikiConstants.MEDIAWIKI_ELEMENT_TOPIC_VERSION_USERNAME.equals(qName)) {
        // Login name in Mediawiki can be longer than 100 characters, so trim to conform to
        // JAMWiki limits.  In general very long login names seem to be used only by vandals,
        // so this should be an acceptable workaround.
        String authorDisplay = currentElementBuffer.toString().trim();
        if (authorDisplay.length() > 100) {
            authorDisplay = authorDisplay.substring(0, 100);
        }
        this.currentTopicVersion.setAuthorDisplay(authorDisplay);
    } else if (MediaWikiConstants.MEDIAWIKI_ELEMENT_TOPIC_VERSION.equals(qName)) {
        this.commitTopicVersion();
    } else if (MediaWikiConstants.MEDIAWIKI_ELEMENT_TOPIC.equals(qName)) {
        // flush any pending topic version data
        this.writeTopicVersion(true);
        this.orderTopicVersions();
    }
}

From source file:org.jamwiki.parser.image.ImageUtil.java

/**
 * Given appropriate parameters write the topic record that corresponds
 * to a file object to the database./*from  ww w  .ja  v a 2  s.co m*/
 *
 * @param virtualWiki The virtual wiki for the record being written.
 * @param pageName The page name for the record being written.  This value
 *  should NOT include a namespace as the namespace is implicitly assumed
 *  to be the FILE namespace.
 * @param contents The topic contents for the file record.
 * @param user The author of the topic record, or <code>null</code> if the
 *  author is anonymous.
 * @param isImage Flag indicating whether the topic is an image or just a
 *  file.
 * @param ipAddress The IP address of the topic record author.
 * @return The Topic that is written for the image record.
 */
public static Topic writeImageTopic(String virtualWiki, String pageName, String contents, WikiUser user,
        boolean isImage, String ipAddress) throws DataAccessException, ParserException, WikiException {
    Namespace namespace = Namespace.namespace(Namespace.FILE_ID);
    Topic topic = WikiBase.getDataHandler().lookupTopic(virtualWiki, namespace, pageName, false);
    int charactersChanged = 0;
    if (topic == null || !StringUtils.equals(virtualWiki, topic.getVirtualWiki())) {
        // if topic doesn't exist or the shared version was returned, create a new record
        topic = new Topic(virtualWiki, namespace, pageName);
        topic.setTopicContent(contents);
        charactersChanged = StringUtils.length(contents);
    }
    if (isImage) {
        topic.setTopicType(TopicType.IMAGE);
    } else {
        topic.setTopicType(TopicType.FILE);
    }
    TopicVersion topicVersion = new TopicVersion(user, ipAddress, contents, topic.getTopicContent(),
            charactersChanged);
    topicVersion.setEditType(TopicVersion.EDIT_UPLOAD);
    ParserOutput parserOutput = ParserUtil.parserOutput(topic.getTopicContent(), virtualWiki, topic.getName());
    WikiBase.getDataHandler().writeTopic(topic, topicVersion, parserOutput.getCategories(),
            parserOutput.getLinks());
    return topic;
}

From source file:org.kalypso.model.wspm.tuhh.core.profile.export.knauf.printer.AbstractKnaufPrinter.java

@Override
public CharSequence println() {
    final StringBuilder builder = new StringBuilder();

    builder.append(String.format("%2d", m_bean.getSatzart())); //$NON-NLS-1$
    builder.append(getContent());/*from ww  w. j  a  va2  s  .  c o  m*/

    final String output = builder.toString();
    if (StringUtils.length(output) > getMaxRowSize()) {
        final CharSequence cutted = output.subSequence(0, getMaxRowSize());
        System.out.println("Knauf Export - too long output string has been detected - cutted:"); //$NON-NLS-1$
        System.out.println(output);
        System.out.println(" -> "); //$NON-NLS-1$
        System.out.println(cutted);

        return cutted;
    }

    return String.format("%s\r\n", output); //$NON-NLS-1$
}

From source file:org.kalypso.shape.dbf.DBFField.java

private void checkName() throws DBaseException {
    if (StringUtils.isBlank(m_name))
        throw new DBaseException("'Name' must not be blank.");

    if (StringUtils.length(m_name) > 11)
        throw new DBaseException("'Name' must not be longer than 11 characters.");
}

From source file:org.oimutils.PasswordUtils.java

/**
 * gets a random character from the input set
 * //from w  ww .  j  a  v  a 2  s .com
 * @param string
 * @return
 */
private static char getRandomChar(String string) {
    int pos = (int) (Math.random() * StringUtils.length(string));
    return CharUtils.toChar(StringUtils.substring(string, pos, pos + 1));
}

From source file:org.openbaton.cli.util.PrintFormat.java

private static int[] colWidths() {
    int cols = -1;

    for (String[] row : rows)
        cols = Math.max(cols, row.length);

    int[] widths = new int[cols];

    for (String[] row : rows) {
        for (int colNum = 0; colNum < row.length; colNum++) {
            widths[colNum] = Math.max(widths[colNum], StringUtils.length(row[colNum]));
        }//  w  w w  . j  a v  a2 s. c o  m
    }

    return widths;
}