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

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

Introduction

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

Prototype

public static String capitalize(final 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.jpos.util.FieldFactory.java

public Binder.BindingBuilder formatField(String id, HasValue field) {
    Binder.BindingBuilder builder = getBinder().forField(field);
    builder = builder.withNullRepresentation("");
    if (viewConfig == null)
        return builder;
    List<Validator> v = getValidators(id);
    for (Validator val : v)
        builder.withValidator(val);
    if (isRequired(id))
        builder.asRequired(getApp().getMessage("errorMessage.req",
                StringUtils.capitalize(getCaptionFromId("field." + id))));
    ViewConfig.FieldConfig config = viewConfig.getFields().get(id);
    String width = config != null ? config.getWidth() : null;
    if (field instanceof AbstractComponent)
        ((AbstractComponent) field).setWidth(width);
    return builder;
}

From source file:org.jsweet.input.typescriptdef.ast.DeclarationHelper.java

public static String getActualFunctionName(FunctionDeclaration function) {
    if (JAVA_OBJECT_METHODS.keySet().contains(function.getName())) {
        if (ArrayUtils.contains(JAVA_OBJECT_METHODS.get(function.getName()), function.getParameters().length)) {
            return StringUtils.capitalize(function.getName());
        }//from   w  w  w .java2 s  .  com
    }
    return function.getName();
}

From source file:org.jsweet.input.typescriptdef.ast.DeclarationHelper.java

public static String toJavaIdentifier(String identifier) {
    if (JSweetDefTranslatorConfig.JAVA_KEYWORDS.contains(identifier)) {
        return StringUtils.capitalize(identifier);
    }/* w ww.java 2 s .  c  om*/
    if (JSweetDefTranslatorConfig.JAVA_TS_KEYWORDS.contains(identifier)) {
        return StringUtils.capitalize(identifier);
    }

    // TODO: this is not what happens... it must have been transformed
    // before
    if (Character.isDigit(identifier.charAt(0))) {
        return "$$" + identifier.charAt(0) + "$$" + identifier.substring(1);
    }
    return identifier;
}

From source file:org.jsweet.input.typescriptdef.ast.TypeReference.java

public String getWrappingTypeName() {
    if ("any".equals(name)) {
        return "java.lang.Object";
    } else if (isPrimitive()) {
        return StringUtils.capitalize(name);
    } else {/*from  ww  w.j  a v  a  2  s.co m*/
        return name;
    }
}

From source file:org.jsweet.input.typescriptdef.util.NameUtils.java

public static String getDiff(String referenceFullSignature, String targetFullSignature) {
    List<String> referenceSegments = new ArrayList<String>(
            Arrays.asList(referenceFullSignature.replace("[]", "s").split("[<>,\\(\\)]")));
    String[] targetSegments = targetFullSignature.replace("[]", "s").split("[<>,\\(\\)]");
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < targetSegments.length; i++) {
        if (!referenceSegments.contains(targetSegments[i])) {
            sb.append(StringUtils.capitalize(targetSegments[i]));
        } else {//from  w w w . j a va  2 s .c  om
            referenceSegments.remove(targetSegments[i]);
        }
    }
    return sb.toString();
}

From source file:org.jsweet.input.typescriptdef.util.Util.java

public static String toJavaName(String name, boolean forceLowerCase) {
    if (name == null) {
        return null;
    }/*from   w  w w .  j av a  2s  . co  m*/
    if (forceLowerCase) {
        if (!StringUtils.isAllLowerCase(name)) {
            name = name.toLowerCase();
        }
    }
    if (name.contains("-") || name.contains("/")) {
        return name.replace('/', '_').replace('-', '_');
    }
    if (Character.isDigit(name.charAt(0))) {
        return "_" + name;
    }
    if (JSweetDefTranslatorConfig.JAVA_KEYWORDS.contains(name)) {
        if (forceLowerCase) {
            return "_" + name;
        } else {
            return StringUtils.capitalize(name);
        }
    }
    return name;
}

From source file:org.jsweet.input.typescriptdef.visitor.DuplicateMethodsCleaner.java

