Example usage for java.util.regex Matcher replaceAll

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

Introduction

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

Prototype

public String replaceAll(Function<MatchResult, String> replacer) 

Source Link

Document

Replaces every subsequence of the input sequence that matches the pattern with the result of applying the given replacer function to the match result of this matcher corresponding to that subsequence.

Usage

From source file:marytts.tools.voiceimport.HTKLabeler.java

/**
* Get word sequence from a single feature file
* @param basename//from  w  w w  . j a  v  a  2s .c  om
* @return String
* @throws Exception
*/
private String getWordLineFromXML(String basename, boolean spause, boolean vpause) throws Exception {

    String line;
    String phoneSeq;
    Matcher matcher;
    Pattern pattern;
    StringBuilder alignBuff = new StringBuilder();
    //alignBuff.append(basename);
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(new File(getProp(PROMPTALLOPHONESDIR) + "/" + basename + xmlExt));
    XPath xpath = XPathFactory.newInstance().newXPath();
    NodeList tokens = (NodeList) xpath.evaluate("//t | //boundary", doc, XPathConstants.NODESET);

    alignBuff.append(collectWordTranscription(tokens));
    phoneSeq = alignBuff.toString();
    pattern = Pattern.compile("pau ssil ");
    matcher = pattern.matcher(phoneSeq);
    phoneSeq = matcher.replaceAll("sil ");

    pattern = Pattern.compile(" ssil pau$");
    matcher = pattern.matcher(phoneSeq);
    phoneSeq = matcher.replaceAll(" sil");

    if (!vpause) {
        /* TODO: Extra code need to write
         * to maintain minimum number of short sil.
         * or consider word boundaries as ssil.
         */
        /* 
         * virtual silence on word boundaries
         * are matched in sp  
         */
        pattern = Pattern.compile("vssil");
        matcher = pattern.matcher(phoneSeq);
        phoneSeq = matcher.replaceAll("");
    } else {
        /* 
         * virtual silence on word boundaries
         * are matched in sp  
         */
        pattern = Pattern.compile("vssil");
        matcher = pattern.matcher(phoneSeq);
        phoneSeq = matcher.replaceAll("");
    }

    // checking
    if (!spause) {
        pattern = Pattern.compile("ssil");
        matcher = pattern.matcher(phoneSeq);
        phoneSeq = matcher.replaceAll("");
    }

    phoneSeq += " .";

    pattern = Pattern.compile("\\s+");
    matcher = pattern.matcher(phoneSeq);
    phoneSeq = matcher.replaceAll("\n");

    //System.out.println(phoneSeq);
    return phoneSeq;
}

From source file:com.photon.phresco.framework.rest.api.QualityService.java

private List<String> testResultFilesunit(String rootModulePath, String subModuleName, List<String> testAgainsts,
        String showDevice, String action) throws PhrescoException {
    List<String> testResultFiles = new ArrayList<String>();
    String reportDir = "";
    String resultExtension = "";
    try {//from ww  w  . j av a  2 s  .  c o  m
        ProjectInfo projectInfo = Utility.getProjectInfo(rootModulePath, subModuleName);
        File testFolderLocation = Utility.getTestFolderLocation(projectInfo, rootModulePath, subModuleName);
        StringBuilder sb = new StringBuilder(testFolderLocation.getPath());
        if (Constants.PHASE_UNIT_TEST.equals(action)) {
            reportDir = FrameworkServiceUtil.getUnitTestRptDir(rootModulePath, subModuleName);
            resultExtension = "xml";
        } /*else {
           reportDir = FrameworkServiceUtil.getLoadTestReportDir(rootModulePath, subModuleName);
           resultExtension = FrameworkServiceUtil.getLoadResultFileExtension(rootModulePath, subModuleName);
          }
          */
        //test against will be available 
        if (StringUtils.isNotEmpty(reportDir) && CollectionUtils.isNotEmpty(testAgainsts)) {
            Pattern p = Pattern.compile(TEST_DIRECTORY);
            Matcher matcher = p.matcher(reportDir);
            reportDir = matcher.replaceAll(testAgainsts.get(0));
            sb.append(reportDir);
        }

        //for android - test type will not be available --- to get device id from result xml
        if (Constants.PHASE_UNIT_TEST.equals(action) && StringUtils.isNotEmpty(showDevice)) {
            sb.append(reportDir);
        }

        File file = new File(sb.toString());

        if (StringUtils.isNotEmpty(resultExtension)) {
            File[] resultFiles = file.listFiles(new XmlNameFileFilter(resultExtension));
            if (!ArrayUtils.isEmpty(resultFiles)) {
                QualityUtil.sortResultFile(resultFiles);
                for (File resultFile : resultFiles) {
                    if (resultFile.isFile()) {
                        testResultFiles.add(resultFile.getName());
                    }
                }
            }
        }
    } catch (Exception e) {
        throw new PhrescoException(e);
    }

    return testResultFiles;
}

