Example usage for java.util.regex Matcher quoteReplacement

List of usage examples for java.util.regex Matcher quoteReplacement

Introduction

In this page you can find the example usage for java.util.regex Matcher quoteReplacement.

Prototype

public static String quoteReplacement(String s) 

Source Link

Document

Returns a literal replacement String for the specified String .

Usage

From source file:com.google.code.siren4j.util.ReflectionUtils.java

/**
 * Replaces field tokens with the actual value in the fields. The field types must be simple property types
 *
 * @param str/*w  w w  . j av  a 2 s .com*/
 * @param fields info
 * @param parentMode
 * @param obj
 * @return
 * @throws Siren4JException
 */
public static String replaceFieldTokens(Object obj, String str, List<ReflectedInfo> fields, boolean parentMode)
        throws Siren4JException {
    Map<String, Field> index = new HashMap<String, Field>();
    if (StringUtils.isBlank(str)) {
        return str;
    }
    if (fields != null) {
        for (ReflectedInfo info : fields) {
            Field f = info.getField();
            if (f != null) {
                index.put(f.getName(), f);
            }
        }
    }
    try {
        for (String key : ReflectionUtils.getTokenKeys(str)) {
            if ((!parentMode && !key.startsWith("parent.")) || (parentMode && key.startsWith("parent."))) {
                String fieldname = key.startsWith("parent.") ? key.substring(7) : key;
                if (index.containsKey(fieldname)) {
                    Field f = index.get(fieldname);
                    if (f.getType().isEnum() || ArrayUtils.contains(propertyTypes, f.getType())) {
                        String replacement = "";
                        Object theObject = f.get(obj);
                        if (f.getType().isEnum()) {
                            replacement = theObject == null ? "" : ((Enum) theObject).name();
                        } else {
                            replacement = theObject == null ? "" : theObject.toString();
                        }
                        str = str.replaceAll("\\{" + key + "\\}", Matcher.quoteReplacement("" + replacement));
                    }
                }
            }
        }
    } catch (Exception e) {
        throw new Siren4JException(e);
    }
    return str;

}

From source file:me.doshou.admin.maintain.staticresource.web.controller.StaticResourceVersionController.java

private StaticResource switchStaticResourceContent(String rootRealPath, String versionedResourceRealPath,
        String fileName, String content, boolean isMin) throws IOException {

    StaticResource resource = extractResource(fileName, content);
    String filePath = resource.getUrl();
    filePath = filePath.replace("${ctx}", rootRealPath);

    if (isMin) {//from   w  w w.  j a v a 2  s.  com
        File file = new File(YuiCompressorUtils.getCompressFileName(filePath));
        if (!file.exists()) {
            throw new RuntimeException("" + resource.getUrl());
        }
    } else {
        File file = new File(YuiCompressorUtils.getNoneCompressFileName(filePath));
        if (!file.exists()) {
            throw new RuntimeException("?" + resource.getUrl());
        }
    }

    content = StringEscapeUtils.unescapeXml(content);

    File file = new File(versionedResourceRealPath + fileName);

    List<String> contents = FileUtils.readLines(file);

    for (int i = 0, l = contents.size(); i < l; i++) {
        String fileContent = contents.get(i);
        if (content.equals(fileContent)) {
            Matcher matcher = scriptPattern.matcher(content);
            if (!matcher.matches()) {
                matcher = linkPattern.matcher(content);
            }
            String newUrl = isMin ? YuiCompressorUtils.getCompressFileName(resource.getUrl())
                    : YuiCompressorUtils.getNoneCompressFileName(resource.getUrl());

            content = matcher.replaceAll("$1" + Matcher.quoteReplacement(newUrl) + "$3$4$5");
            contents.set(i, content);

            resource.setContent(content);
            resource.setUrl(newUrl);

            break;
        }
    }
    FileUtils.writeLines(file, contents);

    return resource;
}

From source file:com.arrow.acn.client.api.DeviceApi.java

