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

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

Introduction

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

Prototype

public static String uncapitalize(String str) 

Source Link

Document

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

Usage

From source file:org.raml.jaxrs.codegen.core.visitors.CodeModelVisitor.java

@Override
public void visit(ResponseClassMethod responseClassMethod) {
    String name = StringUtils.uncapitalize(Names.buildVariableName(Names
            .buildResponseMethodName(responseClassMethod.getStatusCode(), responseClassMethod.getMimeType())));

    JDefinedClass responseClass = responseClassMethod.getResponseClass().getGeneratedObject();
    JMethod responseBuilderMethod = responseClass.method(PUBLIC + STATIC, responseClass, name);

    Response response = responseClassMethod.getResponse();
    String description = null;//from w ww . j  a v a 2s . c  o  m
    if (isNotBlank(response.getDescription())) {
        description = response.getDescription();
    }

    JDocComment javadoc = responseBuilderMethod.javadoc();
    if (isNotBlank(description)) {
        javadoc.add(description);
    }

    MimeType mimeType = responseClassMethod.getMimeType();
    String example = null;
    if (mimeType != null && isNotBlank(mimeType.getExample())) {
        example = EXAMPLE_PREFIX + mimeType.getExample();
    }
    if (isNotBlank(example)) {
        javadoc.add(example);
    }

    JInvocation builderArgument = ((JClass) CodeModelHelper.getGeneratorType(getCodeModel(),
            javax.ws.rs.core.Response.class)).staticInvoke("status")
                    .arg(JExpr.lit(responseClassMethod.getStatusCode()));

    String contentType = null;
    if (mimeType != null) {
        contentType = mimeType.getType();
        builderArgument = builderArgument.invoke("header").arg(HttpHeaders.CONTENT_TYPE).arg(contentType);
    }
    responseClassMethod.setGeneratedObject(responseBuilderMethod);

    // Arguments
    for (ResponseClassMethodArgument argument : responseClassMethod.getArguments()) {
        String argumentName = Names.buildVariableName(argument.getHeaderName());
        builderArgument = builderArgument.invoke("header").arg(argument.getHeaderName())
                .arg(JExpr.ref(argumentName));
        javadoc.addParam(argumentName).add(argument.getParameter().getDescription());
        JType codegenType = argument.getArgumentType().getGeneratedObject();
        responseBuilderMethod.param(codegenType, argumentName);
    }

    final JBlock responseBuilderMethodBody = responseBuilderMethod.body();
    final JVar builderVariable = responseBuilderMethodBody.decl(
            CodeModelHelper.getGeneratorType(getCodeModel(), ResponseBuilder.class), "responseBuilder",
            builderArgument);

    StringBuilder freeFormHeadersDescription = new StringBuilder();
    for (Entry<String, Header> namedHeaderParameter : response.getHeaders().entrySet()) {
        String headerName = namedHeaderParameter.getKey();
        Header header = namedHeaderParameter.getValue();
        if (headerName.contains(RESPONSE_HEADER_WILDCARD_SYMBOL)) {
            RamlHelper.appendParameterJavadocDescription(header, freeFormHeadersDescription);
        }
    }
    if (freeFormHeadersDescription.length() > 0) {

        // generate a Map<String, List<Object>> argument for {?} headers
        JClass listOfObjectsClass = ((JClass) CodeModelHelper.getGeneratorType(getCodeModel(), List.class))
                .narrow(Object.class);
        JClass headersArgument = ((JClass) CodeModelHelper.getGeneratorType(getCodeModel(), Map.class)).narrow(
                (JClass) CodeModelHelper.getGeneratorType(getCodeModel(), String.class), listOfObjectsClass);

        builderArgument = responseBuilderMethodBody.invoke("headers")
                .arg(JExpr.ref(MULTIPLE_RESPONSE_HEADERS_ARGUMENT_NAME)).arg(builderVariable);
        JVar param = responseBuilderMethod.param(headersArgument, MULTIPLE_RESPONSE_HEADERS_ARGUMENT_NAME);
        javadoc.addParam(param).add(freeFormHeadersDescription.toString());
    }

    if (contentType != null) {
        JType entity = null;

        if (!isBlank(mimeType.getSchema())) {
            entity = schemaClasses.get(Types.buildSchemaKey(mimeType));

            if (responseClassMethod.isRepeat()) {
                entity = ((JClass) CodeModelHelper.getGeneratorType(getCodeModel(), List.class)).narrow(entity);
            }
        }

        if (entity == null) {
            if (startsWith(contentType, "text/")) {
                entity = CodeModelHelper.getGeneratorType(getCodeModel(), String.class);
            } else {
                // fallback to a streaming output
                entity = CodeModelHelper.getGeneratorType(getCodeModel(), StreamingOutput.class);
            }
        }
        boolean isLink = entity.name().equalsIgnoreCase(LINK_NAME)
                && getConfiguration().isHateoasResourceSupport();
        if (!isLink) {
            responseBuilderMethodBody.invoke(builderVariable, GENERIC_PAYLOAD_ARGUMENT_NAME)
                    .arg(JExpr.ref(GENERIC_PAYLOAD_ARGUMENT_NAME));
            responseBuilderMethod.param(entity, GENERIC_PAYLOAD_ARGUMENT_NAME);
            javadoc.addParam(GENERIC_PAYLOAD_ARGUMENT_NAME).add(defaultString(example));
        }
    }

    if (getConfiguration().isHateoasResourceSupport()) {
        JClass jClassLink = getCodeModel().ref("javax.ws.rs.core.Link");
        JVar jVar = responseClassMethod.getGeneratedObject().varParam(jClassLink, "link");
        responseClassMethod.getGeneratedObject().body().invoke(JExpr.ref("responseBuilder"), "links").arg(jVar);
    }

    responseBuilderMethodBody._return(JExpr._new(responseClass).arg(builderVariable.invoke("build")));
}

