Example usage for org.apache.commons.lang3 StringUtils replace

List of usage examples for org.apache.commons.lang3 StringUtils replace

Introduction

In this page you can find the example usage for org.apache.commons.lang3 StringUtils replace.

Prototype

public static String replace(final String text, final String searchString, final String replacement) 

Source Link

Document

Replaces all occurrences of a String within another String.

A null reference passed to this method is a no-op.

 StringUtils.replace(null, *, *)        = null StringUtils.replace("", *, *)          = "" StringUtils.replace("any", null, *)    = "any" StringUtils.replace("any", *, null)    = "any" StringUtils.replace("any", "", *)      = "any" StringUtils.replace("aba", "a", null)  = "aba" StringUtils.replace("aba", "a", "")    = "b" StringUtils.replace("aba", "a", "z")   = "zbz" 

Usage

From source file:org.displaytag.export.excel.ExcelUtils.java

/**
 * Escape certain values that are not permitted in excel cells.
 * @param rawValue the object value/*from   www.  j  ava 2 s . co m*/
 * @return the escaped value
 */
public static String escapeColumnValue(Object rawValue) {
    if (rawValue == null) {
        return null;
    }
    // str = Patterns.replaceAll(str, "(\\r\\n|\\r|\\n|\\n\\r)\\s*", "");
    String returnString = rawValue.toString();
    // escape the String to get the tabs, returns, newline explicit as \t \r \n
    returnString = StringEscapeUtils.escapeJava(StringUtils.trimToEmpty(returnString));
    // remove tabs, insert four whitespaces instead
    returnString = StringUtils.replace(StringUtils.trim(returnString), "\\t", "    ");
    // remove the return, only newline valid in excel
    returnString = StringUtils.replace(StringUtils.trim(returnString), "\\r", " ");
    // unescape so that \n gets back to newline
    returnString = StringEscapeUtils.unescapeJava(returnString);
    return returnString;
}

From source file:org.docx4j.template.WordprocessingMLDocxTemplate.java

/**
 * ??????word?//from  w  w w  .j a  v  a2s .  com
 * @param template ?
 * @param variables ??
 * @return {@link WordprocessingMLPackage} 
 * @throws Exception 
 */
@Override
public WordprocessingMLPackage process(String template, Map<String, Object> variables) throws Exception {

    //?
    this.unzipDir = new File(this.sourceDocx.getParentFile(),
            Docx4jProperties.getProperty("docx4j.docx.tmpdir", "unzip_tmpdir"));
    if (!this.unzipDir.exists() || this.unzipDir.isFile()) {
        this.unzipDir.setReadable(true);
        this.unzipDir.setWritable(true);
        this.unzipDir.mkdir();
    }

    /*
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(sourceDocx);
    MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();  
      //??????
      HashMap<String, String> staticMap = getStaticData(variables);
      // ???  
      documentPart.variableReplace(staticMap);  
      */
    //
    WmlZipUtils.unzip(this.sourceDocx, this.unzipDir);
    //?
    File contentXmlFile = new File(this.unzipDir, PATH_TO_CONTENT);
    if (!contentXmlFile.exists()) {
        throw new FileNotFoundException(contentXmlFile.getAbsolutePath());
    }
    //???
    //String template = FileUtils.readFileToString(contentXmlFile, this.inputEncoding );
    for (Map.Entry<String, Object> entry : variables.entrySet()) {
        //???
        template = StringUtils.replace(template, this.placeholderStart + entry.getKey() + this.placeholderEnd,
                String.valueOf(entry.getValue()));
    }
    /*if(this.sourceDel){
       //?
       this.sourceDocx.delete();
    }*/
    //
    String sourceDocxPath = this.sourceDocx.getPath();
    String tempDocxPath = sourceDocxPath.substring(0, sourceDocxPath.lastIndexOf(".")) + "_bak";
    File tempDir = new File(tempDocxPath);
    //
    FileUtils.deleteDirectory(tempDir);
    //?
    FileUtils.copyDirectory(this.unzipDir, tempDir);
    //??
    FileUtils.writeStringToFile(new File(tempDir, PATH_TO_CONTENT), template, this.outputEncoding);
    //?????
    WmlZipUtils.zipDir(tempDir, this.outputDocx, false);
    //
    FileUtils.deleteDirectory(tempDir);
    //WordprocessingMLPackage
    return FontMapperHolder.useFontMapper(WordprocessingMLPackage.load(this.outputDocx));
}

From source file:org.dspace.servicemanager.config.DSpaceEnvironmentConfiguration.java

public static Map<String, Object> getModifiedEnvMap() {
    HashMap<String, Object> env = new HashMap<>(System.getenv().size());
    for (String key : System.getenv().keySet()) {
        // ignore all properties that do not contain __ as those will be loaded
        // by apache commons config environment lookup.
        if (!StringUtils.contains(key, "__")) {
            continue;
        }//from w  w  w .j av a2 s  . c  om

        // replace "__P__" with a single dot.
        // replace "__D__" with a single dash.
        String lookup = StringUtils.replace(key, "__P__", ".");
        lookup = StringUtils.replace(lookup, "__D__", "-");
        if (System.getenv(key) != null) {
            // store the new key with the old value in our new properties map.
            env.put(lookup, System.getenv(key));
            log.debug("Found env " + lookup + " = " + System.getenv(key) + ".");
        } else {
            log.debug("Didn't found env " + lookup + ".");
        }
    }
    return env;
}

