001: /**
002: * Title: OpenUSS - Open Source University Support System
003: * Description: Utility for OpenUSS
004: * Copyright: Copyright (c) B. Lofi Dewanto
005: * Company: University of Muenster
006: * @author B. Lofi Dewanto
007: * @version 1.0
008: */package org.openuss.utility;
009:
010: //import com.lutris.xml.xmlc.*;
011:
012: //import com.opensymphony.util.*;
013:
014: import java.util.*;
015:
016: import org.enhydra.xml.xmlc.html.*;
017:
018: import org.w3c.dom.*;
019: import org.w3c.dom.html.*;
020:
021: /**
022: * Utility for classes.
023: *
024: * @author B. Lofi Dewanto
025: * @version 1.0
026: */
027: public class EnhydraPresentationUtility {
028: /**
029: * Format a normal text to html.
030: * - Cut a long sentence in pieces
031: * - Change all the \n in <br>
032: * - Change all the empty space in " " and
033: * - Check for http://
034: * Return an element.
035: */
036: public static Element formatTextToHtml(String inputText,
037: HTMLObjectImpl inputPage, int width) {
038: // Just cut a long sentence in pieces
039: // and don't format them to fit the given width
040: inputText = formatTextToWidth(inputText, width);
041:
042: // Set the message
043: // Change all the \n in <br>
044: // Change all the empty space in " "
045: String stringResult = new String();
046: StringBuffer sb = new StringBuffer(inputText);
047: int index = 0;
048:
049: Element topElement = inputPage.createElement("span");
050:
051: // Go through the characters
052: while (index < sb.length()) {
053: stringResult = stringResult + sb.charAt(index);
054:
055: // --- Check for \n new line ---
056: if (sb.charAt(index) == '\n') {
057: createElementNewLine(stringResult, inputPage,
058: topElement);
059: stringResult = "";
060: }
061:
062: // --- Check for space ---
063: if (sb.charAt(index) == ' ') {
064: index = createElementSpace(index, sb, stringResult,
065: inputPage, topElement);
066: stringResult = "";
067: }
068:
069: // --- Check for http:// ---
070: if (isTextHttp(sb, index)) {
071: // Yep, the next is a http://
072: Vector result = getTextHttp(sb, index);
073: Integer indexInteger = (Integer) result.get(0);
074: index = indexInteger.intValue();
075: stringResult = (String) result.get(1);
076:
077: createElementHttp(stringResult, inputPage, topElement);
078: stringResult = "";
079: }
080:
081: // Increase the resultIndex
082: index++;
083: }
084:
085: // Add the last string
086: topElement.appendChild(inputPage.createTextNode(stringResult));
087:
088: return topElement;
089: }
090:
091: /**
092: * Create a new line element.
093: */
094: private static void createElementNewLine(String strInput,
095: HTMLObjectImpl inputPage, Element topElement) {
096: // Delete the last empty space
097: strInput = strInput.trim();
098:
099: topElement.appendChild(inputPage.createTextNode(strInput));
100: topElement.appendChild(inputPage.createElement("br"));
101: }
102:
103: /**
104: * Create an http element.
105: */
106: private static void createElementHttp(String strInput,
107: HTMLObjectImpl inputPage, Element topElement) {
108: // Delete the last empty space
109: strInput = strInput.trim();
110:
111: HTMLAnchorElement httpNode = (HTMLAnchorElement) inputPage
112: .createElement("a");
113: httpNode.setHref(strInput);
114: httpNode.appendChild(inputPage.createTextNode(strInput));
115: topElement.appendChild(httpNode);
116: }
117:
118: /**
119: * Create a space or blanks element.
120: */
121: private static int createElementSpace(int index, StringBuffer sb,
122: String strInput, HTMLObjectImpl inputPage,
123: Element topElement) {
124: // Delete the last empty space
125: strInput = strInput.trim();
126:
127: // Check whether the space " " is more than one?
128: int spaceIndex = index + 1;
129:
130: if ((spaceIndex < sb.length())
131: && (sb.charAt(spaceIndex) == ' ')) {
132: // More than one spaces
133: // The first space will be changed with the " "
134: topElement.appendChild(inputPage.createTextNode(strInput));
135: topElement.appendChild(inputPage.createCDATASection(" "));
136:
137: // The next spaces have to be changed with
138: topElement.appendChild(inputPage
139: .createCDATASection(" "));
140:
141: spaceIndex++;
142:
143: // The rest of the spaces have to be changed with
144: while ((spaceIndex < sb.length())
145: && (sb.charAt(spaceIndex) == ' ')) {
146: topElement.appendChild(inputPage
147: .createCDATASection(" "));
148:
149: spaceIndex++;
150: }
151: } else {
152: // Only one space
153: topElement.appendChild(inputPage.createTextNode(strInput));
154: topElement.appendChild(inputPage.createCDATASection(" "));
155: }
156:
157: // Change the index to begin with
158: spaceIndex = spaceIndex - 1;
159:
160: return spaceIndex;
161: }
162:
163: /**
164: * Format a normal text in a given width for html formatting.
165: */
166: private static String formatTextToWidthForHtml(String inputText,
167: int width) {
168: StringBuffer sb = new StringBuffer(inputText);
169: int counter = 0;
170: int emptySpacePos = -1;
171:
172: for (int index = 0; index < sb.length(); index++) {
173: // --- Save the last empty space ---
174: if (sb.charAt(index) == ' ') {
175: // Save the position for later use
176: // in the break position
177: emptySpacePos = index;
178: }
179:
180: // --- Check counter == width? ---
181: if (counter == width) {
182: // Make a break ;-) at the right position
183: // Check first, whether this is in the middle of a word?
184: // Yes, go back to find an empty space and make a break
185: // at that point
186: if (emptySpacePos == -1) {
187: // No empty space before...
188: // Make a break directly at the place
189: sb.insert(index, '\n');
190: } else {
191: // Okay, there was an empty space
192: sb.insert(emptySpacePos, '\n');
193:
194: // After a break delete the empty space
195: if ((sb.charAt(emptySpacePos + 1)) == ' ') {
196: sb.replace(emptySpacePos + 1,
197: emptySpacePos + 2, "");
198: }
199: }
200:
201: // Reset the counter
202: counter = 0;
203: emptySpacePos = -1;
204: }
205:
206: // --- Check for newline? ---
207: if (sb.charAt(index) == '\n') {
208: // Reset the counter
209: counter = 0;
210: emptySpacePos = -1;
211: }
212:
213: counter++;
214: }
215:
216: return sb.toString();
217: }
218:
219: /**
220: * Format a normal text in a given width just by adding an empty space.
221: */
222: public static String formatTextToWidth(String inputText, int width) {
223: StringBuffer sb = new StringBuffer(inputText);
224: int counter = 0;
225:
226: for (int index = 0; index < sb.length(); index++) {
227: // --- The empty space means ok ---
228: if (sb.charAt(index) == ' ') {
229: counter = 0;
230: }
231:
232: // --- Check counter == width? ---
233: if (counter == width) {
234: // Make a break ;-) at the right position
235: sb.insert(index, ' ');
236:
237: // Reset the counter
238: counter = 0;
239: }
240:
241: counter++;
242: }
243:
244: return sb.toString();
245: }
246:
247: /**
248: * Check whether this "h" ends up with http://.
249: */
250: private static boolean isTextHttp(StringBuffer inputStringBuffer,
251: int inputIndex) {
252: // Check for the "h"
253: if (inputStringBuffer.charAt(inputIndex) != 'h') {
254: // Not h!
255: return false;
256: } else {
257: // This is h and check further
258: try {
259: String httpStr = new String(inputStringBuffer
260: .substring(inputIndex, inputIndex + 7));
261:
262: // Check for the rest if no error... until this line
263: // You can be sure that the line is more or equal to 7 digits
264: if (httpStr.equalsIgnoreCase("http://")) {
265: // Yes http://
266: return true;
267: } else {
268: // Nope
269: return false;
270: }
271: } catch (StringIndexOutOfBoundsException ex) {
272: // No chars any longer! This can't be http://
273: return false;
274: }
275: }
276: }
277:
278: /**
279: * Get the http://.
280: */
281: private static Vector getTextHttp(StringBuffer inputStringBuffer,
282: int inputIndex) {
283: String httpStr = new String();
284: int index = inputIndex;
285: boolean notEndHttp = true;
286:
287: // Get the http until an empty space
288: while ((index < inputStringBuffer.length()) && notEndHttp) {
289: httpStr = httpStr + inputStringBuffer.charAt(index);
290:
291: if (inputStringBuffer.charAt(index) == ' ') {
292: // End of http
293: httpStr = httpStr.trim();
294: notEndHttp = false;
295: }
296:
297: if (inputStringBuffer.charAt(index) == '\n') {
298: // End of http
299: httpStr = httpStr.trim();
300: notEndHttp = false;
301: }
302:
303: index++;
304: }
305:
306: // Check for the end of buffer?
307: if (index >= inputStringBuffer.length()) {
308: // End
309: index = index - 1;
310: } else {
311: // Not yet
312: index = index - 2;
313: }
314:
315: // Result1: int
316: // Result2: String
317: Vector result = new Vector();
318: result.add(new Integer(index));
319: result.add(httpStr);
320:
321: return result;
322: }
323:
324: /**
325: * Here the BODY-tag is design, first removing old attribs, then set new
326: * attribs
327: *
328: * @param list List of all nodes found by the NAME-param
329: * @param bodyBgColor
330: * @param bodyLink
331: * @param bodyALink
332: * @param bodyVLink
333: */
334: public static void bodySetter(NodeList list, String bodyBgColor,
335: String bodyLink, String bodyALink, String bodyVLink) {
336: int length = list.getLength();
337: int i;
338:
339: for (i = 0; i < length; i++) {
340: HTMLElement element = (HTMLElement) list.item(i);
341: element.removeAttribute("BGCOLOR");
342: element.removeAttribute("LINK");
343: element.removeAttribute("ALINK");
344: element.removeAttribute("VLINK");
345: element.setAttribute("BGCOLOR", bodyBgColor);
346: element.setAttribute("LINK", bodyLink);
347: element.setAttribute("ALINK", bodyALink);
348: element.setAttribute("VLINK", bodyVLink);
349: }
350: }
351:
352: /**
353: * Here the TABLE-tag is design, first removing old attribs, then set new
354: * attribs
355: *
356: * @param list List of all nodes found by the NAME-param
357: * @param tableBgColor
358: */
359: public static void tableSetter(NodeList list, String tableBgColor) {
360: int length = list.getLength();
361: int i;
362:
363: for (i = 0; i < length; i++) {
364: HTMLElement element = (HTMLElement) list.item(i);
365: element.removeAttribute("BGCOLOR");
366: element.setAttribute("BGCOLOR", tableBgColor);
367: }
368: }
369:
370: /**
371: * Here the TD/TR-tag is design, first removing old attribs, then set new
372: * attribs
373: *
374: * @param list List of all nodes found by the NAME-param
375: * @param tableBgColor
376: */
377: public static void cellSetter(NodeList list, String tableBgColor) {
378: int length = list.getLength();
379: int i;
380:
381: for (i = 0; i < length; i++) {
382: HTMLElement element = (HTMLElement) list.item(i);
383: element.removeAttribute("BGCOLOR");
384: element.setAttribute("BGCOLOR", tableBgColor);
385: }
386: }
387:
388: /**
389: * Here the FONT-tag is design, first removing old attribs, then set new
390: * attribs
391: *
392: * @param list List of all nodes found by the NAME-param
393: * @param fontFace
394: * @param fontSize
395: * @param fontColor
396: */
397: public static void fontSetter(NodeList list, String fontFace,
398: String fontSize, String fontColor) {
399: int length = list.getLength();
400: int i;
401:
402: for (i = 0; i < length; i++) {
403: HTMLElement element = (HTMLElement) list.item(i);
404: element.removeAttribute("FACE");
405: element.removeAttribute("SIZE");
406: element.removeAttribute("COLOR");
407: element.setAttribute("FACE", fontFace);
408: element.setAttribute("SIZE", fontSize);
409: element.setAttribute("COLOR", fontColor);
410: }
411: }
412:
413: /**
414: * Checks the startIndex and Count.
415: */
416: public static Vector checkStartIndexAndCount(String startIndexStr,
417: String countStr, int listAmount) {
418: // Definition
419: int startIndex;
420: int count;
421:
422: if ((startIndexStr == null) || startIndexStr.equals("")) {
423: // Use default value
424: startIndex = 0;
425: } else {
426: startIndex = Integer.valueOf(startIndexStr).intValue();
427: }
428:
429: if ((countStr == null) || countStr.equals("")) {
430: // Use default value
431: count = listAmount;
432: } else {
433: count = Integer.valueOf(countStr).intValue();
434: }
435:
436: // Save the result in a vector:
437: // - startIndex
438: // - count
439: Vector result = new Vector();
440: result.addElement(new Integer(startIndex));
441: result.addElement(new Integer(count));
442:
443: return result;
444: }
445:
446: /**
447: * Show previous and next bar.
448: */
449: public static void showPreviousAndNextBar(String pageName,
450: String extraParam, HTMLAnchorElement linkNext,
451: HTMLAnchorElement linkPrev, int startIndex, int count,
452: int listAmount, int currentListAmount) {
453: // Don't show previous if:
454: // - The startIndex variable = 0
455: if (startIndex == 0) {
456: // Delete the cell in the table
457: Node cellElement = linkPrev.getParentNode().getParentNode();
458: cellElement.getParentNode().removeChild(cellElement);
459: } else {
460: // Calculate the startIndex for Previous
461: int startIndexPrev = startIndex - listAmount;
462:
463: // Show the link before
464: linkPrev.setHref(pageName + "?StartIndex="
465: + String.valueOf(startIndexPrev) + "&Count="
466: + listAmount + extraParam);
467: }
468:
469: // Don't show next if:
470: // - The real amount of the list != the count variable
471: if (currentListAmount != count) {
472: // Delete the cell in the table
473: Node cellElement = linkNext.getParentNode().getParentNode();
474: cellElement.getParentNode().removeChild(cellElement);
475: } else {
476: // Calculate the startIndex for Next
477: int startIndexNext = startIndex + listAmount;
478:
479: // Show the link after
480: linkNext.setHref(pageName + "?StartIndex="
481: + String.valueOf(startIndexNext) + "&Count="
482: + listAmount + extraParam);
483: }
484: }
485: }
|