KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > validation > ValidationError


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.forms.validation;
17
18 import org.xml.sax.ContentHandler JavaDoc;
19 import org.xml.sax.SAXException JavaDoc;
20 import org.apache.cocoon.forms.util.I18nMessage;
21 import org.apache.cocoon.forms.util.StringMessage;
22 import org.apache.commons.lang.ObjectUtils;
23 import org.apache.excalibur.xml.sax.XMLizable;
24
25 /**
26  * An object that holds a validation error message. The error message can
27  * be a simple string or a piece of XML.
28  *
29  * @author <a HREF="http://www.apache.org/~sylvain/">Sylvain Wallez</a>
30  * @version $Id: ValidationError.java 240119 2005-08-25 19:05:40Z joerg $
31  */

32 public class ValidationError {
33
34     /** Holds the error message. */
35     private final XMLizable saxFragment;
36
37     /**
38      * @param i18n should the errorMessage be interpreted as an i18n key?
39      */

40     public ValidationError(String JavaDoc errorMessage, boolean i18n) {
41         if (i18n) {
42             this.saxFragment = new I18nMessage(errorMessage);
43         } else {
44             this.saxFragment = new StringMessage(errorMessage);
45         }
46     }
47
48     /**
49      * @see I18nMessage#I18nMessage(java.lang.String)
50      */

51     public ValidationError(String JavaDoc errorMessageKey) {
52         this(new I18nMessage(errorMessageKey));
53     }
54
55     /**
56      * @see I18nMessage#I18nMessage(java.lang.String, java.lang.String[])
57      */

58     public ValidationError(String JavaDoc errorMessageKey, String JavaDoc[] parameters) {
59         this(new I18nMessage(errorMessageKey, parameters));
60     }
61
62     /**
63      * @see I18nMessage#I18nMessage(java.lang.String, java.lang.String[], boolean[])
64      */

65     public ValidationError(String JavaDoc errorMessageKey, String JavaDoc[] parameters, boolean[] keys) {
66         this(new I18nMessage(errorMessageKey, parameters, keys));
67     }
68
69     /**
70      * @param errorMessage the errormessages in the form of something that is "XMLizable",
71      * i.e. can produce SAX events. It should however not produce start/endDocument calls,
72      * only a piece of embeddable, stand-alone SAX events. Helpful implementations are
73      * {@link org.apache.cocoon.xml.SaxBuffer SaxBuffer}, {@link I18nMessage} or {@link StringMessage}.
74      */

75     public ValidationError(XMLizable errorMessage) {
76         this.saxFragment = errorMessage;
77     }
78
79     /**
80      * Generates SAX events for this ValidationError. In case of the constructors where
81      * a String error message key was supplied, the necessary I18n tags will be generated.
82      */

83     public void generateSaxFragment(ContentHandler JavaDoc contentHandler) throws SAXException JavaDoc {
84         if (this.saxFragment != null) {
85             this.saxFragment.toSAX(contentHandler);
86         }
87     }
88     
89     public boolean equals(Object JavaDoc obj) {
90         if (obj instanceof ValidationError) {
91             return ObjectUtils.equals(this.saxFragment, ((ValidationError)obj).saxFragment);
92         }
93         return false;
94     }
95 }
96
Popular Tags