KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cintoo > messages > error > ErrorHandler


1 /*
2  * Copyright 2006 cintoo, Berlin, Germany
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 cintoo.messages.error;
17
18 import api.cintoo.messages.MessageHandler;
19 import api.cintoo.messages.Messages;
20 import api.cintoo.messages.context.Context;
21 import api.cintoo.messages.context.Contexts;
22
23 import java.text.MessageFormat JavaDoc;
24
25 import cintoo.messages.context.IdContext;
26 import cintoo.messages.context.DefaultContextCache;
27 import cintoo.messages.DefaultMessageHandler;
28 import cintoo.messages.bundle.DefaultBundleManager;
29
30 /**
31  * Format errors from error codes.
32  *
33  * @author Stephan J. Schmidt
34  * @version $id$
35  * @since 1.0
36  */

37 public class ErrorHandler {
38   private Context errorContext;
39
40   private MessageHandler handler;
41
42   private String JavaDoc pattern;
43
44   public ErrorHandler(MessageHandler handler) {
45     this(handler, IdContext.id("errors"));
46   }
47
48   public ErrorHandler(String JavaDoc errorContext) {
49     this(IdContext.id(errorContext));
50   }
51
52   public ErrorHandler(MessageHandler handler, Context errorContext) {
53     this.errorContext = errorContext;
54     this.handler = handler;
55     clearPattern();
56     handler.setBundle("errors", this.errorContext);
57   }
58
59   public ErrorHandler(Context errorContext) {
60     this(new DefaultMessageHandler(new DefaultBundleManager(new DefaultContextCache())), errorContext);
61   }
62
63   public ErrorHandler() {
64     this("errors");
65   }
66
67   public void setPattern(String JavaDoc pattern) {
68     this.pattern = pattern;
69   }
70
71   public String JavaDoc error(Context context, ErrorCode errorCode) {
72      return MessageFormat.format(pattern, handler.format(context, errorCode.getError()), errorCode.getError());
73  }
74
75   public String JavaDoc error(Object JavaDoc context, ErrorCode errorCode) {
76     return MessageFormat.format(pattern, handler.format(context, errorCode.getError()), errorCode.getError());
77   }
78
79   public String JavaDoc error(String JavaDoc context, ErrorCode errorCode) {
80     return MessageFormat.format(pattern, handler.format(Contexts.createFromString(context), errorCode.getError()), errorCode.getError());
81   }
82
83   public String JavaDoc error(ErrorCode errorCode) {
84     return error(errorContext, errorCode);
85   }
86
87   public Context getErrorContext() {
88     return errorContext;
89   }
90
91   public void setErrorContext(Context errorContext) {
92     this.errorContext = errorContext;
93   }
94
95   public void clearPattern() {
96     setPattern("{0}");
97   }
98
99    /**
100    * Set global bundle name
101    *
102    * @param bundleName bundle name for the bundle to be used
103    */

104   public void setBundle(String JavaDoc bundleName) {
105     handler.setBundle(bundleName);
106   }
107
108   /**
109    * Set bundle for the package scope
110    *
111    * @param bundleName name of the bundle to set
112    * @param packageName package name which defines the scope
113    */

114   public void setBundle(String JavaDoc bundleName, String JavaDoc packageName) {
115     handler.setBundle(bundleName, packageName);
116   }
117
118   /**
119    * Set bundle for the context scope
120    *
121    * @param bundleName name of the bundle to set
122    * @param context context wich defines the scope
123    */

124   public void setBundle(String JavaDoc bundleName, Context context) {
125     handler.setBundle(bundleName, context);
126   }
127 }
128
Popular Tags