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

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

Introduction

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

Prototype

public static String capitalize(String str) 

Source Link

Document

Capitalizes a String changing the first letter to title case as per Character#toTitleCase(char) .

Usage

From source file:org.acoveo.tools.Reflection.java

/**
 * Return the getter method matching the given property name, optionally
 * throwing an exception if none.//from   w  ww.  j av  a2 s . co m
 * @throws RuntimeException if mustExist is true and the getter method was not found
 */
public static Method findGetter(Class cls, String prop, boolean mustExist) {
    prop = StringUtils.capitalize(prop);
    String name = "get" + prop;
    Method m;
    //        try {
    // this algorithm searches for a get<prop> or is<prop> method in
    // a breadth-first manner.
    for (Class c = cls; c != null && c != Object.class; c = c.getSuperclass()) {
        m = getDeclaredMethod(c, name, null);
        if (m != null) {
            return m;
        } else {
            m = getDeclaredMethod(c, "is" + prop, null);
            if (m != null && (m.getReturnType() == boolean.class || m.getReturnType() == Boolean.class))
                return m;
        }
    }
    //        } catch (Exception e) {
    //            throw new GeneralException(e);
    //        }

    if (mustExist)
        throw new RuntimeException("Could not find property" + prop + " in class " + cls.getCanonicalName());
    return null;
}

From source file:org.acoveo.tools.Reflection.java

/**
 * Return the setter method matching the given property name, optionally
 * throwing an exception if none.//from   w  ww .  j  av  a 2s  .  c  om
 * @throws RuntimeException if mustExist is true and the getter method was not found
 */
public static Method findSetter(Class cls, String prop, Class param, boolean mustExist) {
    String name = "set" + StringUtils.capitalize(prop);
    Method m;
    //        try {
    for (Class c = cls; c != null && c != Object.class; c = c.getSuperclass()) {
        m = getDeclaredMethod(c, name, param);
        if (m != null)
            return m;
    }
    //        } catch (Exception e) {
    //            throw new GeneralException(e);
    //        }

    if (mustExist)
        throw new RuntimeException(
                "Could not find setter for property " + prop + " in class " + cls.getCanonicalName());
    return null;
}

From source file:org.acoveo.tools.Reflection.java

/**
 * Return the addTo method matching the given collection property name, optionally
 * throwing an exception if none.//www  .  java 2 s  .c o m
 * @throws RuntimeException if mustExist is true and the setter method was not found
 */
public static Method findCollectionAdd(Class cls, String prop, Class param, boolean mustExist) {
    String name = "addTo" + StringUtils.capitalize(prop);
    Method m;
    for (Class c = cls; c != null && c != Object.class; c = c.getSuperclass()) {
        m = getDeclaredMethod(c, name, param);
        if (m != null)
            return m;
    }

    if (mustExist)
        throw new RuntimeException(
                "Could not find addTo for property " + prop + " in class " + cls.getCanonicalName());
    return null;
}

From source file:org.acoveo.tools.Reflection.java

/**
 * Return the removeFrom method matching the given collection property name, optionally
 * throwing an exception if none.//w ww  .  j a v  a2  s . c om
 * @throws RuntimeException if mustExist is true and the setter method was not found
 */
public static Method findCollectionRemove(Class cls, String prop, Class param, boolean mustExist) {
    String name = "removeFrom" + StringUtils.capitalize(prop);
    Method m;
    for (Class c = cls; c != null && c != Object.class; c = c.getSuperclass()) {
        m = getDeclaredMethod(c, name, param);
        if (m != null)
            return m;
    }

    if (mustExist)
        throw new RuntimeException(
                "Could not find removeFrom for property " + prop + " in class " + cls.getCanonicalName());
    return null;
}

From source file:org.acoveo.tools.Reflection.java

/**
 * Return the modifyMap method matching the given collection property name, optionally
 * throwing an exception if none./*from  w  w w.  j av  a 2  s  . c  o m*/
 * @throws RuntimeException if mustExist is true and the setter method was not found
 */
public static Method findModifyMap(Class cls, String prop, Class param, boolean mustExist) {
    String name = "modifyEntry" + StringUtils.capitalize(prop);
    Method m;
    for (Class c = cls; c != null && c != Object.class; c = c.getSuperclass()) {
        m = getDeclaredMethod(c, name, param);
        if (m != null)
            return m;
    }

    if (mustExist)
        throw new RuntimeException(
                "Could not find modifyMap for property " + prop + " in class " + cls.getCanonicalName());
    return null;
}

From source file:org.ambraproject.action.taxonomy.BrowseAction.java