From source file:com.photon.phresco.framework.rest.api.QualityService.java

private List<String> testResultFilesfunctional(String rootModulePath, String subModuleName,
        List<String> testAgainsts, boolean showDevice, String action) throws PhrescoException {
    List<String> testResultFiles = new ArrayList<String>();
    String reportDir = "";
    String resultExtension = "";
    try {//from w w w .j  av a 2s.  c  o m
        ProjectInfo projectInfo = Utility.getProjectInfo(rootModulePath, subModuleName);
        File testFolderLocation = Utility.getTestFolderLocation(projectInfo, rootModulePath, subModuleName);
        StringBuilder sb = new StringBuilder(testFolderLocation.getPath());
        if (Constants.PHASE_FUNCTIONAL_TEST.equals(action)) {
            reportDir = FrameworkServiceUtil.getFunctionalTestRptDir(rootModulePath, subModuleName);
            resultExtension = "xml";
        } /*else {
           reportDir = FrameworkServiceUtil.getLoadTestReportDir(rootModulePath, subModuleName);
           resultExtension = FrameworkServiceUtil.getLoadResultFileExtension(rootModulePath, subModuleName);
          }
          */
        //test against will be available 
        if (StringUtils.isNotEmpty(reportDir) && CollectionUtils.isNotEmpty(testAgainsts)) {
            Pattern p = Pattern.compile(TEST_DIRECTORY);
            Matcher matcher = p.matcher(reportDir);
            reportDir = matcher.replaceAll(testAgainsts.get(0));
            sb.append(reportDir);
        }

        //for android - test type will not be available --- to get device id from result xml
        if (Constants.PHASE_FUNCTIONAL_TEST.equals(action) && showDevice) {
            sb.append(reportDir);
        }

        File file = new File(sb.toString());

        if (StringUtils.isNotEmpty(resultExtension)) {
            File[] resultFiles = file.listFiles(new XmlNameFileFilter(resultExtension));
            if (!ArrayUtils.isEmpty(resultFiles)) {
                QualityUtil.sortResultFile(resultFiles);
                for (File resultFile : resultFiles) {
                    if (resultFile.isFile()) {
                        testResultFiles.add(resultFile.getName());
                    }
                }
            }
        }
    } catch (Exception e) {
        throw new PhrescoException(e);
    }

    return testResultFiles;
}

From source file:marytts.tools.voiceimport.HTKLabeler.java

/**
 * Get word and phone sequence from a single feature file
 * @param basename// www.  j av  a2 s .co m
 * @return String
 * @throws Exception
 */
