001: package nl.hippo.util;
002:
003: public class NameUtil {
004:
005: public NameUtil() {
006: super ();
007: }
008:
009: /**
010: * Change any character other than space, 0-9, A-Z, a-z and underscore to an
011: * 'x'. Also convert space to an underscore.
012: *
013: * @param text
014: * Text of which to convert characters.
015: * @return The text with disallowed characters replaced by an 'x'.
016: */
017: public String convertToValidName(String text) {
018: StringBuffer cleanedText = new StringBuffer(text.length());
019: for (int i = 0; i < text.length(); i++) {
020: char c = text.charAt(i);
021: if (c == ' ') {
022: cleanedText.append('-');
023: } else if (isAllowedNameCharacter(c)) {
024: cleanedText.append(c);
025: } else {
026: cleanedText.append('-');
027: }
028: }
029:
030: String cleanName = cleanedText.toString();
031:
032: if (cleanName.length() > 50) {
033: cleanName = cleanName.substring(0, 50);
034: }
035:
036: return cleanName;
037: }
038:
039: public String convertToValidNameLowerCase(String text) {
040: String validname = convertToValidName(text);
041: return validname.toLowerCase();
042: }
043:
044: /**
045: * Change any character other than space, 0-9, A-Z, a-z and underscore to
046: * an 'x'. Also convert space to an underscore. The extension is not
047: * changed. The result will contain lowercase charaters only.
048: *
049: * @param text
050: * Text of which to convert characters.
051: * @return The text with disallowed characters replaced by an 'x'.
052: */
053: public String convertToValidBinaryFilename(String text) {
054: String result;
055:
056: int indexOfLastDot = text.lastIndexOf('.');
057:
058: if (indexOfLastDot == -1)
059: indexOfLastDot = text.length();
060:
061: String partBeforeExtension = text.substring(0, indexOfLastDot);
062: String extension = text
063: .substring(indexOfLastDot, text.length());
064:
065: partBeforeExtension = convertToValidName(partBeforeExtension);
066:
067: result = partBeforeExtension + extension;
068: result = result.toLowerCase();
069:
070: return result;
071: }
072:
073: /**
074: * Changes at, ampersand, dot, less than, greater than, backslash, slash,
075: * question mark, single quote and double quote to an underscore.
076: *
077: * @param text
078: * Text of which to convert characters.
079: * @return The text with disallowed characters replaced by an underscore.
080: */
081: public String convertToValidCaption(String text) {
082: StringBuffer cleanedText = new StringBuffer(text.length());
083: for (int i = 0; i < text.length(); i++) {
084: char c = text.charAt(i);
085: if (isDisallowedCaptionCharacter(c)) {
086: cleanedText.append('-');
087: } else {
088: cleanedText.append(c);
089: }
090: }
091:
092: return cleanedText.toString();
093: }
094:
095: private boolean isAllowedNameCharacter(char c) {
096: boolean result = false;
097:
098: result |= c == '_';
099: result |= c == '-';
100: // ASCII digits
101: result |= c >= 48 && c <= 57;
102: // ASCII capitals
103: result |= c >= 65 && c <= 90;
104: // ASCII letters
105: result |= c >= 97 && c <= 122;
106:
107: return result;
108: }
109:
110: private boolean isDisallowedCaptionCharacter(char c) {
111: boolean result = false;
112:
113: result |= c == '@';
114: result |= c == '&';
115: result |= c == '.';
116: result |= c == '<';
117: result |= c == '>';
118: result |= c == '\\';
119: result |= c == '/';
120: result |= c == '?';
121: result |= c == '\'';
122: result |= c == '\"';
123:
124: return result;
125: }
126:
127: }
|