From source file:org.sindice.summary.AbstractDumpTest.java

/**
 * Executes the graph summary and asserts the generated RDF data.
 * @param q the {@link AbstractQuery}/*from  w  w w . j  av  a 2s .  co  m*/
 * @param out the path to the generated data
 * @param folder the name of the current test folder with expected results
 * @throws Exception if an error occurred while asserting the data
 */
protected void _assertDump(final AbstractQuery q, final String out, final String folder) throws Exception {
    BufferedReader actual = null;
    BufferedReader expected = null;

    try {
        q.initDump(out);
        q.computeName();
        q.computePredicate();
        q.stopConnexion();

        // actual results
        String line;
        actual = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(out))));
        final List<String> actualResults = new ArrayList<String>();
        while ((line = actual.readLine()) != null) {
            actualResults.add(line);
        }
        Collections.sort(actualResults);

        // Expected results
        final String name = StringUtils.uncapitalize(this.getClass().getSimpleName());
        final String file = "./src/test/resources/" + name + "/" + folder + "/output.txt";
        expected = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
        final List<String> expectedResults = new ArrayList<String>();
        while ((line = expected.readLine()) != null) {
            expectedResults.add(line);
        }
        Collections.sort(expectedResults);

        assertArrayEquals(expectedResults.toArray(new String[0]), actualResults.toArray(new String[0]));
    } finally {
        try {
            if (expected != null) {
                expected.close();
            }
        } finally {
            try {
                if (actual != null) {
                    actual.close();
                }
            } finally {
                q.stopConnexion();
            }
        }
    }
}

From source file:org.springjutsu.validation.ValidationErrorMessageHandler.java

/**
 * Constructs the actual model message key for a field label.
 * @param parentType the class of the bean on which the field path is found
 * @param fieldPath the path of the field for which a message is being resolved
 * @return the constructed message key, prefixed with a configured field label prefix
 *//*w  w w  . j  ava2s .  co m*/
protected String buildMessageKey(Class<?> parentType, String fieldPath) {
    return (fieldLabelPrefix != null && !fieldLabelPrefix.isEmpty() ? fieldLabelPrefix + "." : "")
            + StringUtils.uncapitalize(parentType.getSimpleName()) + "." + fieldPath;
}

From source file:org.springjutsu.validation.ValidationManager.java

/**
 * If we're trying to resolve the message key for a path on the model,
 * this method will unwrap that message key.
 * For instance, consider our model is a Account instance, which has a 
 * field accountOwner of type User, and that User object has a 
 * username field of type String://from  www  .j  a va 2  s  . c  o m
 * If rulePath was "accountOwner.username", then it would return a
 * message key of "user.username", which is the simple classname of the
 * owning object of the failed validation path, and the field name.
 * This is so we can display the label of the field that failed validation
 * in the error message. For instance "User Name must be 8 chars" instead
 * of something cryptic like "accountOwner.username must be 8 chars".
 * @param rulePath Validation rule path to the failed field.
 * @param rootModel The root model owning the field that failed.
 * @return A message key used to resolve a message describing the field
 * that failed.
 */
protected String getModelMessageKey(String rulePath, Object rootModel) {

    if (rulePath == null || rulePath.length() < 1) {
        return rulePath;
    }

    Object parent = null;
    String fieldPath = null;

    if (rulePath.contains(".")) {
        fieldPath = rulePath.substring(rulePath.lastIndexOf(".") + 1);
        String parentPath = rulePath.substring(0, rulePath.lastIndexOf("."));
        BeanWrapperImpl beanWrapper = new BeanWrapperImpl(rootModel);
        parent = beanWrapper.getPropertyValue(parentPath);
    } else {
        fieldPath = rulePath;
        parent = rootModel;
    }

    return fieldLabelPrefix + StringUtils.uncapitalize(parent.getClass().getSimpleName()) + "." + fieldPath;
}

From source file:org.springmodules.xt.model.generator.factory.FactoryGeneratorInterceptor.java

