001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.tools.ws.processor.util;
038:
039: import com.sun.tools.ws.processor.generator.GeneratorException;
040:
041: import java.io.BufferedWriter;
042: import java.io.IOException;
043: import java.io.Writer;
044: import java.nio.charset.Charset;
045: import java.nio.charset.CharsetEncoder;
046: import java.text.MessageFormat;
047:
048: /**
049: *
050: * @author WS Development Team
051: */
052: public class IndentingWriter extends BufferedWriter {
053:
054: private boolean beginningOfLine = true;
055: private int currentIndent = 0;
056: private int indentStep = 4;
057:
058: public IndentingWriter(Writer out) {
059: super (out);
060: }
061:
062: public IndentingWriter(Writer out, int step) {
063: this (out);
064:
065: if (indentStep < 0) {
066: throw new IllegalArgumentException("negative indent step");
067: }
068: indentStep = step;
069: }
070:
071: public void write(int c) throws IOException {
072: checkWrite();
073: super .write(c);
074: }
075:
076: public void write(char[] cbuf, int off, int len) throws IOException {
077: if (len > 0) {
078: checkWrite();
079: }
080: super .write(cbuf, off, len);
081: }
082:
083: public void write(String s, int off, int len) throws IOException {
084: if (len > 0) {
085: checkWrite();
086: }
087: super .write(s, off, len);
088: }
089:
090: public void newLine() throws IOException {
091: super .newLine();
092: beginningOfLine = true;
093: }
094:
095: protected void checkWrite() throws IOException {
096: if (beginningOfLine) {
097: beginningOfLine = false;
098: int i = currentIndent;
099: while (i > 0) {
100: super .write(' ');
101: --i;
102: }
103: }
104: }
105:
106: protected void indentIn() {
107: currentIndent += indentStep;
108: }
109:
110: protected void indentOut() {
111: currentIndent -= indentStep;
112: if (currentIndent < 0) {
113: currentIndent = 0;
114: }
115: }
116:
117: public void pI() {
118: indentIn();
119: }
120:
121: public void pO() {
122: indentOut();
123: }
124:
125: public void pI(int levels) {
126: for (int i = 0; i < levels; ++i) {
127: indentIn();
128: }
129: }
130:
131: public void pO(int levels) {
132: for (int i = 0; i < levels; ++i) {
133: indentOut();
134: }
135: }
136:
137: public void p(String s) throws IOException {
138: /*
139: int tabCount = 0;
140: for (int i = 0; i < s.length(); ++i) {
141: if (s.charAt(i) == '\t') {
142: ++tabCount;
143: indentIn();
144: }
145: }
146:
147: String printStr = s.substring(tabCount);
148: */
149: boolean canEncode = true;
150:
151: //bug fix: 4839636
152: try {
153: if (!canEncode(s)) {
154: canEncode = false;
155: }
156: } catch (Throwable t) {
157:
158: // there was some exception, what should we do?
159: // lets ignore it for now and proceed with the code generation!
160: }
161:
162: if (!canEncode) {
163: throw new GeneratorException(
164: "generator.indentingwriter.charset.cantencode", s);
165: }
166: write(s);
167: /*
168: while (tabCount-- > 0) {
169: indentOut();
170: }
171: */
172: }
173:
174: /**
175: * Check if encode can handle the chars in this string.
176: *
177: */
178: protected boolean canEncode(String s) {
179: final CharsetEncoder encoder = Charset.forName(
180: System.getProperty("file.encoding")).newEncoder();
181: char[] chars = s.toCharArray();
182: for (int i = 0; i < chars.length; i++) {
183: if (!encoder.canEncode(chars[i])) {
184: return false;
185: }
186: }
187: return true;
188: }
189:
190: public void p(String s1, String s2) throws IOException {
191: p(s1);
192: p(s2);
193: }
194:
195: public void p(String s1, String s2, String s3) throws IOException {
196: p(s1);
197: p(s2);
198: p(s3);
199: }
200:
201: public void p(String s1, String s2, String s3, String s4)
202: throws IOException {
203: p(s1);
204: p(s2);
205: p(s3);
206: p(s4);
207: }
208:
209: public void p(String s1, String s2, String s3, String s4, String s5)
210: throws IOException {
211: p(s1);
212: p(s2);
213: p(s3);
214: p(s4);
215: p(s5);
216: }
217:
218: public void pln() throws IOException {
219: newLine();
220: }
221:
222: public void pln(String s) throws IOException {
223: p(s);
224: pln();
225: }
226:
227: public void pln(String s1, String s2) throws IOException {
228: p(s1, s2);
229: pln();
230: }
231:
232: public void pln(String s1, String s2, String s3) throws IOException {
233: p(s1, s2, s3);
234: pln();
235: }
236:
237: public void pln(String s1, String s2, String s3, String s4)
238: throws IOException {
239: p(s1, s2, s3, s4);
240: pln();
241: }
242:
243: public void pln(String s1, String s2, String s3, String s4,
244: String s5) throws IOException {
245: p(s1, s2, s3, s4, s5);
246: pln();
247: }
248:
249: public void plnI(String s) throws IOException {
250: p(s);
251: pln();
252: pI();
253: }
254:
255: public void pO(String s) throws IOException {
256: pO();
257: p(s);
258: }
259:
260: public void pOln(String s) throws IOException {
261: pO(s);
262: pln();
263: }
264:
265: public void pOlnI(String s) throws IOException {
266: pO(s);
267: pln();
268: pI();
269: }
270:
271: public void p(Object o) throws IOException {
272: write(o.toString());
273: }
274:
275: public void pln(Object o) throws IOException {
276: p(o.toString());
277: pln();
278: }
279:
280: public void plnI(Object o) throws IOException {
281: p(o.toString());
282: pln();
283: pI();
284: }
285:
286: public void pO(Object o) throws IOException {
287: pO();
288: p(o.toString());
289: }
290:
291: public void pOln(Object o) throws IOException {
292: pO(o.toString());
293: pln();
294: }
295:
296: public void pOlnI(Object o) throws IOException {
297: pO(o.toString());
298: pln();
299: pI();
300: }
301:
302: public void pM(String s) throws IOException {
303: int i = 0;
304: while (i < s.length()) {
305: int j = s.indexOf('\n', i);
306: if (j == -1) {
307: p(s.substring(i));
308: break;
309: } else {
310: pln(s.substring(i, j));
311: i = j + 1;
312: }
313: }
314: }
315:
316: public void pMln(String s) throws IOException {
317: pM(s);
318: pln();
319: }
320:
321: public void pMlnI(String s) throws IOException {
322: pM(s);
323: pln();
324: pI();
325: }
326:
327: public void pMO(String s) throws IOException {
328: pO();
329: pM(s);
330: }
331:
332: public void pMOln(String s) throws IOException {
333: pMO(s);
334: pln();
335: }
336:
337: public void pF(String pattern, Object[] arguments)
338: throws IOException {
339: pM(MessageFormat.format(pattern, arguments));
340: }
341:
342: public void pFln(String pattern, Object[] arguments)
343: throws IOException {
344: pF(pattern, arguments);
345: pln();
346: }
347: }
|