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.woody.validation;
18:
19: import org.xml.sax.ContentHandler;
20: import org.xml.sax.SAXException;
21: import org.apache.cocoon.woody.util.I18nMessage;
22: import org.apache.cocoon.woody.util.StringMessage;
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 CVS $Id: ValidationError.java 433543 2006-08-22 06:22:54Z crossley $
31: */
32: public class ValidationError {
33:
34: /** Holds the error message. */
35: private 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: saxFragment = new I18nMessage(errorMessage);
43: } else {
44: saxFragment = new StringMessage(errorMessage);
45: }
46: }
47:
48: /**
49: * @see I18nMessage#I18nMessage(java.lang.String)
50: */
51: public ValidationError(String errorMessageKey) {
52: this .saxFragment = 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 .saxFragment = 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 .saxFragment = new I18nMessage(errorMessageKey, parameters,
68: keys);
69: }
70:
71: /**
72: * @param errorMessage the errormessages in the form of something that is "XMLizable",
73: * i.e. can produce SAX events. It should however not produce start/endDocument calls,
74: * only a piece of embeddable, stand-alone SAX events. Helpful implementations are
75: * {@link org.apache.cocoon.xml.SaxBuffer SaxBuffer}, {@link I18nMessage} or {@link StringMessage}.
76: */
77: public ValidationError(XMLizable errorMessage) {
78: this .saxFragment = errorMessage;
79: }
80:
81: /**
82: * Generates SAX events for this ValidationError. In case of the constructors where
83: * a String error message key was supplied, the necessary I18n tags will be generated.
84: */
85: public void generateSaxFragment(ContentHandler contentHandler)
86: throws SAXException {
87: if (saxFragment != null) {
88: saxFragment.toSAX(contentHandler);
89: }
90: }
91: }
|