@Override
public String execute() throws Exception {
    String browseValueKey = "ambra.virtualJournals." + getCurrentJournal() + ".taxonomyBrowser";
    //This journal not configured to use the taxonomy browser, return 404
    if (!configuration.getBoolean(browseValueKey, false)) {
        return INPUT;
    }// ww  w  .  j a  v a 2  s .c o m

    CategoryView categoryView = taxonomyService.parseCategories(super.getCurrentJournal());

    setDefaults();

    if (category != null && category.length() > 0) {
        //Recreate the category name as stored in the DB
        category = category.replace("_", " ");

        CategoryView view = CategoryUtils.findCategory(categoryView, category);

        //If the value is null, we've got a category that doesn't exist any more.  Try to format the name
        //And search for it anyway?
        if (view == null) {
            category = StringUtils.capitalize(category);
            parents = new String[] {};
            children = new String[] {};
        } else {
            category = view.getName();

            if (view.getParents().keySet().size() == 1 && view.getParents().keySet().contains("ROOT")) {
                parents = new String[] {};
            } else {
                parents = view.getParents().keySet().toArray(new String[view.getParents().keySet().size()]);
            }

            children = view.getChildren().keySet().toArray(new String[view.getChildren().keySet().size()]);

            //Get the featured article for this category
            if (this.category != null && this.category.length() > 0) {
                featuredArticle = taxonomyService.getFeaturedArticleForSubjectArea(this.getCurrentJournal(),
                        this.category);
            }
        }

        setFilterSubjects(new String[] { this.category });
    } else {
        category = null;
        parents = new String[] {};
        children = categoryView.getChildren().keySet()
                .toArray(new String[categoryView.getChildren().keySet().size()]);
    }

    resultsSinglePage = this.searchService.advancedSearch(getSearchParameters());

    if (resultsSinglePage.getHits().size() == 0) {
        return INPUT;
    } else {
        UserProfile user = getCurrentUser();
        if (user != null && category != null && !category.isEmpty()) {
            Pair<Boolean, Integer> result = userService.getJournalAlertAndSubjectCount(user.getID(),
                    this.getCurrentJournal(), category);
            subscribed = result != null ? result.getFirst() : false;
            subjectCount = result != null ? result.getSecond() : 0;
        } else {
            subscribed = false;
            subjectCount = 0;
        }
        return SUCCESS;
    }
}

From source file:org.amplafi.hivemind.factory.servicessetter.ServicesSetterImpl.java

/**
 * @see com.sworddance.core.ServicesSetter#wire(java.lang.Object, java.lang.Iterable)
 *///from   www . ja v a 2 s  . co  m
@Override
@SuppressWarnings("unchecked")
public void wire(Object obj, Iterable<String> excludedProperties) {
    if (obj == null) {
        return;
    }
    List<String> props = getWriteableProperties(obj);
    Set<String> alwaysExcludedCollection = this.cachedAlwaysExcludedMap.get(obj.getClass());

    if (alwaysExcludedCollection != null) {
        props.removeAll(alwaysExcludedCollection);
        if (getLog().isDebugEnabled()) {
            getLog().debug(obj.getClass() + ": autowiring. Class has already been filtered down to "
                    + props.size() + "properties. props={" + join(props, ",") + "}");
        }
    } else {
        if (getLog().isDebugEnabled()) {
            getLog().debug(obj.getClass() + ": autowiring for the first time. Class has at most " + props.size()
                    + "properties. props={" + join(props, ",") + "}");
        }
        alwaysExcludedCollection = Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>());
        this.cachedAlwaysExcludedMap.putIfAbsent(obj.getClass(), alwaysExcludedCollection);
        alwaysExcludedCollection = this.cachedAlwaysExcludedMap.get(obj.getClass());
    }
    for (String exclude : excludedProperties) {
        props.remove(exclude);
    }
    int wiredCount = 0;

    for (String prop : props) {
        PropertyAdaptor type = getPropertyAdaptor(obj, prop);
        Class propertyType = type.getPropertyType();
        if (!isWireableClass(propertyType)) {
            // if it is a standard java class then lets exclude it.
            alwaysExcludedCollection.add(prop);
            continue;
        }
        // if it is not readable then, then we can't verify that
        // we are not overwriting non-null property.
        if (!type.isReadable() || !type.isWritable()) {
            alwaysExcludedCollection.add(prop);
            continue;
        }
        // check to see if we have a service to offer before bothering
        // to checking if the property can be set. This avoids triggering
        // actions caused by calling the get/setters.
        Object srv = null;
        if (type.getPropertyType() == Log.class) {
            // log is special.
            srv = LogFactory.getLog(obj.getClass());
        } else {
            ConcurrentMap<String, String> classServiceMap = this.serviceMap.get(obj.getClass());
            if (classServiceMap == null) {
                this.serviceMap.putIfAbsent(obj.getClass(), new ConcurrentHashMap<String, String>());
                classServiceMap = this.serviceMap.get(obj.getClass());
            }
            String serviceName = classServiceMap.get(prop);
            if (serviceName == null) {
                InjectService service;
                try {
                    service = findInjectService(obj, type);
                } catch (DontInjectException e) {
                    // do nothing
                    alwaysExcludedCollection.add(prop);
                    continue;
                }

                if (service != null) {
                    serviceName = service.value();
                    if (isNotBlank(serviceName)) {
                        for (String attempt : new String[] { serviceName,
                                serviceName + '.' + type.getPropertyName(),
                                serviceName + '.' + StringUtils.capitalize(type.getPropertyName()) }) {
                            srv = getService(attempt, propertyType);
                            if (srv != null) {
                                serviceName = attempt;
                                break;
                            }
                        }
                    }
                }
                if (srv != null) {
                    classServiceMap.putIfAbsent(prop, serviceName);
                } else {
                    // we looked but did not find... no need to look again.
                    classServiceMap.putIfAbsent(prop, "");
                }
            } else if (!serviceName.isEmpty()) {
                // we already found the service.
                srv = getService(serviceName, propertyType);
            }
            if (srv == null && !noServiceForType.containsKey(propertyType)) {
                try {
                    srv = this.module.getService(propertyType);
                } catch (Exception e) {
                    noServiceForType.put(propertyType, e);
                    getLog().debug("Look up of class " + propertyType
                            + " failed. The failure is caused if there is not exactly 1 service implementing the class. Further searches by this property class will be ignored.");
                }
            }
        }
        if (srv == null) {
            alwaysExcludedCollection.add(prop);
        } else if (type.read(obj) == null) {
            // Doing the read check last avoids
            // triggering problems caused by lazy initialization and read-only properties.
            if (type.getPropertyType().isAssignableFrom(srv.getClass())) {
                type.write(obj, srv);
                wiredCount++;
            } else {
                // this is probably an error so we do not just add to the exclude list.
                throw new ApplicationRuntimeException("Trying to set property " + obj.getClass() + "." + prop
                        + " however, the property type=" + type.getPropertyType()
                        + " is not a superclass or same class as " + srv.getClass() + ". srv=" + srv);
            }
        }
    }
    if (getLog().isDebugEnabled()) {
        getLog().debug(obj.getClass() + ": done autowiring. actual number of properties wired=" + wiredCount
                + " excluded properties=" + alwaysExcludedCollection);
    }
}

