KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > polyglot > ext > jl > ast > StringLit_c


1 package polyglot.ext.jl.ast;
2
3 import polyglot.ast.*;
4 import polyglot.types.*;
5 import polyglot.visit.*;
6 import polyglot.util.*;
7 import java.util.*;
8
9 /**
10  * A <code>StringLit</code> represents an immutable instance of a
11  * <code>String</code> which corresponds to a literal string in Java code.
12  */

13 public class StringLit_c extends Lit_c implements StringLit
14 {
15     protected String JavaDoc value;
16
17     public StringLit_c(Position pos, String JavaDoc value) {
18     super(pos);
19     this.value = value;
20     }
21
22     /** Get the value of the expression. */
23     public String JavaDoc value() {
24     return this.value;
25     }
26
27     /** Set the value of the expression. */
28     public StringLit value(String JavaDoc value) {
29     StringLit_c n = (StringLit_c) copy();
30     n.value = value;
31     return n;
32     }
33
34     /** Type check the expression. */
35     public Node typeCheck(TypeChecker tc) throws SemanticException {
36         return type(tc.typeSystem().String());
37     }
38
39     public String JavaDoc toString() {
40         if (StringUtil.unicodeEscape(value).length() > 11) {
41             return "\"" + StringUtil.unicodeEscape(value).substring(0,8) + "...\"";
42         }
43                 
44     return "\"" + StringUtil.unicodeEscape(value) + "\"";
45     }
46
47     protected int MAX_LENGTH = 60;
48  
49     /** Write the expression to an output file. */
50     public void prettyPrint(CodeWriter w, PrettyPrinter tr) {
51         List l = breakupString();
52
53         // If we break up the string, parenthesize it to avoid precedence bugs.
54
if (l.size() > 1) {
55             w.write("(");
56         }
57
58         for (Iterator i = l.iterator(); i.hasNext(); ) {
59             String JavaDoc s = (String JavaDoc) i.next();
60             w.begin(0);
61         }
62
63         for (Iterator i = l.iterator(); i.hasNext(); ) {
64             String JavaDoc s = (String JavaDoc) i.next();
65
66             w.write("\"");
67             w.write(StringUtil.escape(s));
68             w.write("\"");
69             w.end();
70
71             if (i.hasNext()) {
72                 w.write(" +");
73                 w.allowBreak(0, " ");
74             }
75         }
76
77         if (l.size() > 1) {
78             w.write(")");
79         }
80     }
81
82     /**
83      * Break a long string literal into a concatenation of small string
84      * literals. This avoids messing up the pretty printer and editors.
85      */

86     protected List breakupString() {
87         List result = new LinkedList();
88         int n = value.length();
89         int i = 0;
90
91         while (i < n) {
92             int j;
93
94             // Compensate for the unicode transformation by computing
95
// the length of the encoded string.
96
int len = 0;
97
98             for (j = i; j < n; j++) {
99                 char c = value.charAt(j);
100                 int k = StringUtil.unicodeEscape(c).length();
101                 if (len + k > MAX_LENGTH) break;
102                 len += k;
103             }
104
105             result.add(value.substring(i, j));
106
107             i = j;
108         }
109
110         if (result.isEmpty()) {
111             // This should only happen when value == "".
112
if (! value.equals("")) {
113                 throw new InternalCompilerError("breakupString failed");
114             }
115             result.add(value);
116         }
117
118         return result;
119     }
120     
121     public Object JavaDoc constantValue() {
122     return value;
123     }
124 }
125
Popular Tags