1 | /* |
2 | * $Id: IndentPrintWriter.java,v 1.3 2004/05/25 21:04:38 hastings Exp $ |
3 | * |
4 | * (c) Copyright, Moebius Solutions, Inc., 2004 |
5 | * |
6 | * All Rights Reserved |
7 | * |
8 | * This material may be reproduced by or for the U. S. Government |
9 | * pursuant to the copyright license under the clause at |
10 | * DFARS 252.227-7014 (OCT 2001). |
11 | */ |
12 | |
13 | package com.moesol.generator.core; |
14 | |
15 | import java.io.*; |
16 | |
17 | public class IndentPrintWriter extends PrintWriter { |
18 | public IndentPrintWriter(OutputStream os) { |
19 | super(os, false); |
20 | } |
21 | public IndentPrintWriter(Writer w) { |
22 | super(w, false); |
23 | } |
24 | |
25 | /** |
26 | * Sets how many spaces each indent level will be. |
27 | * @param i new indent level. |
28 | */ |
29 | public void setIndentSize(int i) { |
30 | m_indentSize = i; |
31 | } |
32 | /** |
33 | * @return number of spaces printed for each indent level. |
34 | */ |
35 | public int getIndentSize() { |
36 | return m_indentSize; |
37 | } |
38 | /** |
39 | * Increases the indent level. |
40 | */ |
41 | public void indent() { |
42 | m_indent++; |
43 | } |
44 | /** |
45 | * Decreases the indent level. |
46 | */ |
47 | public void outdent() { |
48 | m_indent--; |
49 | } |
50 | /** |
51 | * Prints the spaces needed for the current indent level. |
52 | * The number of spaces is computed by m_indent * m_indentSize. |
53 | */ |
54 | public void printdent() { |
55 | final int n = m_indent * m_indentSize; |
56 | for (int i = 0; i < n; i++) { |
57 | print(' '); |
58 | } |
59 | } |
60 | /** |
61 | * Prints quotes around a string |
62 | * @param s String to be quoted. |
63 | */ |
64 | public void quote(String s) { |
65 | print('"'); |
66 | print(s); |
67 | print('"'); |
68 | } |
69 | |
70 | /** |
71 | * Number of spaces to print for each indent level. Spaces printed |
72 | * by printdent are m_indent * m_indentSize. |
73 | */ |
74 | protected int m_indentSize = 4; |
75 | /** |
76 | * Current indent level. |
77 | */ |
78 | protected int m_indent = 0; |
79 | } |