From source file:org.amplafi.hivemind.factory.servicessetter.ServicesSetterImpl.java

/**
 * @param obj/*from w  ww  .  j ava 2  s .  c  o  m*/
 * @param type
 * @param propertyType
 * @return
 * @throws DontInjectException
 */
private InjectService findInjectService(Object obj, PropertyAdaptor type) throws DontInjectException {
    InjectService service;
    Class<?> propertyType = type.getPropertyType();
    String propertyAccessorMethodName = "set" + StringUtils.capitalize(type.getPropertyName());
    service = findServiceAnnotation(obj, propertyAccessorMethodName, propertyType);
    if (service == null) {
        propertyAccessorMethodName = "get" + StringUtils.capitalize(type.getPropertyName());
        service = findServiceAnnotation(obj, propertyAccessorMethodName);
    }
    if (service == null && (propertyType == boolean.class || propertyType == Boolean.class)) {
        propertyAccessorMethodName = "is" + StringUtils.capitalize(type.getPropertyName());
        service = findServiceAnnotation(obj, propertyAccessorMethodName);
    }
    return service;
}

From source file:org.andromda.cartridges.gui.metafacades.GuiActionLogicImpl.java

/**
 * Constructs the form bean name, with our without prefixing the use case name.
 * /*from w w w.ja  va 2s.  c  om*/
 * @param withUseCaseName whether or not to prefix the use case name.
 * @return the constructed form bean name.
 */
private String getFormBeanName(final boolean withUseCaseName) {

    final String pattern = ObjectUtils.toString(this.getConfiguredProperty(GuiGlobals.FORM_BEAN_PATTERN));
    final ModelElementFacade useCase = this.getUseCase();
    final String useCaseName = (withUseCaseName && (useCase != null))
            ? StringUtilsHelper.lowerCamelCaseName(useCase.getName())
            : "";
    final String formBeanName = pattern.replaceFirst("\\{0\\}", useCaseName);
    final String triggerName = (!pattern.equals(formBeanName)) ? StringUtils.capitalize(this.getTriggerName())
            : this.getTriggerName();

    return formBeanName.replaceFirst("\\{1\\}", triggerName);

}

From source file:org.andromda.cartridges.gui.metafacades.GuiActionLogicImpl.java

/**
 * @return formImplementationName//from   ww  w .  j a v a 2s  .  c om
 * @see org.andromda.cartridges.gui.metafacades.GuiAction#getFormImplementationName()
 */
@Override
protected String handleGetFormImplementationName() {

    final String pattern = ObjectUtils
            .toString(this.getConfiguredProperty(GuiGlobals.FORM_IMPLEMENTATION_PATTERN));

    return pattern.replaceFirst("\\{0\\}", StringUtils.capitalize(this.getTriggerName()));

}