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  /***
19   * @author pramirez
20   * @version $Revision$
21   * 
22   */
23  public class Utility {
24      
25      public static String stripWhitespace(String value) {
26          return filterString(stripNewLines(value));
27      }
28      
29      public static String stripNewLines(String value) {
30          String filteredValue = value; 
31          //Perform whitespace stripping
32          //First replace all hyphen line.separator with ""
33          filteredValue = filteredValue.replaceAll("-" + System.getProperty("line.separator"), "");
34          //Next replace all line.separator with " "
35          filteredValue = filteredValue.replaceAll(System.getProperty("line.separator"), " ");
36          return filteredValue;
37      }
38      
39      public static String filterString(String value) {
40          String filteredValue = value;
41          //Replace all '_' with ' '
42          filteredValue = filteredValue.replaceAll("_", " ");
43          //Replace multiple spaces with a single space
44          filteredValue = filteredValue.replaceAll("//s+", " ");
45          //Trim whitespace
46          filteredValue = filteredValue.trim();
47          return filteredValue;
48      }
49      
50      public static String trimString(String value, int length) {
51          String trimmedString = value;
52          
53          if (trimmedString.length() > length*3)
54              trimmedString = trimmedString.substring(0, length*3);
55          trimmedString = stripNewLines(trimmedString);
56          trimmedString = filterString(trimmedString);
57          if (trimmedString.length() > length)
58              trimmedString = trimmedString.substring(0, length-1);
59          
60          return trimmedString;
61      }
62  }