/**
 * Sends GET request to obtain logs related to specific device and
 * corresponding {@code criteria}//from   w w  w  .j  a va 2 s.c o m
 *
 * @param hid
 *            {@link String} representing device {@code hid}
 * @param criteria
 *            {@link LogsSearchCriteria} representing search filter
 *            parameters.
 *
 * @return subset of {@link AuditLogModel} containing event parameters.
 *         <b>Note:</b> resulting subset may contain not all logs
 *         corresponding to search parameters because it cannot exceed page
 *         size passed in {@code criteria}
 *
 * @throws AcnClientException
 *             if request failed
 */
public PagingResultModel<AuditLogModel> listDeviceAuditLogs(String hid, LogsSearchCriteria criteria) {
    String method = "listDeviceAuditLogs";
    try {
        URI uri = buildUri(PATTERN.matcher(SPECIFIC_LOGS_URL).replaceAll(Matcher.quoteReplacement(hid)),
                criteria);
        PagingResultModel<AuditLogModel> result = execute(new HttpGet(uri), criteria,
                getAuditLogModelTypeRef());
        log(method, result);
        return result;
    } catch (Throwable e) {
        logError(method, e);
        throw new AcnClientException(method, e);
    }
}

From source file:org.springframework.integration.ftp.outbound.FtpServerOutboundTests.java

@Test
@SuppressWarnings("unchecked")
public void testInt3172LocalDirectoryExpressionMGETRecursive() throws IOException {
    String dir = "ftpSource/";
    long modified = setModifiedOnSource1();
    File secondRemote = new File(getSourceRemoteDirectory(), "ftpSource2.txt");
    secondRemote.setLastModified(System.currentTimeMillis() - 1_000_000);
    this.inboundMGetRecursive.send(new GenericMessage<Object>("*"));
    Message<?> result = this.output.receive(1000);
    assertNotNull(result);/*  w ww .  j av a2  s.c o m*/
    List<File> localFiles = (List<File>) result.getPayload();
    assertEquals(3, localFiles.size());

    boolean assertedModified = false;
    for (File file : localFiles) {
        assertThat(file.getPath().replaceAll(Matcher.quoteReplacement(File.separator), "/"),
                containsString(dir));
        if (file.getPath().contains("localTarget1")) {
            assertedModified = assertPreserved(modified, file);
        }
    }
    assertTrue(assertedModified);
    assertThat(localFiles.get(2).getPath().replaceAll(Matcher.quoteReplacement(File.separator), "/"),
            containsString(dir + "subFtpSource"));

    File secondTarget = new File(getTargetLocalDirectory() + File.separator + "ftpSource", "localTarget2.txt");
    ByteArrayOutputStream remoteContents = new ByteArrayOutputStream();
    ByteArrayOutputStream localContents = new ByteArrayOutputStream();
    FileUtils.copyFile(secondRemote, remoteContents);
    FileUtils.copyFile(secondTarget, localContents);
    String localAsString = new String(localContents.toByteArray());
    assertEquals(new String(remoteContents.toByteArray()), localAsString);
    long oldLastModified = secondRemote.lastModified();
    FileUtils.copyInputStreamToFile(new ByteArrayInputStream("junk".getBytes()), secondRemote);
    long newLastModified = secondRemote.lastModified();
    secondRemote.setLastModified(oldLastModified);
    this.inboundMGetRecursive.send(new GenericMessage<Object>("*"));
    this.output.receive(0);
    localContents = new ByteArrayOutputStream();
    FileUtils.copyFile(secondTarget, localContents);
    assertEquals(localAsString, new String(localContents.toByteArray()));
    secondRemote.setLastModified(newLastModified);
    this.inboundMGetRecursive.send(new GenericMessage<Object>("*"));
    this.output.receive(0);
    localContents = new ByteArrayOutputStream();
    FileUtils.copyFile(secondTarget, localContents);
    assertEquals("junk", new String(localContents.toByteArray()));
    // restore the remote file contents
    FileUtils.copyInputStreamToFile(new ByteArrayInputStream(localAsString.getBytes()), secondRemote);
}

From source file:org.entando.edo.builder.TestBuilderNoPlugin.java