private Object putProperty(final Object[] args, final Method method) {
    if (args.length != 1) {
        throw new IllegalStateException(
                "The setter method " + method.getName() + " must have only one argument!");
    } else {/* w w  w. ja  v a  2s .  co m*/
        String name = StringUtils.uncapitalize(method.getName().substring(3));
        Property annotation = method.getAnnotation(Property.class);
        Property.AccessType access = annotation.access();
        PropertyPair pair = new PropertyPair();
        pair.setValue(args[0]);
        pair.setAccess(access);
        this.properties.put(name, pair);
        this.values.put(name, args[0]);
        logger.debug(new StringBuilder("Put property with name and value: ").append(name).append(",")
                .append(args[0]));
        return null;
    }
}

From source file:org.springmodules.xt.model.generator.factory.FactoryGeneratorInterceptor.java

private Object putValue(final Object[] args, final Method method) {
    if (args.length != 1) {
        throw new IllegalStateException(
                "The setter method " + method.getName() + " must have only one argument!");
    } else {// w  w w  .j ava  2  s.  com
        String name = StringUtils.uncapitalize(method.getName().substring(3));
        this.values.put(name, args[0]);
        logger.debug(new StringBuilder("Put value property with name and value: ").append(name).append(",")
                .append(args[0]));
        return null;
    }
}

From source file:org.springmodules.xt.model.generator.factory.FactoryGeneratorInterceptor.java

private Object putConstructorArg(final Object[] args, final Method method) {
    if (args.length != 1) {
        throw new IllegalStateException(
                "The setter method " + method.getName() + " must have only one argument!");
    } else {/* w  w w  . j av  a 2  s  .c  om*/
        String name = StringUtils.uncapitalize(method.getName().substring(3));
        ConstructorArg annotation = method.getAnnotation(ConstructorArg.class);
        int position = annotation.position();
        Class type = null;
        if (method.isAnnotationPresent(ConstructorArgType.class)) {
            type = method.getAnnotation(ConstructorArgType.class).type();
        } else {
            type = args[0].getClass();
        }
        ConstructorArgPair pair = new ConstructorArgPair();
        pair.setValue(args[0]);
        pair.setType(type);
        this.constructorArgs.put(position, pair);
        this.values.put(name, args[0]);
        logger.debug(new StringBuilder("Put constructor arg with position and value: ").append(position)
                .append(",").append(args[0]));
        return null;
    }
}

From source file:org.springmodules.xt.model.generator.factory.FactoryGeneratorInterceptor.java

private Object readProperty(final Object[] args, final Method method) {
    if (args != null && args.length != 0) {
        throw new IllegalStateException("The getter method " + method.getName() + " must have no arguments!");
    }// w  ww. j a  va 2s .co  m
    if (method.getReturnType().isPrimitive()) {
        throw new IllegalReturnTypeException("Return types cannot be primitives.");
    } else {
        String name = null;
        if (method.getName().startsWith("get")) {
            name = StringUtils.uncapitalize(method.getName().substring(3));
        } else {
            name = StringUtils.uncapitalize(method.getName().substring(2));
        }
        Object value = values.get(name);
        logger.debug(new StringBuilder("Read property with name and value: ").append(name).append(",")
                .append(value));
        return value;
    }
}

From source file:org.springmodules.xt.model.introductor.bean.BeanIntroductorInterceptor.java

protected Object executeOnTargetField(MethodInvocation methodInvocation, Method method) throws Exception {
    logger.debug("Mapping to target field; method: " + method.getName());
    Object result = null;//from w  ww.j ava  2s .c  o  m
    try {
        Object target = methodInvocation.getThis();
        if (method.getName().startsWith("get")) {
            String fieldName = StringUtils.uncapitalize(method.getName().substring(3));
            Field field = target.getClass().getDeclaredField(fieldName);
            field.setAccessible(true);
            result = field.get(target);
        } else if (method.getName().startsWith("is")) {
            String fieldName = StringUtils.uncapitalize(method.getName().substring(2));
            Field field = target.getClass().getDeclaredField(fieldName);
            field.setAccessible(true);
            result = field.get(target);
        } else if (method.getName().startsWith("set")) {
            String fieldName = StringUtils.uncapitalize(method.getName().substring(3));
            Field field = target.getClass().getDeclaredField(fieldName);
            field.setAccessible(true);
            if (methodInvocation.getArguments().length != 1) {
                throw new IllegalStateException(
                        "The setter method " + method.getName() + " must have only one argument!");
            } else {
                field.set(target, methodInvocation.getArguments()[0]);
            }
        } else {
            throw new UnsupportedOperationException(
                    "The introduced interface must contain only setter and getter methods.");
        }
    } catch (Exception ex) {
        logger.warn("Something wrong happened calling: " + method.getName());
        logger.warn("Exception message: " + ex.getMessage());
        throw ex;
    }
    return result;
}

From source file:org.swordess.ldap.bean.Specification.java

public static String getPropertyName(Method method) {
    if (isSetter(method)) {
        return StringUtils.uncapitalize(method.getName().substring(3));

    } else if (isGetter(method)) {
        if (method.getName().startsWith("get")) {
            return StringUtils.uncapitalize(method.getName().substring(3));
        } else if (method.getName().startsWith("is")) {
            return StringUtils.uncapitalize(method.getName().substring(2));
        } else {/*  w ww .  j  a  v a 2  s  . c  om*/
            /* should not occur */
        }
    }

    return null;
}