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.datatype.convertor;
18:
19: import org.apache.cocoon.forms.validation.ValidationError;
20: import org.apache.cocoon.forms.util.I18nMessage;
21: import org.apache.cocoon.forms.FormsConstants;
22:
23: /**
24: * Object returned as result of {@link Convertor#convertFromString(java.lang.String, java.util.Locale, org.apache.cocoon.forms.datatype.convertor.Convertor.FormatCache)}.
25: */
26: public class ConversionResult {
27: private ValidationError validationError;
28: private boolean successful;
29: private Object result;
30:
31: /**
32: * Constructs a successful ConversionResult.
33: */
34: public ConversionResult(Object result) {
35: this .successful = true;
36: this .result = result;
37: }
38:
39: /**
40: * Constructs an unsuccessful ConversionResult.
41: */
42: public ConversionResult(ValidationError validationError) {
43: this .successful = false;
44: this .validationError = validationError;
45: }
46:
47: /**
48: * Constructs an unsuccessful ConversionResult. Will create
49: * a default ValidationError message using the given
50: * datatypeName.
51: *
52: * <p>Note: this is not done as a constructor because
53: * it would conflict with the constructor which takes
54: * an Object as argument.
55: */
56: public static ConversionResult create(String datatypeName) {
57: ValidationError validationError = new ValidationError(
58: new I18nMessage("datatype.conversion-failed",
59: new String[] { "datatype." + datatypeName },
60: new boolean[] { true },
61: FormsConstants.I18N_CATALOGUE));
62: return new ConversionResult(validationError);
63: }
64:
65: public boolean isSuccessful() {
66: return successful;
67: }
68:
69: public ValidationError getValidationError() {
70: if (successful)
71: throw new IllegalStateException(
72: "Cannot call getValidationError() if conversion is successful.");
73:
74: return validationError;
75: }
76:
77: public Object getResult() {
78: if (!successful)
79: throw new IllegalStateException(
80: "Cannot call getResult() if conversion is not successful.");
81:
82: return result;
83: }
84: }
|