List of usage examples for java.lang StringBuilder deleteCharAt
@Override public StringBuilder deleteCharAt(int index)
From source file:com.wxxr.nirvana.json.JSONValidationInterceptor.java
/** * @return JSON string that contains the errors and field errors *//*from w ww .ja v a2 s . c o m*/ @SuppressWarnings("unchecked") protected String buildResponse(ValidationAware validationAware) { //should we use FreeMarker here? StringBuilder sb = new StringBuilder(); sb.append("{ "); if (validationAware.hasErrors()) { //action errors if (validationAware.hasActionErrors()) { sb.append("\"errors\":"); sb.append(buildArray(validationAware.getActionErrors())); } //field errors if (validationAware.hasFieldErrors()) { if (validationAware.hasActionErrors()) sb.append(","); sb.append("\"fieldErrors\": {"); Map<String, List<String>> fieldErrors = validationAware.getFieldErrors(); for (Map.Entry<String, List<String>> fieldError : fieldErrors.entrySet()) { sb.append("\""); //if it is model driven, remove "model." see WW-2721 String fieldErrorKey = fieldError.getKey(); sb.append(((validationAware instanceof ModelDriven) && fieldErrorKey.startsWith("model.")) ? fieldErrorKey.substring(6) : fieldErrorKey); sb.append("\":"); sb.append(buildArray(fieldError.getValue())); sb.append(","); } //remove trailing comma, IE creates an empty object, duh sb.deleteCharAt(sb.length() - 1); sb.append("}"); } } sb.append("}"); /*response should be something like: * { * "errors": ["this", "that"], * "fieldErrors": { * field1: "this", * field2: "that" * } * } */ return sb.toString(); }
From source file:com.sunsharing.eos.uddi.service.NodeJSService.java
private void createMockConfigFile(String path, List<TService> services) throws Exception { StringBuilder sb = new StringBuilder(); sb.append("module.exports = {\n" + " mock:{\n"); for (TService service : services) { TServiceVersion serviceVersion = service.getVersions().get(0); sb.append(" " + service.getServiceCode()).append(":{\n"); for (TMethod method : serviceVersion.getMethods()) { String methodName = method.getMethodName(); if (methodName.startsWith("void ")) { methodName = methodName.replace("void ", ""); }//w ww .j a v a 2 s .c o m sb.append(" " + methodName).append(":\"\", //"); JSONArray methodMockResult = JSON.parseArray(method.getMockResult()); if (methodMockResult.size() > 0) { for (int i = 0, l = methodMockResult.size(); i < l; i++) { JSONObject jo = methodMockResult.getJSONObject(i); sb.append(jo.getString("status")); sb.append(","); } sb.deleteCharAt(sb.lastIndexOf(",")); } sb.append("\n"); } int deleteIdx = sb.lastIndexOf(", //"); sb.deleteCharAt(deleteIdx); sb.append(" },\n"); } sb.deleteCharAt(sb.lastIndexOf(",")); sb.append(" },\n" + " offlineData:{\n"); for (TService service : services) { TServiceVersion serviceVersion = service.getVersions().get(0); sb.append(" " + service.getServiceCode()).append(":{\n"); for (TMethod method : serviceVersion.getMethods()) { String methodName = method.getMethodName(); if (methodName.startsWith("void ")) { methodName = methodName.replace("void ", ""); } sb.append(" " + methodName).append(":{\n"); JSONArray methodMockResult = JSON.parseArray(method.getMockResult()); if (methodMockResult.size() > 0) { for (int i = 0, l = methodMockResult.size(); i < l; i++) { JSONObject jo = methodMockResult.getJSONObject(i); sb.append(" //" + jo.getString("desc")); sb.append("\n"); sb.append(" " + jo.getString("status")); sb.append(":"); String content = jo.getString("content") == null ? "null" : jo.getString("content").trim(); if (!(content.startsWith("[") || content.startsWith("{"))) { // content = JSON.toJSONString(content); } sb.append(content); sb.append(","); sb.append("\n"); } sb.deleteCharAt(sb.lastIndexOf(",")); } sb.append(" },\n"); } int deleteIdx = sb.lastIndexOf(","); sb.deleteCharAt(deleteIdx); sb.append(" },\n"); } sb.deleteCharAt(sb.lastIndexOf(",")); sb.append(" }\n" + "}"); FileUtil.createFile(path + File.separator + "config_mock.js", sb.toString(), "utf-8"); }
From source file:com.impetus.client.cassandra.thrift.CQLTranslator.java
/** * Prepares column name or column values. * /*w w w . j a v a2 s .c om*/ * @param record * entity. * @param entityMetadata * entity meta data * @param type * translation type. * @param externalProperties * the external properties * @param kunderaMetadata * the kundera metadata * @return Map containing translation type as key and string as translated * CQL string. */ public HashMap<TranslationType, Map<String, StringBuilder>> prepareColumnOrColumnValues(final Object record, final EntityMetadata entityMetadata, TranslationType type, Map<String, Object> externalProperties, final KunderaMetadata kunderaMetadata) { HashMap<TranslationType, Map<String, StringBuilder>> parsedColumnOrColumnValue = new HashMap<CQLTranslator.TranslationType, Map<String, StringBuilder>>(); if (type == null) { throw new TranslationException("Please specify TranslationType: either COLUMN or VALUE"); } MetamodelImpl metaModel = (MetamodelImpl) kunderaMetadata.getApplicationMetadata() .getMetamodel(entityMetadata.getPersistenceUnit()); Class entityClazz = entityMetadata.getEntityClazz(); EntityType entityType = metaModel.entity(entityClazz); Map<String, StringBuilder> builders = new HashMap<String, StringBuilder>(); Map<String, StringBuilder> columnBuilders = new HashMap<String, StringBuilder>(); onTranslation(record, entityMetadata, type, metaModel, entityClazz, entityType, builders, columnBuilders, externalProperties, kunderaMetadata); for (String tableName : columnBuilders.keySet()) { StringBuilder builder = builders.get(tableName); StringBuilder columnBuilder = columnBuilders.get(tableName); if (type.equals(TranslationType.ALL) || type.equals(TranslationType.VALUE)) { builder.deleteCharAt(builder.length() - 1); } if (type.equals(TranslationType.ALL) || type.equals(TranslationType.COLUMN)) { columnBuilder.deleteCharAt(columnBuilder.length() - 1); } } parsedColumnOrColumnValue.put(TranslationType.COLUMN, columnBuilders); parsedColumnOrColumnValue.put(TranslationType.VALUE, builders); return parsedColumnOrColumnValue; }
From source file:com.autonavi.gxdtaojin.toolbox.volley.Request.java
/** * Converts <code>params</code> into an application/x-www-form-urlencoded encoded string. *//*from w ww. j a v a 2s . c o m*/ private byte[] encodeParameters(Map<String, String> params, String paramsEncoding) { try { StringBuilder entryParams = new StringBuilder(); // POST int flag = Methods.getUrlEnt(mUrl); List<BasicNameValuePair> list = null; if (flag == 2) { // AOS?XXTea list = XXTeaHelper.getInstance().encryptAndEncodeToPost(params, true); } else if (flag == 1) { // ?AES list = AESHelper.getInstance().encryptAndHexToPost(params, true); } else { // ? list = new LinkedList<BasicNameValuePair>(); for (ConcurrentHashMap.Entry<String, String> entry : params.entrySet()) { list.add(new BasicNameValuePair(Methods.setEncodeParams(entry.getKey()), Methods.setEncodeParams(entry.getValue()))); } } for (int i = 0; i < list.size(); i++) { entryParams.append(list.get(i)).append("&"); } entryParams.deleteCharAt(entryParams.length() - 1); return entryParams.toString().getBytes(paramsEncoding); } catch (UnsupportedEncodingException uee) { throw new RuntimeException("Encoding not supported: " + paramsEncoding, uee); } }
From source file:org.openspaces.rest.space.ReplicationRESTController.java
protected String tspecToLookups(CloudifyRestEndpoint ep, TopologySpec tspec, Map<String, List<String>> addresses) { if (tspec == null || addresses == null) return "[]"; StringBuilder sb = new StringBuilder("["); Set<String> done = new HashSet<String>(); for (TopologyEdge edge : tspec.edges) { for (String site : Arrays.asList(edge.fromSite, edge.toSite)) { if (done.contains(site)) continue; done.add(site);/*from w ww .jav a 2s .c o m*/ sb.append("["); sb.append("gwname:").append(site).append(","); if (ep.siteId.equals(edge.fromSite)) { sb.append("address:").append(addresses.get(site).get(0)).append(","); } else { sb.append("address:").append(addresses.get(site).get(1)).append(","); } sb.append("discoport:").append(tspec.ports.get(site).get(0)).append(","); sb.append("commport:").append(tspec.ports.get(site).get(1)); sb.append("],"); } } sb.deleteCharAt(sb.length() - 1); //trim comma sb.append("]"); return sb.toString(); }
From source file:com.heliosapm.opentsdb.TSDBSubmitterImpl.java
/** * {@inheritDoc}/* w w w . j a v a2 s . c o m*/ * @see com.heliosapm.opentsdb.TSDBSubmitter#trace(long, java.lang.String, double, java.util.Map) */ @Override public void trace(final long timestamp, final String metric, final double value, final Map<String, String> tags) { if (!matches(metric, tags)) return; StringBuilder b = getSB(); b.append("put ").append(clean(metric)).append(" ").append(time()).append(" ").append(value).append(" "); appendRootTags(b); for (Map.Entry<String, String> entry : tags.entrySet()) { b.append(clean(entry.getKey())).append("=").append(clean(entry.getValue())).append(" "); } final byte[] trace = b.deleteCharAt(b.length() - 1).append("\n").toString().getBytes(CHARSET); synchronized (dataBuffer) { dataBuffer.writeBytes(trace); } tsdbConnection.traceCount.incrementAndGet(); }
From source file:com.heliosapm.opentsdb.TSDBSubmitterImpl.java
/** * {@inheritDoc}/*from w w w . jav a 2 s . c om*/ * @see com.heliosapm.opentsdb.TSDBSubmitter#trace(long, java.lang.String, long, java.util.Map) */ @Override public void trace(final long timestamp, final String metric, final long value, final Map<String, String> tags) { if (!matches(metric, tags)) return; StringBuilder b = getSB(); b.append("put ").append(clean(metric)).append(" ").append(timestamp).append(" ").append(value).append(" "); appendRootTags(b); for (Map.Entry<String, String> entry : tags.entrySet()) { b.append(clean(entry.getKey())).append("=").append(clean(entry.getValue())).append(" "); } final byte[] trace = b.deleteCharAt(b.length() - 1).append("\n").toString().getBytes(CHARSET); synchronized (dataBuffer) { dataBuffer.writeBytes(trace); } tsdbConnection.traceCount.incrementAndGet(); }
From source file:org.ngrinder.perftest.service.PerfTestService.java
private void completeCustomData(Map<String, String> returnMap, String key, StringBuilder customData) { if (customData.charAt(customData.length() - 1) == ',') { customData.deleteCharAt(customData.length() - 1); }//from ww w . j av a 2 s . c o m returnMap.put(key, customData.append("]").toString()); }