List of usage examples for org.apache.commons.lang StringUtils replaceEach
public static String replaceEach(String text, String[] searchList, String[] replacementList)
Replaces all occurrences of Strings within another String.
From source file:net.unit8.maven.plugins.handlebars.HandlebarsEngine.java
public void precompile(Collection<File> templates, File outputFile, boolean purgeWhitespace) throws IOException { Context cx = Context.enter(); PrintWriter out = null;//from w w w . j ava 2 s. co m LOG.info("precompile " + templates + " to " + outputFile); try { out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(outputFile), encoding)); out.print("(function() {\n var template = Handlebars.template, " + "templates = Handlebars.templates = Handlebars.templates || {};\n"); // Rhino for Handlebars Template ScriptableObject global = cx.initStandardObjects(); InputStreamReader in = new InputStreamReader(handlebarsUrl.openStream()); cx.evaluateReader(global, in, handlebarsName, 1, null); IOUtils.closeQuietly(in); Scriptable options = new Options(new KnownHelpers(knownHelpers), knownHelpersOnly); ScriptableObject.putProperty(global, "options", options); for (File template : templates) { String data = FileUtils.readFileToString(template, encoding); if (purgeWhitespace) data = StringUtils.replaceEach(data, new String[] { "\n", "\r", "\t" }, new String[] { "", "", "" }); ScriptableObject.putProperty(global, "data", data); Object obj = cx.evaluateString(global, "Handlebars.precompile(String(data), options);", "<cmd>", 1, null); out.println("templates['" + FilenameUtils.getBaseName(template.getName()) + "']=template(" + obj.toString() + ");"); } } finally { Context.exit(); if (out != null) out.println("})();"); IOUtils.closeQuietly(out); } }
From source file:net.yacy.kelondro.util.FileUtils.java
public static void saveMap(final File file, final Map<String, String> props, final String comment) { boolean err = false; PrintWriter pw = null;/*from w ww. jav a 2 s .co m*/ final File tf = new File(file.toString() + "." + (System.currentTimeMillis() % 1000)); try { pw = new PrintWriter(tf, StandardCharsets.UTF_8.name()); pw.println("# " + comment); String key, value; for (final Map.Entry<String, String> entry : props.entrySet()) { key = entry.getKey(); if (key != null) { key = StringUtils.replaceEach(key, unescaped_strings_in, escaped_strings_out); } if (entry.getValue() == null) { value = ""; } else { value = entry.getValue(); value = StringUtils.replaceEach(value, unescaped_strings_in, escaped_strings_out); } pw.println(key + "=" + value); } pw.println("# EOF"); } catch (final FileNotFoundException e) { ConcurrentLog.warn("FileUtils", e.getMessage(), e); err = true; } catch (final UnsupportedEncodingException e) { ConcurrentLog.warn("FileUtils", e.getMessage(), e); err = true; } finally { if (pw != null) { pw.close(); } pw = null; } if (!err) try { forceMove(tf, file); } catch (final IOException e) { // ignore } }
From source file:net.yacy.kelondro.util.FileUtils.java
public static ConcurrentHashMap<String, String> table(final Iterator<String> li) { String line;/*from w w w. j a v a2 s . c o m*/ final ConcurrentHashMap<String, String> props = new ConcurrentHashMap<String, String>(); while (li.hasNext()) { int pos = 0; line = li.next().trim(); if (!line.isEmpty() && line.charAt(0) == '#') { continue; // exclude comments } do { // search for unescaped = pos = line.indexOf('=', pos + 1); } while (pos > 0 && line.charAt(pos - 1) == '\\'); if (pos > 0) try { String key = StringUtils.replaceEach(line.substring(0, pos).trim(), escaped_strings_in, unescaped_strings_out); String value = StringUtils.replaceEach(line.substring(pos + 1).trim(), escaped_strings_in, unescaped_strings_out); //System.out.println("key = " + key + ", value = " + value); props.put(key, value); } catch (final IndexOutOfBoundsException e) { ConcurrentLog.logException(e); } } return props; }
From source file:nl.knaw.huygens.timbuctoo.tools.util.EncodingFixer.java
public String convert1(String text) { return StringUtils.replaceEach(text, SOURCE, TARGET); }
From source file:org.apache.ofbiz.solr.webapp.OFBizSolrLoadAdminUiServlet.java
@Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { boolean isForwarded = forwardUrl(request, response); if (isForwarded) { return;/*from w w w . j a va 2 s .co m*/ } // This attribute is set by the SolrDispatchFilter CoreContainer cores = (CoreContainer) request.getAttribute("org.apache.solr.CoreContainer"); InputStream in = getServletContext().getResourceAsStream("/admin.html"); if (in != null && cores != null) { try { response.setCharacterEncoding("UTF-8"); response.setContentType("text/html"); Writer out = new OutputStreamWriter(response.getOutputStream(), StandardCharsets.UTF_8); String html = IOUtils.toString(in, "UTF-8"); Package pack = SolrCore.class.getPackage(); String[] search = new String[] { "${contextPath}", "${adminPath}", "${version}" }; String[] replace = new String[] { StringEscapeUtils.escapeJavaScript(request.getContextPath()), StringEscapeUtils.escapeJavaScript(CommonParams.CORES_HANDLER_PATH), StringEscapeUtils.escapeJavaScript(pack.getSpecificationVersion()) }; out.write(StringUtils.replaceEach(html, search, replace)); out.flush(); } finally { IOUtils.closeQuietly(in); } } else { response.sendError(404); } }
From source file:org.apache.phoenix.hive.query.PhoenixQueryBuilder.java
private List<String> buildWhereClause(JobConf jobConf, StringBuilder sql, String whereClause, Map<String, TypeInfo> columnTypeMap) throws IOException { if (whereClause == null || whereClause.isEmpty()) { return Collections.emptyList(); }/*w ww. j a v a 2 s.co m*/ List<String> conditionColumnList = Lists.newArrayList(); sql.append(" where "); whereClause = StringUtils.replaceEach(whereClause, new String[] { "UDFToString" }, new String[] { "to_char" }); for (String columnName : columnTypeMap.keySet()) { if (whereClause.contains(columnName)) { String column = findReplacement(jobConf, columnName); whereClause = StringUtils.replaceEach(whereClause, new String[] { columnName }, new String[] { "\"" + column + "\"" }); conditionColumnList.add(column); if (PhoenixStorageHandlerConstants.DATE_TYPE.equals(columnTypeMap.get(columnName).getTypeName())) { whereClause = applyDateFunctionUsingRegex(whereClause, columnName); } else if (PhoenixStorageHandlerConstants.TIMESTAMP_TYPE .equals(columnTypeMap.get(columnName).getTypeName())) { whereClause = applyTimestampFunctionUsingRegex(whereClause, columnName); } } } sql.append(whereClause); return conditionColumnList; }
From source file:org.apache.phoenix.hive.query.PhoenixQueryBuilder.java
private String applyFunctionForCommonOperator(String whereClause, String columnName, boolean isDate) { String targetPattern = isDate ? PhoenixStorageHandlerConstants.DATE_PATTERN : PhoenixStorageHandlerConstants.TIMESTAMP_PATTERN; String pattern = StringUtils .replaceEach(PhoenixStorageHandlerConstants.COMMON_OPERATOR_PATTERN, new String[] { PhoenixStorageHandlerConstants.COLUMNE_MARKER, PhoenixStorageHandlerConstants.PATERN_MARKER }, new String[] { columnName, targetPattern }); Matcher matcher = Pattern.compile(pattern).matcher(whereClause); while (matcher.find()) { String token = matcher.group(1); String datePart = matcher.group(3); String convertString = token .replace(datePart,/*from www . j a v a 2s . c o m*/ applyFunction( isDate ? PhoenixStorageHandlerConstants.DATE_FUNCTION_TEMPLETE : PhoenixStorageHandlerConstants.TIMESTAMP_FUNCTION_TEMPLATE, datePart)); whereClause = whereClause.replaceAll( StringUtils.replaceEach(token, new String[] { "(", ")" }, new String[] { "\\(", "\\)" }), convertString); } return whereClause; }
From source file:org.apache.phoenix.hive.query.PhoenixQueryBuilder.java
private String applyFunctionForBetweenOperator(String whereClause, String columnName, boolean isDate) { String targetPattern = isDate ? PhoenixStorageHandlerConstants.DATE_PATTERN : PhoenixStorageHandlerConstants.TIMESTAMP_PATTERN; String pattern = StringUtils .replaceEach(PhoenixStorageHandlerConstants.BETWEEN_OPERATOR_PATTERN, new String[] { PhoenixStorageHandlerConstants.COLUMNE_MARKER, PhoenixStorageHandlerConstants.PATERN_MARKER }, new String[] { columnName, targetPattern }); Matcher matcher = Pattern.compile(pattern).matcher(whereClause); while (matcher.find()) { String token = matcher.group(1); boolean isNot = matcher.group(2) == null ? false : true; String fromDate = matcher.group(3); String toDate = matcher.group(4); String convertString = StringUtils .replaceEach(token, new String[] { fromDate, toDate }, new String[] { applyFunction( isDate ? PhoenixStorageHandlerConstants.DATE_FUNCTION_TEMPLETE : PhoenixStorageHandlerConstants.TIMESTAMP_FUNCTION_TEMPLATE, fromDate), applyFunction( isDate ? PhoenixStorageHandlerConstants.DATE_FUNCTION_TEMPLETE : PhoenixStorageHandlerConstants.TIMESTAMP_FUNCTION_TEMPLATE, toDate) }); whereClause = whereClause.replaceAll(pattern, convertString); }//w w w . j ava2s .c om return whereClause; }
From source file:org.apache.phoenix.hive.query.PhoenixQueryBuilder.java
private String applyFunctionForInOperator(String whereClause, String columnName, boolean isDate) { String targetPattern = isDate ? PhoenixStorageHandlerConstants.DATE_PATTERN : PhoenixStorageHandlerConstants.TIMESTAMP_PATTERN; String pattern = StringUtils .replaceEach(PhoenixStorageHandlerConstants.IN_OPERATOR_PATTERN, new String[] { PhoenixStorageHandlerConstants.COLUMNE_MARKER, PhoenixStorageHandlerConstants.PATERN_MARKER }, new String[] { columnName, targetPattern }); String itemPattern = "(" + targetPattern + ")"; Matcher matcher = Pattern.compile(pattern).matcher(whereClause); while (matcher.find()) { String token = matcher.group(1); Matcher itemMatcher = Pattern.compile(itemPattern).matcher(token); while (itemMatcher.find()) { String item = itemMatcher.group(1); token = token//w ww. j av a 2 s . c o m .replace(item, applyFunction( isDate ? PhoenixStorageHandlerConstants.DATE_FUNCTION_TEMPLETE : PhoenixStorageHandlerConstants.TIMESTAMP_FUNCTION_TEMPLATE, item)); } whereClause = whereClause.replaceAll(pattern, token); } return whereClause; }
From source file:org.apache.solr.servlet.LoadAdminUiServlet.java
@Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { response.addHeader("X-Frame-Options", "DENY"); // security: SOLR-7966 - avoid clickjacking for admin interface // This attribute is set by the SolrDispatchFilter String admin = request.getRequestURI().substring(request.getContextPath().length()); CoreContainer cores = (CoreContainer) request.getAttribute("org.apache.solr.CoreContainer"); InputStream in = getServletContext().getResourceAsStream(admin); Writer out = null;//from w w w. ja va 2 s.c o m if (in != null && cores != null) { try { response.setCharacterEncoding("UTF-8"); response.setContentType("text/html"); // Protect container owned streams from being closed by us, see SOLR-8933 out = new OutputStreamWriter(new CloseShieldOutputStream(response.getOutputStream()), StandardCharsets.UTF_8); String html = IOUtils.toString(in, "UTF-8"); Package pack = SolrCore.class.getPackage(); String[] search = new String[] { "${contextPath}", "${adminPath}", "${version}" }; String[] replace = new String[] { StringEscapeUtils.escapeJavaScript(request.getContextPath()), StringEscapeUtils.escapeJavaScript(CommonParams.CORES_HANDLER_PATH), StringEscapeUtils.escapeJavaScript(pack.getSpecificationVersion()) }; out.write(StringUtils.replaceEach(html, search, replace)); } finally { IOUtils.closeQuietly(in); IOUtils.closeQuietly(out); } } else { response.sendError(404); } }