1 19 20 package org.netbeans.modules.schema2beansdev.gen; 21 22 import java.util.*; 23 import java.io.*; 24 25 public class IndentingWriter extends GenBuffer { 26 protected boolean crDone[]; 27 protected int indentLevel[]; 28 protected String indentString = "\t"; 29 30 public IndentingWriter(int bufferCount) { 31 super(bufferCount); 32 crDone = new boolean[bufferCount]; 33 indentLevel = new int[bufferCount]; 34 privateInit(); 35 } 36 37 public IndentingWriter(IndentingWriter source) { 38 super(source); 39 indentString = source.indentString; 40 crDone = new boolean[bufferCount]; 41 indentLevel = new int[bufferCount]; 42 for (int i = 0; i < bufferCount; i++) { 43 crDone[i] = source.crDone[i]; 44 indentLevel[i] = source.indentLevel[i]; 45 } 46 } 47 48 public void reset() { 49 super.reset(); 50 privateInit(); 51 } 52 53 private void privateInit() { 54 for (int i = 0; i < bufferCount; i++) { 55 crDone[i] = true; 56 indentLevel[i] = 0; 57 } 58 } 59 60 public void writeTo(GenBuffer o) { 61 super.writeTo(o); 62 if (o instanceof IndentingWriter) { 63 IndentingWriter out = (IndentingWriter) o; 64 int minInCommonBufferCount = bufferCount; 65 if (out.bufferCount < bufferCount) 66 minInCommonBufferCount = out.bufferCount; 67 for (int i = 0; i < minInCommonBufferCount; i++) { 68 out.crDone[i] = crDone[i]; 69 out.indentLevel[i] = indentLevel[i]; 70 } 71 } 72 } 73 74 79 public void insertAdditionalBuffers(int offset, int count) { 80 boolean[] newCrDone = new boolean[bufferCount + count]; 81 System.arraycopy(crDone, 0, newCrDone, 0, offset+1); 83 System.arraycopy(crDone, offset+1, newCrDone, 85 offset + 1 + count, bufferCount - offset - 1); 86 for (int i = 0; i < count; ++i) { 88 newCrDone[offset + 1 + i] = true; 89 } 90 crDone = newCrDone; 91 92 int[] newIndentLevel = new int[bufferCount + count]; 93 System.arraycopy(indentLevel, 0, newIndentLevel, 0, offset+1); 95 System.arraycopy(indentLevel, offset+1, newIndentLevel, 97 offset + 1 + count, bufferCount - offset - 1); 98 for (int i = 0; i < count; ++i) { 100 newIndentLevel[offset + 1 + i] = 0; 101 } 102 indentLevel = newIndentLevel; 103 104 super.insertAdditionalBuffers(offset, count); 105 } 106 107 public void setIndent(String indent) { 108 this.indentString = indent; 109 } 110 111 public String getIndent() { 112 return indentString; 113 } 114 115 public void cr() throws IOException { 116 listOut[curOut].append("\n"); 117 crDone[curOut] = true; 118 } 119 120 public void write(String str) throws IOException { 121 int len = str.length(); 122 if (len == 0) 123 return; 124 char lastChar = str.charAt(len-1); 125 if (lastChar == '\n') { 126 char firstChar = str.charAt(0); 127 char secondLastChar = (len <= 1) ? ' ' : str.charAt(len-2); 128 if (firstChar == '}' || secondLastChar == '}') { 129 indentLeft(); 130 } 131 super.write(str.substring(0, len-1)); 132 cr(); 133 if (secondLastChar == '{') { 134 indentRight(); 135 } 136 } else { 137 super.write(str); 138 } 139 } 140 141 public void writecr(String str) throws IOException { 142 super.write(str); 143 cr(); 144 } 145 146 public void writecr(String s1, String s2) throws IOException { 147 super.write(s1, s2); 148 cr(); 149 } 150 151 public void writecr(String s1, String s2, String s3) throws IOException { 152 super.write(s1, s2, s3); 153 cr(); 154 } 155 156 public void writecr(String s1, String s2, String s3, String s4) throws IOException { 157 super.write(s1, s2, s3, s4); 158 cr(); 159 } 160 161 public void indentRight() { 162 ++indentLevel[curOut]; 163 } 164 165 public void indentLeft() { 166 --indentLevel[curOut]; 167 } 168 169 protected void beforeWriteHook() { 170 if (crDone[curOut]) { 171 indent(); 172 crDone[curOut] = false; 173 } 174 } 175 176 179 public void indentOneLevel() { 180 listOut[curOut].append(indentString); 181 } 182 183 188 public void indent() { 189 for (int i = 0; i < indentLevel[curOut]; ++i) 191 indentOneLevel(); 192 } 193 } 194 | Popular Tags |