KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > dev > util > msg > Message


1 /*
2  * Copyright 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.dev.util.msg;
17
18 import com.google.gwt.core.ext.TreeLogger;
19
20 import java.io.File JavaDoc;
21 import java.lang.reflect.Method JavaDoc;
22 import java.net.URL JavaDoc;
23
24 /**
25  * Fast way to produce messages for the logger. Use $N to specify the
26  * replacement argument. Caveats: - N must be a single digit (you shouldn't need
27  * more than 10 args, right?) - '$' cannot be escaped - each arg can only appear
28  * once
29  */

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   /**
51    * Creates a lazily-formatted message.
52    */

53   protected Message(TreeLogger.Type type, String JavaDoc 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 JavaDoc("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 JavaDoc c) {
88     return FMT_CLASS;
89   }
90
91   protected final Formatter getFormatter(File JavaDoc f) {
92     return FMT_FILE;
93   }
94
95   protected final Formatter getFormatter(Integer JavaDoc i) {
96     return FMT_INTEGER;
97   }
98
99   protected Formatter getFormatter(Long JavaDoc l) {
100     return FMT_LONG;
101   }
102
103   protected final Formatter getFormatter(Method JavaDoc m) {
104     return FMT_METHOD;
105   }
106
107   protected final Formatter getFormatter(String JavaDoc s) {
108     return FMT_STRING;
109   }
110
111   protected final Formatter getFormatter(String JavaDoc[] sa) {
112     return FMT_STRING_ARRAY;
113   }
114
115   protected final Formatter getFormatter(URL JavaDoc u) {
116     return FMT_URL;
117   }
118
119   protected final Formatter getToStringFormatter() {
120     return FMT_TOSTRING;
121   }
122 }
123
Popular Tags