List of usage examples for org.apache.commons.lang StringUtils replace
public static String replace(String text, String searchString, String replacement)
Replaces all occurrences of a String within another String.
From source file:com.sfs.dao.BackupDAOImpl.java
/** * Filter data.//from ww w. j ava2s. c o m * * @param dataVal the data * * @return the string */ private String filterData(final String dataVal) { String data = ""; data = StringUtils.replace(dataVal, "[']", "\\\\'"); data = StringUtils.replace(data, "[\"]", "\\\\\""); data = StringUtils.replace(data, "[\n]", "\\\\n"); data = StringUtils.replace(data, "[\t]", "\\\\t"); data = StringUtils.replace(data, "[\r]", "\\\\r"); return data; }
From source file:com.opengamma.web.position.WebPositionsResource.java
@POST @Consumes(MediaType.APPLICATION_FORM_URLENCODED) @Produces(MediaType.TEXT_HTML)/*from w ww. jav a2s. c o m*/ public Response postHTML(@FormParam("quantity") String quantityStr, @FormParam("idscheme") String idScheme, @FormParam("idvalue") String idValue) { quantityStr = StringUtils.replace(StringUtils.trimToNull(quantityStr), ",", ""); BigDecimal quantity = quantityStr != null && NumberUtils.isNumber(quantityStr) ? new BigDecimal(quantityStr) : null; idScheme = StringUtils.trimToNull(idScheme); idValue = StringUtils.trimToNull(idValue); if (quantity == null || idScheme == null || idValue == null) { FlexiBean out = createRootData(); if (quantityStr == null) { out.put("err_quantityMissing", true); } if (quantity == null) { out.put("err_quantityNotNumeric", true); } if (idScheme == null) { out.put("err_idschemeMissing", true); } if (idValue == null) { out.put("err_idvalueMissing", true); } String html = getFreemarker().build(HTML_DIR + "positions-add.ftl", out); return Response.ok(html).build(); } ExternalIdBundle id = ExternalIdBundle.of(ExternalId.of(idScheme, idValue)); UniqueId secUid = getSecurityUniqueId(id); if (secUid == null) { FlexiBean out = createRootData(); out.put("err_idvalueNotFound", true); String html = getFreemarker().build(HTML_DIR + "positions-add.ftl", out); return Response.ok(html).build(); } URI uri = addPosition(quantity, secUid); return Response.seeOther(uri).build(); }
From source file:edu.ku.brc.specify.toycode.RegPivot.java
/** * @param newTblName/* ww w.j a va2 s. c o m*/ * @param tblName * @param keyName */ private void process(final String newTblName, final String tblName, final String keyName, final String defSQL, final String fillSQL, final boolean isRegBuild) { String sql = String.format("SELECT DISTINCT Name FROM %s", tblName); String sql2 = "SELECT MAX(LENGTH(Value)) FROM " + tblName + " WHERE Name = '%s'"; int instCnt = 0; Statement stmt = null; try { stmt = connection.createStatement(); BasicSQLUtils.setDBConnection(connection); boolean doBuild = true; if (doBuild) { StringBuilder tblSQL = new StringBuilder(String .format("CREATE TABLE %s (`%s` INT(11) NOT NULL AUTO_INCREMENT, \n", newTblName, keyName)); Vector<String> dbFieldNames = new Vector<String>(); Vector<Integer> dbFieldTypes = new Vector<Integer>(); if (defSQL != null) { ResultSet rs = stmt.executeQuery(defSQL); ResultSetMetaData rsmd = rs.getMetaData(); for (int i = 1; i <= rsmd.getColumnCount(); i++) { if (i > 1) tblSQL.append(",\n "); String name = rsmd.getColumnName(i); dbFieldNames.add(rsmd.getColumnName(i)); dbFieldTypes.add(rsmd.getColumnType(i)); switch (rsmd.getColumnType(i)) { case java.sql.Types.INTEGER: tblSQL.append(String.format("`%s` INT(11) DEFAULT NULL", name)); break; case java.sql.Types.VARCHAR: tblSQL.append(String.format("`%s` VARCHAR(%s) DEFAULT NULL", name, 64)); break; case java.sql.Types.TIMESTAMP: tblSQL.append(String.format("`%s` DATETIME DEFAULT NULL", name)); break; default: System.err.println(String.format("No case for %s %d", name, rsmd.getColumnType(i))); break; } } rs.close(); } int secInx = dbFieldNames.size() + 1; System.out.println("secInx: " + secInx + " " + tblSQL.toString()); HashSet<String> nameSet = new HashSet<String>(); int cnt = 0; for (Object nmObj : BasicSQLUtils.querySingleCol(connection, sql)) { String name = nmObj.toString(); if (name.endsWith("ID")) { continue; } name = StringUtils.replace(name, "(", "_"); name = StringUtils.replace(name, ")", "_"); if (nameSet.contains(name)) continue; nameSet.add(name); tblSQL.append(",\n "); if (name.startsWith("num_") || name.startsWith("Usage_")) { tblSQL.append(String.format("`%s` INT(11) DEFAULT NULL", name)); dbFieldNames.add(name); dbFieldTypes.add(java.sql.Types.INTEGER); } else if (name.endsWith("_number")) { tblSQL.append(String.format("`%s` VARCHAR(16) DEFAULT NULL", name)); dbFieldNames.add(name); dbFieldTypes.add(java.sql.Types.VARCHAR); } else { int maxLen = BasicSQLUtils.getCountAsInt(connection, String.format(sql2, name)); tblSQL.append(String.format("`%s` VARCHAR(%s) DEFAULT NULL", name, maxLen + 1)); dbFieldNames.add(name); dbFieldTypes.add(java.sql.Types.VARCHAR); } cnt++; } if (isRegBuild) { tblSQL.append(String.format(",\n`RecordType`INT(11) DEFAULT NULL")); } tblSQL.append(String.format(",\n PRIMARY KEY (`%s`)) ENGINE=InnoDB DEFAULT CHARSET=UTF8", keyName)); System.out.println(tblSQL.toString()); DBMSUserMgr dbMgr = DBMSUserMgr.getInstance(); dbMgr.setConnection(connection); if (dbMgr.doesDBHaveTable(newTblName)) { BasicSQLUtils.update(connection, "DROP TABLE " + newTblName); } BasicSQLUtils.update(connection, tblSQL.toString()); HashMap<Integer, String> inxToName = new HashMap<Integer, String>(); StringBuilder fields = new StringBuilder(); StringBuilder vals = new StringBuilder(); int inx = 0; for (String nm : dbFieldNames) { if (fields.length() > 0) fields.append(","); fields.append(nm); if (vals.length() > 0) vals.append(","); vals.append('?'); inxToName.put(inx, nm); inx++; } if (isRegBuild) { if (fields.length() > 0) fields.append(","); fields.append("RecordType"); if (vals.length() > 0) vals.append(","); vals.append('?'); } String insertSQL = String.format("INSERT INTO %s (%s) VALUES(%s)", newTblName, fields.toString(), vals.toString()); System.out.println(insertSQL); PreparedStatement pStmt = connection.prepareStatement(insertSQL); if (isRegBuild) { fillRegisterTable(newTblName, stmt, pStmt, fillSQL, secInx, dbFieldTypes, dbFieldNames, inxToName); } else { fillTrackTable(newTblName, stmt, pStmt, fillSQL, secInx, dbFieldTypes, dbFieldNames, inxToName); } System.out.println("InstCnt: " + instCnt); pStmt.close(); } boolean doIP = false; if (doIP) { HTTPGetter httpGetter = new HTTPGetter(); sql = "SELECT RegID, IP from reg"; PreparedStatement pStmt = connection.prepareStatement(String .format("UPDATE %s SET lookup=?, Country=?, City=? WHERE %s = ?", newTblName, keyName)); HashMap<String, String> ipHash = new HashMap<String, String>(); HashMap<String, Pair<String, String>> ccHash = new HashMap<String, Pair<String, String>>(); ResultSet rs = stmt.executeQuery(sql); while (rs.next()) { int regId = rs.getInt(1); String ip = rs.getString(2); String hostName = ipHash.get(ip); String country = null; String city = null; if (hostName == null) { String rvStr = new String( httpGetter.doHTTPRequest("http://api.hostip.info/get_html.php?ip=" + ip)); country = parse(rvStr, "Country:"); city = parse(rvStr, "City:"); System.out.println(rvStr + "[" + country + "][" + city + "]"); try { InetAddress addr = InetAddress.getByName(ip); hostName = addr.getHostName(); ipHash.put(ip, hostName); ccHash.put(ip, new Pair<String, String>(country, city)); } catch (UnknownHostException e) { e.printStackTrace(); } } else { Pair<String, String> p = ccHash.get(ip); if (p != null) { country = p.first; city = p.second; } } pStmt.setString(1, hostName); pStmt.setString(2, country); pStmt.setString(3, city); pStmt.setInt(4, regId); pStmt.executeUpdate(); } pStmt.close(); } stmt.close(); colDBConn.close(); } catch (Exception ex) { ex.printStackTrace(); } System.out.println("Done."); }
From source file:eionet.cr.web.action.admin.staging.RDFExportActionBean.java
/** * Gets the query configuration dump./*from w w w .j a va2s .c o m*/ * * @return the query configuration dump */ public String getQueryConfigurationDump() { StringBuilder sb = new StringBuilder(""); String queryConf = exportDTO == null ? null : exportDTO.getQueryConf(); if (StringUtils.isNotBlank(queryConf)) { queryConf = StringEscapeUtils.escapeXml(queryConf); String[] lines = queryConf.split("\\n"); if (lines != null) { for (int i = 0; i < lines.length; i++) { String line = lines[i]; if (line.startsWith("[") && line.endsWith("]")) { line = StringUtils.replace(line, "[", "<b>"); line = StringUtils.replace(line, "]", ":</b>"); } int j = 0; String indentation = ""; for (; j < line.length(); j++) { if (line.charAt(j) == ' ') { indentation = indentation + " "; } else { break; } } line = indentation + (j >= line.length() ? "" : line.substring(j)); line = StringUtils.replace(line, "\t", " "); sb.append(line).append("<br/>"); } } } return sb.toString(); }
From source file:hydrograph.ui.engine.ui.converter.impl.FilterUiConverter.java
/** * initialize ui object from external file data. * //from w w w . jav a2 s. c o m * @param filterLogicDataStructure * @param typeTransformOpertaionList */ public void populateUIDataFromExternalData(FilterLogicDataStructure filterLogicDataStructure, List<JAXBElement<?>> typeTransformOpertaionList) { TypeExternalSchema typeExternalSchema = (TypeExternalSchema) filter .getOperationOrExpressionOrIncludeExternalOperation().get(0).getValue(); String filePath = typeExternalSchema.getUri(); filePath = StringUtils.replace(filePath, "../", ""); IWorkspace workspace = ResourcesPlugin.getWorkspace(); IPath relativePath = null; relativePath = workspace.getRoot().getFile(new Path(filePath)).getLocation(); if (StringUtils.equals("includeExternalExpression", ((JAXBElement<?>) typeTransformOpertaionList.get(0)).getName().getLocalPart())) { ExpressionData expressionData = FilterLogicExternalOperationExpressionUtil.INSTANCE.importExpression( new File(relativePath.toString()), null, false, uiComponent.getComponentName()); expressionData.getExternalExpressionData().setExternal(true); expressionData.getExternalExpressionData().setFilePath(filePath); filterLogicDataStructure.setExpressionEditorData(expressionData); } else { OperationClassData operationClassData = FilterLogicExternalOperationExpressionUtil.INSTANCE .importOperation(new File(relativePath.toString()), null, false, uiComponent.getComponentName()); operationClassData.getExternalOperationClassData().setExternal(true); operationClassData.getExternalOperationClassData().setFilePath(filePath); filterLogicDataStructure.setOperationClassData(operationClassData); } }
From source file:com.flexive.war.JsonWriter.java
private void writeStringValue(String value) throws IOException { out.write(singleQuotesForStrings ? "'" + escapeStringValue(StringUtils.replace(value, "'", "\\'")) + "'" : "\"" + escapeStringValue(StringUtils.replace(value, "\"", "\\\"")) + "\""); }
From source file:com.edmunds.common.configuration.dns.ConfigurationUtilImpl.java
private String replaceTokens(String value, Map<String, String> tokenMap) { for (Map.Entry<String, String> entry : tokenMap.entrySet()) { String token = entry.getKey(); String replace = entry.getValue(); if (StringUtils.isBlank(replace)) { replace = ""; }/* w w w. ja v a 2s . c om*/ value = StringUtils.replace(value, token, replace); } return value; }
From source file:com.demo.common.extreme.view.XlsView.java
private void writeToCellFormatted(HSSFCell cell, String value, String styleModifier) { double numeric = NON_NUMERIC; try {/*from w w w.j a v a 2s . c o m*/ numeric = Double.parseDouble(value); } catch (Exception e) { numeric = NON_NUMERIC; } if (value.startsWith("$") || value.endsWith("%") || value.startsWith("($")) { boolean moneyFlag = (value.startsWith("$") || value.startsWith("($")); boolean percentFlag = value.endsWith("%"); value = StringUtils.replace(value, "$", ""); value = StringUtils.replace(value, "%", ""); value = StringUtils.replace(value, ",", ""); value = StringUtils.replace(value, "(", "-"); value = StringUtils.replace(value, ")", ""); try { numeric = Double.parseDouble(value); } catch (Exception e) { numeric = NON_NUMERIC; } cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC); if (moneyFlag) { // format money cell.setCellStyle((HSSFCellStyle) styles.get("moneyStyle" + styleModifier)); } else if (percentFlag) { // format percent numeric = numeric / 100; cell.setCellStyle((HSSFCellStyle) styles.get("percentStyle" + styleModifier)); } } else if (numeric != NON_NUMERIC) { // format numeric cell.setCellStyle((HSSFCellStyle) styles.get("numericStyle" + styleModifier)); } else { // format text if (value.trim().equals(NBSP)) { value = ""; } cell.setCellStyle((HSSFCellStyle) styles.get("textStyle" + styleModifier)); } fixWidthAndPopulate(cell, numeric, value); }
From source file:gov.nih.nci.caIMAGE.util.SafeHTMLUtil.java
public static String cleanGeneName(String s) { String clean = Translate.decode(s).replace("[", "").replace("]", ""); clean = StringUtils.replace(clean, "script", ""); clean = StringUtils.replace(clean, "%", ""); clean = StringUtils.replace(clean, "#", ""); clean = StringUtils.replace(clean, ";", ""); clean = StringUtils.replace(clean, "'", ""); clean = StringUtils.replace(clean, "\"", ""); clean = StringUtils.replace(clean, "$", ""); clean = StringUtils.replace(clean, "&", ""); clean = StringUtils.replace(clean, "\\", ""); if (clean.length() == 0) { clean = "empty"; }//from www . ja v a2 s . c o m return clean; }
From source file:de.awtools.basic.AWTools.java
/** * Ersetzt die ${...} Platzhalter in einem String. Die Ersetzung werden * in einer Map gelagert. Die Schluessel repraesentieren die Platzhalter * im String. Die Ersetzungen sind die Werte der Schluessel in der * <code>placeholders</code> Map. * * @param string Der zu pruefende String. * @param placeholders Die Ersetzungen./*from w w w . ja v a 2 s . c o m*/ * @return Der ueberarbeitete String. */ public static String replacePlaceholder(final String string, final Map<String, String> placeholders) { String result = string; String[] keys = StringUtils.substringsBetween(string, "${", "}"); if (keys != null) { for (String key : keys) { String value = placeholders.get(key); if (value != null) { String sb = "${" + key + "}"; result = StringUtils.replace(result, sb.toString(), value); } } } return result; }