Package org.apache.commons.lang3.text

Examples of org.apache.commons.lang3.text.StrBuilder

The aim has been to provide an API that mimics very closely what StringBuffer provides, but with additional methods. It should be noted that some edge cases, with invalid indices or null input, have been altered - see individual methods. The biggest of these changes is that by default, null will not output the text 'null'. This can be controlled by a property, {@link #setNullText(String)}.

Prior to 3.0, this class implemented Cloneable but did not implement the clone method so could not be used. From 3.0 onwards it no longer implements the interface. @since 2.2 @version $Id: StrBuilder.java 1153484 2011-08-03 13:39:42Z ggregory $


        int limit = 2;
        int offset = 3;

        // Only offset, default limit.
        query.setRowOffset(offset);
        StrBuilder queryStringBuilder = new StrBuilder();
        SqlDataSourceHelper.appendLimitAndOffsetClause(query, queryStringBuilder);
        assertEquals(" OFFSET " + offset, queryStringBuilder.toString());

        // Both limit and offset.
        query.setRowLimit(limit);
        queryStringBuilder.delete(0, queryStringBuilder.length());
        SqlDataSourceHelper.appendLimitAndOffsetClause(query, queryStringBuilder);
        assertEquals("LIMIT " + limit + " OFFSET " + offset,
                queryStringBuilder.toString());

        // Only limit.
        query = new Query();
        queryStringBuilder.delete(0, queryStringBuilder.length());
        query.setRowLimit(limit);
        SqlDataSourceHelper.appendLimitAndOffsetClause(query, queryStringBuilder);
        assertEquals("LIMIT " + limit, queryStringBuilder.toString());

        // No limit and no offset.
        query = new Query();
        queryStringBuilder.delete(0, queryStringBuilder.length());
        SqlDataSourceHelper.appendLimitAndOffsetClause(query, queryStringBuilder);
        assertEquals("", queryStringBuilder.toString());
    }
View Full Code Here


        }
        if(description != null) {
            objectParts.add("\"detailed_message\":\"" + EscapeUtil.jsonEscape(description)
                    + "\"");
        }
        return new StrBuilder("{").appendWithSeparators(objectParts, ",").append("}").toString();
    }
View Full Code Here

        return new StrBuilder("{").appendWithSeparators(objectParts, ",").append("}").toString();
    }

    @Override
    public CharSequence error(final DataSourceRequest request, final ResponseStatus status) {
        StrBuilder sb = new StrBuilder();
        sb.append("{\"version\":\"0.6\"");

        final DataSourceParameters dsParams = request.getDataSourceParameters();
        // If no reqId found in the request, do not return reqId in the response.
        String requestId = dsParams.getRequestId();
        if(requestId != null) {
            sb.append(",\"reqId\":\"").append(EscapeUtil.jsonEscape(requestId)).append("\"");
        }
        // Check signature.
        String previousSignature = dsParams.getSignature();
        StatusType statusType = status.getStatusType();
        sb.append(",\"status\":\"").append(statusType.lowerCaseString()).append("\"");
        // Status is error.
        sb.append(",\"errors\":[");
        sb.append(getFaultString(status.getReasonType(), status.getDescription()));
        sb.append("]");
        sb.append("}");
        return sb.toString();
    }
View Full Code Here

    }


    @Override
    public CharSequence render(final DataSourceRequest request, final DataTable data) {
        StrBuilder sb = new StrBuilder();
        sb.append("{\"version\":\"0.6\"");
        // If no reqId found in the request, do not return reqId in the response.
        String requestId = request.getDataSourceParameters().getRequestId();
        if(requestId != null) {
            sb.append(",\"reqId\":\"").append(EscapeUtil.jsonEscape(requestId)).append("\"");
        }
        ResponseStatus responseStatus = null;
        if(!data.getWarnings().isEmpty()) {
            responseStatus = new ResponseStatus(StatusType.WARNING);
        }
        if(responseStatus == null) {
            // Check signature.
            final String previousSignature = request.getDataSourceParameters().getSignature();
            if(!StringUtils.isEmpty(previousSignature) && (this.getSignature(data).equals(previousSignature))) {
                responseStatus = new ResponseStatus(StatusType.ERROR, ReasonType.NOT_MODIFIED, null);
            }
            else {
                responseStatus = new ResponseStatus(StatusType.OK, null, null);
            }
        }
        StatusType statusType = responseStatus.getStatusType();
        sb.append(",\"status\":\"").append(statusType.lowerCaseString()).append("\"");
        // There are reason and messages if the status is WARNING/ERROR.
        if(statusType != StatusType.OK) {
            // Status is warning or error.
            if(statusType == StatusType.WARNING) {
                List<Warning> warnings = data.getWarnings();
                List<String> warningJsonStrings = Lists.newArrayList();
                if(warnings != null) {
                    for(Warning warning : warnings) {
                        warningJsonStrings.add(getFaultString(warning.getReasonType(), warning.getMessage()));
                    }
                }
                sb.append(",\"warnings\":[").appendWithSeparators(warningJsonStrings, ",").append("]");

            }
            else { // Status is error.
                sb.append(",\"errors\":[");
                sb.append(getFaultString(responseStatus.getReasonType(), responseStatus.getDescription()));
                sb.append("]");
            }
        }

        if((statusType != StatusType.ERROR)) {
            // MessageType OK or WARNING,
            // so need to attach a data table (and a signature).
            sb.append(",\"sig\":\"").append(this.getSignature(data)).append("\"");
            sb.append(",\"table\":").append(this.renderDataTable(data, true, true));
        }
        sb.append("}");
        return sb.toString();
    }