private String getWordandPhoneLineFromXML(String basename, boolean spause, boolean vpause) throws Exception {

    String line;
    String phoneSeq;
    Matcher matcher;
    Pattern pattern;
    StringBuilder alignBuff = new StringBuilder();
    //alignBuff.append(basename);
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(new File(getProp(PROMPTALLOPHONESDIR) + "/" + basename + xmlExt));
    XPath xpath = XPathFactory.newInstance().newXPath();
    NodeList tokens = (NodeList) xpath.evaluate("//t | //boundary", doc, XPathConstants.NODESET);

    alignBuff.append(collectTranscriptionAndWord(tokens));
    phoneSeq = alignBuff.toString();
    pattern = Pattern.compile("pau ssil ");
    matcher = pattern.matcher(phoneSeq);
    phoneSeq = matcher.replaceAll("sil ");

    pattern = Pattern.compile(" ssil pau$");
    matcher = pattern.matcher(phoneSeq);
    phoneSeq = matcher.replaceAll(" sil");

    if (!vpause) {
        /* TODO: Extra code need to write
         * to maintain minimum number of short sil.
         * or consider word boundaries as ssil.
         */
        /* 
         * virtual silence on word boundaries
         * are matched in sp  
         */
        pattern = Pattern.compile("vssil");
        matcher = pattern.matcher(phoneSeq);
        phoneSeq = matcher.replaceAll("");
    } else {
        /* 
         * virtual silence on word boundaries
         * are matched in sp  
         */
        pattern = Pattern.compile("vssil");
        matcher = pattern.matcher(phoneSeq);
        phoneSeq = matcher.replaceAll("sp");
    }

    // checking
    if (!spause) {
        pattern = Pattern.compile("ssil");
        matcher = pattern.matcher(phoneSeq);
        phoneSeq = matcher.replaceAll("");
    }

    phoneSeq += " .";

    pattern = Pattern.compile("\\s+");
    matcher = pattern.matcher(phoneSeq);
    phoneSeq = matcher.replaceAll("\n");

    pattern = Pattern.compile("-");
    matcher = pattern.matcher(phoneSeq);
    phoneSeq = matcher.replaceAll(" ");

    //System.out.println(phoneSeq);
    return phoneSeq;
}

From source file:com.photon.phresco.framework.rest.api.QualityService.java

private List<String> testResultFiles(String rootModulePath, String subModuleName, List<String> testAgainsts,
        boolean showDevice, String action) throws PhrescoException {
    List<String> testResultFiles = new ArrayList<String>();
    String reportDir = "";
    String resultExtension = "";
    try {/*from   w w  w.j a v  a  2s .com*/
        ProjectInfo projectInfo = Utility.getProjectInfo(rootModulePath, subModuleName);
        File testFolderLocation = Utility.getTestFolderLocation(projectInfo, rootModulePath, subModuleName);
        StringBuilder sb = new StringBuilder(testFolderLocation.getPath());
        if (Constants.PHASE_PERFORMANCE_TEST.equals(action)) {
            reportDir = FrameworkServiceUtil.getPerformanceTestReportDir(rootModulePath, subModuleName);
            resultExtension = FrameworkServiceUtil.getPerformanceResultFileExtension(rootModulePath,
                    subModuleName);
        } else {
            reportDir = FrameworkServiceUtil.getLoadTestReportDir(rootModulePath, subModuleName);
            resultExtension = FrameworkServiceUtil.getLoadResultFileExtension(rootModulePath, subModuleName);
        }

        //test against will be available 
        if (StringUtils.isNotEmpty(reportDir) && CollectionUtils.isNotEmpty(testAgainsts)) {
            Pattern p = Pattern.compile(TEST_DIRECTORY);
            Matcher matcher = p.matcher(reportDir);
            reportDir = matcher.replaceAll(testAgainsts.get(0));
            sb.append(reportDir);
        }

        //for android - test type will not be available --- to get device id from result xml
        if (Constants.PHASE_PERFORMANCE_TEST.equals(action) && showDevice) {
            sb.append(reportDir);
        }

        File file = new File(sb.toString());

        if (StringUtils.isNotEmpty(resultExtension)) {
            File[] resultFiles = file.listFiles(new XmlNameFileFilter(resultExtension));
            if (!ArrayUtils.isEmpty(resultFiles)) {
                QualityUtil.sortResultFile(resultFiles);
                for (File resultFile : resultFiles) {
                    if (resultFile.isFile()) {
                        testResultFiles.add(resultFile.getName());
                    }
                }
            }
        }
    } catch (Exception e) {
        throw new PhrescoException(e);
    }

    return testResultFiles;
}

