01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.forms.validation;
18:
19: import org.xml.sax.ContentHandler;
20: import org.xml.sax.SAXException;
21: import org.apache.cocoon.forms.util.I18nMessage;
22: import org.apache.cocoon.forms.util.StringMessage;
23: import org.apache.commons.lang.ObjectUtils;
24: import org.apache.excalibur.xml.sax.XMLizable;
25:
26: /**
27: * An object that holds a validation error message. The error message can
28: * be a simple string or a piece of XML.
29: *
30: * @version $Id: ValidationError.java 449149 2006-09-23 03:58:05Z crossley $
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 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 errorMessageKey) {
52: this (new I18nMessage(errorMessageKey));
53: }
54:
55: /**
56: * @see I18nMessage#I18nMessage(java.lang.String, java.lang.String[])
57: */
58: public ValidationError(String errorMessageKey, String[] 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 errorMessageKey, String[] parameters,
66: boolean[] keys) {
67: this (new I18nMessage(errorMessageKey, parameters, keys));
68: }
69:
70: /**
71: * @param errorMessage the errormessages in the form of something that is "XMLizable",
72: * i.e. can produce SAX events. It should however not produce start/endDocument calls,
73: * only a piece of embeddable, stand-alone SAX events. Helpful implementations are
74: * {@link org.apache.cocoon.xml.SaxBuffer SaxBuffer}, {@link I18nMessage} or {@link StringMessage}.
75: */
76: public ValidationError(XMLizable errorMessage) {
77: this .saxFragment = errorMessage;
78: }
79:
80: /**
81: * Generates SAX events for this ValidationError. In case of the constructors where
82: * a String error message key was supplied, the necessary I18n tags will be generated.
83: */
84: public void generateSaxFragment(ContentHandler contentHandler)
85: throws SAXException {
86: if (this .saxFragment != null) {
87: this .saxFragment.toSAX(contentHandler);
88: }
89: }
90:
91: public boolean equals(Object obj) {
92: if (obj instanceof ValidationError) {
93: return ObjectUtils.equals(this .saxFragment,
94: ((ValidationError) obj).saxFragment);
95: }
96: return false;
97: }
98: }
|