@Test
public void test_Controller_Tiles() throws IOException {
    String commonPath = "src/main/webapp/WEB-INF/sandbox/apsadmin".replaceAll("/",
            Matcher.quoteReplacement(File.separator));

    String actualPath = ACTUAL_BASE_FOLDER + commonPath;

    File actualDir = new File(actualPath);
    Assert.assertTrue(actualDir.exists());

    FileFilter excludeSubfolders = new FileFilter() {
        @Override/*  w  w  w . j av a 2  s .c  om*/
        public boolean accept(File pathname) {
            if (pathname.isDirectory() && !pathname.getName().equals("apsadmin"))
                return false;
            return true;
        }
    };

    List<File> actualFiles = this.searchFiles(actualDir, excludeSubfolders);
    Assert.assertEquals(1, actualFiles.size());
    this.compareFiles(actualFiles);
}

From source file:org.apache.sqoop.connector.idf.CSVIntermediateDataFormat.java

private String getRegExp(String orig) {
    return orig.replaceAll("\\\\", Matcher.quoteReplacement("\\\\"));
}

From source file:com.adobe.ags.curly.controller.ActionRunner.java

private String tokenizeParameters(String str) {
    Set<String> variableTokens = ActionUtils.getVariableNames(action);
    int tokenCounter = 0;
    for (String var : variableTokens) {
        String varPattern = Pattern.quote("${") + var + "(\\|.*?)?" + Pattern.quote("}");
        str = str.replaceAll(varPattern, Matcher.quoteReplacement("${" + (tokenCounter++) + "}"));
    }//from w ww.j  a  v a2  s. c o  m
    return str;
}

From source file:org.entando.edo.builder.TestBuilder.java

@Test
public void test_Controller_Tiles() throws IOException {
    String commonPath = "src/main/webapp/WEB-INF/plugins/jppet/apsadmin".replaceAll("/",
            Matcher.quoteReplacement(File.separator));

    String actualPath = ACTUAL_BASE_FOLDER + commonPath;

    File actualDir = new File(actualPath);
    Assert.assertTrue(actualDir.exists());

    FileFilter excludeSubfolders = new FileFilter() {
        @Override// w w  w. j  av  a 2 s .  co  m
        public boolean accept(File pathname) {
            if (pathname.isDirectory() && !pathname.getName().equals("apsadmin"))
                return false;
            return true;
        }
    };

    List<File> actualFiles = this.searchFiles(actualDir, excludeSubfolders);
    Assert.assertEquals(1, actualFiles.size());
    this.compareFiles(actualFiles);
}

From source file:com.qmetry.qaf.automation.step.StringTestStep.java

private void absractArgsAandSetDesciption() {
    String prefix = BDDKeyword.getKeywordFrom(name);
    if (actualArgs == null || actualArgs.length == 0) {
        final String REGEX = ParamType.getParamValueRegx();
        List<String> arguments = new ArrayList<String>();

        description = removePrefix(prefix, name);

        Pattern p = Pattern.compile(REGEX);
        Matcher m = p.matcher(description); // get a matcher object
        int count = 0;

        while (m.find()) {
            String arg = description.substring(m.start(), m.end());
            arguments.add(arg);/*from  w w  w  .j av  a 2s  .  c o  m*/
            count++;
        }
        for (int i = 0; i < count; i++) {
            description = description.replaceFirst(Pattern.quote(arguments.get(i)),
                    Matcher.quoteReplacement("{" + i + "}"));
        }
        actualArgs = arguments.toArray(new String[] {});
    }
    name = StringUtil.toCamelCaseIdentifier(description.length() > 0 ? description : name);
}

From source file:io.github.swagger2markup.markup.builder.internal.confluenceMarkup.ConfluenceMarkupBuilder.java

private String escapeCellPipes(String cell) {
    Matcher m = ESCAPE_CELL_PIPE_PATTERN.matcher(cell);

    StringBuffer res = new StringBuffer();
    while (m.find()) {
        String repl = m.group(1);
        if (repl.equals(ConfluenceMarkup.TABLE_COLUMN_DELIMITER.toString()))
            repl = "\\" + ConfluenceMarkup.TABLE_COLUMN_DELIMITER.toString();
        m.appendReplacement(res, Matcher.quoteReplacement(repl));
    }//from  ww w.j a va 2  s .  c  om
    m.appendTail(res);

    return res.toString();
}