private List<String> calculateNames(TypeDeclaration highestTypeDeclaration, Strategy strategy,
        boolean functionalDisambiguation, List<FullFunctionDeclaration> l, FunctionDeclaration reference,
        int paramIndex) {
    List<String> names = new ArrayList<String>();
    switch (strategy) {
    case USER_FRIENDLY:
        if (functionalDisambiguation) {
            // apply a diff strategy (more user friendly in some cases)
            for (FullFunctionDeclaration f : l) {
                String newInterfaceName = StringUtils
                        .capitalize(reference.getParameters()[paramIndex].getName())
                        + NameUtils.getDiff(
                                context.getShortTypeNameNoErasure(
                                        reference.getParameters()[paramIndex].getType()),
                                context.getShortTypeNameNoErasure(
                                        f.function.getParameters()[paramIndex].getType()));
                if (newInterfaceName.length() > MAX_NAME_LENGTH) {
                    newInterfaceName = StringUtils.capitalize(reference.getParameters()[paramIndex].getName())
                            + names.size();
                }//from  w  w  w.j  av a 2 s.  c  o  m
                TypeReference parameterType = reference.getParameters()[paramIndex].getType();
                if (parameterType.getName().contains("Consumer")) {
                    switch (parameterType.getTypeArguments().length) {
                    case 1:
                        newInterfaceName += "Consumer";
                        break;
                    case 2:
                        newInterfaceName += "BiConsumer";
                        break;
                    case 3:
                        newInterfaceName += "TriConsumer";
                        break;
                    default:
                        newInterfaceName += "Consumer";
                        newInterfaceName += parameterType.getTypeArguments().length;
                        break;
                    }
                }
                if (parameterType.getName().contains("Function")) {
                    switch (parameterType.getTypeArguments().length) {
                    case 2:
                        newInterfaceName += "Function";
                        break;
                    case 3:
                        newInterfaceName += "BiFunction";
                        break;
                    case 4:
                        newInterfaceName += "TriFunction";
                        break;
                    default:
                        newInterfaceName += "Function";
                        newInterfaceName += parameterType.getTypeArguments().length - 1;
                        break;
                    }
                }
                if (parameterType.getName().contains("Supplier")) {
                    newInterfaceName += "Supplier";
                }
                // this naming convention seems to avoid all name clashes
                // newInterfaceName =
                // resolveNameClash(functionalDisambiguation,
                // highestTypeDeclaration,
                // f.function.getParameters()[paramIndex].getType(),
                // newInterfaceName);
                names.add(newInterfaceName);
            }
        } else {
            for (FullFunctionDeclaration f : l) {
                // TODO: the parameter happens to clash sometime (so full
                // strategy from scratch until we find a better way)
                // SEE: underscore: _._Chain.map(...) methods
                // String newInterfaceName =
                // StringUtils.capitalize(reference.getParameters()[paramIndex].getName());

                String newInterfaceName = null;
                if (DeclarationHelper.getTypeOrComponentType(
                        reference.getParameters()[paramIndex].getType()) instanceof TypeParameterDeclaration) {
                    newInterfaceName = StringUtils.capitalize(reference.getParameters()[paramIndex].getName());
                } else {
                    newInterfaceName = StringUtils.capitalize(
                            context.getShortTypeNameErased(reference.getParameters()[paramIndex].getType())
                                    .replace("[]", "s"));
                }
                if (f.function.getParameters()[paramIndex].getType().getTypeArguments() != null) {
                    for (TypeReference t : f.function.getParameters()[paramIndex].getType()
                            .getTypeArguments()) {
                        String[] tokens = context.getShortTypeNameNoErasure(t).split("[<>,]");
                        for (String token : tokens) {
                            newInterfaceName += StringUtils.capitalize(token).replace("[]", "s");
                        }
                    }
                } else {
                    String[] tokens = context
                            .getShortTypeNameNoErasure(f.function.getParameters()[paramIndex].getType())
                            .split("[<>,]");
                    for (String token : tokens) {
                        newInterfaceName += StringUtils.capitalize(token).replace("[]", "s");
                    }
                }
                if (newInterfaceName.length() > MAX_NAME_LENGTH) {
                    newInterfaceName = StringUtils.capitalize(reference.getParameters()[paramIndex].getName())
                            + names.size();
                }
                newInterfaceName = resolveNameClash(functionalDisambiguation, highestTypeDeclaration,
                        f.function.getParameters()[paramIndex].getType(), newInterfaceName);
                names.add(newInterfaceName);
            }
        }
        break;
    case FULL:
        // apply a whole name strategy
        if (functionalDisambiguation) {
            for (FullFunctionDeclaration f : l) {
                String newInterfaceName = StringUtils
                        .capitalize(reference.getParameters()[paramIndex].getName());
                if (f.function.getParameters()[paramIndex].getType().getTypeArguments() != null) {
                    for (TypeReference t : f.function.getParameters()[paramIndex].getType()
                            .getTypeArguments()) {
                        String[] tokens = context.getShortTypeNameNoErasure(t).split("[<>,]");
                        for (String token : tokens) {
                            newInterfaceName += StringUtils.capitalize(token).replace("[]", "s");
                        }
                    }
                } else {
                    String[] tokens = context
                            .getShortTypeNameNoErasure(f.function.getParameters()[paramIndex].getType())
                            .split("[<>,]");
                    for (String token : tokens) {
                        newInterfaceName += StringUtils.capitalize(token).replace("[]", "s");
                    }
                }
                if (newInterfaceName.length() > MAX_NAME_LENGTH) {
                    newInterfaceName = StringUtils.capitalize(reference.getParameters()[paramIndex].getName())
                            + names.size();
                }
                newInterfaceName = resolveNameClash(functionalDisambiguation, highestTypeDeclaration,
                        f.function.getParameters()[paramIndex].getType(), newInterfaceName);
                names.add(newInterfaceName);
            }
        } else {
            for (FullFunctionDeclaration f : l) {
                String newInterfaceName = StringUtils.capitalize(
                        context.getShortTypeNameErased(reference.getParameters()[paramIndex].getType())
                                .replace("[]", "s"));
                if (f.function.getParameters()[paramIndex].getType().getTypeArguments() != null) {
                    for (TypeReference t : f.function.getParameters()[paramIndex].getType()
                            .getTypeArguments()) {
                        String[] tokens = context.getShortTypeNameNoErasure(t).split("[<>,]");
                        for (String token : tokens) {
                            newInterfaceName += StringUtils.capitalize(token).replace("[]", "s");
                        }
                    }
                } else {
                    String[] tokens = context
                            .getShortTypeNameNoErasure(f.function.getParameters()[paramIndex].getType())
                            .split("[<>,]");
                    for (String token : tokens) {
                        newInterfaceName += StringUtils.capitalize(token).replace("[]", "s");
                    }
                }
                if (newInterfaceName.length() > MAX_NAME_LENGTH) {
                    newInterfaceName = StringUtils.capitalize(reference.getParameters()[paramIndex].getName())
                            + names.size();
                }
                newInterfaceName = resolveNameClash(functionalDisambiguation, highestTypeDeclaration,
                        f.function.getParameters()[paramIndex].getType(), newInterfaceName);
                names.add(newInterfaceName);
            }
        }
    }
    return names;
}

