/**
  * Line endings can be an issue when running tests on different OSs. This function standardizes
  * the line endings to use <code>\n</code>
  *
  * <p>It will get the text from the given editor, change the line endings, and then save the
  * editor
  *
  * @param editor standardize the line endings of the text presented in this editor.
  */
 private static void standardizeLineEndings(ITextEditor editor) {
   IDocument doc = editor.getDocumentProvider().getDocument(editor.getEditorInput());
   String contents = doc.get();
   contents = StringUtils.replace(contents, "\r\n", "\n");
   contents = StringUtils.replace(contents, "\r", "\n");
   doc.set(contents);
 }
  private static void launchOtherOsFile(String filepath) throws IOException {
    String commandStr;
    if (MediaType.getVideoMediaType().isFilenameOf(filepath)) {
      commandStr = SystemToolsPrefs.OpenVideoCmdOtherOS.get();
    } else if (MediaType.getImageMediaType().isFilenameOf(filepath)) {
      commandStr = SystemToolsPrefs.OpenImageCmdOtherOS.get();
    } else if (MediaType.getAudioMediaType().isFilenameOf(filepath)) {
      commandStr = SystemToolsPrefs.OpenAudioCmdOtherOS.get();
    } else {
      commandStr = SystemToolsPrefs.OpenBrowserCmdOtherOS.get();
    }

    // Define a pattern which will split on spaces but respect quotes
    Pattern p = Pattern.compile("(\".*?\"|\\S+)", Pattern.CASE_INSENSITIVE);
    Matcher m = p.matcher(commandStr);
    List tokens = new LinkedList();
    while (m.find()) {
      tokens.add(m.group(1));
    }
    String[] result = new String[tokens.size()];
    tokens.toArray(result);

    StringBuffer replacedCmd = new StringBuffer();
    for (int i = 0; i < result.length; i++) {
      result[i] = StringUtils.replace(result[i], "%filepath%", filepath, -1);

      replacedCmd.append(result[i]);
      replacedCmd.append(" ");
    }
    NLogger.debug(SystemShellExecute.class, "Executing " + replacedCmd);
    Runtime.getRuntime().exec(result);
  }
示例#3
0
 /**
  * 修复路径,将 \\ 或 / 等替换为 File.separator
  *
  * @param path
  * @return
  */
 public static String path(String path) {
   String p = StringUtils.replace(path, "\\", "/");
   p = StringUtils.join(StringUtils.split(p, "/"), "/");
   if (!StringUtils.startsWithAny(p, "/") && StringUtils.startsWithAny(path, "\\", "/")) {
     p += "/";
   }
   if (!StringUtils.endsWithAny(p, "/") && StringUtils.endsWithAny(path, "\\", "/")) {
     p = p + "/";
   }
   return p;
 }
  @Test
  public void testReplace() throws Exception {
    String inString = "a6AazAaa77abaa";
    String oldPattern = "aa";
    String newPattern = "foo";

    // Simple replace
    String s = StringUtils.replace(inString, oldPattern, newPattern);
    assertTrue("Replace 1 worked", s.equals("a6AazAfoo77abfoo"));

    // Non match: no change
    s = StringUtils.replace(inString, "qwoeiruqopwieurpoqwieur", newPattern);
    assertTrue("Replace non matched is equal", s.equals(inString));

    // Null new pattern: should ignore
    s = StringUtils.replace(inString, oldPattern, null);
    assertTrue("Replace non matched is equal", s.equals(inString));

    // Null old pattern: should ignore
    s = StringUtils.replace(inString, null, newPattern);
    assertTrue("Replace non matched is equal", s.equals(inString));
  }
示例#5
0
 private String encode(String value, Page page) {
   if (StringUtils.isEmpty(value)) {
     return "";
   }
   String val =
       StringUtils.replace(
           SessionStorage.getInstance(page.getRequest()).encodeURL(value), " ", "%20");
   int pos = val.indexOf(";");
   if (pos > -1) {
     if (pos == 0) {
       return "";
     }
     val = val.substring(0, pos);
   }
   return val;
 }
    @Override
    public int translate(final CharSequence input, final int index, final Writer out)
        throws IOException {

      if (index != 0) {
        throw new IllegalStateException("CsvEscaper should never reach the [1] index");
      }

      if (StringUtils.containsNone(input.toString(), CSV_SEARCH_CHARS)) {
        out.write(input.toString());
      } else {
        out.write(CSV_QUOTE);
        out.write(
            StringUtils.replace(input.toString(), CSV_QUOTE_STR, CSV_QUOTE_STR + CSV_QUOTE_STR));
        out.write(CSV_QUOTE);
      }
      return input.length();
    }
