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

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

Introduction

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

Prototype

public static String trim(final String str) 

Source Link

Document

Removes control characters (char <= 32) from both ends of this String, handling null by returning null .

The String is trimmed using String#trim() .

Usage

From source file:org.asqatasun.rules.seo.SeoRule06031Test.java

@Override
protected void setProcess() {
    //----------------------------------------------------------------------
    //------------------------------1Passed-01------------------------------
    //----------------------------------------------------------------------
    ProcessResult processResult = processPageTest("Seo.Test.6.3.1-1Passed-01");
    // check number of elements in the page
    assertEquals(1, processResult.getElementCounter());
    // check test result
    assertEquals(TestSolution.PASSED, processResult.getValue());
    // check number of remarks and their value
    assertNull(processResult.getRemarkSet());

    //----------------------------------------------------------------------
    //------------------------------1Passed-02------------------------------
    //----------------------------------------------------------------------
    processResult = processPageTest("Seo.Test.6.3.1-1Passed-02");
    // check number of elements in the page
    assertEquals(1, processResult.getElementCounter());
    // check test result
    assertEquals(TestSolution.PASSED, processResult.getValue());
    // check number of remarks and their value
    assertNull(processResult.getRemarkSet());

    //----------------------------------------------------------------------
    //------------------------------1Passed-03------------------------------
    //----------------------------------------------------------------------
    processResult = processPageTest("Seo.Test.6.3.1-1Passed-03");
    // check number of elements in the page
    assertEquals(1, processResult.getElementCounter());
    // check test result
    assertEquals(TestSolution.PASSED, processResult.getValue());
    // check number of remarks and their value
    assertNull(processResult.getRemarkSet());

    //----------------------------------------------------------------------
    //------------------------------2Failed-01------------------------------
    //----------------------------------------------------------------------
    processResult = processPageTest("Seo.Test.6.3.1-2Failed-01");
    // check number of elements in the page
    assertEquals(1, processResult.getElementCounter());
    // check test result
    assertEquals(TestSolution.FAILED, processResult.getValue());
    // check number of remarks and their value
    assertEquals(1, processResult.getRemarkSet().size());
    Iterator<ProcessRemark> iter = processResult.getRemarkSet().iterator();
    SourceCodeRemark sourceCodeRemark = (SourceCodeRemark) iter.next();
    assertEquals(TestSolution.FAILED, sourceCodeRemark.getIssue());
    assertEquals("TitleTagLengthExceedLimit", sourceCodeRemark.getMessageCode());
    assertEquals(HtmlElementStore.TITLE_ELEMENT, sourceCodeRemark.getTarget());
    // check number of evidence elements and their value
    assertEquals(1, sourceCodeRemark.getElementList().size());
    EvidenceElement ee = sourceCodeRemark.getElementList().iterator().next();
    String value = StringUtils.trim(ee.getValue()).replaceAll("[\n\r]", "").replaceAll("\\s+", " ");
    System.out.println(ee.getValue());
    System.out.println(StringUtils.trim(ee.getValue()));
    System.out.println(StringUtils.trim(ee.getValue()).replaceAll("[\n\r]", ""));
    System.out.println(value);/*from  w w w.j av  a2 s.  c  om*/
    assertEquals(
            "Seo10 Test.06.03.1 Failed 01 with a length superior to 100 characters Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent imperdiet eget risus non",
            value);
    assertEquals(HtmlElementStore.TEXT_ELEMENT2, ee.getEvidence().getCode());

    //----------------------------------------------------------------------
    //------------------------------2Failed-02------------------------------
    //----------------------------------------------------------------------
    processResult = processPageTest("Seo.Test.6.3.1-2Failed-02");
    // check number of elements in the page
    assertEquals(1, processResult.getElementCounter());
    // check test result
    assertEquals(TestSolution.FAILED, processResult.getValue());
    // check number of remarks and their value
    assertEquals(1, processResult.getRemarkSet().size());
    iter = processResult.getRemarkSet().iterator();
    sourceCodeRemark = (SourceCodeRemark) iter.next();
    assertEquals(TestSolution.FAILED, sourceCodeRemark.getIssue());
    assertEquals("TitleTagLengthExceedLimit", sourceCodeRemark.getMessageCode());
    assertEquals(HtmlElementStore.TITLE_ELEMENT, sourceCodeRemark.getTarget());
    // check number of evidence elements and their value
    assertEquals(1, sourceCodeRemark.getElementList().size());
    ee = sourceCodeRemark.getElementList().iterator().next();
    value = StringUtils.trim(ee.getValue());
    assertEquals(
            "Seo10 Test.06.03.1 Failed 02 n1 with a length that exceeds 100 characters (------------------------------)",
            value);
    assertEquals(HtmlElementStore.TEXT_ELEMENT2, ee.getEvidence().getCode());

    //----------------------------------------------------------------------
    //------------------------------2Failed-03------------------------------
    //----------------------------------------------------------------------
    processResult = processPageTest("Seo.Test.6.3.1-2Failed-03");
    // check number of elements in the page
    assertEquals(1, processResult.getElementCounter());
    // check test result
    assertEquals(TestSolution.FAILED, processResult.getValue());
    // check number of remarks and their value
    assertEquals(1, processResult.getRemarkSet().size());
    iter = processResult.getRemarkSet().iterator();
    sourceCodeRemark = (SourceCodeRemark) iter.next();
    assertEquals(TestSolution.FAILED, sourceCodeRemark.getIssue());
    assertEquals("TitleTagLengthExceedLimit", sourceCodeRemark.getMessageCode());
    assertEquals(HtmlElementStore.TITLE_ELEMENT, sourceCodeRemark.getTarget());
    // check number of evidence elements and their value
    assertEquals(1, sourceCodeRemark.getElementList().size());
    ee = sourceCodeRemark.getElementList().iterator().next();
    value = StringUtils.trim(ee.getValue());
    assertEquals(
            "Seo10 Test.06.03.1 Failed 03 n1 with a length that exceeds 100 characters (------------------------------)",
            value);
    assertEquals(HtmlElementStore.TEXT_ELEMENT2, ee.getEvidence().getCode());

    //----------------------------------------------------------------------
    //------------------------------4NA-01---------------------------------
    //----------------------------------------------------------------------
    processResult = processPageTest("Seo.Test.6.3.1-4NA-01");
    // check test result
    assertEquals(TestSolution.NOT_APPLICABLE, processResult.getValue());
    // check test has no remark
    assertNull(processResult.getRemarkSet());
}

