001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/sam/trunk/component/src/java/org/sakaiproject/tool/assessment/util/StringParseUtils.java $
003: * $Id: StringParseUtils.java 9273 2006-05-10 22:34:28Z daisyf@stanford.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the"License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.tool.assessment.util;
021:
022: import java.util.ArrayList;
023: import java.util.StringTokenizer;
024:
025: public class StringParseUtils {
026: public static void main(String[] args) {
027: }
028:
029: /**
030: * Create an all lowercase version substituting "_" for non-alphanumeric with
031: * trailing "_". Limits the length.
032: *
033: * @param s the String
034: * @param max the max number of characters to be output
035: * @param min the min number of characters to be outpu
036: *
037: * @return
038: */
039: public static String simplifyString(String s, int max, int min) {
040: s = "" + s;
041: if (min > max) {
042: min = max;
043: }
044:
045: // normalize to lowercase
046: s = s.toLowerCase();
047:
048: // hold in a StringBuffer
049: StringBuffer sb = new StringBuffer();
050:
051: // replace any non alphanumeric chars with underscore
052: for (int i = 0; (i < s.length()) && (i < max); i++) {
053: char c = s.charAt(i);
054: if (((c > ('a' - 1)) && (c < ('z' + 1)))
055: || ((c > ('0' - 1)) && (c < ('9' + 1)))) {
056: sb.append(c);
057: } else {
058: sb.append('_');
059: }
060: }
061:
062: sb.append('_');
063:
064: // fill out to minimum length with underscores
065: while (sb.length() < min) {
066: sb.append('_');
067: }
068:
069: // return the StringBuffer as a String.
070: return sb.toString();
071: }
072:
073: /**
074: * utility.
075: * @param name full name
076: * @return (hopefully) first name
077: */
078: public static String getFirstNameFromName(String name) {
079: String[] names = getNameArray(name);
080: if (names.length == 0) {
081: return "";
082: } else if (names.length == 1) {
083: return names[0];
084: }
085:
086: String[] tempNames = new String[names.length - 1];
087: System.arraycopy(names, 0, tempNames, 0, tempNames.length);
088: names = tempNames;
089:
090: String s = "";
091:
092: for (int i = 0; i < names.length; i++) {
093: if (names[i].length() > 0) {
094: if (Character.isLowerCase(names[i].charAt(0))) {
095: break;
096: }
097: s += names[i] + " ";
098: } // ifnames
099: } //for
100:
101: s = s.trim();
102:
103: return s;
104: }
105:
106: /**
107: * utility.
108: * @param name full name
109: * @return (hopefully) last name
110: */
111: public static String getLastNameFromName(String name) {
112: String[] names = getNameArray(name);
113: if (names.length == 0) {
114: return "";
115: } else if (names.length == 1) {
116: return names[0];
117: }
118:
119: String s = "";
120: String lastWord = names[names.length - 1];
121: boolean smallFound = false;
122:
123: for (int i = 0; i < names.length; i++) {
124: if (names[i].length() > 0) {
125: if (Character.isLowerCase(names[i].charAt(0))) {
126: smallFound = true;
127: }
128: if (smallFound) {
129: s += names[i] + " ";
130: } //if
131: } // ifnames
132: } //for
133:
134: s = s.trim();
135:
136: if (s.length() == 0) {
137: s = lastWord;
138: }
139:
140: return s;
141: }
142:
143: /**
144: * util
145: * @param name
146: * @return each word in name
147: */
148: private static String[] getNameArray(String name) {
149: ArrayList list = new ArrayList();
150: StringTokenizer st = new StringTokenizer(name, " ");
151: while (st.hasMoreElements()) {
152: list.add(st.nextToken());
153: }
154:
155: int size = list.size();
156: String sa[] = new String[size];
157:
158: for (int i = 0; i < size; i++) {
159: sa[i] = (String) list.get(i);
160: }
161:
162: return sa;
163: }
164:
165: }
|