View Javadoc

1   // Copyright 2006-2007, by the California Institute of Technology.
2   // ALL RIGHTS RESERVED. United States Government Sponsorship acknowledged.
3   // Any commercial use must be negotiated with the Office of Technology Transfer
4   // at the California Institute of Technology.
5   //
6   // This software is subject to U. S. export control laws and regulations
7   // (22 C.F.R. 120-130 and 15 C.F.R. 730-774). To the extent that the software
8   // is subject to U.S. export control laws and regulations, the recipient has
9   // the responsibility to obtain export licenses or other export authority as
10  // may be required before exporting such information to foreign countries or
11  // providing access to foreign nationals.
12  //
13  // $Id$ 
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$
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          //Perform whitespace stripping
36          //First replace all hyphen line.separator with ""
37          filteredValue = filteredValue.replaceAll("-" + System.getProperty("line.separator"), "");
38          //Next replace all line.separator with " "
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          //Replace all '_' with ' '
46          filteredValue = filteredValue.replaceAll("_", " ");
47          //Replace multiple spaces with a single space
48          filteredValue = filteredValue.replaceAll("//s+", " ");
49          //Trim whitespace
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  }