From source file:org.asqatasun.rules.textbuilder.CompleteTextElementBuilder.java

/**
 * The textual content of an element can be composed with :
 * <ul>/*  ww  w  . j  a va  2s. c o m*/
 *     <li> The text of the element</li>
 *     <li> The text of the alt attribute of the element</li>
 *     <li> The text of the title attribute of the element</li>
 *     <li> The text of the summary attribute of the element</li>
 *     <li> The text of the value attribute of the element</li>
 *     <li> The text of the content attribute of the element when 
 *          the value of the name attribute is "description"
 *      </li>
 * </ul>
 * 
 * @param element
 * @return the textual content of an element
 */
@Override
public String buildTextFromElement(Element element) {
    StringBuilder strb = new StringBuilder();
    if (StringUtils.isNotBlank(element.ownText())) {
        strb.append(SPACER);
        strb.append(element.ownText().trim());
    }

    strb.append(getTextualContentOfAttribute(element, AttributeStore.ALT_ATTR));
    strb.append(getTextualContentOfAttribute(element, AttributeStore.TITLE_ATTR));
    strb.append(getTextualContentOfAttribute(element, AttributeStore.SUMMARY_ATTR));
    strb.append(getTextualContentOfAttribute(element, AttributeStore.VALUE_ATTR));

    if (element.hasAttr(AttributeStore.CONTENT_ATTR) && element.hasAttr(AttributeStore.NAME_ATTR)
            && StringUtils.equalsIgnoreCase(element.attr(AttributeStore.NAME_ATTR), "description")
            && StringUtils.isNotBlank(element.attr(AttributeStore.CONTENT_ATTR))) {
        strb.append(SPACER);
        strb.append(getTextualContentOfAttribute(element, AttributeStore.CONTENT_ATTR));
    }
    return StringUtils.trim(strb.toString());
}

From source file:org.asqatasun.rules.textbuilder.CompleteTextElementBuilder.java

/**
 * /*from www . j  av  a 2  s .c om*/
 * @param element
 * @param attributeName
 * @return the textual content of an attribute
 */
private String getTextualContentOfAttribute(Element element, String attributeName) {
    if (element.hasAttr(attributeName)) {
        return SPACER + StringUtils.trim(element.attr(attributeName));
    }
    return "";
}

From source file:org.asqatasun.rules.textbuilder.DeepTextElementBuilder.java

@Override
public String buildTextFromElement(Element element) {
    StringBuilder elementText = new StringBuilder();
    if (element.hasAttr(ALT_ATTR)) {
        elementText.append(SPACER);//from   www .  j ava 2s . co m
        elementText.append(altAttrTextBuilder.buildTextFromElement(element));
    }
    for (Node child : element.childNodes()) {
        if (child instanceof TextNode && !((TextNode) child).isBlank()) {
            elementText.append(SPACER);
            elementText.append(StringUtils.trim(((TextNode) child).text()));
        } else if (child instanceof Element) {
            elementText.append(SPACER);
            elementText.append(buildTextFromElement((Element) child));
        }
    }
    return StringUtils.trim(elementText.toString());
}

From source file:org.asqatasun.rules.textbuilder.OwnTextElementBuilder.java

@Override
public String buildTextFromElement(Element element) {
    return StringUtils.trim(element.ownText());
}

From source file:org.asqatasun.rules.textbuilder.SimpleTextElementBuilder.java

@Override
public String buildTextFromElement(Element element) {
    return StringUtils.trim(element.text());
}

From source file:org.blocks4j.reconf.client.constructors.SimpleConstructor.java

