1 31 32 package org.opencms.xml.content; 33 34 import org.opencms.configuration.CmsConfigurationManager; 35 import org.opencms.file.CmsFile; 36 import org.opencms.file.CmsObject; 37 import org.opencms.file.CmsProperty; 38 import org.opencms.file.CmsResource; 39 import org.opencms.file.CmsResourceFilter; 40 import org.opencms.i18n.CmsEncoder; 41 import org.opencms.i18n.CmsMessages; 42 import org.opencms.main.CmsException; 43 import org.opencms.main.CmsRuntimeException; 44 import org.opencms.main.OpenCms; 45 import org.opencms.util.CmsFileUtil; 46 import org.opencms.util.CmsHtmlConverter; 47 import org.opencms.util.CmsMacroResolver; 48 import org.opencms.util.CmsStringUtil; 49 import org.opencms.widgets.I_CmsWidget; 50 import org.opencms.xml.CmsXmlContentDefinition; 51 import org.opencms.xml.CmsXmlEntityResolver; 52 import org.opencms.xml.CmsXmlException; 53 import org.opencms.xml.CmsXmlUtils; 54 import org.opencms.xml.types.I_CmsXmlContentValue; 55 import org.opencms.xml.types.I_CmsXmlSchemaType; 56 57 import java.util.HashMap ; 58 import java.util.Iterator ; 59 import java.util.List ; 60 import java.util.Locale ; 61 import java.util.Map ; 62 import java.util.regex.Pattern ; 63 64 import org.dom4j.Document; 65 import org.dom4j.DocumentHelper; 66 import org.dom4j.Element; 67 68 78 public class CmsDefaultXmlContentHandler implements I_CmsXmlContentHandler { 79 80 81 public static final String APPINFO_APPINFO = "appinfo"; 82 83 84 public static final String APPINFO_ATTR_CONFIGURATION = "configuration"; 85 86 87 public static final String APPINFO_ATTR_ELEMENT = "element"; 88 89 90 public static final String APPINFO_ATTR_MAPTO = "mapto"; 91 92 93 public static final String APPINFO_ATTR_MESSAGE = "message"; 94 95 96 public static final String APPINFO_ATTR_NAME = "name"; 97 98 99 public static final String APPINFO_ATTR_REGEX = "regex"; 100 101 102 public static final String APPINFO_ATTR_TYPE = "type"; 103 104 105 public static final String APPINFO_ATTR_TYPE_WARNING = "warning"; 106 107 108 public static final String APPINFO_ATTR_URI = "uri"; 109 110 111 public static final String APPINFO_ATTR_VALUE = "value"; 112 113 114 public static final String APPINFO_ATTR_WIDGET = "widget"; 115 116 117 public static final String APPINFO_DEFAULT = "default"; 118 119 120 public static final String APPINFO_DEFAULTS = "defaults"; 121 122 123 public static final String APPINFO_LAYOUT = "layout"; 124 125 126 public static final String APPINFO_LAYOUTS = "layouts"; 127 128 129 public static final String APPINFO_MAPPING = "mapping"; 130 131 132 public static final String APPINFO_MAPPINGS = "mappings"; 133 134 135 public static final String APPINFO_PREVIEW = "preview"; 136 137 138 public static final String APPINFO_RESOURCEBUNDLE = "resourcebundle"; 139 140 141 public static final String APPINFO_RULE = "rule"; 142 143 144 public static final String APPINFO_SCHEMA_FILE = "org/opencms/xml/content/DefaultAppinfo.xsd"; 145 146 147 public static final String APPINFO_SCHEMA_SYSTEM_ID = CmsConfigurationManager.DEFAULT_DTD_PREFIX 148 + APPINFO_SCHEMA_FILE; 149 150 151 public static final String APPINFO_VALIDATIONRULES = "validationrules"; 152 153 154 public static final String MACRO_PREVIEW_TEMPFILE = "previewtempfile"; 155 156 157 protected static final String MESSAGE_VALIDATION_DEFAULT_ERROR = "${validation.path}: " 158 + "${key." + Messages.GUI_EDITOR_XMLCONTENT_VALIDATION_ERROR_2 + "|${validation.value}|[${validation.regex}]}"; 159 160 161 protected static final String MESSAGE_VALIDATION_DEFAULT_WARNING = "${validation.path}: " 162 + "${key." + Messages.GUI_EDITOR_XMLCONTENT_VALIDATION_WARNING_2 + "|${validation.value}|[${validation.regex}]}"; 163 164 165 protected Map m_configurationValues; 166 167 168 protected Map m_defaultValues; 169 170 171 protected Map m_elementMappings; 172 173 174 protected Map m_elementWidgets; 175 176 177 protected String m_messageBundleName; 178 179 180 protected String m_previewLocation; 181 182 183 protected Map m_validationErrorMessages; 184 185 186 protected Map m_validationErrorRules; 187 188 189 protected Map m_validationWarningMessages; 190 191 192 protected Map m_validationWarningRules; 193 194 197 public CmsDefaultXmlContentHandler() { 198 199 init(); 200 } 201 202 205 static { 206 207 byte[] appinfoSchema; 209 try { 210 appinfoSchema = CmsFileUtil.readFile(APPINFO_SCHEMA_FILE); 211 } catch (Exception e) { 212 throw new CmsRuntimeException(Messages.get().container( 213 org.opencms.xml.types.Messages.ERR_XMLCONTENT_LOAD_SCHEMA_1, 214 APPINFO_SCHEMA_FILE), e); 215 } 216 CmsXmlEntityResolver.cacheSystemId(APPINFO_SCHEMA_SYSTEM_ID, appinfoSchema); 217 } 218 219 222 public String getConfiguration(I_CmsXmlSchemaType type) { 223 224 String elementName = type.getName(); 225 return (String )m_configurationValues.get(elementName); 226 } 227 228 231 public String getDefault(CmsObject cms, I_CmsXmlContentValue value, Locale locale) { 232 233 String defaultValue; 234 if (value.getElement() == null) { 235 defaultValue = value.getDefault(locale); 237 } else { 238 String xpath = value.getPath(); 239 defaultValue = (String )m_defaultValues.get(xpath); 241 if (defaultValue == null) { 242 xpath = CmsXmlUtils.removeXpath(xpath); 244 xpath = CmsXmlUtils.createXpath(xpath, 1); 245 defaultValue = (String )m_defaultValues.get(xpath); 247 } 248 } 249 if (defaultValue != null) { 250 CmsMacroResolver resolver = CmsMacroResolver.newInstance().setCmsObject(cms).setMessages( 252 getMessages(locale)); 253 return resolver.resolveMacros(defaultValue); 254 } 255 return null; 257 } 258 259 265 public String getMapping(String elementName) { 266 267 return (String )m_elementMappings.get(elementName); 268 } 269 270 273 public CmsMessages getMessages(Locale locale) { 274 275 if (m_messageBundleName == null) { 276 return null; 278 } 279 280 return new CmsMessages(m_messageBundleName, locale); 281 } 282 283 286 public String getPreview(CmsObject cms, CmsXmlContent content, String resourcename) { 287 288 CmsMacroResolver resolver = CmsMacroResolver.newInstance().setCmsObject(cms); 289 resolver.addMacro(MACRO_PREVIEW_TEMPFILE, resourcename); 290 291 return resolver.resolveMacros(m_previewLocation); 292 } 293 294 297 public I_CmsWidget getWidget(I_CmsXmlContentValue value) { 298 299 I_CmsWidget result = (I_CmsWidget)m_elementWidgets.get(value.getName()); 301 if (result == null) { 302 result = OpenCms.getXmlContentTypeManager().getWidgetDefault(value.getTypeName()); 304 } else { 305 result = result.newInstance(); 306 } 307 result.setConfiguration(getConfiguration(value)); 309 310 return result; 311 } 312 313 316 public synchronized void initialize(Element appInfoElement, CmsXmlContentDefinition contentDefinition) 317 throws CmsXmlException { 318 319 if (appInfoElement == null) { 320 return; 322 } 323 324 validateAppinfoElement(appInfoElement); 326 327 init(); 329 330 Iterator i = appInfoElement.elements().iterator(); 331 while (i.hasNext()) { 332 Element element = (Element)i.next(); 334 String nodeName = element.getName(); 335 if (nodeName.equals(APPINFO_MAPPINGS)) { 336 initMappings(element, contentDefinition); 337 } else if (nodeName.equals(APPINFO_LAYOUTS)) { 338 initLayouts(element, contentDefinition); 339 } else if (nodeName.equals(APPINFO_VALIDATIONRULES)) { 340 initValidationRules(element, contentDefinition); 341 } else if (nodeName.equals(APPINFO_DEFAULTS)) { 342 initDefaultValues(element, contentDefinition); 343 } else if (nodeName.equals(APPINFO_PREVIEW)) { 344 initPreview(element, contentDefinition); 345 } else if (nodeName.equals(APPINFO_RESOURCEBUNDLE)) { 346 initResourceBundle(element, contentDefinition); 347 } 348 } 349 } 350 351 354 public CmsFile prepareForWrite(CmsObject cms, CmsXmlContent content, CmsFile file) throws CmsException { 355 356 content.validateXmlStructure(new CmsXmlEntityResolver(cms)); 359 String contentConversion = CmsHtmlConverter.getConversionSettings(cms, file); 361 if (CmsStringUtil.isEmptyOrWhitespaceOnly(contentConversion)) { 362 contentConversion = CmsHtmlConverter.PARAM_XHTML; 364 } 365 content.setConversion(contentConversion); 366 file = content.correctXmlStructure(cms); 368 content.setFile(file); 369 content.resolveMappings(cms); 371 removeEmptyMappings(cms, content); 373 374 return file; 375 } 376 377 380 public void resolveMapping(CmsObject cms, CmsXmlContent content, I_CmsXmlContentValue value) throws CmsException { 381 382 if (!value.isSimpleType()) { 383 return; 387 } 388 389 CmsFile file = content.getFile(); 391 if (file == null) { 392 throw new CmsXmlException(Messages.get().container(Messages.ERR_XMLCONTENT_RESOLVE_FILE_NOT_FOUND_0)); 393 } 394 395 String mapping = getMapping(value.getPath()); 397 398 if (CmsStringUtil.isNotEmpty(mapping)) { 399 400 String rootPath = content.getFile().getRootPath(); 402 403 try { 404 cms.getRequestContext().saveSiteRoot(); 406 cms.getRequestContext().setSiteRoot("/"); 407 408 List siblings = cms.readSiblings(rootPath, CmsResourceFilter.IGNORE_EXPIRATION); 410 411 415 for (int i = (siblings.size() - 1); i >= 0; i--) { 416 String filename = ((CmsResource)siblings.get(i)).getRootPath(); 418 Locale locale = OpenCms.getLocaleManager().getDefaultLocale(cms, filename); 419 420 if (!locale.equals(value.getLocale())) { 421 continue; 423 } 424 425 String stringValue = value.getStringValue(cms); 427 if (mapping.startsWith(MAPTO_PROPERTY_LIST) && (value.getIndex() == 0)) { 428 429 boolean mapToShared; 430 int prefixLength; 431 if (mapping.startsWith(MAPTO_PROPERTY_LIST_SHARED)) { 433 mapToShared = true; 434 prefixLength = MAPTO_PROPERTY_LIST_SHARED.length(); 435 } else if (mapping.startsWith(MAPTO_PROPERTY_LIST_INDIVIDUAL)) { 436 mapToShared = false; 437 prefixLength = MAPTO_PROPERTY_LIST_INDIVIDUAL.length(); 438 } else { 439 mapToShared = false; 440 prefixLength = MAPTO_PROPERTY_LIST.length(); 441 } 442 443 String property = mapping.substring(prefixLength); 445 446 String path = CmsXmlUtils.removeXpathIndex(value.getPath()); 447 List values = content.getValues(path, locale); 448 Iterator j = values.iterator(); 449 StringBuffer result = new StringBuffer (values.size() * 64); 450 while (j.hasNext()) { 451 I_CmsXmlContentValue val = (I_CmsXmlContentValue)j.next(); 452 result.append(val.getStringValue(cms)); 453 if (j.hasNext()) { 454 result.append(CmsProperty.VALUE_LIST_DELIMITER); 455 } 456 } 457 458 CmsProperty p; 459 if (mapToShared) { 460 p = new CmsProperty(property, null, result.toString()); 462 } else { 463 p = new CmsProperty(property, result.toString(), null); 465 } 466 cms.writePropertyObject(filename, p); 468 if (mapToShared) { 469 i = 0; 471 } 472 473 } else if (mapping.startsWith(MAPTO_PROPERTY)) { 474 475 boolean mapToShared; 476 int prefixLength; 477 if (mapping.startsWith(MAPTO_PROPERTY_SHARED)) { 479 mapToShared = true; 480 prefixLength = MAPTO_PROPERTY_SHARED.length(); 481 } else if (mapping.startsWith(MAPTO_PROPERTY_INDIVIDUAL)) { 482 mapToShared = false; 483 prefixLength = MAPTO_PROPERTY_INDIVIDUAL.length(); 484 } else { 485 mapToShared = false; 486 prefixLength = MAPTO_PROPERTY.length(); 487 } 488 489 String property = mapping.substring(prefixLength); 491 492 CmsProperty p; 493 if (mapToShared) { 494 p = new CmsProperty(property, null, stringValue); 496 } else { 497 p = new CmsProperty(property, stringValue, null); 499 } 500 cms.writePropertyObject(filename, p); 502 if (mapToShared) { 503 i = 0; 505 } 506 507 } else if (mapping.startsWith(MAPTO_ATTRIBUTE)) { 508 509 String attribute = mapping.substring(MAPTO_ATTRIBUTE.length()); 511 switch (ATTRIBUTES.indexOf(attribute)) { 512 case 0: long date; 514 date = Long.valueOf(stringValue).longValue(); 515 if (date == 0) { 516 date = CmsResource.DATE_RELEASED_DEFAULT; 517 } 518 file.setDateReleased(date); 519 break; 520 case 1: date = Long.valueOf(stringValue).longValue(); 522 if (date == 0) { 523 date = CmsResource.DATE_EXPIRED_DEFAULT; 524 } 525 file.setDateExpired(date); 526 break; 527 default: 528 } 530 } 531 } 532 533 } finally { 534 cms.getRequestContext().restoreSiteRoot(); 536 } 537 } 538 } 539 540 543 public CmsXmlContentErrorHandler resolveValidation( 544 CmsObject cms, 545 I_CmsXmlContentValue value, 546 CmsXmlContentErrorHandler errorHandler) { 547 548 if (errorHandler == null) { 549 errorHandler = new CmsXmlContentErrorHandler(); 551 } 552 553 if (!value.isSimpleType()) { 554 return errorHandler; 558 } 559 560 errorHandler = validateValue(cms, value, errorHandler, m_validationErrorRules, false); 562 errorHandler = validateValue(cms, value, errorHandler, m_validationWarningRules, true); 564 565 return errorHandler; 567 } 568 569 578 protected void addConfiguration( 579 CmsXmlContentDefinition contentDefinition, 580 String elementName, 581 String configurationValue) throws CmsXmlException { 582 583 if (contentDefinition.getSchemaType(elementName) == null) { 584 throw new CmsXmlException(Messages.get().container( 585 Messages.ERR_XMLCONTENT_CONFIG_ELEM_UNKNOWN_1, 586 elementName)); 587 } 588 589 m_configurationValues.put(elementName, configurationValue); 590 } 591 592 601 protected void addDefault(CmsXmlContentDefinition contentDefinition, String elementName, String defaultValue) 602 throws CmsXmlException { 603 604 if (contentDefinition.getSchemaType(elementName) == null) { 605 throw new CmsXmlException(org.opencms.xml.types.Messages.get().container( 606 org.opencms.xml.types.Messages.ERR_XMLCONTENT_INVALID_ELEM_DEFAULT_1, 607 elementName)); 608 } 609 String xpath = CmsXmlUtils.createXpath(elementName, 1); 611 m_defaultValues.put(xpath, defaultValue); 612 } 613 614 623 protected void addMapping(CmsXmlContentDefinition contentDefinition, String elementName, String mapping) 624 throws CmsXmlException { 625 626 if (contentDefinition.getSchemaType(elementName) == null) { 627 throw new CmsXmlException(Messages.get().container( 628 Messages.ERR_XMLCONTENT_INVALID_ELEM_MAPPING_1, 629 elementName)); 630 } 631 632 String xpath = CmsXmlUtils.createXpath(elementName, 1); 634 m_elementMappings.put(xpath, mapping); 635 } 636 637 648 protected void addValidationRule( 649 CmsXmlContentDefinition contentDefinition, 650 String elementName, 651 String regex, 652 String message, 653 boolean isWarning) throws CmsXmlException { 654 655 if (contentDefinition.getSchemaType(elementName) == null) { 656 throw new CmsXmlException(Messages.get().container( 657 Messages.ERR_XMLCONTENT_INVALID_ELEM_VALIDATION_1, 658 elementName)); 659 } 660 661 if (isWarning) { 662 m_validationWarningRules.put(elementName, regex); 663 if (message != null) { 664 m_validationWarningMessages.put(elementName, message); 665 } 666 } else { 667 m_validationErrorRules.put(elementName, regex); 668 if (message != null) { 669 m_validationErrorMessages.put(elementName, message); 670 } 671 } 672 } 673 674 683 protected void addWidget(CmsXmlContentDefinition contentDefinition, String elementName, String widgetClassOrAlias) 684 throws CmsXmlException { 685 686 if (contentDefinition.getSchemaType(elementName) == null) { 687 throw new CmsXmlException(Messages.get().container( 688 Messages.ERR_XMLCONTENT_INVALID_ELEM_LAYOUTWIDGET_1, 689 elementName)); 690 } 691 692 I_CmsWidget widget = OpenCms.getXmlContentTypeManager().getWidget(widgetClassOrAlias); 694 695 if (widget == null) { 696 if (CmsStringUtil.isValidJavaClassName(widgetClassOrAlias)) { 698 try { 700 Class specialWidgetClass = Class.forName(widgetClassOrAlias); 701 widget = (I_CmsWidget)specialWidgetClass.newInstance(); 702 } catch (Exception e) { 703 throw new CmsXmlException(Messages.get().container( 704 Messages.ERR_XMLCONTENT_INVALID_CUSTOM_CLASS_3, 705 widgetClassOrAlias, 706 elementName, 707 contentDefinition.getSchemaLocation()), e); 708 } 709 } 710 if (widget == null) { 711 throw new CmsXmlException(Messages.get().container( 713 Messages.ERR_XMLCONTENT_INVALID_WIDGET_3, 714 widgetClassOrAlias, 715 elementName, 716 contentDefinition.getSchemaLocation())); 717 } 718 } 719 m_elementWidgets.put(elementName, widget); 720 } 721 722 734 protected String getValidationMessage( 735 CmsObject cms, 736 I_CmsXmlContentValue value, 737 String regex, 738 String valueStr, 739 boolean matchResult, 740 boolean isWarning) { 741 742 String message = null; 743 if (isWarning) { 744 message = (String )m_validationWarningMessages.get(value.getName()); 745 } else { 746 message = (String )m_validationErrorMessages.get(value.getName()); 747 } 748 749 if (message == null) { 750 if (isWarning) { 751 message = MESSAGE_VALIDATION_DEFAULT_WARNING; 752 } else { 753 message = MESSAGE_VALIDATION_DEFAULT_ERROR; 754 } 755 } 756 757 Map additionalValues = new HashMap (); 759 additionalValues.put(CmsMacroResolver.KEY_VALIDATION_VALUE, valueStr); 760 additionalValues.put(CmsMacroResolver.KEY_VALIDATION_REGEX, ((!matchResult) ? "!" : "") + regex); 761 additionalValues.put(CmsMacroResolver.KEY_VALIDATION_PATH, value.getPath()); 762 763 CmsMacroResolver resolver = CmsMacroResolver.newInstance().setCmsObject(cms).setMessages( 764 getMessages(cms.getRequestContext().getLocale())).setAdditionalMacros(additionalValues); 765 766 return resolver.resolveMacros(message); 767 } 768 769 772 protected void init() { 773 774 m_elementMappings = new HashMap (); 775 m_elementWidgets = new HashMap (); 776 m_validationErrorRules = new HashMap (); 777 m_validationErrorMessages = new HashMap (); 778 m_validationWarningRules = new HashMap (); 779 m_validationWarningMessages = new HashMap (); 780 m_defaultValues = new HashMap (); 781 m_configurationValues = new HashMap (); 782 m_previewLocation = null; 783 } 784 785 796 protected void initDefaultValues(Element root, CmsXmlContentDefinition contentDefinition) throws CmsXmlException { 797 798 Iterator i = root.elementIterator(APPINFO_DEFAULT); 799 while (i.hasNext()) { 800 Element element = (Element)i.next(); 802 String elementName = element.attributeValue(APPINFO_ATTR_ELEMENT); 803 String defaultValue = element.attributeValue(APPINFO_ATTR_VALUE); 804 if ((elementName != null) && (defaultValue != null)) { 805 addDefault(contentDefinition, elementName, defaultValue); 807 } 808 } 809 } 810 811 836 protected void initLayouts(Element root, CmsXmlContentDefinition contentDefinition) throws CmsXmlException { 837 838 Iterator i = root.elementIterator(APPINFO_LAYOUT); 839 while (i.hasNext()) { 840 Element element = (Element)i.next(); 842 String elementName = element.attributeValue(APPINFO_ATTR_ELEMENT); 843 String widgetClassOrAlias = element.attributeValue(APPINFO_ATTR_WIDGET); 844 String configuration = element.attributeValue(APPINFO_ATTR_CONFIGURATION); 845 if ((elementName != null) && (widgetClassOrAlias != null)) { 846 addWidget(contentDefinition, elementName, widgetClassOrAlias); 848 if (configuration != null) { 849 addConfiguration(contentDefinition, elementName, configuration); 850 } 851 } 852 } 853 } 854 855 866 protected void initMappings(Element root, CmsXmlContentDefinition contentDefinition) throws CmsXmlException { 867 868 Iterator i = root.elementIterator(APPINFO_MAPPING); 869 while (i.hasNext()) { 870 Element element = (Element)i.next(); 872 String elementName = element.attributeValue(APPINFO_ATTR_ELEMENT); 874 String maptoName = element.attributeValue(APPINFO_ATTR_MAPTO); 875 if ((elementName != null) && (maptoName != null)) { 876 addMapping(contentDefinition, elementName, maptoName); 878 } 879 } 880 } 881 882 889 protected void initPreview(Element root, CmsXmlContentDefinition contentDefinition) throws CmsXmlException { 890 891 String preview = root.attributeValue(APPINFO_ATTR_URI); 892 if (preview == null) { 893 throw new CmsXmlException(Messages.get().container( 894 Messages.ERR_XMLCONTENT_MISSING_PREVIEW_URI_2, 895 root.getName(), 896 contentDefinition.getSchemaLocation())); 897 } 898 m_previewLocation = preview; 899 } 900 901 909 protected void initResourceBundle(Element root, CmsXmlContentDefinition contentDefinition) throws CmsXmlException { 910 911 String name = root.attributeValue(APPINFO_ATTR_NAME); 912 if (name == null) { 913 throw new CmsXmlException(Messages.get().container( 914 Messages.ERR_XMLCONTENT_MISSING_RESOURCE_BUNDLE_NAME_2, 915 root.getName(), 916 contentDefinition.getSchemaLocation())); 917 } 918 m_messageBundleName = name; 919 } 920 921 932 protected void initValidationRules(Element root, CmsXmlContentDefinition contentDefinition) throws CmsXmlException { 933 934 Iterator i = root.elementIterator(APPINFO_RULE); 935 while (i.hasNext()) { 936 Element element = (Element)i.next(); 938 String elementName = element.attributeValue(APPINFO_ATTR_ELEMENT); 939 String regex = element.attributeValue(APPINFO_ATTR_REGEX); 940 String type = element.attributeValue(APPINFO_ATTR_TYPE); 941 String message = element.attributeValue(APPINFO_ATTR_MESSAGE); 942 if ((elementName != null) && (regex != null)) { 943 addValidationRule( 945 contentDefinition, 946 elementName, 947 regex, 948 message, 949 APPINFO_ATTR_TYPE_WARNING.equals(type)); 950 } 951 } 952 } 953 954 970 protected String key(String keyName, Locale locale) { 971 972 CmsMessages messages = getMessages(locale); 973 if (messages != null) { 974 return messages.key(keyName); 975 } 976 return CmsMessages.formatUnknownKey(keyName); 977 } 978 979 987 protected void removeEmptyMappings(CmsObject cms, CmsXmlContent content) throws CmsException { 988 989 Iterator mappings = m_elementMappings.keySet().iterator(); 990 991 String rootPath = null; 992 List siblings = null; 993 994 while (mappings.hasNext()) { 995 String path = (String )mappings.next(); 996 String mapping = (String )m_elementMappings.get(path); 997 998 if (mapping.startsWith(MAPTO_PROPERTY_LIST) || mapping.startsWith(MAPTO_PROPERTY)) { 999 1000 if (rootPath == null) { 1002 rootPath = content.getFile().getRootPath(); 1003 } 1004 1005 try { 1006 cms.getRequestContext().saveSiteRoot(); 1008 cms.getRequestContext().setSiteRoot("/"); 1009 1010 if (siblings == null) { 1012 siblings = cms.readSiblings(rootPath, CmsResourceFilter.IGNORE_EXPIRATION); 1013 } 1014 1015 for (int i = 0; i < siblings.size(); i++) { 1016 1017 String filename = ((CmsResource)siblings.get(i)).getRootPath(); 1019 Locale locale = OpenCms.getLocaleManager().getDefaultLocale(cms, filename); 1020 1021 if (!content.hasLocale(locale)) { 1022 continue; 1024 } 1025 if (content.hasValue(path, locale)) { 1026 continue; 1028 } 1029 1030 String property; 1031 if (mapping.startsWith(MAPTO_PROPERTY_LIST)) { 1032 property = mapping.substring(MAPTO_PROPERTY_LIST.length()); 1034 } else { 1035 property = mapping.substring(MAPTO_PROPERTY.length()); 1037 } 1038 cms.writePropertyObject(filename, new CmsProperty(property, CmsProperty.DELETE_VALUE, null)); 1040 } 1041 1042 } finally { 1043 cms.getRequestContext().restoreSiteRoot(); 1045 } 1046 } 1047 } 1048 } 1049 1050 1058 protected void validateAppinfoElement(Element appinfoElement) throws CmsXmlException { 1059 1060 Document doc = DocumentHelper.createDocument(); 1062 Element root = doc.addElement(APPINFO_APPINFO); 1063 root.add(I_CmsXmlSchemaType.XSI_NAMESPACE); 1065 root.addAttribute(I_CmsXmlSchemaType.XSI_NAMESPACE_ATTRIBUTE_NO_SCHEMA_LOCATION, APPINFO_SCHEMA_SYSTEM_ID); 1066 root.appendContent(appinfoElement); 1068 CmsXmlUtils.validateXmlStructure(doc, CmsEncoder.ENCODING_UTF_8, new CmsXmlEntityResolver(null)); 1070 } 1071 1072 1083 protected CmsXmlContentErrorHandler validateValue( 1084 CmsObject cms, 1085 I_CmsXmlContentValue value, 1086 CmsXmlContentErrorHandler errorHandler, 1087 Map rules, 1088 boolean isWarning) { 1089 1090 String valueStr; 1091 try { 1092 valueStr = value.getStringValue(cms); 1093 } catch (Exception e) { 1094 errorHandler.addError(value, e.getMessage()); 1096 return errorHandler; 1097 } 1098 1099 String regex = (String )rules.get(value.getName()); 1100 if (regex == null) { 1101 return validateValue(cms, value, valueStr, errorHandler, isWarning); 1103 } 1104 1105 boolean matchResult = true; 1106 if (regex.charAt(0) == '!') { 1107 matchResult = false; 1109 regex = regex.substring(1); 1110 } 1111 1112 String matchValue = valueStr; 1113 if (matchValue == null) { 1114 matchValue = ""; 1116 } 1117 1118 if (matchResult != Pattern.matches(regex, matchValue)) { 1120 String message = getValidationMessage(cms, value, regex, valueStr, matchResult, isWarning); 1122 if (isWarning) { 1123 errorHandler.addWarning(value, message); 1124 } else { 1125 errorHandler.addError(value, message); 1126 return errorHandler; 1128 } 1129 } 1130 1131 return validateValue(cms, value, valueStr, errorHandler, isWarning); 1133 } 1134 1135 1148 protected CmsXmlContentErrorHandler validateValue( 1149 CmsObject cms, 1150 I_CmsXmlContentValue value, 1151 String valueStr, 1152 CmsXmlContentErrorHandler errorHandler, 1153 boolean isWarning) { 1154 1155 if (isWarning) { 1156 return errorHandler; 1158 } 1159 1160 if (!value.validateValue(valueStr)) { 1161 String message = getValidationMessage(cms, value, value.getTypeName(), valueStr, true, false); 1163 errorHandler.addError(value, message); 1164 } 1165 1166 return errorHandler; 1167 } 1168} | Popular Tags |