From source file:com.photon.phresco.framework.rest.api.QualityService.java

public boolean testResultAvailunit(String rootModulePath, String subModuleName, List<String> testAgainsts,
        String action) throws PhrescoException {
    boolean resultAvailable = false;
    try {//from w  w  w .ja va2s. co  m
        String reportDir = "";
        String resultExtension = "";
        ProjectInfo projectInfo = Utility.getProjectInfo(rootModulePath, subModuleName);
        File testFolderLocation = Utility.getTestFolderLocation(projectInfo, rootModulePath, subModuleName);
        if (Constants.PHASE_UNIT_TEST.equals(action)) {
            reportDir = FrameworkServiceUtil.getUnitTestRptDir(rootModulePath, subModuleName);
            resultExtension = "xml";
        } /*else {
           reportDir = FrameworkServiceUtil.getLoadTestReportDir(rootModulePath, subModuleName);
           resultExtension = FrameworkServiceUtil.getLoadResultFileExtension(rootModulePath, subModuleName);
          }*/
        for (String testAgainst : testAgainsts) {
            StringBuilder sb = new StringBuilder(testFolderLocation.getPath());
            if (Constants.PHASE_UNIT_TEST.equals(action)) {
                reportDir = FrameworkServiceUtil.getUnitTestRptDir(rootModulePath, subModuleName);
            } /*else {
               reportDir = FrameworkServiceUtil.getLoadTestReportDir(rootModulePath, subModuleName);
              }*/

            if (StringUtils.isNotEmpty(reportDir) && StringUtils.isNotEmpty(testAgainst)) {
                Pattern p = Pattern.compile(TEST_DIRECTORY);
                Matcher matcher = p.matcher(reportDir);
                reportDir = matcher.replaceAll(testAgainst);
                sb.append(reportDir);
            }

            File file = new File(sb.toString());
            if (StringUtils.isNotEmpty(resultExtension) && file.exists()) {
                File[] children = file.listFiles(new XmlNameFileFilter(resultExtension));
                if (!ArrayUtils.isEmpty(children)) {
                    resultAvailable = true;
                    break;
                }
            }
        }

        if (CollectionUtils.isEmpty(testAgainsts) && Constants.PHASE_UNIT_TEST.equals(action)
                && StringUtils.isNotEmpty(reportDir)) {
            StringBuilder sb = new StringBuilder(testFolderLocation.getPath());
            sb.append(reportDir);
            File file = new File(sb.toString());
            if (StringUtils.isNotEmpty(resultExtension)) {
                File[] children = file.listFiles(new XmlNameFileFilter(resultExtension));
                if (!ArrayUtils.isEmpty(children)) {
                    resultAvailable = true;
                }
            }
        }

    } catch (Exception e) {
        throw new PhrescoException(e);
    }

    return resultAvailable;
}

From source file:com.photon.phresco.framework.rest.api.QualityService.java