From source file:org.jsweet.input.typescriptdef.visitor.GlobalsCreator.java

@Override
public void visitModuleDeclaration(ModuleDeclaration moduleDeclaration) {
    for (Declaration declaration : moduleDeclaration.getMembers()) {
        if (declaration.isHidden()) {
            continue;
        }/*w  w  w . jav  a 2  s . com*/
        if ((declaration instanceof VariableDeclaration) || (declaration instanceof FunctionDeclaration)) {
            TypeDeclaration globalsClass = getOrCreateGlobalsType(context, moduleDeclaration,
                    getParent(ModuleDeclaration.class));
            if (DeclarationHelper.JS_OBJECT_METHOD_NAMES.contains(declaration.getName())) {
                declaration.setName(StringUtils.capitalize(declaration.getName()));
            }
            moduleDeclaration.removeMember(declaration);
            if (declaration instanceof VariableDeclaration) {
                VariableDeclaration existing = globalsClass.findVariable(declaration.getName());
                if (existing != null && !existing.isHidden()) {
                    context.reportWarning("skip variable " + declaration + " - already exists in "
                            + moduleDeclaration + " (" + existing + ")");
                    // variable already exists
                    continue;
                }
            }
            globalsClass.addMember(declaration);
            declaration.addModifier("static");
            if (declaration instanceof VariableDeclaration) {
                VariableDeclaration varDecl = (VariableDeclaration) declaration;
                Type t = lookupType(varDecl.getType(), null);
                if (t instanceof TypeDeclaration) {
                    final TypeDeclaration typeDeclaration = (TypeDeclaration) t;
                    if (!typeDeclaration.isExternal() && typeDeclaration.isStatic()) {
                        globalsClass.removeMember(declaration);
                    }
                }
            }
        }
    }
    super.visitModuleDeclaration(moduleDeclaration);
}

From source file:org.jsweet.input.typescriptdef.visitor.ObjectTypeCreator.java

