1 31 32 package org.opencms.workplace.editors; 33 34 import org.opencms.file.CmsFile; 35 import org.opencms.file.CmsRequestContext; 36 import org.opencms.file.CmsResource; 37 import org.opencms.file.CmsResourceFilter; 38 import org.opencms.file.collectors.I_CmsResourceCollector; 39 import org.opencms.i18n.CmsEncoder; 40 import org.opencms.i18n.CmsLocaleManager; 41 import org.opencms.jsp.CmsJspActionElement; 42 import org.opencms.lock.CmsLock; 43 import org.opencms.main.CmsException; 44 import org.opencms.main.CmsLog; 45 import org.opencms.main.OpenCms; 46 import org.opencms.util.CmsRequestUtil; 47 import org.opencms.util.CmsStringUtil; 48 import org.opencms.widgets.A_CmsWidget; 49 import org.opencms.widgets.I_CmsWidget; 50 import org.opencms.widgets.I_CmsWidgetDialog; 51 import org.opencms.widgets.I_CmsWidgetParameter; 52 import org.opencms.workplace.CmsWorkplaceSettings; 53 import org.opencms.workplace.editors.directedit.CmsDirectEditButtonSelection; 54 import org.opencms.xml.CmsXmlContentDefinition; 55 import org.opencms.xml.CmsXmlException; 56 import org.opencms.xml.CmsXmlUtils; 57 import org.opencms.xml.content.CmsXmlContent; 58 import org.opencms.xml.content.CmsXmlContentErrorHandler; 59 import org.opencms.xml.content.CmsXmlContentFactory; 60 import org.opencms.xml.content.CmsXmlContentValueSequence; 61 import org.opencms.xml.types.CmsXmlNestedContentDefinition; 62 import org.opencms.xml.types.I_CmsXmlContentValue; 63 import org.opencms.xml.types.I_CmsXmlSchemaType; 64 65 import java.io.IOException ; 66 import java.io.UnsupportedEncodingException ; 67 import java.util.ArrayList ; 68 import java.util.HashSet ; 69 import java.util.Iterator ; 70 import java.util.List ; 71 import java.util.Locale ; 72 import java.util.Set ; 73 74 import javax.servlet.ServletException ; 75 import javax.servlet.http.HttpServletRequest ; 76 import javax.servlet.jsp.JspException ; 77 78 import org.apache.commons.logging.Log; 79 80 90 public class CmsXmlContentEditor extends CmsEditor implements I_CmsWidgetDialog { 91 92 93 public static final int ACTION_CHECK = 151; 94 95 96 public static final int ACTION_ELEMENT_ADD = 152; 97 98 99 public static final int ACTION_ELEMENT_MOVE_DOWN = 154; 100 101 102 public static final int ACTION_ELEMENT_MOVE_UP = 153; 103 104 105 public static final int ACTION_ELEMENT_REMOVE = 155; 106 107 108 public static final int ACTION_NEW = 156; 109 110 111 public static final String EDITOR_ACTION_CHECK = "check"; 112 113 114 public static final String EDITOR_ACTION_ELEMENT_ADD = "addelement"; 115 116 117 public static final String EDITOR_ACTION_ELEMENT_MOVE_DOWN = "elementdown"; 118 119 120 public static final String EDITOR_ACTION_ELEMENT_MOVE_UP = "elementup"; 121 122 123 public static final String EDITOR_ACTION_ELEMENT_REMOVE = "removeelement"; 124 125 126 public static final String EDITOR_ACTION_NEW = CmsDirectEditButtonSelection.VALUE_NEW; 127 128 129 public static final String PARAM_ELEMENTINDEX = "elementindex"; 130 131 132 public static final String PARAM_ELEMENTNAME = "elementname"; 133 134 135 private static final String EDITOR_TYPE = "xmlcontent"; 136 137 138 private static final Log LOG = CmsLog.getLog(CmsXmlContentEditor.class); 139 140 141 private CmsXmlContent m_content; 142 143 144 private Locale m_elementLocale; 145 146 147 private CmsXmlContentErrorHandler m_errorHandler; 148 149 150 private CmsFile m_file; 151 152 153 private Set m_helpMessageIds; 154 155 156 private boolean m_optionalElementPresent; 157 158 159 private String m_paramElementIndex; 160 161 162 private String m_paramElementName; 163 164 165 private String m_paramNewLink; 166 167 168 private CmsXmlContentWidgetVisitor m_widgetCollector; 169 170 175 public CmsXmlContentEditor(CmsJspActionElement jsp) { 176 177 super(jsp); 178 } 179 180 183 public void actionChangeElementLanguage() { 184 185 Locale oldLocale = CmsLocaleManager.getLocale(getParamOldelementlanguage()); 187 try { 188 setEditorValues(oldLocale); 189 if (!getErrorHandler().hasErrors()) { 190 if (!m_content.hasLocale(getElementLocale())) { 192 try { 194 m_content.addLocale(getCms(), getElementLocale()); 195 } catch (CmsXmlException e) { 196 if (LOG.isErrorEnabled()) { 197 LOG.error(e.getLocalizedMessage(), e); 198 } 199 } 200 } 201 writeContent(); 203 setAction(ACTION_DEFAULT); 205 } else { 206 setParamElementlanguage(getParamOldelementlanguage()); 208 m_elementLocale = null; 210 } 211 } catch (Exception e) { 212 if (LOG.isInfoEnabled()) { 214 LOG.info(e.getLocalizedMessage(), e); 215 } 216 } 217 } 218 219 224 public void actionClear(boolean forceUnlock) { 225 226 deleteTempFile(); 228 boolean directEditMode = Boolean.valueOf(getParamDirectedit()).booleanValue(); 229 boolean modified = Boolean.valueOf(getParamModified()).booleanValue(); 230 if (directEditMode || forceUnlock || !modified) { 231 try { 233 getCms().unlockResource(getParamResource()); 234 } catch (CmsException e) { 235 if (LOG.isInfoEnabled()) { 237 LOG.info(e.getLocalizedMessage(), e); 238 } 239 } 240 } 241 } 242 243 248 public void actionDeleteElementLocale() throws JspException { 249 250 try { 251 Locale loc = getElementLocale(); 252 m_content.removeLocale(loc); 253 writeContent(); 255 List locales = m_content.getLocales(); 256 if (locales.size() > 0) { 257 Locale newLoc = (Locale )locales.get(0); 259 setParamElementlanguage(newLoc.toString()); 260 m_elementLocale = newLoc; 261 } else { 262 if (LOG.isErrorEnabled()) { 263 LOG.error(Messages.get().getBundle().key(Messages.LOG_GET_LOCALES_1, getParamResource())); 264 } 265 } 266 267 } catch (CmsXmlException e) { 268 showErrorPage(e); 270 } catch (CmsException e) { 271 if (LOG.isInfoEnabled()) { 273 LOG.info(e.getLocalizedMessage(), e); 274 } 275 } 276 } 277 278 287 public void actionDirectEdit() throws IOException , JspException , ServletException { 288 289 I_CmsEditorActionHandler actionClass = OpenCms.getWorkplaceManager().getEditorActionHandler(); 291 if (actionClass == null) { 292 actionSave(); 294 actionExit(); 295 } else { 296 actionClass.editorAction(this, getJsp()); 297 } 298 } 299 300 305 public void actionExit() throws IOException , JspException , ServletException { 306 307 if (getAction() == ACTION_CANCEL) { 308 return; 310 } 311 actionClear(false); 313 actionClose(); 315 } 316 317 324 public void actionMoveElement() throws JspException { 325 326 try { 328 setEditorValues(getElementLocale()); 329 } catch (CmsXmlException e) { 330 showErrorPage(e); 332 return; 333 } 334 int index = 0; 336 try { 337 index = Integer.parseInt(getParamElementIndex()); 338 } catch (Exception e) { 339 } 341 342 I_CmsXmlContentValue value = m_content.getValue(getParamElementName(), getElementLocale(), index); 344 345 if (getAction() == ACTION_ELEMENT_MOVE_DOWN) { 346 value.moveDown(); 348 } else { 349 value.moveUp(); 351 } 352 353 if (getErrorHandler().hasWarnings(getElementLocale())) { 354 resetErrorHandler(); 356 } 357 358 try { 359 writeContent(); 361 } catch (CmsException e) { 362 showErrorPage(e); 364 } 365 } 366 367 372 public void actionNew() throws JspException { 373 374 int pos = m_paramNewLink.indexOf('|'); 376 String collectorName = m_paramNewLink.substring(0, pos); 377 String param = m_paramNewLink.substring(pos + 1); 378 379 I_CmsResourceCollector collector = OpenCms.getResourceManager().getContentCollector(collectorName); 381 String newFileName = ""; 382 try { 383 384 CmsFile templateFile = getCms().readFile(getParamResource(), CmsResourceFilter.IGNORE_EXPIRATION); 386 CmsXmlContent template = CmsXmlContentFactory.unmarshal(getCms(), templateFile); 387 Locale locale = (Locale )OpenCms.getLocaleManager().getDefaultLocales(getCms(), getParamResource()).get(0); 388 389 CmsXmlContent newContent = CmsXmlContentFactory.createDocument( 391 getCms(), 392 locale, 393 template.getEncoding(), 394 template.getContentDefinition()); 395 396 newFileName = collector.getCreateLink(getCms(), collectorName, param); 398 399 getCms().createResource(newFileName, templateFile.getTypeId()); 401 CmsFile newFile = getCms().readFile(newFileName, CmsResourceFilter.ALL); 403 newFile.setContents(newContent.marshal()); 404 getCms().writeFile(newFile); 406 407 setParamNewLink(null); 409 setParamAction(null); 410 setParamResource(newFileName); 411 setAction(ACTION_DEFAULT); 412 413 setParamTempfile(createTempFile()); 415 416 m_file = getCms().readFile(getParamTempfile(), CmsResourceFilter.ALL); 418 m_content = newContent; 419 420 } catch (CmsException e) { 421 if (LOG.isErrorEnabled()) { 422 LOG.error(Messages.get().getBundle().key(Messages.LOG_CREATE_XML_CONTENT_ITEM_1, m_paramNewLink), e); 423 } 424 throw new JspException (e); 425 } finally { 426 try { 427 getCms().deleteResource(newFileName, CmsResource.DELETE_PRESERVE_SIBLINGS); 429 } catch (CmsException e) { 430 } 432 } 433 } 434 435 441 public void actionPreview() throws IOException , JspException { 442 443 try { 444 setEditorValues(getElementLocale()); 446 writeContent(); 447 getCms().setDateReleased(getParamTempfile(), CmsResource.DATE_RELEASED_DEFAULT, false); 449 getCms().setDateExpired(getParamTempfile(), CmsResource.DATE_EXPIRED_DEFAULT, false); 450 } catch (CmsException e) { 451 showErrorPage(this, e); 453 } 454 455 String previewUri = m_content.getContentDefinition().getContentHandler().getPreview( 457 getCms(), 458 m_content, 459 getParamTempfile()); 460 461 StringBuffer param = new StringBuffer (8); 463 if (previewUri.indexOf('?') != -1) { 464 param.append("&"); 465 } else { 466 param.append("?"); 467 } 468 param.append(CmsLocaleManager.PARAMETER_LOCALE); 469 param.append("="); 470 param.append(getParamElementlanguage()); 471 472 sendCmsRedirect(previewUri + param); 474 } 475 476 481 public void actionSave() throws JspException { 482 483 actionSave(getElementLocale()); 484 if (getAction() != ACTION_CANCEL) { 485 setAction(ACTION_SAVE); 487 } 488 } 489 490 498 public void actionSave(Locale locale) throws JspException { 499 500 try { 501 setEditorValues(locale); 502 if (!getErrorHandler().hasErrors()) { 504 writeContent(); 506 commitTempFile(); 507 setParamModified(Boolean.TRUE.toString()); 509 } 510 } catch (CmsException e) { 511 showErrorPage(e); 512 } 513 514 } 515 516 523 public void actionToggleElement() throws JspException { 524 525 try { 527 setEditorValues(getElementLocale()); 528 } catch (CmsXmlException e) { 529 showErrorPage(e); 531 return; 532 } 533 534 int index = 0; 536 try { 537 index = Integer.parseInt(getParamElementIndex()); 538 } catch (Exception e) { 539 } 541 542 if (getAction() == ACTION_ELEMENT_REMOVE) { 543 m_content.removeValue(getParamElementName(), getElementLocale(), index); 545 } else { 546 if (m_content.hasValue(getParamElementName(), getElementLocale())) { 548 index += 1; 550 } 551 m_content.addValue(getCms(), getParamElementName(), getElementLocale(), index); 552 } 553 554 if (getErrorHandler().hasWarnings(getElementLocale())) { 555 resetErrorHandler(); 557 } 558 559 try { 560 writeContent(); 562 } catch (CmsException e) { 563 showErrorPage(e); 565 } 566 } 567 568 577 public String buildSelectElementLanguage(String attributes) { 578 579 return buildSelectElementLanguage(attributes, getParamResource(), getElementLocale()); 580 } 581 582 585 public int getButtonStyle() { 586 587 return getSettings().getUserSettings().getEditorButtonStyle(); 588 } 589 590 593 public String getEditorResourceUri() { 594 595 return getSkinUri() + "editors/" + EDITOR_TYPE + "/"; 596 } 597 598 603 public Locale getElementLocale() { 604 605 if (m_elementLocale == null) { 606 if (CmsStringUtil.isNotEmpty(getParamElementlanguage()) && !"null".equals(getParamElementlanguage())) { 607 m_elementLocale = CmsLocaleManager.getLocale(getParamElementlanguage()); 608 } else { 609 initElementLanguage(); 610 m_elementLocale = CmsLocaleManager.getLocale(getParamElementlanguage()); 611 } 612 } 613 return m_elementLocale; 614 } 615 616 619 public Set getHelpMessageIds() { 620 621 if (m_helpMessageIds == null) { 622 m_helpMessageIds = new HashSet (); 623 } 624 return m_helpMessageIds; 625 } 626 627 632 public String getParamElementIndex() { 633 634 return m_paramElementIndex; 635 } 636 637 642 public String getParamElementName() { 643 644 return m_paramElementName; 645 } 646 647 652 public String getParamNewLink() { 653 654 return m_paramNewLink; 655 } 656 657 660 public String getUserAgent() { 661 662 return getJsp().getRequest().getHeader(CmsRequestUtil.HEADER_USER_AGENT); 663 } 664 665 670 public String getXmlEditorForm() { 671 672 getCms().getRequestContext().setAttribute(CmsRequestContext.ATTRIBUTE_EDITOR, Boolean.TRUE); 674 675 addMessages(m_content.getContentDefinition().getContentHandler().getMessages(getLocale())); 677 678 return getXmlEditorForm(m_content.getContentDefinition(), "", true).toString(); 679 } 680 681 687 public String getXmlEditorHtmlEnd() throws JspException { 688 689 StringBuffer result = new StringBuffer (16384); 690 if (m_optionalElementPresent) { 691 resetWidgetCollector(); 693 } 694 try { 695 Iterator i = getWidgetCollector().getWidgets().keySet().iterator(); 697 while (i.hasNext()) { 698 String key = (String )i.next(); 700 I_CmsXmlContentValue value = (I_CmsXmlContentValue)getWidgetCollector().getValues().get(key); 701 I_CmsWidget widget = (I_CmsWidget)getWidgetCollector().getWidgets().get(key); 702 result.append(widget.getDialogHtmlEnd(getCms(), this, (I_CmsWidgetParameter)value)); 703 704 } 705 706 result.append("<div class=\"help\" id=\"helpText\" "); 708 result.append("onmouseover=\"showHelpText();\" onmouseout=\"hideHelpText();\"></div>\n"); 709 710 result.append("<div class=\"xmlButtons\" id=\"xmlElementButtons\" "); 712 result.append("onmouseover=\"checkElementButtons(true);\" onmouseout=\"checkElementButtons(false);\"></div>\n"); 713 714 return result.toString(); 716 } catch (Exception e) { 717 showErrorPage(e); 718 return ""; 719 } 720 } 721 722 728 public String getXmlEditorIncludes() throws JspException { 729 730 StringBuffer result = new StringBuffer (32); 731 try { 732 Iterator i = getWidgetCollector().getUniqueWidgets().iterator(); 734 while (i.hasNext()) { 735 I_CmsWidget widget = (I_CmsWidget)i.next(); 736 result.append(widget.getDialogIncludes(getCms(), this)); 737 result.append("\n"); 738 } 739 } catch (Exception e) { 740 showErrorPage(e); 741 } 742 return result.toString(); 743 } 744 745 751 public String getXmlEditorInitCalls() throws JspException { 752 753 StringBuffer result = new StringBuffer (32); 754 try { 755 Iterator i = getWidgetCollector().getUniqueWidgets().iterator(); 757 while (i.hasNext()) { 758 I_CmsWidget widget = (I_CmsWidget)i.next(); 759 result.append(widget.getDialogInitCall(getCms(), this)); 760 } 761 } catch (Exception e) { 762 showErrorPage(e); 763 } 764 return result.toString(); 765 } 766 767 774 public String getXmlEditorInitMethods() throws JspException { 775 776 StringBuffer result = new StringBuffer (32); 777 try { 778 Iterator i = getWidgetCollector().getUniqueWidgets().iterator(); 780 while (i.hasNext()) { 781 I_CmsWidget widget = (I_CmsWidget)i.next(); 782 result.append(widget.getDialogInitMethod(getCms(), this)); 783 result.append("\n"); 784 } 785 } catch (Exception e) { 786 showErrorPage(e); 787 } 788 return result.toString(); 789 } 790 791 796 public boolean hasValidationErrors() { 797 798 return getErrorHandler().hasErrors(); 799 } 800 801 809 public boolean isPreviewEnabled() { 810 811 try { 812 CmsFile file = getCms().readFile(getParamResource(), CmsResourceFilter.ALL); 814 CmsXmlContent content = CmsXmlContentFactory.unmarshal(getCms(), file); 815 return content.getContentDefinition().getContentHandler().getPreview( 816 getCms(), 817 m_content, 818 getParamResource()) != null; 819 } catch (Exception e) { 820 return false; 822 } 823 } 824 825 834 public void setEditorValues(Locale locale) throws CmsXmlException { 835 836 List valueNames = getSimpleValueNames(m_content.getContentDefinition(), "", locale); 837 Iterator i = valueNames.iterator(); 838 while (i.hasNext()) { 839 String valueName = (String )i.next(); 840 I_CmsXmlContentValue value = m_content.getValue(valueName, locale); 841 I_CmsWidget widget = value.getContentDefinition().getContentHandler().getWidget(value); 842 widget.setEditorValue(getCms(), getJsp().getRequest().getParameterMap(), this, (I_CmsWidgetParameter)value); 843 } 844 } 845 846 851 public void setParamElementIndex(String elementIndex) { 852 853 m_paramElementIndex = elementIndex; 854 } 855 856 861 public void setParamElementName(String elementName) { 862 863 m_paramElementName = elementName; 864 } 865 866 871 public void setParamNewLink(String paramNewLink) { 872 873 m_paramNewLink = CmsEncoder.decode(paramNewLink); 874 } 875 876 881 public boolean showElementLanguageSelector() { 882 883 List locales = OpenCms.getLocaleManager().getAvailableLocales(getCms(), getParamResource()); 884 if (locales == null || locales.size() < 2) { 885 return false; 887 } 888 return true; 889 } 890 891 894 public boolean useNewStyle() { 895 896 return false; 897 } 898 899 904 protected void initContent() { 905 906 } 908 909 912 protected void initElementLanguage() { 913 914 List locales = new ArrayList (); 916 if (m_content != null) { 917 locales = m_content.getLocales(); 918 } 919 Locale defaultLocale = (Locale )OpenCms.getLocaleManager().getDefaultLocales(getCms(), getParamResource()).get(0); 920 921 if (locales.size() > 0) { 922 if (locales.contains(defaultLocale)) { 924 setParamElementlanguage(defaultLocale.toString()); 926 return; 927 } else { 928 setParamElementlanguage(locales.get(0).toString()); 930 return; 931 } 932 } 933 setParamElementlanguage(defaultLocale.toString()); 934 } 935 936 939 protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) { 940 941 fillParamValues(request); 943 setParamDialogtype(EDITOR_TYPE); 945 946 if (getParamNewLink() != null) { 947 setParamAction(EDITOR_ACTION_NEW); 948 } else { 949 if (getParamTempfile() != null && !"null".equals(getParamTempfile())) { 951 try { 952 m_file = getCms().readFile(this.getParamTempfile(), CmsResourceFilter.ALL); 953 m_content = CmsXmlContentFactory.unmarshal(getCms(), m_file); 954 } catch (CmsException e) { 955 try { 957 showErrorPage(this, e); 958 } catch (JspException exc) { 959 if (LOG.isInfoEnabled()) { 961 LOG.info(exc); 962 } 963 } 964 } 965 } 966 } 967 968 if (EDITOR_SAVE.equals(getParamAction())) { 970 setAction(ACTION_SAVE); 971 } else if (EDITOR_SAVEEXIT.equals(getParamAction())) { 972 setAction(ACTION_SAVEEXIT); 973 } else if (EDITOR_EXIT.equals(getParamAction())) { 974 setAction(ACTION_EXIT); 975 } else if (EDITOR_CLOSEBROWSER.equals(getParamAction())) { 976 actionClear(true); 978 return; 979 } else if (EDITOR_ACTION_CHECK.equals(getParamAction())) { 980 setAction(ACTION_CHECK); 981 } else if (EDITOR_SAVEACTION.equals(getParamAction())) { 982 setAction(ACTION_SAVEACTION); 983 try { 984 actionDirectEdit(); 985 } catch (Exception e) { 986 if (LOG.isInfoEnabled()) { 988 LOG.info(e.getLocalizedMessage(), e); 989 } 990 } 991 setAction(ACTION_EXIT); 992 } else if (EDITOR_DELETELOCALE.equals(getParamAction())) { 993 setAction(ACTION_DELETELOCALE); 994 } else if (EDITOR_SHOW.equals(getParamAction())) { 995 setAction(ACTION_SHOW); 996 } else if (EDITOR_SHOW_ERRORMESSAGE.equals(getParamAction())) { 997 setAction(ACTION_SHOW_ERRORMESSAGE); 998 } else if (EDITOR_CHANGE_ELEMENT.equals(getParamAction())) { 999 setAction(ACTION_SHOW); 1000 actionChangeElementLanguage(); 1001 } else if (EDITOR_ACTION_ELEMENT_ADD.equals(getParamAction())) { 1002 setAction(ACTION_ELEMENT_ADD); 1003 try { 1004 actionToggleElement(); 1005 } catch (JspException e) { 1006 if (LOG.isErrorEnabled()) { 1007 LOG.error(org.opencms.workplace.Messages.get().getBundle().key( 1008 org.opencms.workplace.Messages.LOG_INCLUDE_ERRORPAGE_FAILED_0)); 1009 } 1010 } 1011 if (getAction() != ACTION_CANCEL && getAction() != ACTION_SHOW_ERRORMESSAGE) { 1012 setAction(ACTION_SHOW); 1014 } 1015 } else if (EDITOR_ACTION_ELEMENT_REMOVE.equals(getParamAction())) { 1016 setAction(ACTION_ELEMENT_REMOVE); 1017 try { 1018 actionToggleElement(); 1019 } catch (JspException e) { 1020 if (LOG.isErrorEnabled()) { 1021 LOG.error(org.opencms.workplace.Messages.get().getBundle().key( 1022 org.opencms.workplace.Messages.LOG_INCLUDE_ERRORPAGE_FAILED_0)); 1023 } 1024 } 1025 if (getAction() != ACTION_CANCEL && getAction() != ACTION_SHOW_ERRORMESSAGE) { 1026 setAction(ACTION_SHOW); 1028 } 1029 } else if (EDITOR_ACTION_ELEMENT_MOVE_DOWN.equals(getParamAction())) { 1030 setAction(ACTION_ELEMENT_MOVE_DOWN); 1031 try { 1032 actionMoveElement(); 1033 } catch (JspException e) { 1034 if (LOG.isErrorEnabled()) { 1035 LOG.error(org.opencms.workplace.Messages.get().getBundle().key( 1036 org.opencms.workplace.Messages.LOG_INCLUDE_ERRORPAGE_FAILED_0)); 1037 } 1038 } 1039 if (getAction() != ACTION_CANCEL && getAction() != ACTION_SHOW_ERRORMESSAGE) { 1040 setAction(ACTION_SHOW); 1042 } 1043 } else if (EDITOR_ACTION_ELEMENT_MOVE_UP.equals(getParamAction())) { 1044 setAction(ACTION_ELEMENT_MOVE_UP); 1045 try { 1046 actionMoveElement(); 1047 } catch (JspException e) { 1048 if (LOG.isErrorEnabled()) { 1049 LOG.error(org.opencms.workplace.Messages.get().getBundle().key( 1050 org.opencms.workplace.Messages.LOG_INCLUDE_ERRORPAGE_FAILED_0)); 1051 } 1052 } 1053 if (getAction() != ACTION_CANCEL && getAction() != ACTION_SHOW_ERRORMESSAGE) { 1054 setAction(ACTION_SHOW); 1056 } 1057 } else if (EDITOR_ACTION_NEW.equals(getParamAction())) { 1058 setAction(ACTION_NEW); 1059 return; 1060 } else if (EDITOR_PREVIEW.equals(getParamAction())) { 1061 setAction(ACTION_PREVIEW); 1062 } else { 1063 setAction(ACTION_DEFAULT); 1065 try { 1066 if (Boolean.valueOf(getParamDirectedit()).booleanValue()) { 1068 checkLock(getParamResource(), CmsLock.TEMPORARY); 1070 } else { 1071 checkLock(getParamResource()); 1073 } 1074 setParamTempfile(createTempFile()); 1076 m_file = getCms().readFile(this.getParamTempfile(), CmsResourceFilter.ALL); 1078 m_content = CmsXmlContentFactory.unmarshal(getCms(), m_file); 1079 } catch (CmsException e) { 1080 try { 1082 showErrorPage(this, e); 1083 } catch (JspException exc) { 1084 if (LOG.isInfoEnabled()) { 1086 LOG.info(exc); 1087 } 1088 } 1089 } 1090 if (getParamElementlanguage() == null) { 1092 initElementLanguage(); 1093 } 1094 } 1095 } 1096 1097 1106 private String buildElementButtons(String elementName, int index, boolean addElement, boolean removeElement) { 1107 1108 StringBuffer jsCall = new StringBuffer (512); 1109 1110 boolean buttonPresent = false; 1112 1113 jsCall.append("showElementButtons('"); 1114 jsCall.append(elementName); 1115 jsCall.append("', "); 1116 jsCall.append(index); 1117 jsCall.append(", "); 1118 1119 if (removeElement) { 1121 jsCall.append(Boolean.TRUE); 1122 buttonPresent = true; 1123 } else { 1124 jsCall.append(Boolean.FALSE); 1125 } 1126 jsCall.append(", "); 1127 1128 if (index > 0) { 1130 jsCall.append(Boolean.TRUE); 1132 buttonPresent = true; 1133 } else { 1134 jsCall.append(Boolean.FALSE); 1135 } 1136 jsCall.append(", "); 1137 1138 int indexCount = m_content.getIndexCount(elementName, getElementLocale()); 1140 if (index < (indexCount - 1)) { 1141 jsCall.append(Boolean.TRUE); 1143 buttonPresent = true; 1144 } else { 1145 jsCall.append(Boolean.FALSE); 1146 } 1147 jsCall.append(", "); 1148 1149 if (addElement) { 1151 jsCall.append(Boolean.TRUE); 1152 buttonPresent = true; 1153 } else { 1154 jsCall.append(Boolean.FALSE); 1155 } 1156 jsCall.append(");"); 1157 1158 String result; 1159 if (buttonPresent) { 1160 String btIcon = "xmledit.png"; 1162 String btAction = jsCall.toString(); 1163 if (addElement && removeElement) { 1165 btIcon = "xmledit_del_add.png"; 1166 } else if (addElement) { 1167 btIcon = "xmledit_add.png"; 1168 StringBuffer action = new StringBuffer (128); 1170 action.append("addElement('"); 1171 action.append(elementName); 1172 action.append("', "); 1173 action.append(index); 1174 action.append(");"); 1175 btAction = action.toString(); 1176 } else if (removeElement) { 1177 btIcon = "xmledit_del.png"; 1178 StringBuffer action = new StringBuffer (128); 1180 action.append("removeElement('"); 1181 action.append(elementName); 1182 action.append("', "); 1183 action.append(index); 1184 action.append(");"); 1185 btAction = action.toString(); 1186 } 1187 StringBuffer href = new StringBuffer (512); 1188 href.append("javascript:"); 1189 href.append(btAction); 1190 href.append("\" onmouseover=\""); 1191 href.append(jsCall); 1192 href.append("checkElementButtons(true);\" onmouseout=\"checkElementButtons(false);\" id=\"btimg."); 1193 href.append(elementName).append(".").append(index); 1194 result = button(href.toString(), null, btIcon, Messages.GUI_EDITOR_XMLCONTENT_ELEMENT_BUTTONS_0, 0); 1195 } else { 1196 result = buttonBarSpacer(1); 1198 } 1199 1200 return result; 1201 } 1202 1203 1208 private CmsXmlContentErrorHandler getErrorHandler() { 1209 1210 if (m_errorHandler == null) { 1211 m_errorHandler = m_content.validate(getCms()); 1213 } 1214 return m_errorHandler; 1215 } 1216 1217 1225 private List getSimpleValueNames(CmsXmlContentDefinition contentDefinition, String pathPrefix, Locale locale) { 1226 1227 List valueNames = new ArrayList (); 1228 Iterator i = contentDefinition.getTypeSequence().iterator(); 1229 while (i.hasNext()) { 1230 1231 I_CmsXmlSchemaType type = (I_CmsXmlSchemaType)i.next(); 1232 1233 String name = pathPrefix + type.getName(); 1234 1235 CmsXmlContentValueSequence elementSequence = m_content.getValueSequence(name, locale); 1237 int elementCount = elementSequence.getElementCount(); 1238 for (int j = 0; j < elementCount; j++) { 1240 I_CmsXmlContentValue value = elementSequence.getValue(j); 1241 1242 StringBuffer xPath = new StringBuffer (pathPrefix.length() + 16); 1243 xPath.append(pathPrefix); 1244 xPath.append(CmsXmlUtils.createXpathElement(type.getName(), value.getIndex() + 1)); 1245 1246 if (!type.isSimpleType()) { 1247 CmsXmlNestedContentDefinition nestedSchema = (CmsXmlNestedContentDefinition)type; 1249 xPath.append("/"); 1250 valueNames.addAll(getSimpleValueNames( 1251 nestedSchema.getNestedContentDefinition(), 1252 xPath.toString(), 1253 locale)); 1254 } else { 1255 valueNames.add(xPath.toString()); 1257 } 1258 } 1259 } 1260 return valueNames; 1261 } 1262 1263 1268 private CmsXmlContentWidgetVisitor getWidgetCollector() { 1269 1270 if (m_widgetCollector == null) { 1271 m_widgetCollector = new CmsXmlContentWidgetVisitor(getElementLocale()); 1273 m_content.visitAllValuesWith(m_widgetCollector); 1274 } 1275 return m_widgetCollector; 1276 } 1277 1278 1290 private StringBuffer getXmlEditorForm( 1291 CmsXmlContentDefinition contentDefinition, 1292 String pathPrefix, 1293 boolean showHelpBubble) { 1294 1295 StringBuffer result = new StringBuffer (1024); 1296 boolean showErrors = (getAction() != ACTION_NEW) && (getAction() != ACTION_DEFAULT); 1298 1299 try { 1300 boolean nested = CmsStringUtil.isNotEmpty(pathPrefix); 1302 1303 result.append("<table class=\"xmlTable"); 1305 if (nested) { 1306 result.append("Nested"); 1308 } 1309 result.append("\">\n"); 1310 1311 if (!nested && showErrors && getErrorHandler().hasErrors(getElementLocale())) { 1313 result.append("<tr><td colspan=\"4\"> </td></tr>\n"); 1314 result.append("<tr><td colspan=\"2\"> </td>"); 1315 result.append("<td class=\"xmlTdErrorHeader\">"); 1316 result.append(key(Messages.ERR_EDITOR_XMLCONTENT_VALIDATION_ERROR_TITLE_0)); 1317 result.append("</td><td> "); 1318 result.append("</td></tr>\n"); 1319 } 1320 1321 Iterator i = contentDefinition.getTypeSequence().iterator(); 1323 while (i.hasNext()) { 1324 I_CmsXmlSchemaType type = (I_CmsXmlSchemaType)i.next(); 1326 CmsXmlContentDefinition nestedContentDefinition = contentDefinition; 1327 if (!type.isSimpleType()) { 1328 CmsXmlNestedContentDefinition nestedSchema = (CmsXmlNestedContentDefinition)type; 1330 nestedContentDefinition = nestedSchema.getNestedContentDefinition(); 1331 } 1332 String name = pathPrefix + type.getName(); 1334 1335 CmsXmlContentValueSequence elementSequence = m_content.getValueSequence(name, getElementLocale()); 1337 int elementCount = elementSequence.getElementCount(); 1338 1339 boolean addValue = false; 1341 if (elementCount < type.getMaxOccurs()) { 1342 addValue = true; 1343 } 1344 boolean removeValue = false; 1345 if (elementCount > type.getMinOccurs()) { 1346 removeValue = true; 1347 } 1348 1349 boolean disabledElement = false; 1351 if (elementCount < 1) { 1352 elementCount = 1; 1354 elementSequence.addValue(getCms(), 0); 1355 disabledElement = true; 1356 m_optionalElementPresent = true; 1357 } 1358 1359 for (int j = 0; j < elementCount; j++) { 1361 I_CmsXmlContentValue value = elementSequence.getValue(j); 1363 I_CmsWidget widget = null; 1364 if (type.isSimpleType()) { 1365 widget = contentDefinition.getContentHandler().getWidget(value); 1366 } 1367 1368 result.append("<tr>"); 1370 result.append("<td class=\"xmlLabel"); 1371 if (disabledElement) { 1372 result.append("Disabled"); 1374 } 1375 result.append("\">"); 1376 result.append(keyDefault(A_CmsWidget.getLabelKey((I_CmsWidgetParameter)value), value.getName())); 1377 if (elementCount > 1) { 1378 result.append(" [").append(value.getIndex() + 1).append("]"); 1379 } 1380 result.append(": </td>"); 1381 if (showHelpBubble && type.isSimpleType() && value.getIndex() == 0) { 1382 result.append(widget.getHelpBubble(getCms(), this, (I_CmsWidgetParameter)value)); 1384 } else { 1385 result.append(buttonBarSpacer(16)); 1387 } 1388 1389 if (!disabledElement) { 1391 if (!type.isSimpleType()) { 1392 String newPath = CmsXmlUtils.createXpathElement(value.getName(), value.getIndex() + 1); 1394 result.append("<td class=\"maxwidth\">"); 1395 boolean showHelp = (j == 0); 1396 result.append(getXmlEditorForm( 1397 nestedContentDefinition, 1398 pathPrefix + newPath + "/", 1399 showHelp)); 1400 result.append("</td>"); 1401 } else { 1402 result.append(widget.getDialogWidget(getCms(), this, (I_CmsWidgetParameter)value)); 1404 } 1405 } else { 1406 result.append("<td class=\"xmlTdDisabled maxwidth\">"); 1408 result.append(key(Messages.GUI_EDITOR_XMLCONTENT_OPTIONALELEMENT_0)); 1409 result.append("</td>"); 1410 } 1411 1412 result.append(buildElementButtons(name, value.getIndex(), addValue, removeValue)); 1414 1415 result.append("</tr>\n"); 1417 1418 String key = value.getPath(); 1420 if (showErrors 1421 && getErrorHandler().hasErrors(getElementLocale()) 1422 && getErrorHandler().getErrors(getElementLocale()).containsKey(key)) { 1423 result.append("<tr><td></td><td><img SRC=\""); 1425 result.append(getEditorResourceUri()); 1426 result.append("error.png"); 1427 result.append("\" border=\"0\" alt=\"\"></td><td class=\"xmlTdError\">"); 1428 result.append(resolveMacros((String )getErrorHandler().getErrors(getElementLocale()).get(key))); 1429 result.append("</td><td></td></tr>\n"); 1430 } 1431 if (showErrors 1433 && getErrorHandler().hasWarnings(getElementLocale()) 1434 && getErrorHandler().getWarnings(getElementLocale()).containsKey(key)) { 1435 result.append("<tr><td></td><td><img SRC=\""); 1437 result.append(getEditorResourceUri()); 1438 result.append("warning.png"); 1439 result.append("\" border=\"0\" alt=\"\"></td><td class=\"xmlTdWarning\">"); 1440 result.append(resolveMacros((String )getErrorHandler().getWarnings(getElementLocale()).get(key))); 1441 result.append("</td><td></td></tr>\n"); 1442 } 1443 } 1444 } 1445 result.append("</table>\n"); 1447 } catch (Throwable t) { 1448 LOG.error(Messages.get().getBundle().key(Messages.ERR_XML_EDITOR_0), t); 1449 } 1450 return result; 1451 } 1452 1453 1456 private void resetErrorHandler() { 1457 1458 m_errorHandler = null; 1459 } 1460 1461 1466 private void resetWidgetCollector() { 1467 1468 m_widgetCollector = null; 1469 } 1470 1471 1476 private void writeContent() throws CmsException { 1477 1478 String decodedContent = m_content.toString(); 1479 try { 1480 m_file.setContents(decodedContent.getBytes(getFileEncoding())); 1481 } catch (UnsupportedEncodingException e) { 1482 throw new CmsException(Messages.get().container(Messages.ERR_INVALID_CONTENT_ENC_1, getParamResource()), e); 1483 } 1484 m_file = getCms().writeFile(m_file); 1486 m_content = CmsXmlContentFactory.unmarshal(getCms(), m_file); 1487 } 1488} | Popular Tags |