From source file:org.eclipse.jdt.internal.junit.ui.BugTrace.java

/**
 * Create a part of row in the table representing the given test
 * /*from   w  w  w .jav a  2s  .c o  m*/
 * @param test the primitive test element (without children)
 * @return HTML TD string
 */
private String getExpectedAndActualColumns(ITestElement test) {
    Result result = test.getTestResult(true);
    if (result == Result.ERROR) {
        return getUknownError(test);
    }
    if (test instanceof TestElement) {
        TestElement testElement = (TestElement) test;
        if (!testElement.isComparisonFailure()) {
            return getUknownError(test);
        }
    }
    FailureTrace trace = test.getFailureTrace();
    StringBuilder b = new StringBuilder();

    b.append("<td><span class='e'>"); //$NON-NLS-1$
    b.append(StringUtils.replace(trace.getExpected(), "\n", "<br/>")); //$NON-NLS-1$//$NON-NLS-2$
    b.append("</span></td>"); //$NON-NLS-1$

    b.append("<td><span class='a'>"); //$NON-NLS-1$
    b.append("<a href='openTest#"); //$NON-NLS-1$
    b.append(test.hashCode());
    b.append("?'>"); //$NON-NLS-1$
    b.append(createPrettyHTMLDiff(trace));
    b.append("</a>"); //$NON-NLS-1$
    b.append("</span></td>"); //$NON-NLS-1$
    return b.toString();
}

From source file:org.eclipse.jdt.internal.junit.ui.BugTrace.java

/**
 * Initial implementation of a pretty diff algorithm
 * //from w  ww  .ja v  a  2s.  c o m
 * @param trace that contains actual and expected value
 * @return HTML enable text that contains ins and del tags
 */
private String createPrettyHTMLDiff(FailureTrace trace) {
    DiffMatchPatch diffMaker = new DiffMatchPatch(); //based on https://code.google.com/p/google-diff-match-patch/wiki/API
    if (NULL.equals(trace.getActual()) || NULL.equals(trace.getExpected())
            || EMPTY_STRING.equals(trace.getActual())) {
        return "<ins class='err'>" + StringUtils.replace(trace.getActual(), "\n", "<br/>") + "</ins>"; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
    }
    LinkedList<Diff> differences = diffMaker.diff_main(trace.getExpected(), trace.getActual(), false);
    return diffMaker.diff_prettyHtml(differences);
}

From source file:org.eclipse.vorto.codegen.internal.ui.wizard.GeneratorWizardPage.java

private void setDirectoryToWorkspaceField(String selectedDirectory) throws IOException {
    selectedDirectory = StringUtils.replace(selectedDirectory, "\\", "/");

    if (selectedDirectory != null) {
        workspaceLocation = selectedDirectory;
        updateWorkspaceLocationField(workspaceLocation);
        dialogChanged();/*from w w  w  .j  av a 2 s  . co m*/
    }
}

From source file:org.eclipse.vorto.core.api.model.model.ModelId.java

private String delimitVersion(String version) {
    return StringUtils.replace(version, ".", UNDERSCORE);
}

From source file:org.eclipse.vorto.wizard.AbstractVortoWizardPage.java

protected void handleBrowse(SelectionEvent e) {
    DirectoryDialog directoryDialog = new DirectoryDialog(getShell());
    directoryDialog.setFilterPath(workspaceLocation);
    directoryDialog.setText("Workspace folder selection");
    directoryDialog.setMessage("Select a directory for this project");

    String selectedDirectory = directoryDialog.open();
    selectedDirectory = StringUtils.replace(selectedDirectory, "\\", "/");

    if (selectedDirectory != null) {
        workspaceLocation = selectedDirectory;
        updateWorkspaceLocationField(workspaceLocation);
        dialogChanged();/*  ww  w .  j  av a  2  s  .  co m*/
    }
}

From source file:org.efaps.dataexporter.AbstractDataExporterTestBase.java

/**
 * Compare text.//from  www. java 2 s  .  co m
 *
 * @param message the message
 * @param file the file
 * @param text the text
 * @throws IOException Signals that an I/O exception has occurred.
 */
protected void compareText(final String message, final String file, String text) throws IOException {
    final InputStream inputStream = this.getClass().getResourceAsStream(file);
    Assert.assertNotNull(inputStream, "Couldn't read the reference template");

    String expected = IOUtils.toString(inputStream);
    if (isNotEmpty(message)) {
        System.out.println("\nExpected (" + message + "/" + file + ")\n" + expected);
    } else {
        System.out.println("\nExpected (" + file + ")\n" + expected);
    }
    System.out.println("\nProduced:\n" + text);

    expected = StringUtils.replace(expected, "\r\n", "\n");
    text = StringUtils.replace(text, "\r\n", "\n");
    Assert.assertEquals(expected.trim(), text.trim());
}

From source file:org.efaps.db.databases.AbstractDatabase.java

/**
 * @param _value String value to be escaped
 * @return escaped value in "'"/*from w  ww .j a va 2 s. c o  m*/
 */
public String escapeForWhere(final String _value) {
    return "'" + StringUtils.replace(_value, "'", "''") + "'";
}