KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > core > ext > Generator


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.core.ext;
17
18 /**
19  * Generates source code for subclasses during deferred binding requests.
20  * Subclasses must be thread-safe.
21  */

22 public abstract class Generator {
23
24   /**
25    * Escapes string content to be a valid string literal.
26    *
27    * @return an escaped version of <code>unescaped</code>, suitable for being
28    * enclosed in double quotes in Java source
29    */

30   public static String JavaDoc escape(String JavaDoc unescaped) {
31     int extra = 0;
32     for (int in = 0, n = unescaped.length(); in < n; ++in) {
33       switch (unescaped.charAt(in)) {
34         case '\n':
35         case '\r':
36         case '\"':
37         case '\\':
38           ++extra;
39           break;
40       }
41     }
42
43     if (extra == 0) {
44       return unescaped;
45     }
46
47     char[] oldChars = unescaped.toCharArray();
48     char[] newChars = new char[oldChars.length + extra];
49     for (int in = 0, out = 0, n = oldChars.length; in < n; ++in, ++out) {
50       char c = oldChars[in];
51       switch (c) {
52         case '\n':
53           newChars[out++] = '\\';
54           c = 'n';
55           break;
56         case '\r':
57           newChars[out++] = '\\';
58           c = 'r';
59           break;
60         case '\"':
61           newChars[out++] = '\\';
62           c = '"';
63           break;
64         case '\\':
65           newChars[out++] = '\\';
66           c = '\\';
67           break;
68       }
69       newChars[out] = c;
70     }
71
72     return String.valueOf(newChars);
73   }
74
75   /**
76    * Generate a default constructible subclass of the requested type. The
77    * generator throws <code>UnableToCompleteException</code> if for any reason
78    * it cannot provide a substitute class
79    *
80    * @return the name of a subclass to substitute for the requested class, or
81    * return <code>null</code> to cause the requested type itself to be
82    * used
83    *
84    */

85   public abstract String JavaDoc generate(TreeLogger logger, GeneratorContext context,
86       String JavaDoc typeName) throws UnableToCompleteException;
87 }
88
Popular Tags