001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software GmbH & Co. KG, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.xml.content; 029 030import org.opencms.main.CmsLog; 031import org.opencms.xml.types.I_CmsXmlContentValue; 032 033import java.util.HashMap; 034import java.util.Locale; 035import java.util.Map; 036 037import org.apache.commons.logging.Log; 038 039/** 040 * Handler for issues found during XML content validation.<p> 041 * 042 * @since 6.0.0 043 */ 044public class CmsXmlContentErrorHandler { 045 046 /** Static reference to the log. */ 047 private static final Log LOG = CmsLog.getLog(CmsXmlContentErrorHandler.class); 048 049 /** The list of validation errors. */ 050 private Map<Locale, Map<String, String>> m_errors; 051 052 /** Indicates that the validated content has errors. */ 053 private boolean m_hasErrors; 054 055 /** Indicates that the validated content has warnings. */ 056 private boolean m_hasWarnings; 057 058 /** The list of validation warnings. */ 059 private Map<Locale, Map<String, String>> m_warnings; 060 061 /** 062 * Create a new instance of the validation handler.<p> 063 */ 064 public CmsXmlContentErrorHandler() { 065 066 // initialize the internal error / warning list 067 m_warnings = new HashMap<Locale, Map<String, String>>(); 068 m_errors = new HashMap<Locale, Map<String, String>>(); 069 } 070 071 /** 072 * Adds an error message to the internal list of errors, 073 * also raised the "has errors" flag.<p> 074 * 075 * @param value the value that contains the error 076 * @param message the error message to add 077 */ 078 public void addError(I_CmsXmlContentValue value, String message) { 079 080 m_hasErrors = true; 081 Locale locale = value.getLocale(); 082 Map<String, String> localeErrors = getLocalIssueMap(m_errors, locale); 083 localeErrors.put(value.getPath(), message); 084 085 if (LOG.isDebugEnabled()) { 086 LOG.debug( 087 Messages.get().getBundle().key(Messages.LOG_XMLCONTENT_VALIDATION_ERR_2, value.getPath(), message)); 088 } 089 } 090 091 /** 092 * Adds an warning message to the internal list of errors, 093 * also raised the "has warning" flag.<p> 094 * 095 * @param value the value that contians the warning 096 * @param message the warning message to add 097 */ 098 public void addWarning(I_CmsXmlContentValue value, String message) { 099 100 m_hasWarnings = true; 101 Locale locale = value.getLocale(); 102 Map<String, String> localeWarnings = getLocalIssueMap(m_warnings, locale); 103 localeWarnings.put(value.getPath(), message); 104 105 if (LOG.isDebugEnabled()) { 106 LOG.debug( 107 Messages.get().getBundle().key(Messages.LOG_XMLCONTENT_VALIDATION_WARN_2, value.getPath(), message)); 108 } 109 } 110 111 /** 112 * Returns the map of validation errors.<p> 113 * 114 * The map contains further maps. The key of the "first" map is the 115 * {@link java.util.Locale} of the language where issues where found. The key of the "second" map 116 * is a mapping from the element node name obtained with {@link I_CmsXmlContentValue#getPath()} to the error message 117 * which is a String.<p> 118 * 119 * @return the map of validation errors 120 */ 121 public Map<Locale, Map<String, String>> getErrors() { 122 123 return m_errors; 124 } 125 126 /** 127 * Returns the Map of errors for the selected locale.<p> 128 * 129 * @param locale the locale to get the errors for 130 * 131 * @return the Map of errors for the selected locale 132 */ 133 public Map<String, String> getErrors(Locale locale) { 134 135 return m_errors.get(locale); 136 } 137 138 /** 139 * Returns the map of validation warnings.<p> 140 * 141 * The map contains further maps. The key of the "first" map is the 142 * {@link java.util.Locale} of the language where issues where found. The key of the "second" map 143 * is a mapping from the element node name obtained with {@link I_CmsXmlContentValue#getPath()} to the error message 144 * which is a String.<p> 145 * 146 * @return the map of validation warnings 147 */ 148 public Map<Locale, Map<String, String>> getWarnings() { 149 150 return m_warnings; 151 } 152 153 /** 154 * Returns the Map of warnings for the selected locale.<p> 155 * 156 * @param locale the locale to get the warnings for 157 * 158 * @return the Map of warnings for the selected locale 159 */ 160 public Map<String, String> getWarnings(Locale locale) { 161 162 return m_warnings.get(locale); 163 } 164 165 /** 166 * Returns true if the validated content had errors.<p> 167 * 168 * @return true if the validated content had errors 169 */ 170 public boolean hasErrors() { 171 172 return m_hasErrors; 173 } 174 175 /** 176 * Returns <code>true</code> if there is at last one error in the selected locale.<p> 177 * 178 * @param locale the locale to check 179 * 180 * @return <code>true</code> if there is at last one error in the selected locale 181 */ 182 public boolean hasErrors(Locale locale) { 183 184 return null != getErrors(locale); 185 } 186 187 /** 188 * Returns true if the validated content has warnings.<p> 189 * 190 * @return true if the validated content had warnings 191 */ 192 public boolean hasWarnings() { 193 194 return m_hasWarnings; 195 } 196 197 /** 198 * Returns <code>true</code> if there is at last one warning in the selected locale.<p> 199 * 200 * @param locale the locale to check 201 * 202 * @return <code>true</code> if there is at last one warning in the selected locale 203 */ 204 public boolean hasWarnings(Locale locale) { 205 206 return null != getWarnings(locale); 207 } 208 209 /** 210 * Returns the localized issue map from the given base map.<p> 211 * 212 * If the base map does not contains an issue map for the given locale, 213 * a new map is added for the locale.<p> 214 * 215 * @param base the base issue map 216 * @param locale the locale to get the localized issue map for 217 * 218 * @return the localized issue map from the given base map 219 */ 220 private Map<String, String> getLocalIssueMap(Map<Locale, Map<String, String>> base, Locale locale) { 221 222 Map<String, String> result = base.get(locale); 223 if (result == null) { 224 result = new HashMap<String, String>(); 225 base.put(locale, result); 226 } 227 return result; 228 } 229}