KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openuss > utility > EnhydraPresentationUtility


1 /**
2  * Title: OpenUSS - Open Source University Support System
3  * Description: Utility for OpenUSS
4  * Copyright: Copyright (c) B. Lofi Dewanto
5  * Company: University of Muenster
6  * @author B. Lofi Dewanto
7  * @version 1.0
8  */

9 package org.openuss.utility;
10
11 //import com.lutris.xml.xmlc.*;
12

13 //import com.opensymphony.util.*;
14

15 import java.util.*;
16
17 import org.enhydra.xml.xmlc.html.*;
18
19 import org.w3c.dom.*;
20 import org.w3c.dom.html.*;
21
22
23 /**
24  * Utility for classes.
25  *
26  * @author B. Lofi Dewanto
27  * @version 1.0
28  */

29 public class EnhydraPresentationUtility {
30     /**
31      * Format a normal text to html.
32      * - Cut a long sentence in pieces
33      * - Change all the \n in <br>
34      * - Change all the empty space in " " and &nbsp;
35      * - Check for http://
36      * Return an element.
37      */

38     public static Element formatTextToHtml(String JavaDoc inputText,
39                                            HTMLObjectImpl inputPage, int width) {
40         // Just cut a long sentence in pieces
41
// and don't format them to fit the given width
42
inputText = formatTextToWidth(inputText, width);
43
44         // Set the message
45
// Change all the \n in <br>
46
// Change all the empty space in " "
47
String JavaDoc stringResult = new String JavaDoc();
48         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(inputText);
49         int index = 0;
50
51         Element topElement = inputPage.createElement("span");
52
53         // Go through the characters
54
while (index < sb.length()) {
55             stringResult = stringResult + sb.charAt(index);
56
57             // --- Check for \n new line ---
58
if (sb.charAt(index) == '\n') {
59                 createElementNewLine(stringResult, inputPage, topElement);
60                 stringResult = "";
61             }
62
63             // --- Check for space ---
64
if (sb.charAt(index) == ' ') {
65                 index = createElementSpace(index, sb, stringResult, inputPage,
66                                            topElement);
67                 stringResult = "";
68             }
69
70             // --- Check for http:// ---
71
if (isTextHttp(sb, index)) {
72                 // Yep, the next is a http://
73
Vector result = getTextHttp(sb, index);
74                 Integer JavaDoc indexInteger = (Integer JavaDoc) result.get(0);
75                 index = indexInteger.intValue();
76                 stringResult = (String JavaDoc) result.get(1);
77
78                 createElementHttp(stringResult, inputPage, topElement);
79                 stringResult = "";
80             }
81
82
83             // Increase the resultIndex
84
index++;
85         }
86
87
88         // Add the last string
89
topElement.appendChild(inputPage.createTextNode(stringResult));
90
91         return topElement;
92     }
93
94     /**
95      * Create a new line element.
96      */

97     private static void createElementNewLine(String JavaDoc strInput,
98                                              HTMLObjectImpl inputPage,
99                                              Element topElement) {
100         // Delete the last empty space
101
strInput = strInput.trim();
102
103         topElement.appendChild(inputPage.createTextNode(strInput));
104         topElement.appendChild(inputPage.createElement("br"));
105     }
106
107     /**
108      * Create an http element.
109      */

110     private static void createElementHttp(String JavaDoc strInput,
111                                           HTMLObjectImpl inputPage,
112                                           Element topElement) {
113         // Delete the last empty space
114
strInput = strInput.trim();
115
116         HTMLAnchorElement httpNode = (HTMLAnchorElement) inputPage.createElement(
117                                              "a");
118         httpNode.setHref(strInput);
119         httpNode.appendChild(inputPage.createTextNode(strInput));
120         topElement.appendChild(httpNode);
121     }
122
123     /**
124      * Create a space or blanks element.
125      */

126     private static int createElementSpace(int index, StringBuffer JavaDoc sb,
127                                           String JavaDoc strInput,
128                                           HTMLObjectImpl inputPage,
129                                           Element topElement) {
130         // Delete the last empty space
131
strInput = strInput.trim();
132
133         // Check whether the space " " is more than one?
134
int spaceIndex = index + 1;
135
136         if ((spaceIndex < sb.length()) && (sb.charAt(spaceIndex) == ' ')) {
137             // More than one spaces
138
// The first space will be changed with the " "
139
topElement.appendChild(inputPage.createTextNode(strInput));
140             topElement.appendChild(inputPage.createCDATASection(" "));
141
142
143             // The next spaces have to be changed with &nbsp;
144
topElement.appendChild(inputPage.createCDATASection("&nbsp;"));
145
146             spaceIndex++;
147
148             // The rest of the spaces have to be changed with &nbsp;
149
while ((spaceIndex < sb.length()) &&
150                    (sb.charAt(spaceIndex) == ' ')) {
151                 topElement.appendChild(inputPage.createCDATASection("&nbsp;"));
152
153                 spaceIndex++;
154             }
155         } else {
156             // Only one space
157
topElement.appendChild(inputPage.createTextNode(strInput));
158             topElement.appendChild(inputPage.createCDATASection(" "));
159         }
160
161
162         // Change the index to begin with
163
spaceIndex = spaceIndex - 1;
164
165         return spaceIndex;
166     }
167
168     /**
169      * Format a normal text in a given width for html formatting.
170      */

171     private static String JavaDoc formatTextToWidthForHtml(String JavaDoc inputText, int width) {
172         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(inputText);
173         int counter = 0;
174         int emptySpacePos = -1;
175
176         for (int index = 0; index < sb.length(); index++) {
177             // --- Save the last empty space ---
178
if (sb.charAt(index) == ' ') {
179                 // Save the position for later use
180
// in the break position
181
emptySpacePos = index;
182             }
183
184             // --- Check counter == width? ---
185
if (counter == width) {
186                 // Make a break ;-) at the right position
187
// Check first, whether this is in the middle of a word?
188
// Yes, go back to find an empty space and make a break
189
// at that point
190
if (emptySpacePos == -1) {
191                     // No empty space before...
192
// Make a break directly at the place
193
sb.insert(index, '\n');
194                 } else {
195                     // Okay, there was an empty space
196
sb.insert(emptySpacePos, '\n');
197
198                     // After a break delete the empty space
199
if ((sb.charAt(emptySpacePos + 1)) == ' ') {
200                         sb.replace(emptySpacePos + 1, emptySpacePos + 2, "");
201                     }
202                 }
203
204
205                 // Reset the counter
206
counter = 0;
207                 emptySpacePos = -1;
208             }
209
210             // --- Check for newline? ---
211
if (sb.charAt(index) == '\n') {
212                 // Reset the counter
213
counter = 0;
214                 emptySpacePos = -1;
215             }
216
217             counter++;
218         }
219
220         return sb.toString();
221     }
222
223     /**
224      * Format a normal text in a given width just by adding an empty space.
225      */

226     public static String JavaDoc formatTextToWidth(String JavaDoc inputText, int width) {
227         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(inputText);
228         int counter = 0;
229
230         for (int index = 0; index < sb.length(); index++) {
231             // --- The empty space means ok ---
232
if (sb.charAt(index) == ' ') {
233                 counter = 0;
234             }
235
236             // --- Check counter == width? ---
237
if (counter == width) {
238                 // Make a break ;-) at the right position
239
sb.insert(index, ' ');
240
241
242                 // Reset the counter
243
counter = 0;
244             }
245
246             counter++;
247         }
248
249         return sb.toString();
250     }
251
252     /**
253      * Check whether this "h" ends up with http://.
254      */

255     private static boolean isTextHttp(StringBuffer JavaDoc inputStringBuffer,
256                                       int inputIndex) {
257         // Check for the "h"
258
if (inputStringBuffer.charAt(inputIndex) != 'h') {
259             // Not h!
260
return false;
261         } else {
262             // This is h and check further
263
try {
264                 String JavaDoc httpStr = new String JavaDoc(inputStringBuffer.substring(
265                                                     inputIndex, inputIndex + 7));
266
267                 // Check for the rest if no error... until this line
268
// You can be sure that the line is more or equal to 7 digits
269
if (httpStr.equalsIgnoreCase("http://")) {
270                     // Yes http://
271
return true;
272                 } else {
273                     // Nope
274
return false;
275                 }
276             } catch (StringIndexOutOfBoundsException JavaDoc ex) {
277                 // No chars any longer! This can't be http://
278
return false;
279             }
280         }
281     }
282
283     /**
284      * Get the http://.
285      */

286     private static Vector getTextHttp(StringBuffer JavaDoc inputStringBuffer,
287                                       int inputIndex) {
288         String JavaDoc httpStr = new String JavaDoc();
289         int index = inputIndex;
290         boolean notEndHttp = true;
291
292         // Get the http until an empty space
293
while ((index < inputStringBuffer.length()) && notEndHttp) {
294             httpStr = httpStr + inputStringBuffer.charAt(index);
295
296             if (inputStringBuffer.charAt(index) == ' ') {
297                 // End of http
298
httpStr = httpStr.trim();
299                 notEndHttp = false;
300             }
301
302             if (inputStringBuffer.charAt(index) == '\n') {
303                 // End of http
304
httpStr = httpStr.trim();
305                 notEndHttp = false;
306             }
307
308             index++;
309         }
310
311         // Check for the end of buffer?
312
if (index >= inputStringBuffer.length()) {
313             // End
314
index = index - 1;
315         } else {
316             // Not yet
317
index = index - 2;
318         }
319
320         // Result1: int
321
// Result2: String
322
Vector result = new Vector();
323         result.add(new Integer JavaDoc(index));
324         result.add(httpStr);
325
326         return result;
327     }
328
329     /**
330      * Here the BODY-tag is design, first removing old attribs, then set new
331      * attribs
332      *
333      * @param list List of all nodes found by the NAME-param
334      * @param bodyBgColor
335      * @param bodyLink
336      * @param bodyALink
337      * @param bodyVLink
338      */

339     public static void bodySetter(NodeList list, String JavaDoc bodyBgColor,
340                                   String JavaDoc bodyLink, String JavaDoc bodyALink,
341                                   String JavaDoc bodyVLink) {
342         int length = list.getLength();
343         int i;
344
345         for (i = 0; i < length; i++) {
346             HTMLElement element = (HTMLElement) list.item(i);
347             element.removeAttribute("BGCOLOR");
348             element.removeAttribute("LINK");
349             element.removeAttribute("ALINK");
350             element.removeAttribute("VLINK");
351             element.setAttribute("BGCOLOR", bodyBgColor);
352             element.setAttribute("LINK", bodyLink);
353             element.setAttribute("ALINK", bodyALink);
354             element.setAttribute("VLINK", bodyVLink);
355         }
356     }
357
358     /**
359      * Here the TABLE-tag is design, first removing old attribs, then set new
360      * attribs
361      *
362      * @param list List of all nodes found by the NAME-param
363      * @param tableBgColor
364      */

365     public static void tableSetter(NodeList list, String JavaDoc tableBgColor) {
366         int length = list.getLength();
367         int i;
368
369         for (i = 0; i < length; i++) {
370             HTMLElement element = (HTMLElement) list.item(i);
371             element.removeAttribute("BGCOLOR");
372             element.setAttribute("BGCOLOR", tableBgColor);
373         }
374     }
375
376     /**
377      * Here the TD/TR-tag is design, first removing old attribs, then set new
378      * attribs
379      *
380      * @param list List of all nodes found by the NAME-param
381      * @param tableBgColor
382      */

383     public static void cellSetter(NodeList list, String JavaDoc tableBgColor) {
384         int length = list.getLength();
385         int i;
386
387         for (i = 0; i < length; i++) {
388             HTMLElement element = (HTMLElement) list.item(i);
389             element.removeAttribute("BGCOLOR");
390             element.setAttribute("BGCOLOR", tableBgColor);
391         }
392     }
393
394     /**
395      * Here the FONT-tag is design, first removing old attribs, then set new
396      * attribs
397      *
398      * @param list List of all nodes found by the NAME-param
399      * @param fontFace
400      * @param fontSize
401      * @param fontColor
402      */

403     public static void fontSetter(NodeList list, String JavaDoc fontFace,
404                                   String JavaDoc fontSize, String JavaDoc fontColor) {
405         int length = list.getLength();
406         int i;
407
408         for (i = 0; i < length; i++) {
409             HTMLElement element = (HTMLElement) list.item(i);
410             element.removeAttribute("FACE");
411             element.removeAttribute("SIZE");
412             element.removeAttribute("COLOR");
413             element.setAttribute("FACE", fontFace);
414             element.setAttribute("SIZE", fontSize);
415             element.setAttribute("COLOR", fontColor);
416         }
417     }
418
419     /**
420      * Checks the startIndex and Count.
421      */

422     public static Vector checkStartIndexAndCount(String JavaDoc startIndexStr,
423                                                  String JavaDoc countStr,
424                                                  int listAmount) {
425         // Definition
426
int startIndex;
427         int count;
428
429         if ((startIndexStr == null) || startIndexStr.equals("")) {
430             // Use default value
431
startIndex = 0;
432         } else {
433             startIndex = Integer.valueOf(startIndexStr).intValue();
434         }
435
436         if ((countStr == null) || countStr.equals("")) {
437             // Use default value
438
count = listAmount;
439         } else {
440             count = Integer.valueOf(countStr).intValue();
441         }
442
443         // Save the result in a vector:
444
// - startIndex
445
// - count
446
Vector result = new Vector();
447         result.addElement(new Integer JavaDoc(startIndex));
448         result.addElement(new Integer JavaDoc(count));
449
450         return result;
451     }
452
453     /**
454      * Show previous and next bar.
455      */

456     public static void showPreviousAndNextBar(String JavaDoc pageName,
457                                               String JavaDoc extraParam,
458                                               HTMLAnchorElement linkNext,
459                                               HTMLAnchorElement linkPrev,
460                                               int startIndex, int count,
461                                               int listAmount,
462                                               int currentListAmount) {
463         // Don't show previous if:
464
// - The startIndex variable = 0
465
if (startIndex == 0) {
466             // Delete the cell in the table
467
Node cellElement = linkPrev.getParentNode().getParentNode();
468             cellElement.getParentNode().removeChild(cellElement);
469         } else {
470             // Calculate the startIndex for Previous
471
int startIndexPrev = startIndex - listAmount;
472
473
474             // Show the link before
475
linkPrev.setHref(pageName + "?StartIndex=" +
476                              String.valueOf(startIndexPrev) + "&Count=" +
477                              listAmount + extraParam);
478         }
479
480         // Don't show next if:
481
// - The real amount of the list != the count variable
482
if (currentListAmount != count) {
483             // Delete the cell in the table
484
Node cellElement = linkNext.getParentNode().getParentNode();
485             cellElement.getParentNode().removeChild(cellElement);
486         } else {
487             // Calculate the startIndex for Next
488
int startIndexNext = startIndex + listAmount;
489
490
491             // Show the link after
492
linkNext.setHref(pageName + "?StartIndex=" +
493                              String.valueOf(startIndexNext) + "&Count=" +
494                              listAmount + extraParam);
495         }
496     }
497 }
Popular Tags