1 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 ; 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 37 public class ErrorHandler { 38 private Context errorContext; 39 40 private MessageHandler handler; 41 42 private String pattern; 43 44 public ErrorHandler(MessageHandler handler) { 45 this(handler, IdContext.id("errors")); 46 } 47 48 public ErrorHandler(String 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 pattern) { 68 this.pattern = pattern; 69 } 70 71 public String error(Context context, ErrorCode errorCode) { 72 return MessageFormat.format(pattern, handler.format(context, errorCode.getError()), errorCode.getError()); 73 } 74 75 public String error(Object context, ErrorCode errorCode) { 76 return MessageFormat.format(pattern, handler.format(context, errorCode.getError()), errorCode.getError()); 77 } 78 79 public String error(String context, ErrorCode errorCode) { 80 return MessageFormat.format(pattern, handler.format(Contexts.createFromString(context), errorCode.getError()), errorCode.getError()); 81 } 82 83 public String 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 104 public void setBundle(String bundleName) { 105 handler.setBundle(bundleName); 106 } 107 108 114 public void setBundle(String bundleName, String packageName) { 115 handler.setBundle(bundleName, packageName); 116 } 117 118 124 public void setBundle(String bundleName, Context context) { 125 handler.setBundle(bundleName, context); 126 } 127 } 128 | Popular Tags |