public boolean testResultAvailfunctional(String rootModulePath, String subModuleName, List<String> testAgainsts,
        String action) throws PhrescoException {
    boolean resultAvailable = false;
    try {/*from www .j  a  v a  2 s.c o  m*/
        String reportDir = "";
        String resultExtension = "";
        ProjectInfo projectInfo = Utility.getProjectInfo(rootModulePath, subModuleName);
        File testFolderLocation = Utility.getTestFolderLocation(projectInfo, rootModulePath, subModuleName);
        if (Constants.PHASE_FUNCTIONAL_TEST.equals(action)) {
            reportDir = FrameworkServiceUtil.getFunctionalTestRptDir(rootModulePath, subModuleName);
            resultExtension = "xml";
        } /*else {
           reportDir = FrameworkServiceUtil.getLoadTestReportDir(rootModulePath, subModuleName);
           resultExtension = FrameworkServiceUtil.getLoadResultFileExtension(rootModulePath, subModuleName);
          }*/
        for (String testAgainst : testAgainsts) {
            StringBuilder sb = new StringBuilder(testFolderLocation.getPath());
            if (Constants.PHASE_FUNCTIONAL_TEST.equals(action)) {
                reportDir = FrameworkServiceUtil.getFunctionalTestRptDir(rootModulePath, subModuleName);
            } /*else {
               reportDir = FrameworkServiceUtil.getLoadTestReportDir(rootModulePath, subModuleName);
              }*/

            if (StringUtils.isNotEmpty(reportDir) && StringUtils.isNotEmpty(testAgainst)) {
                Pattern p = Pattern.compile(TEST_DIRECTORY);
                Matcher matcher = p.matcher(reportDir);
                reportDir = matcher.replaceAll(testAgainst);
                sb.append(reportDir);
            }

            File file = new File(sb.toString());
            if (StringUtils.isNotEmpty(resultExtension) && file.exists()) {
                File[] children = file.listFiles(new XmlNameFileFilter(resultExtension));
                if (!ArrayUtils.isEmpty(children)) {
                    resultAvailable = true;
                    break;
                }
            }
        }

        if (CollectionUtils.isEmpty(testAgainsts) && Constants.PHASE_FUNCTIONAL_TEST.equals(action)
                && StringUtils.isNotEmpty(reportDir)) {
            StringBuilder sb = new StringBuilder(testFolderLocation.getPath());
            sb.append(reportDir);
            File file = new File(sb.toString());
            if (StringUtils.isNotEmpty(resultExtension)) {
                File[] children = file.listFiles(new XmlNameFileFilter(resultExtension));
                if (!ArrayUtils.isEmpty(children)) {
                    resultAvailable = true;
                }
            }
        }

    } catch (Exception e) {
        throw new PhrescoException(e);
    }

    return resultAvailable;
}

From source file:com.photon.phresco.framework.rest.api.QualityService.java

public boolean testResultAvail(String rootModulePath, String subModuleName, List<String> testAgainsts,
        String action) throws PhrescoException {
    boolean resultAvailable = false;
    try {//w  w w.ja  v  a  2 s .  c  o m
        String reportDir = "";
        String resultExtension = "";
        ProjectInfo projectInfo = Utility.getProjectInfo(rootModulePath, subModuleName);
        File testFolderLocation = Utility.getTestFolderLocation(projectInfo, rootModulePath, subModuleName);
        if (Constants.PHASE_PERFORMANCE_TEST.equals(action)) {
            reportDir = FrameworkServiceUtil.getPerformanceTestReportDir(rootModulePath, subModuleName);
            resultExtension = FrameworkServiceUtil.getPerformanceResultFileExtension(rootModulePath,
                    subModuleName);
        } else {
            reportDir = FrameworkServiceUtil.getLoadTestReportDir(rootModulePath, subModuleName);
            resultExtension = FrameworkServiceUtil.getLoadResultFileExtension(rootModulePath, subModuleName);
        }
        for (String testAgainst : testAgainsts) {
            StringBuilder sb = new StringBuilder(testFolderLocation.getPath());
            if (Constants.PHASE_PERFORMANCE_TEST.equals(action)) {
                reportDir = FrameworkServiceUtil.getPerformanceTestReportDir(rootModulePath, subModuleName);
            } else {
                reportDir = FrameworkServiceUtil.getLoadTestReportDir(rootModulePath, subModuleName);
            }

            if (StringUtils.isNotEmpty(reportDir) && StringUtils.isNotEmpty(testAgainst)) {
                Pattern p = Pattern.compile(TEST_DIRECTORY);
                Matcher matcher = p.matcher(reportDir);
                reportDir = matcher.replaceAll(testAgainst);
                sb.append(reportDir);
            }

            File file = new File(sb.toString());
            if (StringUtils.isNotEmpty(resultExtension) && file.exists()) {
                File[] children = file.listFiles(new XmlNameFileFilter(resultExtension));
                if (!ArrayUtils.isEmpty(children)) {
                    resultAvailable = true;
                    break;
                }
            }
        }

        if (CollectionUtils.isEmpty(testAgainsts) && Constants.PHASE_PERFORMANCE_TEST.equals(action)
                && StringUtils.isNotEmpty(reportDir)) {
            StringBuilder sb = new StringBuilder(testFolderLocation.getPath());
            sb.append(reportDir);
            File file = new File(sb.toString());
            if (StringUtils.isNotEmpty(resultExtension)) {
                File[] children = file.listFiles(new XmlNameFileFilter(resultExtension));
                if (!ArrayUtils.isEmpty(children)) {
                    resultAvailable = true;
                }
            }
        }

    } catch (Exception e) {
        throw new PhrescoException(e);
    }

    return resultAvailable;
}

