001: /* -*- Mode: java; tab-width: 4; indent-tabs-mode: 1; c-basic-offset: 4 -*-
002: *
003: * ***** BEGIN LICENSE BLOCK *****
004: * Version: MPL 1.1/GPL 2.0
005: *
006: * The contents of this file are subject to the Mozilla Public License Version
007: * 1.1 (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: * http://www.mozilla.org/MPL/
010: *
011: * Software distributed under the License is distributed on an "AS IS" basis,
012: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
013: * for the specific language governing rights and limitations under the
014: * License.
015: *
016: * The Original Code is Rhino code, released
017: * May 6, 1999.
018: *
019: * The Initial Developer of the Original Code is
020: * Netscape Communications Corporation.
021: * Portions created by the Initial Developer are Copyright (C) 1997-1999
022: * the Initial Developer. All Rights Reserved.
023: *
024: * Contributor(s):
025: * Igor Bukanov
026: *
027: * Alternatively, the contents of this file may be used under the terms of
028: * the GNU General Public License Version 2 or later (the "GPL"), in which
029: * case the provisions of the GPL are applicable instead of those above. If
030: * you wish to allow use of your version of this file only under the terms of
031: * the GPL and not to allow others to use your version of this file under the
032: * MPL, indicate your decision by deleting the provisions above and replacing
033: * them with the notice and other provisions required by the GPL. If you do
034: * not delete the provisions above, a recipient may use your version of this
035: * file under either the MPL or the GPL.
036: *
037: * ***** END LICENSE BLOCK ***** */
038: package org.mozilla.javascript.tools.idswitch;
039:
040: class CodePrinter {
041:
042: // length of u-type escape like \u12AB
043: private static final int LITERAL_CHAR_MAX_SIZE = 6;
044:
045: private String lineTerminator = "\n";
046:
047: private int indentStep = 4;
048: private int indentTabSize = 8;
049:
050: private char[] buffer = new char[1 << 12]; // 4K
051: private int offset;
052:
053: public String getLineTerminator() {
054: return lineTerminator;
055: }
056:
057: public void setLineTerminator(String value) {
058: lineTerminator = value;
059: }
060:
061: public int getIndentStep() {
062: return indentStep;
063: }
064:
065: public void setIndentStep(int char_count) {
066: indentStep = char_count;
067: }
068:
069: public int getIndentTabSize() {
070: return indentTabSize;
071: }
072:
073: public void setIndentTabSize(int tab_size) {
074: indentTabSize = tab_size;
075: }
076:
077: public void clear() {
078: offset = 0;
079: }
080:
081: private int ensure_area(int area_size) {
082: int begin = offset;
083: int end = begin + area_size;
084: if (end > buffer.length) {
085: int new_capacity = buffer.length * 2;
086: if (end > new_capacity) {
087: new_capacity = end;
088: }
089: char[] tmp = new char[new_capacity];
090: System.arraycopy(buffer, 0, tmp, 0, begin);
091: buffer = tmp;
092: }
093: return begin;
094: }
095:
096: private int add_area(int area_size) {
097: int pos = ensure_area(area_size);
098: offset = pos + area_size;
099: return pos;
100: }
101:
102: public int getOffset() {
103: return offset;
104: }
105:
106: public int getLastChar() {
107: return offset == 0 ? -1 : buffer[offset - 1];
108: }
109:
110: public void p(char c) {
111: int pos = add_area(1);
112: buffer[pos] = c;
113: }
114:
115: public void p(String s) {
116: int l = s.length();
117: int pos = add_area(l);
118: s.getChars(0, l, buffer, pos);
119: }
120:
121: public final void p(char[] array) {
122: p(array, 0, array.length);
123: }
124:
125: public void p(char[] array, int begin, int end) {
126: int l = end - begin;
127: int pos = add_area(l);
128: System.arraycopy(array, begin, buffer, pos, l);
129: }
130:
131: public void p(int i) {
132: p(Integer.toString(i));
133: }
134:
135: public void qchar(int c) {
136: int pos = ensure_area(2 + LITERAL_CHAR_MAX_SIZE);
137: buffer[pos] = '\'';
138: pos = put_string_literal_char(pos + 1, c, false);
139: buffer[pos] = '\'';
140: offset = pos + 1;
141: }
142:
143: public void qstring(String s) {
144: int l = s.length();
145: int pos = ensure_area(2 + LITERAL_CHAR_MAX_SIZE * l);
146: buffer[pos] = '"';
147: ++pos;
148: for (int i = 0; i != l; ++i) {
149: pos = put_string_literal_char(pos, s.charAt(i), true);
150: }
151: buffer[pos] = '"';
152: offset = pos + 1;
153: }
154:
155: private int put_string_literal_char(int pos, int c,
156: boolean in_string) {
157: boolean backslash_symbol = true;
158: switch (c) {
159: case '\b':
160: c = 'b';
161: break;
162: case '\t':
163: c = 't';
164: break;
165: case '\n':
166: c = 'n';
167: break;
168: case '\f':
169: c = 'f';
170: break;
171: case '\r':
172: c = 'r';
173: break;
174: case '\'':
175: backslash_symbol = !in_string;
176: break;
177: case '"':
178: backslash_symbol = in_string;
179: break;
180: default:
181: backslash_symbol = false;
182: }
183:
184: if (backslash_symbol) {
185: buffer[pos] = '\\';
186: buffer[pos + 1] = (char) c;
187: pos += 2;
188: } else if (' ' <= c && c <= 126) {
189: buffer[pos] = (char) c;
190: ++pos;
191: } else {
192: buffer[pos] = '\\';
193: buffer[pos + 1] = 'u';
194: buffer[pos + 2] = digit_to_hex_letter(0xF & (c >> 12));
195: buffer[pos + 3] = digit_to_hex_letter(0xF & (c >> 8));
196: buffer[pos + 4] = digit_to_hex_letter(0xF & (c >> 4));
197: buffer[pos + 5] = digit_to_hex_letter(0xF & c);
198: pos += 6;
199: }
200: return pos;
201: }
202:
203: private static char digit_to_hex_letter(int d) {
204: return (char) ((d < 10) ? '0' + d : 'A' - 10 + d);
205: }
206:
207: public void indent(int level) {
208: int visible_size = indentStep * level;
209: int indent_size, tab_count;
210: if (indentTabSize <= 0) {
211: tab_count = 0;
212: indent_size = visible_size;
213: } else {
214: tab_count = visible_size / indentTabSize;
215: indent_size = tab_count + visible_size % indentTabSize;
216: }
217: int pos = add_area(indent_size);
218: int tab_end = pos + tab_count;
219: int indent_end = pos + indent_size;
220: for (; pos != tab_end; ++pos) {
221: buffer[pos] = '\t';
222: }
223: for (; pos != indent_end; ++pos) {
224: buffer[pos] = ' ';
225: }
226: }
227:
228: public void nl() {
229: p('\n');
230: }
231:
232: public void line(int indent_level, String s) {
233: indent(indent_level);
234: p(s);
235: nl();
236: }
237:
238: public void erase(int begin, int end) {
239: System.arraycopy(buffer, end, buffer, begin, offset - end);
240: offset -= end - begin;
241: }
242:
243: public String toString() {
244: return new String(buffer, 0, offset);
245: }
246:
247: }
|