public Object construct(MethodData data) throws Throwable {
    Class<?> returnClass = (Class<?>) data.getReturnType();

    String trimmed = StringUtils.defaultString(StringUtils.trim(data.getValue()));
    if (!trimmed.startsWith("'") || !trimmed.endsWith("'")) {
        throw new RuntimeException(msg.format("error.invalid.string", data.getValue(), data.getMethod()));
    }//from   ww  w  . j a  va  2  s . com

    String wholeValue = StringUtils.substring(trimmed, 1, trimmed.length() - 1);

    if (String.class.equals(returnClass)) {
        return wholeValue;
    }

    if (null == data.getValue()) {
        return null;
    }

    if (Object.class.equals(returnClass)) {
        returnClass = String.class;
    }

    if (char.class.equals(data.getReturnType()) || Character.class.equals(data.getReturnType())) {
        if (StringUtils.length(wholeValue) == 1) {
            return CharUtils.toChar(wholeValue);
        }
        return CharUtils.toChar(StringUtils.replace(wholeValue, " ", ""));
    }

    if (primitiveBoxing.containsKey(returnClass)) {
        if (StringUtils.isBlank(wholeValue)) {
            return null;
        }
        Method parser = primitiveBoxing.get(returnClass).getMethod(
                "parse" + StringUtils.capitalize(returnClass.getSimpleName()), new Class<?>[] { String.class });
        return parser.invoke(primitiveBoxing.get(returnClass), new Object[] { StringUtils.trim(wholeValue) });
    }

    Method valueOf = null;
    try {
        valueOf = returnClass.getMethod("valueOf", new Class<?>[] { String.class });
    } catch (NoSuchMethodException ignored) {
    }

    if (valueOf == null) {
        try {

            valueOf = returnClass.getMethod("valueOf", new Class<?>[] { Object.class });
        } catch (NoSuchMethodException ignored) {
        }
    }

    if (null != valueOf) {
        if (StringUtils.isEmpty(wholeValue)) {
            return null;
        }
        return valueOf.invoke(data.getReturnType(), new Object[] { StringUtils.trim(wholeValue) });
    }

    Constructor<?> constructor = null;

    try {
        constructor = returnClass.getConstructor(String.class);
        constructor.setAccessible(true);

    } catch (NoSuchMethodException ignored) {
        throw new IllegalStateException(
                msg.format("error.string.constructor", returnClass.getSimpleName(), data.getMethod()));
    }

    try {
        return constructor.newInstance(wholeValue);

    } catch (Exception e) {
        if (e.getCause() != null && e.getCause() instanceof NumberFormatException) {
            return constructor.newInstance(StringUtils.trim(wholeValue));
        }
        throw e;
    }
}

From source file:org.blocks4j.reconf.client.constructors.StringParser.java

public StringParser(MethodData arg) {
    this.data = arg;

    if (StringUtils.isEmpty(data.getValue())) {
        return;//from w  ww  . j  av  a  2s  .  c  o  m
    }

    String trimmed = StringUtils.defaultString(StringUtils.trim(data.getValue()));
    if (!trimmed.startsWith("[") || !trimmed.endsWith("]")) {
        throw new RuntimeException(msg.format("error.complex.type", data.getMethod()));
    }

    String wholeValue = StringUtils.substring(trimmed, 1, trimmed.length() - 1);
    extractTokens(wholeValue);
    if (openClose.size() % 2 != 0) {
        throw new IllegalArgumentException(
                msg.format("error.invalid.string", data.getValue(), data.getMethod()));
    }
}

From source file:org.bonitasoft.web.designer.utils.assertions.JsAssert.java

/**
 * For each string lines we trim them to avoid whitespace issue when comparing strings
 *///from   w ww  . j  a v  a 2s  .c  o  m
private String trimEachLines(String string) {
    String[] lines = string.split(System.lineSeparator());
    StringBuilder sb = new StringBuilder();
    for (String line : lines) {
        sb.append(StringUtils.trim(line)).append(System.lineSeparator());
    }
    return StringUtils.chop(sb.toString());
}

From source file:org.cgiar.ccafs.ap.action.json.global.ManageUsersAction.java

@Override
public void prepare() throws Exception {
    // if searching a user, we need to get the queried String.
    if (ActionContext.getContext().getName().equals("searchUsers")) {
        queryParameter = StringUtils.trim(this.getRequest().getParameter(APConstants.QUERY_PARAMETER));
    } else if (ActionContext.getContext().getName().equals("createUser")) {
        // if Adding a new user, we need to get the info to be added.
        newUser = new User();
        newUser.setId(-1);/*from  w  ww. j  a  va  2 s  .c o m*/
        newUser.setFirstName(StringUtils.trim(this.getRequest().getParameter(PARAM_FIRST_NAME)));
        newUser.setLastName(StringUtils.trim(this.getRequest().getParameter(PARAM_LAST_NAME)));
        newUser.setEmail(StringUtils.trim(this.getRequest().getParameter(PARAM_EMAIL)));
        newUser.setActive(
                StringUtils.trim(this.getRequest().getParameter(PARAM_IS_ACTIVE)).equals("1") ? true : false);
    }

}