@Override
public void visitTypeReference(TypeReference typeReference) {
    if (typeReference.isObjectType()) {
        DeclarationContainer container = getParent(t -> {
            return (t instanceof DeclarationContainer) && !(t instanceof TypeMacroDeclaration);
        });//from  w  w  w.  j av  a2s  . c  o m
        TypedDeclaration parentDeclaration = getParent(TypedDeclaration.class);
        if (typeReference.getObjectType().getMembers().length == 0) {
            Util.substituteTypeReference(this, parentDeclaration, typeReference,
                    new TypeReference(null, Object.class.getName(), null));
        } else {

            String rootName = StringUtils.capitalize(parentDeclaration.getName());
            String name = rootName;
            int i = 1;

            TypeParameterDeclaration[] typeParameters = Util.findTypeParameters(this, typeReference);
            for (TypeParameterDeclaration p : typeParameters) {
                if (Util.containsAstNode(typeReference, p)) {
                    typeParameters = ArrayUtils.remove(typeParameters, ArrayUtils.indexOf(typeParameters, p));
                }
            }
            if (typeParameters.length == 0) {
                typeParameters = null;
            }
            TypeDeclaration newClass = null;

            while (newClass == null) {
                // first check that no type of the same level has the same
                // name
                TypeDeclaration conflictingType = container.findType(name);
                boolean conflict = false;
                if (conflictingType != null) {
                    for (Declaration member : typeReference.getObjectType().getMembers()) {
                        if (!ArrayUtils.contains(conflictingType.getMembers(), member)) {
                            conflict = true;
                            break;
                        }
                    }
                    if (!conflict) {
                        newClass = conflictingType;
                    } else {
                        name = rootName + getSuffix(i++);
                    }
                } else {
                    // check enclosing types names (in Java, enclosed types
                    // cannot be named like an enclosing one)
                    List<TypeDeclaration> parents = getParents(TypeDeclaration.class);
                    for (TypeDeclaration parent : parents) {
                        if (name.equals(parent.getName())) {
                            conflict = true;
                        }
                    }
                    if (!conflict) {
                        NameConflictFinder finder = new NameConflictFinder(context, name);
                        container.accept(finder);
                        conflict = finder.conflict;
                    }
                    if (conflict) {
                        name = rootName + getSuffix(i++);
                    } else {
                        // finally, no conflict, we create the class
                        newClass = new TypeDeclaration(null, "class", name,
                                DeclarationHelper.copy(typeParameters), null, null);
                        newClass.setDocumentation(
                                "/** This is an automatically generated object type (see the source definition). */");
                        if (container instanceof TypeDeclaration) {
                            newClass.addModifier("static");
                        }
                        newClass.addMembers(typeReference.getObjectType().getMembers());
                        newClass.addStringAnnotation(JSweetDefTranslatorConfig.ANNOTATION_OBJECT_TYPE);
                        container.addMember(newClass);
                    }
                }
            }

            String objectTypeFullName = getCurrentContainerName() + "." + newClass.getName();

            logger.trace("registering object type: " + objectTypeFullName + " in container " + container + " ("
                    + container.getClass().getSimpleName() + ")");
            List<TypeDeclaration> containerGeneratedTypes = context.generatedObjectTypes.get(container);
            if (containerGeneratedTypes == null) {
                context.generatedObjectTypes.put(container,
                        containerGeneratedTypes = new LinkedList<TypeDeclaration>());
            }
            containerGeneratedTypes.add(newClass);

            // parentDeclaration.setType(new TypeReference(null,
            // newClass.getName(), DeclarationHelper
            // .toTypeArguments(typeParameters)));

            Util.substituteTypeReference(this, parentDeclaration, typeReference, new TypeReference(null,
                    newClass.getName(), DeclarationHelper.toTypeArguments(typeParameters)));
            context.registerType(objectTypeFullName, newClass);
            // recursively scan the newly created class
            scan(newClass);
        }
    }
    scan(typeReference.getTypeArguments());
}

From source file:org.jsweet.input.typescriptdef.visitor.ParentMethodReturnTypeSusbtitutor.java

@Override
public void visitFunctionDeclaration(FunctionDeclaration functionDeclaration) {
    TypeDeclaration declaringType = getParent(TypeDeclaration.class);
    if (declaringType != null && functionDeclaration.getType() != null
            && functionDeclaration.getType().getName() != null
            && !functionDeclaration.getType().getName().equals("void")) {
        applyToSuperMethod(declaringType, functionDeclaration, (parentType, parentFunction) -> {
            if (functionDeclaration.getType().isPrimitive() && !parentFunction.getType().isPrimitive()) {
                functionDeclaration.getType()
                        .setName(StringUtils.capitalize(functionDeclaration.getType().getName()));
            } else {
                if ("void".equals(parentFunction.getType().getName())) {

                    if (context.isInDependency(parentType)) {
                        // if parent class belongs to a dependency, we cant
                        // change its definition
                        logger.info("modify return type of " + context.getTypeName(declaringType) + "."
                                + functionDeclaration + ": " + functionDeclaration.getType() + " -> "
                                + parentFunction.getType());
                        functionDeclaration.setType(parentFunction.getType());
                    } else {
                        logger.info("modify return type of " + context.getTypeName(parentType) + "."
                                + parentFunction + ": " + parentFunction.getType() + " -> "
                                + functionDeclaration.getType());
                        parentFunction.setType(functionDeclaration.getType());
                    }/* ww  w  . ja va 2s.  c  o  m*/
                }
            }
        });
    }
}