KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > xml > content > CmsXmlContentErrorHandler


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/xml/content/CmsXmlContentErrorHandler.java,v $
3  * Date : $Date: 2006/03/27 14:52:36 $
4  * Version: $Revision: 1.12 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.xml.content;
33
34 import org.opencms.main.CmsLog;
35 import org.opencms.xml.types.I_CmsXmlContentValue;
36
37 import java.util.HashMap JavaDoc;
38 import java.util.Locale JavaDoc;
39 import java.util.Map JavaDoc;
40
41 import org.apache.commons.logging.Log;
42
43 /**
44  * Handler for issues found during XML content validation.<p>
45  *
46  * @author Alexander Kandzior
47  *
48  * @version $Revision: 1.12 $
49  *
50  * @since 6.0.0
51  */

52 public class CmsXmlContentErrorHandler {
53
54     /** Static reference to the log. */
55     private static final Log LOG = CmsLog.getLog(CmsXmlContentErrorHandler.class);
56
57     /** The list of validation errors. */
58     private Map JavaDoc m_errors;
59
60     /** Indicates that the validated content has errors. */
61     private boolean m_hasErrors;
62
63     /** Indicates that the validated content has warnings. */
64     private boolean m_hasWarnings;
65
66     /** The list of validation warnings. */
67     private Map JavaDoc m_warnings;
68
69     /**
70      * Create a new instance of the validation handler.<p>
71      */

72     public CmsXmlContentErrorHandler() {
73
74         // initialize the internal error / warning list
75
m_warnings = new HashMap JavaDoc();
76         m_errors = new HashMap JavaDoc();
77     }
78
79     /**
80      * Adds an error message to the internal list of errors,
81      * also raised the "has errors" flag.<p>
82      *
83      * @param value the value that contains the error
84      * @param message the error message to add
85      */

86     public void addError(I_CmsXmlContentValue value, String JavaDoc message) {
87
88         m_hasErrors = true;
89         Locale JavaDoc locale = value.getLocale();
90         Map JavaDoc localeErrors = getLocalIssueMap(m_errors, locale);
91         localeErrors.put(value.getPath(), message);
92
93         if (LOG.isDebugEnabled()) {
94             LOG.debug(Messages.get().getBundle().key(Messages.LOG_XMLCONTENT_VALIDATION_ERR_2, value.getPath(), message));
95         }
96     }
97
98     /**
99      * Adds an warning message to the internal list of errors,
100      * also raised the "has warning" flag.<p>
101      *
102      * @param value the value that contians the warning
103      * @param message the warning message to add
104      */

105     public void addWarning(I_CmsXmlContentValue value, String JavaDoc message) {
106
107         m_hasWarnings = true;
108         Locale JavaDoc locale = value.getLocale();
109         Map JavaDoc localeWarnings = getLocalIssueMap(m_warnings, locale);
110         localeWarnings.put(value.getPath(), message);
111
112         if (LOG.isDebugEnabled()) {
113             LOG.debug(Messages.get().getBundle().key(
114                 Messages.LOG_XMLCONTENT_VALIDATION_WARN_2,
115                 value.getPath(),
116                 message));
117         }
118     }
119
120     /**
121      * Returns the map of validation errors.<p>
122      *
123      * The map contains further maps. The key of the "first" map is the
124      * {@link java.util.Locale} of the language where issues where found. The key of the "second" map
125      * is a mapping from the element node name obtained with {@link I_CmsXmlContentValue#getPath()} to the error message
126      * which is a String.<p>
127      *
128      * @return the map of validation errors
129      */

130     public Map JavaDoc getErrors() {
131
132         return m_errors;
133     }
134
135     /**
136      * Returns the Map of errors for the selected locale.<p>
137      *
138      * @param locale the locale to get the errors for
139      *
140      * @return the Map of errors for the selected locale
141      */

142     public Map JavaDoc getErrors(Locale JavaDoc locale) {
143
144         return (Map JavaDoc)m_errors.get(locale);
145     }
146
147     /**
148      * Returns the map of validation warnings.<p>
149      *
150      * The map contains further maps. The key of the "first" map is the
151      * {@link java.util.Locale} of the language where issues where found. The key of the "second" map
152      * is a mapping from the element node name obtained with {@link I_CmsXmlContentValue#getPath()} to the error message
153      * which is a String.<p>
154      *
155      * @return the map of validation warnings
156      */

157     public Map JavaDoc getWarnings() {
158
159         return m_warnings;
160     }
161
162     /**
163      * Returns the Map of warnings for the selected locale.<p>
164      *
165      * @param locale the locale to get the warnings for
166      *
167      * @return the Map of warnings for the selected locale
168      */

169     public Map JavaDoc getWarnings(Locale JavaDoc locale) {
170
171         return (Map JavaDoc)m_warnings.get(locale);
172     }
173
174     /**
175      * Returns true if the validated content had errors.<p>
176      *
177      * @return true if the validated content had errors
178      */

179     public boolean hasErrors() {
180
181         return m_hasErrors;
182     }
183
184     /**
185      * Returns <code>true</code> if there is at last one error in the selected locale.<p>
186      *
187      * @param locale the locale to check
188      *
189      * @return <code>true</code> if there is at last one error in the selected locale
190      */

191     public boolean hasErrors(Locale JavaDoc locale) {
192
193         return null != getErrors(locale);
194     }
195
196     /**
197      * Returns true if the validated content has warnings.<p>
198      *
199      * @return true if the validated content had warnings
200      */

201     public boolean hasWarnings() {
202
203         return m_hasWarnings;
204     }
205
206     /**
207      * Returns <code>true</code> if there is at last one warning in the selected locale.<p>
208      *
209      * @param locale the locale to check
210      *
211      * @return <code>true</code> if there is at last one warning in the selected locale
212      */

213     public boolean hasWarnings(Locale JavaDoc locale) {
214
215         return null != getWarnings(locale);
216     }
217
218     /**
219      * Returns the localized issue map from the given base map.<p>
220      *
221      * If the base map does not contains an issue map for the given locale,
222      * a new map is added for the locale.<p>
223      *
224      * @param base the base issue map
225      * @param locale the locale to get the localized issue map for
226      *
227      * @return the localized issue map from the given base map
228      */

229     private Map JavaDoc getLocalIssueMap(Map JavaDoc base, Locale JavaDoc locale) {
230
231         Map JavaDoc result = (Map JavaDoc)base.get(locale);
232         if (result == null) {
233             result = new HashMap JavaDoc();
234             base.put(locale, result);
235         }
236         return result;
237     }
238 }
Popular Tags