Example usage for org.springframework.util StringUtils trimAllWhitespace

List of usage examples for org.springframework.util StringUtils trimAllWhitespace

Introduction

In this page you can find the example usage for org.springframework.util StringUtils trimAllWhitespace.

Prototype

public static String trimAllWhitespace(String str) 

Source Link

Document

Trim all whitespace from the given String : leading, trailing, and in between characters.

Usage

From source file:org.jboss.spring.factory.NamedXmlApplicationContext.java

private void initializeNames(Resource resource) {
    try {//from w w w . j a v  a 2 s.  com
        XPath xPath = XPathFactory.newInstance().newXPath();
        xPath.setNamespaceContext(new NamespaceContext() {
            @Override
            public String getNamespaceURI(String prefix) {
                return "http://www.springframework.org/schema/beans";
            }

            @Override
            public String getPrefix(String namespaceURI) {
                return "beans";
            }

            @Override
            public Iterator getPrefixes(String namespaceURI) {
                return Collections.singleton("beans").iterator();
            }
        });
        String expression = "/beans:beans/beans:description";
        InputSource inputSource = new InputSource(resource.getInputStream());
        String description = xPath.evaluate(expression, inputSource);
        if (description != null) {
            Matcher bfm = Pattern.compile(Constants.BEAN_FACTORY_ELEMENT).matcher(description);
            if (bfm.find()) {
                this.name = bfm.group(1);
            }
            Matcher pbfm = Pattern.compile(Constants.PARENT_BEAN_FACTORY_ELEMENT).matcher(description);
            if (pbfm.find()) {
                String parentName = pbfm.group(1);
                try {
                    this.getBeanFactory()
                            .setParentBeanFactory((BeanFactory) Util.lookup(parentName, BeanFactory.class));
                } catch (Exception e) {
                    throw new BeanDefinitionStoreException(
                            "Failure during parent bean factory JNDI lookup: " + parentName, e);
                }
            }
        }
        if (this.name == null || "".equals(StringUtils.trimAllWhitespace(this.name))) {
            this.name = this.defaultName;
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.consol.citrus.admin.service.TestCaseService.java

/**
 * Find all tests in give source file. Method is finding tests by their annotation presence of @CitrusTest or @CitrusXmlTest.
 * @param sourceFile/* w w w. j  a  va 2s  . c o  m*/
 * @param packageName
 * @param className
 * @return
 */
private List<Test> findTests(File sourceFile, String packageName, String className) {
    List<Test> tests = new ArrayList<>();

    try {
        String sourceCode = FileUtils.readToString(new FileSystemResource(sourceFile));

        Matcher matcher = Pattern.compile("[^/\\*]\\s@CitrusTest").matcher(sourceCode);
        while (matcher.find()) {
            Test test = new Test();
            test.setType(TestType.JAVA);
            test.setClassName(className);
            test.setPackageName(packageName);

            String snippet = StringUtils.trimAllWhitespace(sourceCode.substring(matcher.start()));
            snippet = snippet.substring(0, snippet.indexOf("){"));
            String methodName = snippet.substring(snippet.indexOf("publicvoid") + 10);
            methodName = methodName.substring(0, methodName.indexOf("("));
            test.setMethodName(methodName);

            if (snippet.contains("@CitrusTest(name=")) {
                String explicitName = snippet.substring(snippet.indexOf("name=\"") + 6);
                explicitName = explicitName.substring(0, explicitName.indexOf("\""));
                test.setName(explicitName);
            } else {
                test.setName(className + "." + methodName);
            }

            tests.add(test);
        }

        matcher = Pattern.compile("[^/\\*]\\s@CitrusXmlTest").matcher(sourceCode);
        while (matcher.find()) {
            Test test = new Test();
            test.setType(TestType.XML);
            test.setClassName(className);
            test.setPackageName(packageName);

            String snippet = StringUtils.trimAllWhitespace(sourceCode.substring(matcher.start()));
            snippet = snippet.substring(0, snippet.indexOf('{', snippet.indexOf("publicvoid")));
            String methodName = snippet.substring(snippet.indexOf("publicvoid") + 10);
            methodName = methodName.substring(0, methodName.indexOf("("));
            test.setMethodName(methodName);

            if (snippet.contains("@CitrusXmlTest(name=\"")) {
                String explicitName = snippet.substring(snippet.indexOf("name=\"") + 6);
                explicitName = explicitName.substring(0, explicitName.indexOf("\""));
                test.setName(explicitName);
            } else if (snippet.contains("@CitrusXmlTest(name={\"")) {
                String explicitName = snippet.substring(snippet.indexOf("name={\"") + 7);
                explicitName = explicitName.substring(0, explicitName.indexOf("\""));
                test.setName(explicitName);
            } else {
                test.setName(methodName);
            }

            if (snippet.contains("packageScan=\"")) {
                String packageScan = snippet.substring(snippet.indexOf("packageScan=\"") + 13);
                packageScan = packageScan.substring(0, packageScan.indexOf("\""));
                test.setPackageName(packageScan);
            }

            if (snippet.contains("packageName=\"")) {
                String explicitPackageName = snippet.substring(snippet.indexOf("packageName=\"") + 13);
                explicitPackageName = explicitPackageName.substring(0, explicitPackageName.indexOf("\""));
                test.setPackageName(explicitPackageName);
            }

            tests.add(test);
        }
    } catch (IOException e) {
        log.error("Failed to read test source file", e);
    }

    return tests;
}

From source file:com.consol.citrus.admin.service.TestCaseServiceImpl.java

/**
 * Adds test info by reading file resource as text content. Searches for class annotations and method annotations
 * on a text based search. This approach does not need to instantiate the class so Java source must not necessarily be
 * part of the classpath./*from w ww  .j ava2s .  c om*/
 * @param testPackage
 * @param testName
 * @param file
 */
private List<TestCaseData> getTestCaseInfoFromFile(String testPackage, String testName, File file) {
    List<TestCaseData> tests = new ArrayList<TestCaseData>();

    try {
        String javaContent = FileUtils.readToString(new FileInputStream(file));
        javaContent = StringUtils.trimAllWhitespace(javaContent);
        String citrusAnnotation = "@CitrusTest";

        if (javaContent.contains(citrusAnnotation)) {
            int position = javaContent.indexOf(citrusAnnotation);
            while (position > 0) {
                String methodContent = javaContent.substring(position);
                TestCaseData testCase = new TestCaseData();
                testCase.setType(TestCaseType.JAVA);
                testCase.setPackageName(testPackage);
                testCase.setFile(file.getParentFile().getAbsolutePath() + File.separator
                        + FilenameUtils.getBaseName(file.getName()));
                testCase.setLastModified(file.lastModified());

                if (methodContent.startsWith(citrusAnnotation + "(")) {
                    String annotationProps = methodContent.substring(methodContent.indexOf('('),
                            methodContent.indexOf(')'));
                    if (StringUtils.hasText(annotationProps) && annotationProps.contains("name=\"")) {
                        String methodName = annotationProps
                                .substring(annotationProps.indexOf("name=\"") + "name=\"".length());
                        methodName = methodName.substring(0, methodName.indexOf('"'));
                        testCase.setName(methodName);
                    }
                }

                if (!StringUtils.hasText(testCase.getName())) {
                    String methodName = methodContent
                            .substring(methodContent.indexOf("publicvoid") + "publicvoid".length());
                    methodName = methodName.substring(0, methodName.indexOf('('));
                    testCase.setName(methodName);
                }

                tests.add(testCase);
                position = javaContent.indexOf(citrusAnnotation, position + citrusAnnotation.length());
            }
        } else if (javaContent.contains(TestNGCitrusTestDesigner.class.getSimpleName())
                || javaContent.contains(JUnit4CitrusTestDesigner.class.getSimpleName())) {
            TestCaseData testCase = new TestCaseData();
            testCase.setType(TestCaseType.JAVA);
            testCase.setName(testName);
            testCase.setPackageName(testPackage);
            testCase.setFile(file.getParentFile().getAbsolutePath() + File.separator
                    + FilenameUtils.getBaseName(file.getName()));
            testCase.setLastModified(file.lastModified());

            tests.add(testCase);
        } else {
            log.debug("Skipping java source as it is not a valid Citrus test: " + testPackage + "." + testName);
        }
    } catch (IOException e) {
        log.warn("Unable to access Java source on file system: " + testPackage + "." + testName, e);
    }

    return tests;
}

From source file:org.obiba.onyx.spring.AnnotatedBeanFinderFactoryBean.java

/**
 * Clean and trim a string read from the context file.
 * //  w  ww.  j av a2s.c o  m
 * @param string string to clean
 * @return cleaned string
 */
private String cleanString(String string) {
    return StringUtils.trimAllWhitespace(string.replaceAll("[\t\n]", ""));
}

From source file:it.geosolutions.geobatch.opensdi.csvingest.utils.CSVSchemaHandler.java

public void reload() {
    typesList = new ArrayList<CSVPropertyType>();
    uniqueList = new ArrayList<Integer>();
    headersList = new ArrayList<String>();
    Map<String, String> configMap = loadEntityProperties(propertiesFileName);
    if (!configMap.keySet().contains(TYPE_LIST) || !configMap.keySet().contains(UNIQUE_LIST)
            || !configMap.keySet().contains(HEADERS_LIST)) {
        throw new IllegalStateException(
                "cannot find TYPE_LIST or HEADERS_LIST or UNIQUE_LIST in the properties file...");
    }// w w w  . ja va 2s .co m
    String typeListString = configMap.get(TYPE_LIST);
    String uniqueListString = configMap.get(UNIQUE_LIST);
    String headersListString = configMap.get(HEADERS_LIST);

    if (typeListString == null || typeListString.isEmpty()) {
        throw new IllegalStateException("TYPE_LIST cannot be null or empty...");
    }
    String[] typeListArray = typeListString.split(LIST_SEPARATOR);
    for (String type : typeListArray) {
        try {
            typesList.add(CSVPropertyType.valueOf(type));
        } catch (Exception e) {
            throw new IllegalStateException("TYPE_LIST contains a not valid value: '" + type + "'");
        }
    }

    if (headersListString == null || headersListString.isEmpty()) {
        throw new IllegalStateException("HEADERS_LIST cannot be null or empty...");
    }
    String[] headersListArray = headersListString.split(LIST_SEPARATOR);
    if (headersListArray.length != typesList.size()) {
        throw new IllegalStateException("HEADERS_LIST and TYPE_LIST have different size...");
    }
    for (String header : headersListArray) {
        headersList.add(header);
    }

    if (!StringUtils.trimAllWhitespace(uniqueListString).isEmpty()) {
        String[] uniqueListArray = uniqueListString.split(LIST_SEPARATOR);
        for (String index : uniqueListArray) {
            try {
                uniqueList.add(Integer.parseInt(index));
            } catch (Exception e) {
                throw new IllegalStateException(
                        "UNIQUE_LIST contains a not valid Integer value: '" + index + "'");
            }
        }
    }
}

From source file:org.kuali.kfs.module.ar.report.service.impl.ContractsGrantsReportHelperServiceImpl.java

/**
 * @see org.kuali.kfs.module.ar.report.service.ContractsGrantsReportHelperService#getPropertyValue(java.lang.Object, java.lang.String)
 *//*www  .  ja v  a2  s . c om*/
@Override
public String getPropertyValue(Object object, String propertyName) {
    Object fieldValue = ObjectUtils.getPropertyValue(object, propertyName);
    return (ObjectUtils.isNull(fieldValue)) ? "" : StringUtils.trimAllWhitespace(fieldValue.toString());
}

From source file:org.springframework.beans.factory.config.PropertyPathFactoryBean.java

/**
 * Specify the name of a target bean to apply the property path to.
 * Alternatively, specify a target object directly.
 * @param targetBeanName the bean name to be looked up in the
 * containing bean factory (e.g. "testBean")
 * @see #setTargetObject/*  w  w  w  .  j av a 2s.  c  om*/
 */
public void setTargetBeanName(String targetBeanName) {
    this.targetBeanName = StringUtils.trimAllWhitespace(targetBeanName);
}

From source file:org.springframework.beans.factory.config.PropertyPathFactoryBean.java

/**
 * Specify the property path to apply to the target.
 * @param propertyPath the property path, potentially nested
 * (e.g. "age" or "spouse.age")//  w w  w  .  ja  v  a  2s.  co  m
 */
public void setPropertyPath(String propertyPath) {
    this.propertyPath = StringUtils.trimAllWhitespace(propertyPath);
}

From source file:org.springframework.beans.factory.config.PropertyPathFactoryBean.java

/**
 * The bean name of this PropertyPathFactoryBean will be interpreted
 * as "beanName.property" pattern, if neither "targetObject" nor
 * "targetBeanName" nor "propertyPath" have been specified.
 * This allows for concise bean definitions with just an id/name.
 *///from  w  w  w . j  av a 2s  .c o m
@Override
public void setBeanName(String beanName) {
    this.beanName = StringUtils.trimAllWhitespace(BeanFactoryUtils.originalBeanName(beanName));
}

From source file:org.springframework.core.env.AbstractEnvironment.java

/**
 * Return the set of active profiles as explicitly set through
 * {@link #setActiveProfiles} or if the current set of active profiles
 * is empty, check for the presence of the {@value #ACTIVE_PROFILES_PROPERTY_NAME}
 * property and assign its value to the set of active profiles.
 * @see #getActiveProfiles()//from w  w  w .ja v a 2  s  .  c om
 * @see #ACTIVE_PROFILES_PROPERTY_NAME
 */
protected Set<String> doGetActiveProfiles() {
    synchronized (this.activeProfiles) {
        if (this.activeProfiles.isEmpty()) {
            String profiles = getProperty(ACTIVE_PROFILES_PROPERTY_NAME);
            if (StringUtils.hasText(profiles)) {
                setActiveProfiles(
                        StringUtils.commaDelimitedListToStringArray(StringUtils.trimAllWhitespace(profiles)));
            }
        }
        return this.activeProfiles;
    }
}