001: /*
002: * Copyright 2006 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.dev.util.msg;
017:
018: import com.google.gwt.core.ext.TreeLogger;
019:
020: import java.io.File;
021: import java.lang.reflect.Method;
022: import java.net.URL;
023:
024: /**
025: * Fast way to produce messages for the logger. Use $N to specify the
026: * replacement argument. Caveats: - N must be a single digit (you shouldn't need
027: * more than 10 args, right?) - '$' cannot be escaped - each arg can only appear
028: * once
029: */
030: public abstract class Message {
031:
032: private static final Formatter FMT_TOSTRING = new FormatterToString();
033: private static final Formatter FMT_CLASS = new FormatterForClass();
034: private static final Formatter FMT_FILE = new FormatterForFile();
035: private static final Formatter FMT_URL = new FormatterForURL();
036: private static final Formatter FMT_INTEGER = new FormatterForInteger();
037: private static final Formatter FMT_LONG = new FormatterForLong();
038: private static final Formatter FMT_METHOD = new FormatterForMethod();
039: private static final Formatter FMT_STRING = new FormatterForString();
040: private static final Formatter FMT_STRING_ARRAY = new FormatterForStringArray();
041:
042: protected final TreeLogger.Type type;
043:
044: protected final char[][] fmtParts;
045:
046: protected final int[] argIndices;
047:
048: protected final int minChars;
049:
050: /**
051: * Creates a lazily-formatted message.
052: */
053: protected Message(TreeLogger.Type type, String fmt, int args) {
054: assert (type != null);
055: assert (fmt != null);
056: assert (args >= 0);
057: this .type = type;
058:
059: fmtParts = new char[args + 1][];
060: argIndices = new int[args];
061: int from = 0;
062: for (int i = 0; i < args; ++i) {
063: int to = fmt.indexOf('$', from);
064: if (to != -1) {
065: if (to < fmt.length() - 1) {
066: char charDigit = fmt.charAt(to + 1);
067: if (Character.isDigit(charDigit)) {
068: int digit = Character.digit(charDigit, 10);
069: fmtParts[i] = fmt.substring(from, to)
070: .toCharArray();
071: argIndices[i] = digit;
072: from = to + 2;
073: continue;
074: }
075: }
076: }
077: throw new IllegalArgumentException("Expected arg $" + i);
078: }
079: fmtParts[args] = fmt.substring(from).toCharArray();
080:
081: int minNumChars = 0;
082: for (int i = 0, n = fmtParts.length; i < n; ++i) {
083: minNumChars += fmtParts[i].length;
084: }
085: this .minChars = minNumChars;
086: }
087:
088: protected final Formatter getFormatter(Class c) {
089: return FMT_CLASS;
090: }
091:
092: protected final Formatter getFormatter(File f) {
093: return FMT_FILE;
094: }
095:
096: protected final Formatter getFormatter(Integer i) {
097: return FMT_INTEGER;
098: }
099:
100: protected Formatter getFormatter(Long l) {
101: return FMT_LONG;
102: }
103:
104: protected final Formatter getFormatter(Method m) {
105: return FMT_METHOD;
106: }
107:
108: protected final Formatter getFormatter(String s) {
109: return FMT_STRING;
110: }
111:
112: protected final Formatter getFormatter(String[] sa) {
113: return FMT_STRING_ARRAY;
114: }
115:
116: protected final Formatter getFormatter(URL u) {
117: return FMT_URL;
118: }
119:
120: protected final Formatter getToStringFormatter() {
121: return FMT_TOSTRING;
122: }
123: }
|