From source file:it.infn.ct.nuclemd.Nuclemd.java

public String RemoveCarriageReturn(String InputFileName, String OutputFileName) {
    // Remove the carriage return char from a named file.                                
    FileInputStream fis;/*  w w w  . j  a v  a  2  s  .co  m*/
    try {

        fis = new FileInputStream(InputFileName);
        BufferedReader in = new BufferedReader(new InputStreamReader(fis));

        File fout = new File(OutputFileName);
        FileOutputStream fos = new FileOutputStream(fout);
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(fos));

        // The pattern matches control characters
        Pattern p = Pattern.compile("\r");
        Matcher m = p.matcher("");
        String aLine = null;

        try {
            while ((aLine = in.readLine()) != null) {
                m.reset(aLine);
                //Replaces control characters with an empty string.
                String result = m.replaceAll("");
                out.write(result);
                out.newLine();
            }
            out.close();
        } catch (IOException ex) {
            Logger.getLogger(Nuclemd.class.getName()).log(Level.SEVERE, null, ex);
        }

    } catch (FileNotFoundException ex) {
        Logger.getLogger(Nuclemd.class.getName()).log(Level.SEVERE, null, ex);
    }

    log.info("\n- Writing the user's stripped file: [ " + OutputFileName.toString() + " ] to disk");

    return OutputFileName;
}

From source file:com.aerohive.nms.web.config.lbs.services.HmFolderServiceImpl.java

@Override
public HmFileUploadResponsedVo exportFolder(Long folderId, Long ownerId) throws Exception {
    HmFolderDataConvertVo vo = prepareFolder(folderId);
    HmFileUploadResponsedVo responseVo = new HmFileUploadResponsedVo();
    String regex = "[/?*|<>:\"\\\\]";
    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(vo.getName());
    String str = m.replaceAll("_").trim();
    final String file_prefix = str + "_" + new Date().getTime();
    Set<String> imageNames = new HashSet<>();
    collectImageNamesFromNode(vo, imageNames);
    File downloadFile = generateXML(file_prefix, vo, ownerId, responseVo);
    if (downloadFile != null && imageNames.size() > 0) {
        downloadFile = makeXMLBgImages2Tar(file_prefix, ownerId, imageNames, responseVo);
    }//from  w ww . j  ava 2s  .  c  o m
    try (FileInputStream input = new FileInputStream(downloadFile)) {
        responseVo.setFileInput(IOUtils.toByteArray(input));
        responseVo.setFileName(downloadFile.getName());
    } catch (Exception e) {
        logger.error(
                String.format("Error to download the file : %s: %s", downloadFile.getName(), e.getMessage()));
        throw new AhCarrierException(e, ErrorCodes.fileDownloadFailed,
                new Object[][] { { "fileName", downloadFile.getName() }, });
    } finally {
        if (downloadFile != null && imageNames.size() > 0) {
            FileUtils.deleteQuietly(downloadFile);
            if (downloadFile.getParentFile().isDirectory()) {
                FileUtils.deleteDirectory(downloadFile.getParentFile());
            }
        } else {
            FileUtils.deleteQuietly(downloadFile);
            File baseDirectory = new File(getFolderFileBasePath(file_prefix, ownerId));
            FileUtils.forceDelete(baseDirectory);
        }
    }
    return responseVo;
}