1 16 package com.google.gwt.dev.util.msg; 17 18 import com.google.gwt.core.ext.TreeLogger; 19 20 import java.io.File ; 21 import java.lang.reflect.Method ; 22 import java.net.URL ; 23 24 30 public abstract class Message { 31 32 private static final Formatter FMT_TOSTRING = new FormatterToString(); 33 private static final Formatter FMT_CLASS = new FormatterForClass(); 34 private static final Formatter FMT_FILE = new FormatterForFile(); 35 private static final Formatter FMT_URL = new FormatterForURL(); 36 private static final Formatter FMT_INTEGER = new FormatterForInteger(); 37 private static final Formatter FMT_LONG = new FormatterForLong(); 38 private static final Formatter FMT_METHOD = new FormatterForMethod(); 39 private static final Formatter FMT_STRING = new FormatterForString(); 40 private static final Formatter FMT_STRING_ARRAY = new FormatterForStringArray(); 41 42 protected final TreeLogger.Type type; 43 44 protected final char[][] fmtParts; 45 46 protected final int[] argIndices; 47 48 protected final int minChars; 49 50 53 protected Message(TreeLogger.Type type, String fmt, int args) { 54 assert (type != null); 55 assert (fmt != null); 56 assert (args >= 0); 57 this.type = type; 58 59 fmtParts = new char[args + 1][]; 60 argIndices = new int[args]; 61 int from = 0; 62 for (int i = 0; i < args; ++i) { 63 int to = fmt.indexOf('$', from); 64 if (to != -1) { 65 if (to < fmt.length() - 1) { 66 char charDigit = fmt.charAt(to + 1); 67 if (Character.isDigit(charDigit)) { 68 int digit = Character.digit(charDigit, 10); 69 fmtParts[i] = fmt.substring(from, to).toCharArray(); 70 argIndices[i] = digit; 71 from = to + 2; 72 continue; 73 } 74 } 75 } 76 throw new IllegalArgumentException ("Expected arg $" + i); 77 } 78 fmtParts[args] = fmt.substring(from).toCharArray(); 79 80 int minNumChars = 0; 81 for (int i = 0, n = fmtParts.length; i < n; ++i) { 82 minNumChars += fmtParts[i].length; 83 } 84 this.minChars = minNumChars; 85 } 86 87 protected final Formatter getFormatter(Class c) { 88 return FMT_CLASS; 89 } 90 91 protected final Formatter getFormatter(File f) { 92 return FMT_FILE; 93 } 94 95 protected final Formatter getFormatter(Integer i) { 96 return FMT_INTEGER; 97 } 98 99 protected Formatter getFormatter(Long l) { 100 return FMT_LONG; 101 } 102 103 protected final Formatter getFormatter(Method m) { 104 return FMT_METHOD; 105 } 106 107 protected final Formatter getFormatter(String s) { 108 return FMT_STRING; 109 } 110 111 protected final Formatter getFormatter(String [] sa) { 112 return FMT_STRING_ARRAY; 113 } 114 115 protected final Formatter getFormatter(URL u) { 116 return FMT_URL; 117 } 118 119 protected final Formatter getToStringFormatter() { 120 return FMT_TOSTRING; 121 } 122 } 123 | Popular Tags |