KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > datatype > convertor > ConversionResult


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.datatype.convertor;
17
18 import org.apache.cocoon.forms.validation.ValidationError;
19 import org.apache.cocoon.forms.util.I18nMessage;
20 import org.apache.cocoon.forms.FormsConstants;
21
22 /**
23  * Object returned as result of {@link Convertor#convertFromString(java.lang.String, java.util.Locale, org.apache.cocoon.forms.datatype.convertor.Convertor.FormatCache)}.
24  */

25 public class ConversionResult {
26     private ValidationError validationError;
27     private boolean successful;
28     private Object JavaDoc result;
29
30     /**
31      * Constructs a successful ConversionResult.
32      */

33     public ConversionResult(Object JavaDoc result) {
34         this.successful = true;
35         this.result = result;
36     }
37
38     /**
39      * Constructs an unsuccessful ConversionResult.
40      */

41     public ConversionResult(ValidationError validationError) {
42         this.successful = false;
43         this.validationError = validationError;
44     }
45
46     /**
47      * Constructs an unsuccessful ConversionResult. Will create
48      * a default ValidationError message using the given
49      * datatypeName.
50      *
51      * <p>Note: this is not done as a constructor because
52      * it would conflict with the constructor which takes
53      * an Object as argument.
54      */

55     public static ConversionResult create(String JavaDoc datatypeName) {
56         ValidationError validationError = new ValidationError(new I18nMessage(
57             "datatype.conversion-failed",
58             new String JavaDoc[] {"datatype." + datatypeName},
59             new boolean[] { true },
60             FormsConstants.I18N_CATALOGUE
61         ));
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 JavaDoc("Cannot call getValidationError() if conversion is successful.");
72
73         return validationError;
74     }
75
76     public Object JavaDoc getResult() {
77         if (!successful)
78             throw new IllegalStateException JavaDoc("Cannot call getResult() if conversion is not successful.");
79
80         return result;
81     }
82 }
83
Popular Tags