1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package gov.nasa.pds.tools.util;
17
18 import java.io.File;
19 import java.net.MalformedURLException;
20 import java.net.URL;
21
22 /***
23 * @author pramirez
24 * @version $Revision: 3461 $
25 *
26 */
27 public class Utility {
28
29 public static String stripWhitespace(String value) {
30 return filterString(stripNewLines(value));
31 }
32
33 public static String stripNewLines(String value) {
34 String filteredValue = value;
35
36
37 filteredValue = filteredValue.replaceAll("-" + System.getProperty("line.separator"), "");
38
39 filteredValue = filteredValue.replaceAll(System.getProperty("line.separator"), " ");
40 return filteredValue;
41 }
42
43 public static String filterString(String value) {
44 String filteredValue = value;
45
46 filteredValue = filteredValue.replaceAll("_", " ");
47
48 filteredValue = filteredValue.replaceAll("//s+", " ");
49
50 filteredValue = filteredValue.trim();
51 return filteredValue;
52 }
53
54 public static String trimString(String value, int length) {
55 String trimmedString = value;
56
57 if (trimmedString.length() > length*3)
58 trimmedString = trimmedString.substring(0, length*3);
59 trimmedString = stripNewLines(trimmedString);
60 trimmedString = filterString(trimmedString);
61 if (trimmedString.length() > length)
62 trimmedString = trimmedString.substring(0, length-1);
63
64 return trimmedString;
65 }
66
67 /***
68 * Convert a string to a URL.
69 * @param s The string to convert
70 * @return A URL of the input string
71 */
72 public static URL toURL(String s) throws MalformedURLException {
73 URL url = null;
74 try {
75 url = new URL(s);
76 } catch (MalformedURLException ex) {
77 url = new File(s).toURI().toURL();
78 }
79 return url;
80 }
81 }