| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
package org.locationtech.jts.util; |
| 14 |
|
| 15 |
import java.io.ByteArrayOutputStream; |
| 16 |
import java.io.IOException; |
| 17 |
import java.io.LineNumberReader; |
| 18 |
import java.io.PrintStream; |
| 19 |
import java.io.StringReader; |
| 20 |
import java.text.DecimalFormat; |
| 21 |
import java.text.NumberFormat; |
| 22 |
import java.util.ArrayList; |
| 23 |
|
| 24 |
import org.locationtech.jts.io.OrdinateFormat; |
| 25 |
|
| 26 |
/** |
| 27 |
* Utility methods for working with {@link String}s. |
| 28 |
* |
| 29 |
* @author Martin Davis |
| 30 |
* |
| 31 |
*/ |
| 32 |
public class StringUtil |
| 33 |
{ |
| 34 |
/** |
| 35 |
* Mimics the the Java SE {@link String#split(String)} method. |
| 36 |
* |
| 37 |
* @param s the string to split. |
| 38 |
* @param separator the separator to use. |
| 39 |
* @return the array of split strings. |
| 40 |
*/ |
| 41 |
public static String[] split(String s, String separator) |
| 42 |
{ |
| 43 |
int separatorlen = separator.length(); |
| 44 |
ArrayList tokenList = new ArrayList(); |
| 45 |
String tmpString = "" + s; |
| 46 |
int pos = tmpString.indexOf(separator); |
| 47 |
while (pos >= 0) { |
| 48 |
String token = tmpString.substring(0, pos); |
| 49 |
tokenList.add(token); |
| 50 |
tmpString = tmpString.substring(pos + separatorlen); |
| 51 |
pos = tmpString.indexOf(separator); |
| 52 |
} |
| 53 |
if (tmpString.length() > 0) |
| 54 |
tokenList.add(tmpString); |
| 55 |
String[] res = new String[tokenList.size()]; |
| 56 |
for (int i = 0; i < res.length; i++) { |
| 57 |
res[i] = (String) tokenList.get(i); |
| 58 |
} |
| 59 |
return res; |
| 60 |
} |
| 61 |
|
| 62 |
public final static String NEWLINE = System.getProperty("line.separator"); |
| 63 |
|
| 64 |
/** |
| 65 |
* Returns an throwable's stack trace |
| 66 |
*/ |
| 67 |
public static String getStackTrace(Throwable t) { |
| 68 |
ByteArrayOutputStream os = new ByteArrayOutputStream(); |
| 69 |
PrintStream ps = new PrintStream(os); |
| 70 |
t.printStackTrace(ps); |
| 71 |
return os.toString(); |
| 72 |
} |
| 73 |
|
| 74 |
public static String getStackTrace(Throwable t, int depth) { |
| 75 |
String stackTrace = ""; |
| 76 |
StringReader stringReader = new StringReader(getStackTrace(t)); |
| 77 |
LineNumberReader lineNumberReader = new LineNumberReader(stringReader); |
| 78 |
for (int i = 0; i < depth; i++) { |
| 79 |
try { |
| 80 |
stackTrace += lineNumberReader.readLine() + NEWLINE; |
| 81 |
} catch (IOException e) { |
| 82 |
Assert.shouldNeverReachHere(); |
| 83 |
} |
| 84 |
} |
| 85 |
return stackTrace; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Returns a string representation of the given number, |
| 90 |
* using a format compatible with WKT. |
| 91 |
* |
| 92 |
* @param d a number |
| 93 |
* @return a string |
| 94 |
* |
| 95 |
* @deprecated use {@link OrdinateFormat} |
| 96 |
*/ |
| 97 |
public static String toString(double d) { |
| 98 |
return OrdinateFormat.DEFAULT.format(d); |
| 99 |
} |
| 100 |
|
| 101 |
public static String spaces(int n) |
| 102 |
{ |
| 103 |
return chars(' ', n); |
| 104 |
} |
| 105 |
|
| 106 |
public static String chars(char c, int n) |
| 107 |
{ |
| 108 |
char[] ch = new char[n]; |
| 109 |
for (int i = 0; i < n; i++) { |
| 110 |
ch[i] = c; |
| 111 |
} |
| 112 |
return new String(ch); |
| 113 |
} |
| 114 |
} |
| 115 |
|