List of usage examples for java.util.regex Matcher quoteReplacement
public static String quoteReplacement(String s)
From source file:org.apache.maven.plugin.cxx.CMakeMojo.java
protected void updateOrCreateCMakeDependenciesFile(List aiDependenciesLib, boolean bMavenDependencies) { String dependencieFile = (bMavenDependencies ? cmakeMavenDependenciesFile : cmakeDependenciesFile); String fullDependenciesFile = dependencieFile; File file = new File(dependencieFile); if (!file.isAbsolute()) { // $FB always use unix path separator with cmake even under windows ! fullDependenciesFile = getProjectDir() + "/" + dependencieFile; }/*from w w w. j av a 2s. co m*/ file = new File(fullDependenciesFile); if (!file.exists()) { try { file.createNewFile(); } catch (IOException e) { getLog().error(dependencieFile + " script can't be created at " + file.getAbsolutePath()); return; } } // check file content InputStream dependenciesStream = null; String content = new String(); try { dependenciesStream = new FileInputStream(file); content = IOUtils.toString(dependenciesStream, "UTF8"); } catch (IOException e) { // shall not happen since file has been created getLog().error(dependencieFile + " script can't be opened at " + file.getAbsolutePath()); } finally { getLog().debug("close input stream at reading"); IOUtils.closeQuietly(dependenciesStream); } String beginDepsPattern = (bMavenDependencies ? (isDebugBuild() ? "# BEGIN MAVEN_DEBUG_DEPENDENCIES" : "# BEGIN MAVEN_OPTIMIZED_DEPENDENCIES") : "# BEGIN CMAKE_DEPENDENCIES"); String endDepsPattern = (bMavenDependencies ? (isDebugBuild() ? "# END MAVEN_DEBUG_DEPENDENCIES" : "# END MAVEN_OPTIMIZED_DEPENDENCIES") : "# END CMAKE_DEPENDENCIES"); String beginIncPattern = "# BEGIN MAVEN_INCLUDE_ROOTS"; String endIncPattern = "# END MAVEN_INCLUDE_ROOTS"; // reset file content if needed if (StringUtils.isEmpty(content) || content.indexOf(beginDepsPattern) == -1) { getLog().info(file.getAbsolutePath() + " content full update"); try { dependenciesStream = getClass() .getResourceAsStream((bMavenDependencies ? "/cmake-cpp-project/CMakeMavenDependencies.txt" : "/cmake-cpp-project/CMakeDependencies.txt")); content = IOUtils.toString(dependenciesStream, "UTF8"); } catch (IOException e) { getLog().error(dependencieFile + " default content not found "); } finally { getLog().debug("close input stream at full update"); IOUtils.closeQuietly(dependenciesStream); } } // update file content String simpleIndentation = "\n "; String doubleIndentation = "\n "; Iterator itDeps = aiDependenciesLib.iterator(); StringBuilder allDepsBuilder = new StringBuilder( (bMavenDependencies ? doubleIndentation : simpleIndentation)); while (itDeps.hasNext()) { String dep = (String) itDeps.next(); if (bMavenDependencies) { String externalDep = generalizeDependencyFileName(dep, true); allDepsBuilder.append("target_link_libraries(${target} " + (isDebugBuild() ? "debug " : "optimized ") + externalDep + ")" + doubleIndentation); } else { String cmakeDep = generalizeDependencyFileName(dep, false); allDepsBuilder.append("# If a \"" + cmakeDep + "\" target has been define, this means we are building " + "an amalgamed cmake project" + simpleIndentation + "# but maven dependencies can be used too" + simpleIndentation + "if(TARGET " + cmakeDep + ")" + doubleIndentation + "message(\"Adding direct " + cmakeDep + " cmake dependencies to target '${target}'\")" + doubleIndentation + "target_link_libraries(${target} " + cmakeDep + ")" + simpleIndentation + "endif()" + simpleIndentation); } } // adding additionalIncludeRoots in cmake maven dependencies file StringBuilder addIncsBuilder = new StringBuilder(doubleIndentation); if (bMavenDependencies && null != additionalIncludeRoots) { addIncsBuilder.append("include_directories( " + doubleIndentation); for (String includeRoot : additionalIncludeRoots) { addIncsBuilder.append("\"" + includeRoot + "\"" + doubleIndentation); } addIncsBuilder.append(")" + doubleIndentation); for (String includeRoot : additionalIncludeRoots) { addIncsBuilder.append( "message(\"Adding '" + includeRoot + "' additional include root.\")" + doubleIndentation); } } getLog().debug(dependencieFile + " depfile was : " + content); String allDeps = Matcher.quoteReplacement(allDepsBuilder.toString()); //.replace( "$", "\\$" ); // Matcher replaceAll() is a bit rigid ! getLog().debug(dependencieFile + " injected dependency will be : " + allDeps); // regexp multi-line replace, see http://stackoverflow.com/questions/4154239/java-regex-replaceall-multiline Pattern p1 = Pattern.compile(beginDepsPattern + ".*" + endDepsPattern, Pattern.DOTALL); Matcher m1 = p1.matcher(content); content = m1.replaceAll(beginDepsPattern + allDeps + endDepsPattern); if (bMavenDependencies && null != additionalIncludeRoots) { String addIncs = Matcher.quoteReplacement(addIncsBuilder.toString()); //.replace( "$", "\\$" ); // Matcher replaceAll() is a bit rigid ! getLog().debug(dependencieFile + " injected includes Roots will be : " + addIncs); Pattern p2 = Pattern.compile(beginIncPattern + ".*" + endIncPattern, Pattern.DOTALL); Matcher m2 = p2.matcher(content); content = m2.replaceAll(beginIncPattern + addIncs + endIncPattern); } getLog().debug(dependencieFile + " depfile now is : " + content); OutputStream outStream = null; try { outStream = new FileOutputStream(file); IOUtils.write(content, outStream, "UTF8"); } catch (IOException e) { getLog().error( dependencieFile + " script can't be written at " + file.getAbsolutePath() + e.toString()); } finally { getLog().debug("close output stream at update"); IOUtils.closeQuietly(outStream); } }
From source file:pt.webdetails.cpf.persistence.PersistenceEngine.java
public void startOrient() throws Exception { InputStream conf = new PersistenceEngineSettingsReader().getConfigurationInputStream(); /* Acquiring a database connection will throw an * exception if the db isn't up. We take advantage * of that here, to decide whether we need to start * up the server/*from w ww . j av a 2 s .co m*/ */ ODatabaseDocumentTx tx = null; try { tx = getConnection(); } catch (Exception e) { final String enc = CharsetHelper.getEncoding(); //Change the default database location to orientPath String confAsString = IOUtils.toString(conf, enc); confAsString = confAsString.replaceAll(Matcher.quoteReplacement("$PATH$"), getOrientPath() + "/"); conf.close(); conf = new ByteArrayInputStream(confAsString.getBytes(enc)); server = OServerMain.create(); server.startup(conf); server.activate(); } finally { if (tx != null) { tx.close(); } conf.close(); } }
From source file:gtu._work.ui.RegexCatchReplacer.java
/** * @param fromPattern//from www .j av a 2 s .com * ???pattern * @param toFormat * ??pattern * @param replaceText * ?? */ void replacer(String fromPattern, String toFormat, String replaceText) { try { Pattern pattern = Pattern.compile(fromPattern); Matcher matcher = pattern.matcher(replaceText); Map<String, Integer> tmap = new LinkedHashMap<String, Integer>(); String tempStr = null; for (; matcher.find();) { tempStr = toFormat.toString(); for (int ii = 0; ii <= matcher.groupCount(); ii++) { System.out.println(ii + " -- " + matcher.group(ii)); tempStr = tempStr.replaceAll("#" + ii + "#", Matcher.quoteReplacement(matcher.group(ii))); if (!tmap.containsKey(tempStr)) { tmap.put(tempStr, 0); } tmap.put(tempStr, tmap.get(tempStr) + 1); } } DefaultTableModel model = JTableUtil.createModel(true, "match", "count"); for (String str : tmap.keySet()) { model.addRow(new Object[] { str, tmap.get(str) }); } setTitle("total : " + model.getRowCount()); resultArea.setModel(model); } catch (Exception ex) { JOptionPaneUtil.newInstance().iconErrorMessage().showMessageDialog(ex.getMessage(), getTitle()); ex.printStackTrace(); } }
From source file:org.jenkinsci.plugins.neoload.integration.NeoBuildAction.java
private void setupTestInfo(final List<String> commands, Launcher launcher) { commands.add("-launch \"" + scenarioName + "\""); // the $Date{.*} value in testResultName must be escaped if we're on linux so that NeoLoad is passed the $. final String escapedTestResultName; if (isOsWindows(launcher)) { escapedTestResultName = testResultName; } else {//from w ww.j a v a2 s .co m escapedTestResultName = testResultName.replaceAll( Pattern.quote("$Date{") + "(.*?)" + Pattern.quote("}"), Matcher.quoteReplacement("\\$Date{") + "$1" + Matcher.quoteReplacement("}")); } if (StringUtils.trimToNull(escapedTestResultName) != null) { commands.add("-testResultName \"" + escapedTestResultName + "\""); } if (StringUtils.trimToNull(testDescription) != null) { commands.add("-description \"" + testDescription + "\""); } }
From source file:fr.fastconnect.factory.tibco.bw.maven.AbstractBWMojo.java
public String replaceProperties(String string) { if (string == null) return null; Matcher m = mavenPropertyPattern.matcher(string); StringBuffer sb = new StringBuffer(); while (m.find()) { String propertyName = m.group(1); String propertyValue = getPropertyValue(propertyName); if (propertyValue != null) { m.appendReplacement(sb, Matcher.quoteReplacement(propertyValue)); }/* ww w .java 2s . c o m*/ } m.appendTail(sb); string = sb.toString(); return string; }
From source file:com.hexidec.ekit.component.HTMLUtilities.java
public static String ensureInlineContent(String s) { if (StringUtils.isEmpty(s)) { return s; }/*from w w w . j av a 2 s. c o m*/ List<String> inlineTags = ExtendedHTMLDocument.getAcceptedInlineTags(); StringBuffer sb = new StringBuffer(); Pattern p = Pattern.compile("</?(\\w+).*?>", Pattern.DOTALL); Matcher m = p.matcher(s); while (m.find()) { String replacement = ""; if (inlineTags.contains(m.group(1).toLowerCase())) { replacement = Matcher.quoteReplacement(m.group()); } m.appendReplacement(sb, replacement); } m.appendTail(sb); return sb.toString(); }
From source file:io.github.swagger2markup.markup.builder.internal.AbstractMarkupDocBuilder.java
public String replaceNewLines(String content, String replacement) { return content.replaceAll(NEW_LINES, Matcher.quoteReplacement(replacement)); }
From source file:org.sigmah.server.servlet.base.AbstractServlet.java
/** * <p>/*w w w .j a va 2 s . co m*/ * Handles the {@code caught} exception. * </p> * <p> * <ul> * <li>If {@code GET} access (direct access and not ajax call), writes into the {@code response} the HTML error page * content.</li> * <li>Else, writes into the {@code response} the given {@code errorCode} as header and * {@link ServletConstants#ERROR_RESPONSE_CONTENT} as content.</li> * </ul> * </p> * * @param request * The HTTP request. * @param response * The HTTP response. * @param servletMethodName * The {@link ServletMethod} value. * @param popupDestination * Is the servlet process destined to be displayed into a pop-up window? * @param caught * The throwable. * @param errorCode * The error code set on the {@code response}. */ private void handleException(final HttpServletRequest request, final HttpServletResponse response, final String servletMethodName, final boolean popupDestination, final Throwable caught, final int errorCode) { if (LOG.isErrorEnabled()) { LOG.error("Exception while executing '" + getClass().getName() + '#' + servletMethodName + "' servlet method.", caught); } try { response.setContentType(FileType.HTML.getContentType()); final String htmlMessage = caught.getClass().getSimpleName() + " : " + caught.getMessage(); final boolean ajaxCall = ClientUtils.isTrue(request.getParameter(ServletConstants.AJAX)); if (DO_GET_METHOD_NAME.equals(servletMethodName) && !ajaxCall) { // If the servlet method is executed using HTTP {@code GET} method. String html = template; html = html.replaceAll(Pattern.quote("<!-- ${MessageContent} -->"), Matcher.quoteReplacement(htmlMessage)); html = html.replaceAll(Pattern.quote("<!-- ${ButtonDisplay} -->"), Servlets.cssDisplay(popupDestination)); response.setCharacterEncoding(Servlets.UTF8_CHARSET); response.getWriter().write(html); } else { // Other method. response.setStatus(errorCode); response.getWriter().write(ServletConstants.buildErrorResponse(errorCode)); } } catch (final IOException ioe) { // Nothing to do ; 'getWriter()' has just failed. if (LOG.isErrorEnabled()) { LOG.error("'getWriter()' method has raised an exception.", ioe); } } }
From source file:fr.arnaudguyon.xmltojsonlib.XmlToJson.java
private void formatValue(Object value, StringBuilder builder) { if (value instanceof String) { String string = (String) value; // Escape special characters string = string.replaceAll("\\\\", "\\\\\\\\"); // escape backslash string = string.replaceAll("\"", Matcher.quoteReplacement("\\\"")); // escape double quotes string = string.replaceAll("/", "\\\\/"); // escape slash string = string.replaceAll("\n", "\\\\n").replaceAll("\t", "\\\\t"); // escape \n and \t builder.append("\""); builder.append(string);//from w w w. j ava 2 s .com builder.append("\""); } else if (value instanceof Long) { Long longValue = (Long) value; builder.append(longValue); } else if (value instanceof Integer) { Integer intValue = (Integer) value; builder.append(intValue); } else if (value instanceof Boolean) { Boolean bool = (Boolean) value; builder.append(bool); } else if (value instanceof Double) { Double db = (Double) value; builder.append(db); } else { builder.append(value.toString()); } }