Revision created by MOE tool push_codebase.
[gae.git] / java / src / main / com / google / appengine / tools / admin / Utility.java
blobf5ca2e47feff36cf080bc2091ee22505117169fd
1 // Copyright 2008 Google Inc. All Rights Reserved.
3 package com.google.appengine.tools.admin;
5 import java.io.File;
7 /**
8 * Utility methods for this package.
11 public class Utility {
13 private static final String FORWARD_SLASH = "/";
15 /**
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() {
23 File ln = null;
24 if (isOsUnix()) {
25 ln = new File("/bin/ln");
26 if (!ln.exists()) {
27 ln = new File("/usr/bin/ln");
28 if (!ln.exists()) {
29 ln = null;
33 return ln;
36 /**
37 * Test for Unix (to include MacOS), vice Windows.
39 public static boolean isOsUnix() {
40 return File.separator.equals(FORWARD_SLASH);
43 /**
44 * Test for Windows, vice Unix (to include MacOS).
46 public static boolean isOsWindows() {
47 return !isOsUnix();
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) {
59 if (offset > 0) {
60 path = path.substring(offset);
63 return path;
66 /**
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);
77 switch (ch) {
78 case '"':
79 stringBuilder.append("\\\"");
80 break;
82 case '\\':
83 stringBuilder.append("\\\\");
84 break;
86 case '\b':
87 stringBuilder.append("\\b");
88 break;
90 case '\f':
91 stringBuilder.append("\\f");
92 break;
94 case '\n':
95 stringBuilder.append("\\n");
96 break;
98 case '\r':
99 stringBuilder.append("\\r");
100 break;
102 case '\t':
103 stringBuilder.append("\\t");
104 break;
106 case '/':
107 stringBuilder.append("\\/");
108 break;
110 default:
111 if ((ch >= 0x20) && (ch < 0x7f)) {
112 stringBuilder.append(ch);
113 } else {
114 stringBuilder.append(String.format("\\u%04x", (int) ch));
116 break;
120 return stringBuilder.toString();
123 private Utility() {