1 23 24 package com.sun.enterprise.tools.guiframework.util; 25 26 import com.iplanet.jato.RequestContext; 27 import com.iplanet.jato.RequestManager; 28 import com.iplanet.jato.view.DisplayField; 29 import com.iplanet.jato.view.View; 30 import com.iplanet.jato.view.ViewBean; 31 import com.iplanet.jato.view.ContainerView; 32 33 import com.sun.enterprise.tools.guiframework.exception.FrameworkException; 34 import com.sun.enterprise.tools.guiframework.view.DescriptorViewManager; 35 import com.sun.enterprise.tools.guiframework.view.descriptors.ViewDescriptor; 36 37 import java.util.ArrayList ; 38 import java.util.Iterator ; 39 import java.util.HashMap ; 40 import java.util.List ; 41 import java.util.Map ; 42 import java.util.Stack ; 43 44 45 public class Util { 46 47 85 public static Object replaceVariableWithAttribute(RequestContext ctx, 86 ViewDescriptor vd, String string, String startToken, 87 String typeDelim, String endToken) { 88 89 int stringLen = string.length(); 90 int delimIndex; 91 int endIndex, matchingIndex; 92 int parenSemi; 93 int startTokenLen = startToken.length(); 94 int delimLen = typeDelim.length(); 95 int endTokenLen = endToken.length(); 96 boolean expressionIsWholeString = false; 97 char firstEndChar = SUB_END.charAt(0); 98 char firstDelimChar = SUB_TYPE_DELIM.charAt(0); 99 char currChar; 100 String type; 101 Object variable; 102 103 for (int startIndex = string.lastIndexOf(startToken); startIndex != -1; 104 startIndex = string.lastIndexOf(startToken, startIndex-1)) { 105 106 delimIndex = string.indexOf(typeDelim, startIndex+startTokenLen); 108 if (delimIndex == -1) { 109 continue; 110 } 111 112 parenSemi = 0; 114 endIndex = -1; 115 for (int curr = delimIndex+delimLen; curr<stringLen; ) { 117 currChar = string.charAt(curr); 119 if ((currChar == firstDelimChar) && typeDelim.equals(string.substring(curr, curr+delimLen))) { 120 parenSemi++; 122 curr += delimLen; 123 continue; 124 } 125 if ((currChar == firstEndChar) && endToken.equals(string.substring(curr, curr+endTokenLen))) { 126 parenSemi--; 127 if (parenSemi < 0) { 128 endIndex = curr; 130 break; 131 } 132 curr += endTokenLen; 134 continue; 135 } 136 curr++; 137 } 138 if (endIndex == -1) { 139 continue; 141 } 142 143 156 157 if ((startIndex == 0) && (endIndex == string.lastIndexOf(endToken)) && (string.endsWith(endToken))) { 162 expressionIsWholeString = true; 164 } 165 166 type = string.substring(startIndex+startTokenLen, delimIndex); 168 DataSource ds = (DataSource)_dataSourceMap.get(type); 169 if (ds == null) { 170 throw new FrameworkException("Invalid type '"+type+ 171 "' in attribute value: '"+string+"'."); 172 } 173 174 variable = string.substring(delimIndex+delimLen, endIndex); 176 177 variable = ds.getValue(ctx, vd, (String )variable); 179 if (expressionIsWholeString) { 180 return variable; 181 } 182 183 string = string.substring(0, startIndex) + ((variable == null) ? "" : variable.toString()) + 186 string.substring(endIndex+endTokenLen); stringLen = string.length(); 188 } 189 190 return string; 192 } 193 194 195 199 public static Object replaceVariablesWithAttributes(Object value, ViewDescriptor vd) { 200 if (value == null) { 201 return null; 202 } 203 return replaceVariablesWithAttributes( 204 RequestManager.getRequestContext(), vd, value); 205 } 206 207 208 212 public static Object replaceVariablesWithAttributes(RequestContext ctx, ViewDescriptor vd, Object value) { 213 if (value == null) { 214 return null; 215 } 216 if (value instanceof String ) { 217 value = Util.replaceVariableWithAttribute( 218 ctx, 219 vd, 220 (String )value, 221 SUB_START, 222 SUB_TYPE_DELIM, 223 SUB_END); 224 } else if (value instanceof List ) { 225 List list = ((List )value); 227 int size = list.size(); 228 List newList = new ArrayList (size); 229 Iterator it = list.iterator(); 230 while (it.hasNext()) { 231 newList.add(replaceVariablesWithAttributes(ctx, vd, it.next())); 232 } 233 return newList; 234 } 235 return value; 236 } 237 238 239 247 public static boolean hasValue(String str) { 248 if (str == null) { 249 return false; 250 } 251 return str.length() > 0; 252 } 253 254 255 public static String replace(String src, String toReplace, String replaceWith) { 256 String str = src; 257 while (true) { 258 int startIndex = str.indexOf(toReplace); 259 if (startIndex < 0) 260 break; 261 int length = str.length(); 262 int endIndex = startIndex + toReplace.length(); 263 str = str.substring(0, startIndex) + replaceWith + str.substring(endIndex, length); 264 } 265 return str; 266 } 267 268 269 274 public static ViewBean getParentViewBean(View view) { 275 while (view != null) { 276 if (view instanceof ViewBean) { 277 if (view.getParent() == null) { 279 return (ViewBean)view; 280 } 281 } 282 view = view.getParent(); 283 } 284 return null; 285 } 286 287 288 297 public interface DataSource { 298 Object getValue(RequestContext ctx, ViewDescriptor vd, String key); 299 } 300 301 302 307 public static class AttributeDataSource implements DataSource { 308 public AttributeDataSource() { 309 } 310 311 public Object getValue(RequestContext ctx, ViewDescriptor vd, String key) { 312 return ctx.getRequest().getAttribute(key); 313 } 314 } 315 316 317 322 public static class PageSessionDataSource implements DataSource { 323 public PageSessionDataSource() { 324 } 325 326 public Object getValue(RequestContext ctx, ViewDescriptor vd, String key) { 327 while (vd.getParent() != null) { 328 vd = vd.getParent(); 329 } 330 return ((ViewBean)vd.getView(ctx)).getPageSessionAttribute(key); 331 } 332 } 333 334 339 public static class RequestParameterDataSource implements DataSource { 340 public RequestParameterDataSource() { 341 } 342 343 public Object getValue(RequestContext ctx, ViewDescriptor vd, String key) { 344 return ctx.getRequest().getParameter(key); 345 } 346 } 347 348 349 357 public static class ParameterDataSource implements DataSource { 358 public ParameterDataSource() { 359 } 360 361 public Object getValue(RequestContext ctx, ViewDescriptor vd, String key) { 362 Object value = null; 363 for (; (value == null) && (vd != null); vd = vd.getParent()) { 364 value = vd.getParameter(key); 365 } 366 return value; 367 } 368 } 369 370 371 378 public static class EscapeDataSource implements DataSource { 379 public EscapeDataSource() { 380 } 381 382 public Object getValue(RequestContext ctx, ViewDescriptor vd, String key) { 383 return key; 384 } 385 } 386 387 388 393 public static class SessionDataSource implements DataSource { 394 public SessionDataSource() { 395 } 396 397 public Object getValue(RequestContext ctx, ViewDescriptor vd, String key) { 398 return ctx.getRequest().getSession().getAttribute(key); 399 } 400 } 401 402 403 411 public static class DisplayFieldDataSource implements DataSource { 412 public DisplayFieldDataSource() { 413 } 414 415 public Object getValue(RequestContext ctx, ViewDescriptor vd, String key) { 416 while (vd != null) { 417 View view = vd.getView(ctx); 418 if (view instanceof ContainerView) { 419 DisplayField child = null; 420 try { 421 child = (DisplayField) ((ContainerView) view).getChild(key); 422 } catch (Exception ex) { 423 } 425 if (child != null) { 426 return child.getValue(); 427 } 428 } 429 vd = vd.getParent(); 430 } 431 return null; 432 } 433 } 434 435 449 public static class ThisDataSource implements DataSource { 450 public ThisDataSource() { 451 } 452 453 public Object getValue(RequestContext ctx, ViewDescriptor vd, String key) { 454 Object value = null; 455 456 if ((key.equalsIgnoreCase(THIS_VIEW)) || (key.length() == 0)) { 457 value = vd.getView(ctx); 458 } else if (key.equalsIgnoreCase(THIS_VIEW_DESCRIPTOR)) { 459 value = vd; 460 } else if (key.equalsIgnoreCase(THIS_PARENT_VIEW_DESCRIPTOR)) { 461 value = vd.getParent(); 462 } else if (key.equalsIgnoreCase(THIS_TOP_VIEW_DESCRIPTOR)) { 463 while (vd.getParent() != null) { 464 vd = vd.getParent(); 465 } 466 value = vd; 467 } else if (key.equalsIgnoreCase(THIS_PARENT_VIEW)) { 468 vd = vd.getParent(); 469 value = (vd == null) ? null : vd.getView(ctx); 470 } else if (key.equalsIgnoreCase(THIS_VIEWBEAN)) { 471 while (vd.getParent() != null) { 473 vd = vd.getParent(); 474 } 475 476 value = vd.getView(ctx); 478 479 while (((View)value).getParent() != null) { 481 value = ((View)value).getParent(); 482 } 483 } else if (key.equalsIgnoreCase(THIS_VIEW_BEAN_NAME)) { 484 while (vd.getParent() != null) { 485 vd = vd.getParent(); 486 } 487 value=vd.getName(); 488 489 490 500 } else if (key.equalsIgnoreCase(THIS_DEFAULT_MODEL)) { 501 View view = vd.getView(ctx); 503 while ((view != null) && !(view instanceof ContainerView)) { 504 view = view.getParent(); 505 } 506 value = ((ContainerView)view).getDefaultModel(); 507 } else { 508 throw new FrameworkException("'"+key+"' is not valid in $this(" 509 +key+"). This was found on '"+vd.getName()+"'."); 510 } 511 512 return value; 513 } 514 515 518 public static final String THIS_VIEW = "View"; 519 520 521 525 public static final String THIS_PARENT_VIEW = "ParentView"; 526 527 528 532 public static final String THIS_VIEWBEAN = "ViewBean"; 533 534 535 539 public static final String THIS_VIEW_DESCRIPTOR = "ViewDescriptor"; 540 541 542 546 public static final String THIS_PARENT_VIEW_DESCRIPTOR= "ParentViewDescriptor"; 547 548 549 554 public static final String THIS_TOP_VIEW_DESCRIPTOR= "TopViewDescriptor"; 555 556 557 561 public static final String THIS_VIEW_BEAN_NAME = "ViewBeanName"; 562 563 564 568 public static final String THIS_DEFAULT_MODEL = "DefaultModel"; 569 } 570 571 public static void main(String args[]) { 572 System.out.println(""+replaceVariableWithAttribute(null, null, "$escape($escape(ViewDescriptor))", "$", "(", ")")); 573 System.out.println(""+replaceVariableWithAttribute(null, null, "$escape($escape(EEPersistenceManager))", "$", "(", ")")); 574 575 System.out.println(""+replaceVariableWithAttribute(null, null, "$es$cape$escape(EEPersistenceManager))", "$", "(", ")")); 576 System.out.println(""+replaceVariableWithAttribute(null, null, "$escape($escapeEEP$ersistenceManager))", "$", "(", ")")); 577 578 System.out.println(""+replaceVariableWithAttribute(null, null, "$escape($escape(EEPersistenceManager)))", "$", "(", ")")); 579 System.out.println(""+replaceVariableWithAttribute(null, null, "$escape($escape(EEPersistenceManager())", "$", "(", ")")); 580 System.out.println(""+replaceVariableWithAttribute(null, null, "$escape($escape($escape(EEPersistenceManager()))==$escape(EEPersistenceManager()))", "$", "(", ")")); 581 System.out.println(""+replaceVariableWithAttribute(null, null, "$escape($escape($escape(EEPersistenceManager()))==$escape(EEPersistenceManager()))", "$", "(", ")")); 582 for (int x=0; x<100000; x++) { 583 System.out.println(""+replaceVariableWithAttribute(null, null, "$escape($escape(EEPers"+x+"istenceManager()))==$escape(EEPersistenceManager())", "$", "(", ")")); 584 } 585 } 586 587 590 public static Map _dataSourceMap = new HashMap (); 591 592 593 597 public static final String ATTRIBUTE = "attribute"; 598 599 600 604 public static final String PAGE_SESSION = "pageSession"; 605 606 607 611 public static final String PARAMETER = "parameter"; 612 613 614 618 public static final String SESSION = "session"; 619 620 621 626 public static final String REQUEST_PARAMETER = "requestParameter"; 627 628 629 633 public static final String DISPLAY = "display"; 634 635 636 643 public static final String THIS = "this"; 644 645 646 651 public static final String ESCAPE = "escape"; 652 653 654 static { 655 AttributeDataSource attrDS = new AttributeDataSource(); 656 _dataSourceMap.put(ATTRIBUTE, attrDS); 657 _dataSourceMap.put("", attrDS); 658 _dataSourceMap.put(PAGE_SESSION, new PageSessionDataSource()); 659 _dataSourceMap.put(PARAMETER, new ParameterDataSource()); 660 _dataSourceMap.put(SESSION, new SessionDataSource()); 661 _dataSourceMap.put(REQUEST_PARAMETER, new RequestParameterDataSource()); 662 _dataSourceMap.put(DISPLAY, new DisplayFieldDataSource()); 663 _dataSourceMap.put(THIS, new ThisDataSource()); 664 _dataSourceMap.put(ESCAPE, new EscapeDataSource()); 665 } 666 667 668 671 public static final String SUB_START = "$"; 672 673 674 678 public static final String SUB_TYPE_DELIM = "("; 679 680 681 685 public static final String SUB_END = ")"; 686 } 687 | Popular Tags |