1 23 24 package com.sun.enterprise.tools.admingui.taglib; 25 26 import java.io.*; 27 import java.lang.reflect.*; 28 import java.util.*; 29 import java.text.NumberFormat ; 30 31 import javax.servlet.http.*; 32 import javax.servlet.jsp.*; 33 import javax.servlet.jsp.tagext.*; 34 35 import org.w3c.dom.*; 36 import org.xml.sax.*; 37 import org.xml.sax.helpers.*; 38 39 import com.iplanet.jato.*; 40 import com.iplanet.jato.model.*; 41 import com.iplanet.jato.taglib.*; 42 import com.iplanet.jato.taglib.html.FormTag; 43 import com.iplanet.jato.util.*; 44 import com.iplanet.jato.view.*; 45 import com.iplanet.jato.view.event.*; 46 import com.iplanet.jato.view.html.*; 47 48 import com.sun.web.ui.common.*; 49 import com.sun.web.ui.model.CCPropertySheetModelInterface; 50 import com.sun.web.ui.taglib.propertysheet.CCPropertySheetTag; 51 import com.sun.web.ui.view.html.*; 52 import com.sun.web.ui.view.propertysheet.*; 53 import com.sun.web.ui.taglib.common.*; 54 import com.sun.web.ui.taglib.html.*; 55 import com.sun.web.ui.taglib.help.*; 56 import com.sun.web.ui.taglib.spacer.*; 57 58 import com.sun.enterprise.tools.admingui.util.Util; 59 60 public class DataSheetTag extends CCPropertySheetTag { 61 62 private int iCurrentSection = 0; 64 private ArrayList dataObjects = null; 65 private static final String DATA_ELEMENT = "dataValue"; 66 private static final String TEMPLATE_ELEMENT = "sectionTemplate"; 67 68 private class MyNodeList extends ArrayList implements NodeList { 70 public Node item(int i) { 71 return (Node)super.get(i); 72 } 73 public int getLength() { 74 return super.size(); 75 } 76 } 77 78 81 public DataSheetTag() { 82 super(); 83 } 84 85 89 94 public void reset() { 95 super.reset(); 96 97 dataObjects = null; 99 } 100 101 105 120 protected String getHTMLStringInternal(Tag parent, PageContext pageContext, 121 View view) throws JspException { 122 if (parent == null) { 123 throw new IllegalArgumentException ("parent cannot be null."); 124 } else if (pageContext == null) { 125 throw new IllegalArgumentException ("pageContext cannot be null."); 126 } else if (view == null) { 127 throw new IllegalArgumentException ("view cannot be null."); 128 } 129 130 checkChildType(view, CCPropertySheet.class); 131 CCPropertySheet field = (CCPropertySheet) view; 132 containerView = (field.getContainerView() != null) 133 ? field.getContainerView() 134 : (ContainerView) field.getParent(); 135 136 CCPropertySheetModelInterface model = field.getModel(); 138 if (model == null) { 139 throw new IllegalArgumentException ( 140 "PropertySheet model cannot be null"); 141 } 142 if (model.getDocument() == null) { 143 throw new RuntimeException ( 144 "The property sheet model getDocument() returned null.\n" + 145 "Be sure a valid XML file was supplied " + 146 "to the property sheet model."); 147 } 148 149 setParent(parent); 151 setPageContext(pageContext); 152 153 if (field.getShowJumpLinks() != null) { 155 setShowJumpLinks(field.getShowJumpLinks().toString()); 156 } 157 NonSyncStringBuffer buffer = new NonSyncStringBuffer( 158 DEFAULT_BUFFER_SIZE); 159 160 try { 162 field.beginDisplay(new JspDisplayEvent(this, pageContext)); 163 } catch (ModelControlException e) { 164 throw new JspException(e.getRootCause()); 165 } 166 numberOfSections = 0; 167 appendSections(buffer, getSections(model), model); 169 appendData(buffer, getTemplates(model), model); 171 172 appendJavaScript(buffer); 174 175 return buffer.toString(); 176 } 177 178 182 private Node findMatchingTemplate(NodeList templates, Object data) { 184 for (int i=0; i<templates.getLength(); i++) { 185 Node template = templates.item(i); 186 String cls = this.getAttributeValue(template, "class", null); 187 if (cls != null) { 188 try { 189 Class clazz = Class.forName(cls); 190 if (clazz.isInstance(data)) { 191 return template; 193 } 194 } catch (Exception ex) { 195 if (Util.isLoggableINFO()) 196 Util.logINFO("Error in finding class: " + cls + 197 "\n" + ex.getMessage()); 198 } 199 } else { 200 if (Util.isLoggableINFO()) 201 Util.logINFO("XML error: No class specified for section template."); 202 } 203 } 204 return null; 205 } 206 207 private NodeList getSectionsForProcessing(NodeList templates, 209 CCPropertySheetModelInterface model) { 210 211 Object [] objs = (Object [])model.getValue("DataObjects"); 212 if (objs == null) 213 return null; 214 215 dataObjects = new ArrayList(); 216 MyNodeList sections = new MyNodeList(); 217 218 for (int i=0; i<objs.length; i++) { 219 Node template = findMatchingTemplate(templates, objs[i]); 220 if (template != null) { 221 sections.add(template); 222 dataObjects.add(objs[i]); 223 } else if (Util.isLoggableINFO()) { 224 Util.logINFO("Could not find matching template."); 225 Util.logINFO("OBJECT CLASS: "+objs[i].getClass().getName()); 226 try { 227 Util.logINFO(" name: "+evaluate(objs[i], "$getName", null, null)); 228 Util.logINFO(" desc: "+evaluate(objs[i], "$getDescription", null, null)); 229 } catch (Exception ex) { 230 Util.logINFO("error in evaluate: "+ex.getMessage()); 231 } 232 } 233 } 234 return sections; 235 } 236 237 private Object getCurrentData() { 239 return dataObjects.get(iCurrentSection); 240 } 241 242 private void appendData(NonSyncStringBuffer buffer, NodeList templates, 244 CCPropertySheetModelInterface model) throws JspException { 245 246 NodeList sections = getSectionsForProcessing(templates, model); 247 if (sections == null || sections.getLength() == 0) 248 return; 249 appendSections(buffer, sections, model); 250 } 251 252 protected void appendSections(NonSyncStringBuffer buffer, NodeList sections, 253 CCPropertySheetModelInterface model) throws JspException { 254 255 boolean added = (numberOfSections != 0) && (hasJumpLinks() == false); 256 numberOfSections = sections.getLength(); if (numberOfSections == 0) 258 return; 259 appendJumpLinks(buffer, sections, model); 260 261 for (int i = 0; i < numberOfSections; i++) { 262 Node section = sections.item(i); 263 if (isVisible(section, model) == false) { 264 continue; 265 } 266 if (added) { 267 appendSeparator(buffer); 269 } 270 iCurrentSection = i; appendSection(buffer, section, model); 272 273 if (hasJumpLinks()) { 274 appendBackToTop(buffer); 275 } else { 276 buffer.append(getSpacerHTMLString("10", "1")); 277 } 278 added = true; 279 } 280 } 281 282 283 285 protected void appendProperty(NonSyncStringBuffer buffer, 286 Node property, CCPropertySheetModelInterface model, int level) 287 throws JspException { 288 289 buffer.append("<tr>\n"); 291 String labelId = null; 292 boolean hasNoValueElement = hasNoValueElement(property); 293 boolean colspan = isSpan(property) || hasNoValueElement; 296 if (colspan == false) { 299 302 labelId = appendLabel(buffer, property, model, level); 303 } 304 305 if (colspan) { 307 buffer.append("<td valign=\"top\" colspan=\"2\">" + 308 "<div class=\""); 309 } else { 310 buffer.append("<td valign=\"top\"><div class=\""); 311 } 312 buffer.append(getDivClass(colspan, level)).append("\">"); 313 314 NodeList subNodes = property.getChildNodes(); 316 for (int i = 0; i < subNodes.getLength(); i++) { 317 Node subNode = subNodes.item(i); 318 String nodeName = subNode.getNodeName(); 319 if (nodeName.equalsIgnoreCase(CCDescriptor.CC_ELEMENT)) { 320 if (i != 0) { 321 buffer.append("\n"); 324 } 325 boolean isTableTag = isTableTag(subNode, colspan, level); 331 if (isTableTag) preTableFormat(buffer); 335 336 labelId = appendValue(buffer, subNode, labelId, 337 (level > 1 || colspan == false)); 338 339 if (isTableTag) postTableFormat(buffer, colspan, level); 344 345 } else if (nodeName.equalsIgnoreCase( 346 CCPropertySheetModelInterface.HELPTEXT_ELEMENT)) { 347 appendFieldHelp(buffer, getNameHTML(subNode)); 348 } else if (nodeName.equalsIgnoreCase( 349 CCPropertySheetModelInterface.VALUESET_ELEMENT)) { 350 appendValueSet(buffer, subNode, model, level + 1); 351 } else if (nodeName.equalsIgnoreCase( 352 CCPropertySheetModelInterface.LABEL_ELEMENT)) { 353 if (hasNoValueElement) { 354 String labelHTML = getNameHTML(subNode); 355 String style = (level > 1) 356 ? CCStyle.LABEL_LEVEL_THREE_TEXT 357 : CCStyle.LABEL_LEVEL_TWO_TEXT; 358 buffer.append(getLabelHTMLString(style, null, null, 359 isError(property, model), isRequired(property), 360 labelHTML)); 361 } 362 } else if (nodeName.equalsIgnoreCase(DATA_ELEMENT)) { 364 labelId = appendDataValue(buffer, subNode, labelId, model); 365 } else { 366 String html = subNode.getNodeValue(); 368 if (html != null) 369 buffer.append(html.trim()); 370 } 371 } 372 buffer.append("</div></td></tr>\n"); } 374 375 private final String Milliseconds = Util.getMessage("label.Milliseconds"); 376 private final String Seconds = Util.getMessage("label.Seconds"); 377 private final String Minutes = Util.getMessage("label.Minutes"); 378 private final String Hours = Util.getMessage("label.Hours"); 379 private final String Days = Util.getMessage("label.Days"); 380 private final String Weeks = Util.getMessage("label.Weeks"); 381 382 private class TimeData { 383 private long seconds = 0; 384 private long minutes = 0; 385 private long hours = 0; 386 private long days = 0; 387 private long weeks = 0; 388 389 private static final long oneWeek = 7*24*60*60; private static final long oneDay = 24*60*60; private static final long oneHour = 60*60; private static final long oneMinute = 60; 394 public TimeData(long secs) { 395 seconds = secs; 396 if (seconds >= oneWeek) { 397 weeks = (seconds/oneWeek); 398 seconds = seconds - weeks*oneWeek; 399 } 400 if (seconds >= oneDay) { 401 days = (seconds/oneDay); 402 seconds = seconds - days*oneDay; 403 } 404 if (seconds >= oneHour) { 405 hours = (seconds/oneHour); 406 seconds = seconds - hours*oneHour; 407 } 408 if (seconds >= oneMinute) { 409 minutes = (seconds/oneMinute); 410 seconds = seconds - minutes*oneMinute; 411 } 412 } 413 414 public String toString() { 415 String str = " "; 416 if (weeks > 0) 417 str += weeks + " " + Weeks + ", "; 418 if (days > 0 || str.length() > 6) 419 str += days + " " + Days + ", "; 420 if (hours > 0 || str.length() > 6) 421 str += hours + " " + Hours + ", "; 422 if (minutes > 0 || str.length() > 6) 423 str += minutes + " " + Minutes + ", "; 424 if (seconds > 0 || str.length() > 6) 425 str += seconds + " " + Seconds + " "; 426 return str; 427 } 428 } 429 430 private String convert(long value, String unit) { 431 if (unit == null || value == 0) 432 return ""; 433 TimeData t = null; 434 if (unit.equalsIgnoreCase(Milliseconds)) { 437 t = new TimeData((value+500)/1000); 438 } else if (unit.equalsIgnoreCase(Seconds)) { 439 t = new TimeData(value); 440 } else if (unit.equalsIgnoreCase(Minutes)) { 441 t = new TimeData(value*60); 442 } else { 443 return ""; 444 } 445 return " (" + t.toString() + ")"; 446 } 447 448 private long getLongValue(Object value) { 450 if (value instanceof Long ) 451 return ((Long )value).longValue(); 452 else if (value instanceof Integer ) 453 return ((Integer )value).longValue(); 454 else 455 return new Long ((String )value.toString()).longValue(); 456 } 457 458 private String evaluate(Object data, String methodName, 460 String formatType, String unit) throws Exception { 461 if (methodName.indexOf('$') == 0) { 462 methodName = methodName.substring(1); 463 } else { 464 throw new RuntimeException ("Method Names must begin with a '$': " + 465 methodName); 466 } 467 Method method = null; 468 try { 469 method = data.getClass().getMethod(methodName, (Class [])null); 470 } catch (NoSuchMethodException ex) { 471 throw new RuntimeException ("Method '" + methodName + 472 "' not found!", ex); 473 } 474 Object value = method.invoke(data, (Object [])null); 475 if (formatType == null || formatType.equalsIgnoreCase("String")) { 476 return value.toString(); 477 } else if (formatType.equalsIgnoreCase("Date")) { 478 Date date = new Date(getLongValue(value)); 479 return date.toString(); 480 } else if (formatType.equalsIgnoreCase("Number")) { 481 long longValue = getLongValue(value); 482 String html = NumberFormat.getInstance().format(longValue); 483 if (unit == null) 484 return html; 485 return html + " " + Util.getMessage(unit) + convert(longValue, unit); 486 } 487 throw new RuntimeException ("Unknown format type for Method: " + 488 methodName); 489 } 490 491 private String appendDataValue(NonSyncStringBuffer buff, Node dataNode, 493 String labelId, CCPropertySheetModelInterface model) { 494 495 String methodName = getAttributeValue(dataNode, "method", null); 496 String formatType = getAttributeValue(dataNode, "formatType", "String"); 497 String unit = getAttributeValue(dataNode, "unit", null); 498 try { 499 if (unit != null && unit.startsWith("$")) { 500 unit = evaluate(getCurrentData(), unit, "String", null); 501 } 502 } catch (Exception ex) { 503 } 505 506 String value = "No Method defined!"; 507 try { 508 if (methodName != null) 509 value = evaluate(getCurrentData(), methodName, formatType, unit); 510 } catch (Exception ex) { 511 value = ex.getMessage(); 512 } 513 514 if (labelId != null) { 515 buff.append("<span id=\"") 517 .append(labelId) 518 .append("\" class=\"" + CCStyle.CONTENT_DEFAULT_TEXT + "\">") 519 .append(value) 520 .append("</span>"); 521 } else { 522 buff.append(value); 523 } 524 return null; 525 } 526 527 528 529 530 534 538 private NodeList getTemplates(CCPropertySheetModelInterface model) { 540 return model.getDocument().getElementsByTagName(TEMPLATE_ELEMENT); 541 } 542 543 protected String getDefaultValue(Node node) throws JspException { 544 String html = getAttributeValue(node, 545 CCPropertySheetModelInterface.DEFAULT_ATTRIBUTE, null); 546 if (html != null) { 547 if (html.indexOf('$') == 0) { 549 try { 550 return getMessage(evaluate(getCurrentData(), html, "String", null)); 551 } catch (Exception ex) { 552 return ex.getMessage(); 553 } 554 } 555 return getMessage(html); 556 } else { 557 return ""; 558 } 559 } 560 561 protected boolean hasNoValueElement(Node propertyNode) { 562 NodeList subNodes = propertyNode.getChildNodes(); 564 for (int i = 0; i < subNodes.getLength(); i++) { 565 Node valueNode = subNodes.item(i); 566 String nodeName = valueNode.getNodeName(); 567 if (nodeName != null 568 && (nodeName.equalsIgnoreCase(CCDescriptor.CC_ELEMENT) 569 || nodeName.equalsIgnoreCase(DATA_ELEMENT))) { 571 return false; 572 } 573 } 574 return true; 575 } 576 577 protected void appendJumpLinks(NonSyncStringBuffer buff, NodeList sections) 578 throws JspException { 579 int count = numberOfSections; if (count == 0) 581 return; 582 583 String names [] = new String [count]; 584 String anchors [] = new String [count]; 585 for (int i = 0; i < count; i++) { 586 Node sectionNode = sections.item(i); 587 iCurrentSection = i; 589 names[i] = getNameHTML(sectionNode); 590 anchors[i] = getAnchor(sectionNode); 591 if (anchors[i].equals("")) 593 anchors[i] = names[i]; 594 } 595 buff.append("<div class=\"" + 597 CCStyle.CONTENT_JUMP_SECTION_DIV + 598 "\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">" + 599 "<tr>\n"); 600 601 if (count < 5) 606 appendJumpLinks(buff, names, anchors, count, 2); 607 else if (count < 10) 608 appendJumpLinks(buff, names, anchors, count, 3); 609 else 610 appendJumpLinks(buff, names, anchors, count, 4); 611 612 buff.append("</tr></table></div>\n"); 614 615 } 636 637 638 protected boolean hasJumpLinks() { 639 if (numberOfSections > 2) { 640 return new Boolean (getShowJumpLinks()).booleanValue(); 643 } 644 return false; 645 } 646 } 647 | Popular Tags |