1 // Copyright 2008 Google Inc. All Rights Reserved.
3 package com
.google
.appengine
.tools
.admin
;
8 * Utility methods for this package.
11 public class Utility
{
13 private static final String FORWARD_SLASH
= "/";
16 * Attempts to find a symlink executable, since Java doesn't natively
17 * support them but we don't like excessive copying.
19 * @return either {@code null} if no links can be made, or an executable
20 * program that we believe will make symlinks.
22 public static File
findLink() {
25 ln
= new File("/bin/ln");
27 ln
= new File("/usr/bin/ln");
37 * Test for Unix (to include MacOS), vice Windows.
39 public static boolean isOsUnix() {
40 return File
.separator
.equals(FORWARD_SLASH
);
44 * Test for Windows, vice Unix (to include MacOS).
46 public static boolean isOsWindows() {
50 public static String
calculatePath(File f
, File base
) {
51 int offset
= base
.getPath().length();
52 String path
= f
.getPath().substring(offset
);
53 if (File
.separatorChar
== '\\') {
54 path
= path
.replace('\\', '/');
57 for (offset
= 0; path
.charAt(offset
) == '/'; ++offset
) {
60 path
= path
.substring(offset
);
67 * Escapes the string as a JSON value.
69 * @param s raw string that we want to set as JSON value.
70 * @return unquoted JSON escaped string
72 public static String
jsonEscape(String s
) {
73 StringBuilder stringBuilder
= new StringBuilder();
74 for (int i
= 0; i
< s
.length(); i
++) {
75 char ch
= s
.charAt(i
);
79 stringBuilder
.append("\\\"");
83 stringBuilder
.append("\\\\");
87 stringBuilder
.append("\\b");
91 stringBuilder
.append("\\f");
95 stringBuilder
.append("\\n");
99 stringBuilder
.append("\\r");
103 stringBuilder
.append("\\t");
107 stringBuilder
.append("\\/");
111 if ((ch
>= 0x20) && (ch
< 0x7f)) {
112 stringBuilder
.append(ch
);
114 stringBuilder
.append(String
.format("\\u%04x", (int) ch
));
120 return stringBuilder
.toString();