View Full Code Here

            for(Map.Entry<String, String> entry : propertiesMap.entrySet()) {
                customPropertiesStrings.add("\""
                        + EscapeUtil.jsonEscape(entry.getKey()) + "\":\""
                        + EscapeUtil.jsonEscape(entry.getValue()) + "\"");
            }
            customPropertiesString = new StrBuilder("{")
                    .appendWithSeparators(customPropertiesStrings, ",").append("}").toString();
        }
        return customPropertiesString;
    }
View Full Code Here

        for(QueryFilter filter : subFilters) {
            subFilterStrings.add("(" + filter.toQueryString() + ")");
        }
        // This works because the names in LogicalOperator are exactly the same as
        // in the query language.
        return new StrBuilder()
                .appendWithSeparators(subFilterStrings, " " + operator.name() + " ").toString();
    }
View Full Code Here

     * The string returned does not contain the LABEL keyword.
     *
     * @return The query string.
     */
    public String toQueryString() {
        StrBuilder builder = new StrBuilder();
        List<String> stringList = Lists.newArrayList();
        for(AbstractColumn col : columnLabels.keySet()) {
            String label = columnLabels.get(col);
            stringList.add(col.toQueryString() + " " + Query.stringToQueryStringLiteral(label));
        }
        builder.appendWithSeparators(stringList, ", ");
        return builder.toString();
    }
View Full Code Here

            throws DataSourceException {
        Connection con = getDatabaseConnection(databaseDescription);
        String tableName = databaseDescription.getTableName();

        // Build the sql query.
        StrBuilder queryStringBuilder = new StrBuilder();
        buildSqlQuery(query, queryStringBuilder, tableName);
        List<String> columnIdsList = null;
        if(query.hasSelection()) {
            columnIdsList = getColumnIdsList(query.getSelection());
        }
        Statement stmt = null;
        try {
            // Execute the sql query.
            stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(queryStringBuilder.toString());

            DataTable table = buildColumns(rs, columnIdsList);

            // Fill the data in the data table.
            buildRows(table, rs);
View Full Code Here

     *
     * @param queryFilter The query filter.
     * @return The sql query WHERE clause as a StrBuilder.
     */
    private static StrBuilder buildWhereClauseRecursively(QueryFilter queryFilter) {
        StrBuilder whereClause = new StrBuilder();

        // Base case of the recursion: the filter is not a compound filter.
        if(queryFilter instanceof ColumnIsNullFilter) {
            buildWhereClauseForIsNullFilter(whereClause, queryFilter);
        }
        else if(queryFilter instanceof ComparisonFilter) {
            buildWhereCluaseForComparisonFilter(whereClause, queryFilter);
        }
        else if(queryFilter instanceof NegationFilter) {
            whereClause.append("(NOT ");
            whereClause.append(buildWhereClauseRecursively(
                    ((NegationFilter) queryFilter).getSubFilter()));
            whereClause.append(")");
        }
        else {
            // queryFilter is a CompoundFilter.
            CompoundFilter compoundFilter = (CompoundFilter) queryFilter;

            int numberOfSubFilters = compoundFilter.getSubFilters().size();

            // If the compound filter is empty, build a where clause according to the
            // logical operator: nothing AND nothing -> WHERE "true", nothing OR
            // nothing -> WHERE "false" (match the query language rules).
            if(numberOfSubFilters == 0) {
                if(compoundFilter.getOperator() == CompoundFilter.LogicalOperator.AND) {
                    whereClause.append("true");
                }
                else {// OR
                    whereClause.append("false");
                }
            }
            else {
                List<String> filterComponents = Lists.newArrayList();
                for(QueryFilter filter : compoundFilter.getSubFilters()) {
                    filterComponents.add(buildWhereClauseRecursively(filter).toString());
                }
                String logicalOperator = getSqlLogicalOperator(compoundFilter.getOperator());
                whereClause.append("(").appendWithSeparators(filterComponents, " " + logicalOperator + " ")
                        .append(")");
            }
        }
        return whereClause;
    }
View Full Code Here

     * @param whereClause A string builder representing the WHERE clause of the SQL query.
     * @param queryFilter The query filter.
     */
    private static void buildWhereCluaseForComparisonFilter(
            StrBuilder whereClause, QueryFilter queryFilter) {
        StrBuilder first = new StrBuilder();
        StrBuilder second = new StrBuilder();

        // Build the left part and the right part of the clause according to the filter's type.
        if(queryFilter instanceof ColumnColumnFilter) {
            ColumnColumnFilter filter = (ColumnColumnFilter) queryFilter;
            first.append(getColumnId(filter.getFirstColumn()));
            second.append(getColumnId(filter.getSecondColumn()));
        }
        else { // The filter is a ColumnValueFilter
            ColumnValueFilter filter = (ColumnValueFilter) queryFilter;
            first.append(getColumnId(filter.getColumn()));
            second.append(filter.getValue().toString());
            if((filter.getValue().getType() == ValueType.TEXT)
                    || (filter.getValue().getType() == ValueType.DATE)
                    || (filter.getValue().getType() == ValueType.DATETIME)
                    || (filter.getValue().getType() == ValueType.TIMEOFDAY)) {
                second.insert(0, "\"");
                second.insert(second.length(), "\"");
            }
        }
        whereClause.append(buildWhereClauseFromRightAndLeftParts(
                first, second, ((ComparisonFilter) queryFilter).getOperator()));
    }
View Full Code Here

TOP

Related Classes of org.apache.commons.lang3.text.StrBuilder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.