1 23 24 29 30 package com.sun.jdo.spi.persistence.utility.generator.io; 31 32 import java.util.*; 33 34 35 39 class FormattedWriter 40 { 41 static private final String lineSeparator = 42 System.getProperty("line.separator"); 43 static private final String indent = " "; 45 private StringBuffer _buffer; 46 private int _initialIndents = 0; 47 48 49 FormattedWriter () 50 { 51 } 52 53 private StringBuffer getBuffer () 54 { 55 if (_buffer == null) 56 _buffer = new StringBuffer (); 57 58 return _buffer; 59 } 60 61 65 public String toString () { return getBuffer().toString(); } 66 67 void writeComments (final String [] comments) 68 { 69 final int n = (comments != null ? comments.length : 0); 70 71 for (int i = 0; i < n; i++) 72 { 73 final String s = comments[i]; 74 75 writeln("// " + (s != null ? s : "")); } 77 } 78 79 private void _write (final int indents, final String s) 80 { 81 final StringBuffer buffer = getBuffer(); 82 83 if (!s.equals(lineSeparator)) 84 { 85 for (int i = 0; i < indents; i++) 86 buffer.append(indent); 87 } 88 89 buffer.append(s); 90 } 91 92 void write (final int indents, final String s) 93 { 94 _write(indents + _initialIndents, s); 95 } 96 97 void write (final String s) 98 { 99 _write(0, s); 100 } 101 102 void writeln (final int indents, final String s) 103 { 104 if (_initialIndents > 0) 105 _write(_initialIndents, ""); 107 _write(indents, s + lineSeparator); 108 } 109 110 void writeln (final String s) 111 { 112 writeln(0, s); 113 } 114 115 void writeln () 116 { 117 writeln(0, ""); } 119 120 void writeList (final int indents, final List list, 121 final boolean addSeparator) 122 { 123 if ((list != null) && (list.size() > 0)) 124 { 125 Iterator iterator = list.iterator(); 126 127 while (iterator.hasNext()) 128 { 129 indent(indents, iterator.next().toString()); 130 131 if (addSeparator) 132 writeln(); 133 } 134 135 if (!addSeparator) 136 writeln(); 137 } 138 } 139 140 void writeList (final int indents, final List list) 141 { 142 writeList(indents, list, false); 143 } 144 145 void writeList (final List list) 146 { 147 writeList(0, list); 148 } 149 150 private void indent (final int indents, final String s) 151 { 152 if (s.indexOf(lineSeparator) != -1) 153 { 154 StringTokenizer tokenizer = 155 new StringTokenizer(s, lineSeparator, true); 156 157 while (tokenizer.hasMoreTokens()) 158 write(indents, tokenizer.nextToken()); 159 } 160 else 161 write(indents, s); 162 } 163 } 164 | Popular Tags |