示例#7
0
  /**
   * Returns a <code>String</code> value for an unescaped CSV column.
   *
   * <p>If the value is enclosed in double quotes, and contains a comma, newline or double quote,
   * then quotes are removed.
   *
   * <p>Any double quote escaped characters (a pair of double quotes) are unescaped to just one
   * double quote.
   *
   * <p>If the value is not enclosed in double quotes, or is and does not contain a comma, newline
   * or double quote, then the String value is returned unchanged. see <a
   * href="http://en.wikipedia.org/wiki/Comma-separated_values">Wikipedia</a> and <a
   * href="http://tools.ietf.org/html/rfc4180">RFC 4180</a>.
   *
   * @param str the input CSV column String, may be null
   * @param out Writer to write the input String to, with enclosing double quotes removed and
   *     embedded double quotes unescaped, <code>null</code> if null string input
   * @throws IOException if error occurs on underlying Writer
   * @since 2.4
   */
  public static void unescapeCsv(Writer out, String str) throws IOException {
    if (str == null) {
      return;
    }
    if (str.length() < 2) {
      out.write(str);
      return;
    }
    if (str.charAt(0) != CSV_QUOTE || str.charAt(str.length() - 1) != CSV_QUOTE) {
      out.write(str);
      return;
    }

    // strip quotes
    String quoteless = str.substring(1, str.length() - 1);

    if (StringUtils.containsAny(quoteless, CSV_SEARCH_CHARS)) {
      // deal with escaped quotes; ie) ""
      str = StringUtils.replace(quoteless, CSV_QUOTE_STR + CSV_QUOTE_STR, CSV_QUOTE_STR);
    }

    out.write(str);
  }
    @Override
    public int translate(final CharSequence input, final int index, final Writer out)
        throws IOException {

      if (index != 0) {
        throw new IllegalStateException("CsvUnescaper should never reach the [1] index");
      }

      if (input.charAt(0) != CSV_QUOTE || input.charAt(input.length() - 1) != CSV_QUOTE) {
        out.write(input.toString());
        return input.length();
      }

      // strip quotes
      final String quoteless = input.subSequence(1, input.length() - 1).toString();

      if (StringUtils.containsAny(quoteless, CSV_SEARCH_CHARS)) {
        // deal with escaped quotes; ie) ""
        out.write(StringUtils.replace(quoteless, CSV_QUOTE_STR + CSV_QUOTE_STR, CSV_QUOTE_STR));
      } else {
        out.write(input.toString());
      }
      return input.length();
    }
 /**
  * Escapes the characters in a <code>String</code> to be suitable to pass to an SQL query.
  *
  * <p>For example,
  *
  * <pre>statement.executeQuery("SELECT * FROM MOVIES WHERE TITLE='" +
  *   StringEscapeUtils.escapeSql("McHale's Navy") +
  *   "'");</pre>
  *
  * <p>At present, this method only turns single-quotes into doubled single-quotes (<code>
  * "McHale's Navy"</code> => <code>"McHale''s Navy"</code>). It does not handle the cases of
  * percent (%) or underscore (_) for use in LIKE clauses. see
  * http://www.jguru.com/faq/view.jsp?EID=8881
  *
  * @param str the string to escape, may be null
  * @return a new String, escaped for SQL, <code>null</code> if null string input
  */
 public static String escapeSql(String str) {
   if (str == null) {
     return null;
   }
   return StringUtils.replace(str, "'", "''");
 }
示例#10
0
 /**
  * Create a URI instance for the given location String, replacing spaces with "%20" quotes first.
  *
  * @param location the location String to convert into a URI instance
  * @return the URI instance
  * @throws URISyntaxException if the location wasn't a valid URI
  */
 public static URI toURI(String location) throws URISyntaxException {
   return new URI(StringUtils.replace(location, " ", "%20"));
 }