001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/sam/trunk/component/src/java/org/sakaiproject/tool/assessment/util/TextFormat.java $
003: * $Id: TextFormat.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.io.UnsupportedEncodingException;
023: import java.net.URLEncoder;
024: import java.util.ArrayList;
025: import java.util.Iterator;
026: import java.util.Vector;
027:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030:
031: /**
032: * <p>Copyright: Copyright (c) 2003-5</p>
033: * <p>Organization: Sakai Project</p>
034: * @author jlannan
035: * @author Ed Smiley esmiley@stanford.edu
036: * @version $Id: TextFormat.java 9273 2006-05-10 22:34:28Z daisyf@stanford.edu $
037: */
038: public class TextFormat {
039: private static Log log = LogFactory.getLog(TextFormat.class);
040: private static final String HTML;
041: private static final String SMART;
042: private static final String PLAIN;
043: private static final Vector vProtocols;
044: //private String upperText;
045: private StringBuffer returnText;
046: private StringBuffer resource;
047: private ArrayList arrLst;
048:
049: static {
050: HTML = "HTML";
051: SMART = "SMART";
052: PLAIN = "PLAIN";
053:
054: vProtocols = new Vector();
055: vProtocols.add("http://");
056: vProtocols.add("https://");
057: vProtocols.add("ftp://");
058: vProtocols.add("www.");
059: vProtocols.add("telent:");
060: vProtocols.add("mailto:");
061: }
062:
063: /**
064: * DOCUMENTATION PENDING
065: *
066: * @param text TEXT TO BE MODIFIED
067: * @param texttype TYPE OF TEXT -- PLAIN, HTML, OR SMART
068: * @param iconPath PATH TO ICON IMAGES IN APPLICATION
069: *
070: * @return DOCUMENTATION PENDING
071: */
072: public String formatText(String text, String texttype,
073: String iconPath) {
074: if (log.isDebugEnabled()) {
075: log.debug("formatText(String " + text + ", String "
076: + texttype + ", String " + iconPath + ")");
077: }
078:
079: returnText = new StringBuffer();
080:
081: if ((texttype == null) || (text == null)) {
082: return text;
083: } else if (texttype.equals(TextFormat.PLAIN)) {
084: return text;
085: } else if (texttype.equals(TextFormat.HTML)) {
086: return text;
087: } else if (texttype.equals(TextFormat.SMART)) {
088: int start = 0;
089: int end = 0;
090:
091: while (true) {
092: arrLst = new ArrayList();
093:
094: // traverse vector of protocol strings
095: Iterator i = vProtocols.iterator();
096: Integer retVal;
097: while (i.hasNext()) {
098: String str = (String) i.next();
099: arrLst.add(retVal = indexOfIgnoreCase(text, str));
100: if (retVal.intValue() == -1) {
101: i.remove();
102: }
103: }
104:
105: start = minimum(arrLst);
106: log.debug("start: " + String.valueOf(start));
107: if ((start == -1) || vProtocols.isEmpty()) {
108: break;
109: }
110:
111: // find either the next space or the end of string whichever comes first
112: if ((end = text.indexOf(" ", start)) == -1) {
113: end = text.length();
114: }
115:
116: // extract text and resource text from StringBuffer
117: if (start != 0) {
118: returnText.append(text.substring(0, start));
119: log.debug("adding pre-resource text: "
120: + text.substring(0, start));
121: }
122:
123: log.debug("end: " + String.valueOf(end));
124:
125: resource = new StringBuffer();
126: String upper = text.substring(start, end).toUpperCase();
127: try {
128: if (upper.startsWith("HTTPS://")) {
129: resource.append("https://");
130: resource.append(URLEncoder.encode(text
131: .substring(start + 8, end), "UTF-8"));
132: log.debug("hi" + resource);
133: } else if (upper.startsWith("HTTP://")
134: || upper.startsWith("MAILTO:")
135: || upper.startsWith("TELNET:")) {
136: resource.append("http://");
137: resource.append(URLEncoder.encode(text
138: .substring(start + 7, end), "UTF-8"));
139: } else if (upper.startsWith("FTP://")) {
140: resource.append("ftp://");
141: resource.append(URLEncoder.encode(text
142: .substring(start + 6, end), "UTF-8"));
143: } else if (upper.startsWith("WWW.")) {
144: resource.append("www.");
145: resource.append(URLEncoder.encode(text
146: .substring(start + 4, end), "UTF-8"));
147: } else {
148: ;
149: }
150: } catch (UnsupportedEncodingException e) {
151: log.error(e.getMessage(), e);
152: }
153:
154: String temp = resource.toString();
155: resource.insert(resource.length(), "', target=_new>"
156: + temp + "</a>");
157: resource.insert(0, "<a href='");
158:
159: //stringParts.add(resource.toString());
160: returnText.append(resource);
161: log.debug("add ing resource: " + resource.toString());
162:
163: // delete resource string
164: text = text.substring(end);
165: }
166:
167: // add remaining characters to buffer
168: if (text.length() != 0) {
169: returnText.append(text);
170: }
171:
172: int temp = 0;
173:
174: // replace emoticons with images
175: while ((temp = returnText.indexOf(":-)")) != -1) {
176: returnText.replace(temp, temp + 3, "<img src='"
177: + iconPath + "smile.gif'/>");
178: }
179:
180: while ((temp = returnText.indexOf(":-(")) != -1) {
181: returnText.replace(temp, temp + 3, "<img src='"
182: + iconPath + "frown.gif'/>");
183: }
184:
185: while ((temp = returnText.indexOf(":-o")) != -1) {
186: returnText.replace(temp, temp + 3, "<img src='"
187: + iconPath + "suprise.gif'/>");
188: }
189:
190: while ((temp = returnText.indexOf(";-)")) != -1) {
191: returnText.replace(temp, temp + 3, "<img src='"
192: + iconPath + "wink.gif'/>");
193: }
194:
195: while ((temp = returnText.indexOf(":)")) != -1) {
196: returnText.replace(temp, temp + 2, "<img src='"
197: + iconPath + "smile.gif'/>");
198: }
199:
200: while ((temp = returnText.indexOf(":(")) != -1) {
201: returnText.replace(temp, temp + 2, "<img src='"
202: + iconPath + "frown.gif'/>");
203: }
204:
205: while ((temp = returnText.indexOf(":o")) != -1) {
206: returnText.replace(temp, temp + 2, "<img src='"
207: + iconPath + "suprise.gif'/>");
208: }
209:
210: while ((temp = returnText.indexOf(";)")) != -1) {
211: returnText.replace(temp, temp + 2, "<img src='"
212: + iconPath + "wink.gif'/>");
213: }
214:
215: if (returnText != null) {
216: return returnText.toString();
217: } else {
218: return "";
219: }
220: } else {
221: return "";
222: }
223: }
224:
225: /**
226: * DOCUMENTATION PENDING
227: *
228: * @param str STRING TO BE SEARCHED
229: * @param searchString STRING TO SEARCH FOR WITHIN str
230: *
231: * @return INDEX LOCATION OF searchString within str
232: */
233: public Integer indexOfIgnoreCase(String str, String searchString) {
234: if (log.isDebugEnabled()) {
235: log.debug("indexOfIgnoreCase(String " + str + ", String "
236: + searchString + ")");
237: }
238:
239: return new Integer(str.toUpperCase().indexOf(
240: searchString.toUpperCase()));
241: }
242:
243: /**
244: * DOCUMENTATION PENDING
245: *
246: * @param a LIST OF INDICIES OF OCCURANCES OF HYPERLINKS WITHIN TEXT
247: *
248: * @return MINIMUM OF ALL OCCURANCES OR -1 IF NO OCCURANCES
249: */
250: public int minimum(ArrayList a) {
251: if (log.isDebugEnabled()) {
252: log.debug("minimum(ArrayList " + a + ")");
253: }
254:
255: boolean firstNumber = true;
256: int tmp = 0;
257: int min = -1;
258:
259: Iterator i = a.iterator();
260: while (i.hasNext()) {
261: tmp = ((Integer) i.next()).intValue();
262:
263: if (firstNumber && (tmp != -1)) {
264: firstNumber = false;
265: min = tmp;
266: }
267:
268: if ((tmp != -1) && (tmp < min)) {
269: min = tmp;
270: }
271: }
272:
273: return min;
274: }
275:
276: /**
277: * test
278: *
279: * @param args
280: */
281: public static void main(String[] args) {
282: TextFormat tf = new TextFormat();
283: log
284: .info(tf
285: .formatText(
286: "www.cs.iupui.edu ui.edu dd dd dd:):-) ff telnet:dd dddddupui.edu",
287: "SMART",
288: ("http://oncourse.iu.edu/images/icons/")));
289: log.info(tf.formatText(
290: "http://www.iupui.edu:80 www.ui.edu :( ::-( ddd",
291: "SMART", ("http://oncourse.iu.edu/images/icons/")));
292:
293: log.debug(String.valueOf(System.identityHashCode(tf)));
294: }
295: }
|