1 9 package org.openuss.utility; 10 11 13 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 29 public class EnhydraPresentationUtility { 30 38 public static Element formatTextToHtml(String inputText, 39 HTMLObjectImpl inputPage, int width) { 40 inputText = formatTextToWidth(inputText, width); 43 44 String stringResult = new String (); 48 StringBuffer sb = new StringBuffer (inputText); 49 int index = 0; 50 51 Element topElement = inputPage.createElement("span"); 52 53 while (index < sb.length()) { 55 stringResult = stringResult + sb.charAt(index); 56 57 if (sb.charAt(index) == '\n') { 59 createElementNewLine(stringResult, inputPage, topElement); 60 stringResult = ""; 61 } 62 63 if (sb.charAt(index) == ' ') { 65 index = createElementSpace(index, sb, stringResult, inputPage, 66 topElement); 67 stringResult = ""; 68 } 69 70 if (isTextHttp(sb, index)) { 72 Vector result = getTextHttp(sb, index); 74 Integer indexInteger = (Integer ) result.get(0); 75 index = indexInteger.intValue(); 76 stringResult = (String ) result.get(1); 77 78 createElementHttp(stringResult, inputPage, topElement); 79 stringResult = ""; 80 } 81 82 83 index++; 85 } 86 87 88 topElement.appendChild(inputPage.createTextNode(stringResult)); 90 91 return topElement; 92 } 93 94 97 private static void createElementNewLine(String strInput, 98 HTMLObjectImpl inputPage, 99 Element topElement) { 100 strInput = strInput.trim(); 102 103 topElement.appendChild(inputPage.createTextNode(strInput)); 104 topElement.appendChild(inputPage.createElement("br")); 105 } 106 107 110 private static void createElementHttp(String strInput, 111 HTMLObjectImpl inputPage, 112 Element topElement) { 113 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 126 private static int createElementSpace(int index, StringBuffer sb, 127 String strInput, 128 HTMLObjectImpl inputPage, 129 Element topElement) { 130 strInput = strInput.trim(); 132 133 int spaceIndex = index + 1; 135 136 if ((spaceIndex < sb.length()) && (sb.charAt(spaceIndex) == ' ')) { 137 topElement.appendChild(inputPage.createTextNode(strInput)); 140 topElement.appendChild(inputPage.createCDATASection(" ")); 141 142 143 topElement.appendChild(inputPage.createCDATASection(" ")); 145 146 spaceIndex++; 147 148 while ((spaceIndex < sb.length()) && 150 (sb.charAt(spaceIndex) == ' ')) { 151 topElement.appendChild(inputPage.createCDATASection(" ")); 152 153 spaceIndex++; 154 } 155 } else { 156 topElement.appendChild(inputPage.createTextNode(strInput)); 158 topElement.appendChild(inputPage.createCDATASection(" ")); 159 } 160 161 162 spaceIndex = spaceIndex - 1; 164 165 return spaceIndex; 166 } 167 168 171 private static String formatTextToWidthForHtml(String inputText, int width) { 172 StringBuffer sb = new StringBuffer (inputText); 173 int counter = 0; 174 int emptySpacePos = -1; 175 176 for (int index = 0; index < sb.length(); index++) { 177 if (sb.charAt(index) == ' ') { 179 emptySpacePos = index; 182 } 183 184 if (counter == width) { 186 if (emptySpacePos == -1) { 191 sb.insert(index, '\n'); 194 } else { 195 sb.insert(emptySpacePos, '\n'); 197 198 if ((sb.charAt(emptySpacePos + 1)) == ' ') { 200 sb.replace(emptySpacePos + 1, emptySpacePos + 2, ""); 201 } 202 } 203 204 205 counter = 0; 207 emptySpacePos = -1; 208 } 209 210 if (sb.charAt(index) == '\n') { 212 counter = 0; 214 emptySpacePos = -1; 215 } 216 217 counter++; 218 } 219 220 return sb.toString(); 221 } 222 223 226 public static String formatTextToWidth(String inputText, int width) { 227 StringBuffer sb = new StringBuffer (inputText); 228 int counter = 0; 229 230 for (int index = 0; index < sb.length(); index++) { 231 if (sb.charAt(index) == ' ') { 233 counter = 0; 234 } 235 236 if (counter == width) { 238 sb.insert(index, ' '); 240 241 242 counter = 0; 244 } 245 246 counter++; 247 } 248 249 return sb.toString(); 250 } 251 252 255 private static boolean isTextHttp(StringBuffer inputStringBuffer, 256 int inputIndex) { 257 if (inputStringBuffer.charAt(inputIndex) != 'h') { 259 return false; 261 } else { 262 try { 264 String httpStr = new String (inputStringBuffer.substring( 265 inputIndex, inputIndex + 7)); 266 267 if (httpStr.equalsIgnoreCase("http://")) { 270 return true; 272 } else { 273 return false; 275 } 276 } catch (StringIndexOutOfBoundsException ex) { 277 return false; 279 } 280 } 281 } 282 283 286 private static Vector getTextHttp(StringBuffer inputStringBuffer, 287 int inputIndex) { 288 String httpStr = new String (); 289 int index = inputIndex; 290 boolean notEndHttp = true; 291 292 while ((index < inputStringBuffer.length()) && notEndHttp) { 294 httpStr = httpStr + inputStringBuffer.charAt(index); 295 296 if (inputStringBuffer.charAt(index) == ' ') { 297 httpStr = httpStr.trim(); 299 notEndHttp = false; 300 } 301 302 if (inputStringBuffer.charAt(index) == '\n') { 303 httpStr = httpStr.trim(); 305 notEndHttp = false; 306 } 307 308 index++; 309 } 310 311 if (index >= inputStringBuffer.length()) { 313 index = index - 1; 315 } else { 316 index = index - 2; 318 } 319 320 Vector result = new Vector(); 323 result.add(new Integer (index)); 324 result.add(httpStr); 325 326 return result; 327 } 328 329 339 public static void bodySetter(NodeList list, String bodyBgColor, 340 String bodyLink, String bodyALink, 341 String 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 365 public static void tableSetter(NodeList list, String 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 383 public static void cellSetter(NodeList list, String 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 403 public static void fontSetter(NodeList list, String fontFace, 404 String fontSize, String 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 422 public static Vector checkStartIndexAndCount(String startIndexStr, 423 String countStr, 424 int listAmount) { 425 int startIndex; 427 int count; 428 429 if ((startIndexStr == null) || startIndexStr.equals("")) { 430 startIndex = 0; 432 } else { 433 startIndex = Integer.valueOf(startIndexStr).intValue(); 434 } 435 436 if ((countStr == null) || countStr.equals("")) { 437 count = listAmount; 439 } else { 440 count = Integer.valueOf(countStr).intValue(); 441 } 442 443 Vector result = new Vector(); 447 result.addElement(new Integer (startIndex)); 448 result.addElement(new Integer (count)); 449 450 return result; 451 } 452 453 456 public static void showPreviousAndNextBar(String pageName, 457 String extraParam, 458 HTMLAnchorElement linkNext, 459 HTMLAnchorElement linkPrev, 460 int startIndex, int count, 461 int listAmount, 462 int currentListAmount) { 463 if (startIndex == 0) { 466 Node cellElement = linkPrev.getParentNode().getParentNode(); 468 cellElement.getParentNode().removeChild(cellElement); 469 } else { 470 int startIndexPrev = startIndex - listAmount; 472 473 474 linkPrev.setHref(pageName + "?StartIndex=" + 476 String.valueOf(startIndexPrev) + "&Count=" + 477 listAmount + extraParam); 478 } 479 480 if (currentListAmount != count) { 483 Node cellElement = linkNext.getParentNode().getParentNode(); 485 cellElement.getParentNode().removeChild(cellElement); 486 } else { 487 int startIndexNext = startIndex + listAmount; 489 490 491 linkNext.setHref(pageName + "?StartIndex=" + 493 String.valueOf(startIndexNext) + "&Count=" + 494 listAmount + extraParam); 495 } 496 } 497 } | Popular Tags |