001: /*
002: * Portions Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.tools.internal.ws.processor.util;
027:
028: import java.io.BufferedWriter;
029: import java.io.IOException;
030: import java.io.Writer;
031:
032: import java.nio.charset.Charset;
033: import java.nio.charset.CharsetEncoder;
034: import java.text.MessageFormat;
035: import com.sun.tools.internal.ws.processor.generator.GeneratorException;
036:
037: /**
038: *
039: * @author WS Development Team
040: */
041: public class IndentingWriter extends BufferedWriter {
042:
043: private boolean beginningOfLine = true;
044: private int currentIndent = 0;
045: private int indentStep = 4;
046:
047: public IndentingWriter(Writer out) {
048: super (out);
049: }
050:
051: public IndentingWriter(Writer out, int step) {
052: this (out);
053:
054: if (indentStep < 0) {
055: throw new IllegalArgumentException("negative indent step");
056: }
057: indentStep = step;
058: }
059:
060: public void write(int c) throws IOException {
061: checkWrite();
062: super .write(c);
063: }
064:
065: public void write(char[] cbuf, int off, int len) throws IOException {
066: if (len > 0) {
067: checkWrite();
068: }
069: super .write(cbuf, off, len);
070: }
071:
072: public void write(String s, int off, int len) throws IOException {
073: if (len > 0) {
074: checkWrite();
075: }
076: super .write(s, off, len);
077: }
078:
079: public void newLine() throws IOException {
080: super .newLine();
081: beginningOfLine = true;
082: }
083:
084: protected void checkWrite() throws IOException {
085: if (beginningOfLine) {
086: beginningOfLine = false;
087: int i = currentIndent;
088: while (i > 0) {
089: super .write(' ');
090: --i;
091: }
092: }
093: }
094:
095: protected void indentIn() {
096: currentIndent += indentStep;
097: }
098:
099: protected void indentOut() {
100: currentIndent -= indentStep;
101: if (currentIndent < 0) {
102: currentIndent = 0;
103: }
104: }
105:
106: public void pI() {
107: indentIn();
108: }
109:
110: public void pO() {
111: indentOut();
112: }
113:
114: public void pI(int levels) {
115: for (int i = 0; i < levels; ++i) {
116: indentIn();
117: }
118: }
119:
120: public void pO(int levels) {
121: for (int i = 0; i < levels; ++i) {
122: indentOut();
123: }
124: }
125:
126: public void p(String s) throws IOException {
127: /*
128: int tabCount = 0;
129: for (int i = 0; i < s.length(); ++i) {
130: if (s.charAt(i) == '\t') {
131: ++tabCount;
132: indentIn();
133: }
134: }
135:
136: String printStr = s.substring(tabCount);
137: */
138: boolean canEncode = true;
139:
140: //bug fix: 4839636
141: try {
142: if (!canEncode(s)) {
143: canEncode = false;
144: }
145: } catch (Throwable t) {
146:
147: // there was some exception, what should we do?
148: // lets ignore it for now and proceed with the code generation!
149: }
150:
151: if (!canEncode) {
152: throw new GeneratorException(
153: "generator.indentingwriter.charset.cantencode", s);
154: }
155: write(s);
156: /*
157: while (tabCount-- > 0) {
158: indentOut();
159: }
160: */
161: }
162:
163: /**
164: * Check if encode can handle the chars in this string.
165: *
166: */
167: protected boolean canEncode(String s) {
168: final CharsetEncoder encoder = Charset.forName(
169: System.getProperty("file.encoding")).newEncoder();
170: char[] chars = s.toCharArray();
171: for (int i = 0; i < chars.length; i++) {
172: if (!encoder.canEncode(chars[i])) {
173: return false;
174: }
175: }
176: return true;
177: }
178:
179: public void p(String s1, String s2) throws IOException {
180: p(s1);
181: p(s2);
182: }
183:
184: public void p(String s1, String s2, String s3) throws IOException {
185: p(s1);
186: p(s2);
187: p(s3);
188: }
189:
190: public void p(String s1, String s2, String s3, String s4)
191: throws IOException {
192: p(s1);
193: p(s2);
194: p(s3);
195: p(s4);
196: }
197:
198: public void p(String s1, String s2, String s3, String s4, String s5)
199: throws IOException {
200: p(s1);
201: p(s2);
202: p(s3);
203: p(s4);
204: p(s5);
205: }
206:
207: public void pln() throws IOException {
208: newLine();
209: }
210:
211: public void pln(String s) throws IOException {
212: p(s);
213: pln();
214: }
215:
216: public void pln(String s1, String s2) throws IOException {
217: p(s1, s2);
218: pln();
219: }
220:
221: public void pln(String s1, String s2, String s3) throws IOException {
222: p(s1, s2, s3);
223: pln();
224: }
225:
226: public void pln(String s1, String s2, String s3, String s4)
227: throws IOException {
228: p(s1, s2, s3, s4);
229: pln();
230: }
231:
232: public void pln(String s1, String s2, String s3, String s4,
233: String s5) throws IOException {
234: p(s1, s2, s3, s4, s5);
235: pln();
236: }
237:
238: public void plnI(String s) throws IOException {
239: p(s);
240: pln();
241: pI();
242: }
243:
244: public void pO(String s) throws IOException {
245: pO();
246: p(s);
247: }
248:
249: public void pOln(String s) throws IOException {
250: pO(s);
251: pln();
252: }
253:
254: public void pOlnI(String s) throws IOException {
255: pO(s);
256: pln();
257: pI();
258: }
259:
260: public void p(Object o) throws IOException {
261: write(o.toString());
262: }
263:
264: public void pln(Object o) throws IOException {
265: p(o.toString());
266: pln();
267: }
268:
269: public void plnI(Object o) throws IOException {
270: p(o.toString());
271: pln();
272: pI();
273: }
274:
275: public void pO(Object o) throws IOException {
276: pO();
277: p(o.toString());
278: }
279:
280: public void pOln(Object o) throws IOException {
281: pO(o.toString());
282: pln();
283: }
284:
285: public void pOlnI(Object o) throws IOException {
286: pO(o.toString());
287: pln();
288: pI();
289: }
290:
291: public void pM(String s) throws IOException {
292: int i = 0;
293: while (i < s.length()) {
294: int j = s.indexOf('\n', i);
295: if (j == -1) {
296: p(s.substring(i));
297: break;
298: } else {
299: pln(s.substring(i, j));
300: i = j + 1;
301: }
302: }
303: }
304:
305: public void pMln(String s) throws IOException {
306: pM(s);
307: pln();
308: }
309:
310: public void pMlnI(String s) throws IOException {
311: pM(s);
312: pln();
313: pI();
314: }
315:
316: public void pMO(String s) throws IOException {
317: pO();
318: pM(s);
319: }
320:
321: public void pMOln(String s) throws IOException {
322: pMO(s);
323: pln();
324: }
325:
326: public void pF(String pattern, Object[] arguments)
327: throws IOException {
328: pM(MessageFormat.format(pattern, arguments));
329: }
330:
331: public void pFln(String pattern, Object[] arguments)
332: throws IOException {
333: pF(pattern, arguments);
334: pln();
335: }
336: }
|