Showing the changes to java/pkg/sun/rmi/rmic/IndentingWriter.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,299 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1997, 2007, Oracle and/or its affiliates. All rights reserved. | ||
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
| 4 | * | ||
| 5 | * This code is free software; you can redistribute it and/or modify it | ||
| 6 | * under the terms of the GNU General Public License version 2 only, as | ||
| 7 | * published by the Free Software Foundation. Oracle designates this | ||
| 8 | * particular file as subject to the "Classpath" exception as provided | ||
| 9 | * by Oracle in the LICENSE file that accompanied this code. | ||
| 10 | * | ||
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT | ||
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| 14 | * version 2 for more details (a copy is included in the LICENSE file that | ||
| 15 | * accompanied this code). | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU General Public License version | ||
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, | ||
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
| 20 | * | ||
| 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
| 22 | * or visit www.oracle.com if you need additional information or have any | ||
| 23 | * questions. | ||
| 24 | */ | ||
| 25 | |||
| 26 | /*****************************************************************************/ | ||
| 27 | /* Copyright (c) IBM Corporation 1998 */ | ||
| 28 | /* */ | ||
| 29 | /* (C) Copyright IBM Corp. 1998 */ | ||
| 30 | /* */ | ||
| 31 | /*****************************************************************************/ | ||
| 32 | |||
| 33 | package sun.rmi.rmic; | ||
| 34 | |||
| 35 | import java.io.Writer; | ||
| 36 | import java.io.BufferedWriter; | ||
| 37 | import java.io.IOException; | ||
| 38 | |||
| 39 | /** | ||
| 40 | * IndentingWriter is a BufferedWriter subclass that supports automatic | ||
| 41 | * indentation of lines of text written to the underlying Writer. | ||
| 42 | * | ||
| 43 | * Methods are provided for compact, convenient indenting, writing text, | ||
| 44 | * and writing lines in various combinations. | ||
| 45 | * | ||
| 46 | * WARNING: The contents of this source file are not part of any | ||
| 47 | * supported API. Code that depends on them does so at its own risk: | ||
| 48 | * they are subject to change or removal without notice. | ||
| 49 | */ | ||
| 50 | public class IndentingWriter extends BufferedWriter { | ||
| 51 | |||
| 52 | /** true if the next character written is the first on a line */ | ||
| 53 | private boolean beginningOfLine = true; | ||
| 54 | |||
| 55 | /** current number of spaces to prepend to lines */ | ||
| 56 | private int currentIndent = 0; | ||
| 57 | |||
| 58 | /** number of spaces to change indent when indenting in or out */ | ||
| 59 | private int indentStep = 4; | ||
| 60 | |||
| 61 | /** number of spaces to convert into tabs. Use MAX_VALUE to disable */ | ||
| 62 | private int tabSize = 8; | ||
| 63 | |||
| 64 | /** | ||
| 65 | * Create a new IndentingWriter that writes indented text to the | ||
| 66 | * given Writer. Use the default indent step of four spaces. | ||
| 67 | */ | ||
| 68 | public IndentingWriter(Writer out) { | ||
| 69 | super(out); | ||
| 70 | } | ||
| 71 | |||
| 72 | /** | ||
| 73 | * Create a new IndentingWriter that writes indented text to the | ||
| 74 | * given Writer and uses the supplied indent step. | ||
| 75 | */ | ||
| 76 | public IndentingWriter(Writer out, int step) { | ||
| 77 | this(out); | ||
| 78 | |||
| 79 | if (indentStep < 0) | ||
| 80 | throw new IllegalArgumentException("negative indent step"); | ||
| 81 | |||
| 82 | indentStep = step; | ||
| 83 | } | ||
| 84 | |||
| 85 | /** | ||
| 86 | * Create a new IndentingWriter that writes indented text to the | ||
| 87 | * given Writer and uses the supplied indent step and tab size. | ||
| 88 | */ | ||
| 89 | public IndentingWriter(Writer out, int step, int tabSize) { | ||
| 90 | this(out); | ||
| 91 | |||
| 92 | if (indentStep < 0) | ||
| 93 | throw new IllegalArgumentException("negative indent step"); | ||
| 94 | |||
| 95 | indentStep = step; | ||
| 96 | this.tabSize = tabSize; | ||
| 97 | } | ||
| 98 | |||
| 99 | /** | ||
| 100 | * Write a single character. | ||
| 101 | */ | ||
| 102 | public void write(int c) throws IOException { | ||
| 103 | checkWrite(); | ||
| 104 | super.write(c); | ||
| 105 | } | ||
| 106 | |||
| 107 | /** | ||
| 108 | * Write a portion of an array of characters. | ||
| 109 | */ | ||
| 110 | public void write(char[] cbuf, int off, int len) throws IOException { | ||
| 111 | if (len > 0) { | ||
| 112 | checkWrite(); | ||
| 113 | } | ||
| 114 | super.write(cbuf, off, len); | ||
| 115 | } | ||
| 116 | |||
| 117 | /** | ||
| 118 | * Write a portion of a String. | ||
| 119 | */ | ||
| 120 | public void write(String s, int off, int len) throws IOException { | ||
| 121 | if (len > 0) { | ||
| 122 | checkWrite(); | ||
| 123 | } | ||
| 124 | super.write(s, off, len); | ||
| 125 | } | ||
| 126 | |||
| 127 | /** | ||
| 128 | * Write a line separator. The next character written will be | ||
| 129 | * preceded by an indent. | ||
| 130 | */ | ||
| 131 | public void newLine() throws IOException { | ||
| 132 | super.newLine(); | ||
| 133 | beginningOfLine = true; | ||
| 134 | } | ||
| 135 | |||
| 136 | /** | ||
| 137 | * Check if an indent needs to be written before writing the next | ||
| 138 | * character. | ||
| 139 | * | ||
| 140 | * The indent generation is optimized (and made consistent with | ||
| 141 | * certain coding conventions) by condensing groups of eight spaces | ||
| 142 | * into tab characters. | ||
| 143 | */ | ||
| 144 | protected void checkWrite() throws IOException { | ||
| 145 | if (beginningOfLine) { | ||
| 146 | beginningOfLine = false; | ||
| 147 | int i = currentIndent; | ||
| 148 | while (i >= tabSize) { | ||
| 149 | super.write('\t'); | ||
| 150 | i -= tabSize; | ||
| 151 | } | ||
| 152 | while (i > 0) { | ||
| 153 | super.write(' '); | ||
| 154 | -- i; | ||
| 155 | } | ||
| 156 | } | ||
| 157 | } | ||
| 158 | |||
| 159 | /** | ||
| 160 | * Increase the current indent by the indent step. | ||
| 161 | */ | ||
| 162 | protected void indentIn() { | ||
| 163 | currentIndent += indentStep; | ||
| 164 | } | ||
| 165 | |||
| 166 | /** | ||
| 167 | * Decrease the current indent by the indent step. | ||
| 168 | */ | ||
| 169 | protected void indentOut() { | ||
| 170 | currentIndent -= indentStep; | ||
| 171 | if (currentIndent < 0) | ||
| 172 | currentIndent = 0; | ||
| 173 | } | ||
| 174 | |||
| 175 | /** | ||
| 176 | * Indent in. | ||
| 177 | */ | ||
| 178 | public void pI() { | ||
| 179 | indentIn(); | ||
| 180 | } | ||
| 181 | |||
| 182 | /** | ||
| 183 | * Indent out. | ||
| 184 | */ | ||
| 185 | public void pO() { | ||
| 186 | indentOut(); | ||
| 187 | } | ||
| 188 | |||
| 189 | /** | ||
| 190 | * Write string. | ||
| 191 | */ | ||
| 192 | public void p(String s) throws IOException { | ||
| 193 | write(s); | ||
| 194 | } | ||
| 195 | |||
| 196 | /** | ||
| 197 | * End current line. | ||
| 198 | */ | ||
| 199 | public void pln() throws IOException { | ||
| 200 | newLine(); | ||
| 201 | } | ||
| 202 | |||
| 203 | /** | ||
| 204 | * Write string; end current line. | ||
| 205 | */ | ||
| 206 | public void pln(String s) throws IOException { | ||
| 207 | p(s); | ||
| 208 | pln(); | ||
| 209 | } | ||
| 210 | |||
| 211 | /** | ||
| 212 | * Write string; end current line; indent in. | ||
| 213 | */ | ||
| 214 | public void plnI(String s) throws IOException { | ||
| 215 | p(s); | ||
| 216 | pln(); | ||
| 217 | pI(); | ||
| 218 | } | ||
| 219 | |||
| 220 | /** | ||
| 221 | * Indent out; write string. | ||
| 222 | */ | ||
| 223 | public void pO(String s) throws IOException { | ||
| 224 | pO(); | ||
| 225 | p(s); | ||
| 226 | } | ||
| 227 | |||
| 228 | /** | ||
| 229 | * Indent out; write string; end current line. | ||
| 230 | */ | ||
| 231 | public void pOln(String s) throws IOException { | ||
| 232 | pO(s); | ||
| 233 | pln(); | ||
| 234 | } | ||
| 235 | |||
| 236 | /** | ||
| 237 | * Indent out; write string; end current line; indent in. | ||
| 238 | * | ||
| 239 | * This method is useful for generating lines of code that both | ||
| 240 | * end and begin nested blocks, like "} else {". | ||
| 241 | */ | ||
| 242 | public void pOlnI(String s) throws IOException { | ||
| 243 | pO(s); | ||
| 244 | pln(); | ||
| 245 | pI(); | ||
| 246 | } | ||
| 247 | |||
| 248 | /** | ||
| 249 | * Write Object. | ||
| 250 | */ | ||
| 251 | public void p(Object o) throws IOException { | ||
| 252 | write(o.toString()); | ||
| 253 | } | ||
| 254 | /** | ||
| 255 | * Write Object; end current line. | ||
| 256 | */ | ||
| 257 | public void pln(Object o) throws IOException { | ||
| 258 | p(o.toString()); | ||
| 259 | pln(); | ||
| 260 | } | ||
| 261 | |||
| 262 | /** | ||
| 263 | * Write Object; end current line; indent in. | ||
| 264 | */ | ||
| 265 | public void plnI(Object o) throws IOException { | ||
| 266 | p(o.toString()); | ||
| 267 | pln(); | ||
| 268 | pI(); | ||
| 269 | } | ||
| 270 | |||
| 271 | /** | ||
| 272 | * Indent out; write Object. | ||
| 273 | */ | ||
| 274 | public void pO(Object o) throws IOException { | ||
| 275 | pO(); | ||
| 276 | p(o.toString()); | ||
| 277 | } | ||
| 278 | |||
| 279 | /** | ||
| 280 | * Indent out; write Object; end current line. | ||
| 281 | */ | ||
| 282 | public void pOln(Object o) throws IOException { | ||
| 283 | pO(o.toString()); | ||
| 284 | pln(); | ||
| 285 | } | ||
| 286 | |||
| 287 | /** | ||
| 288 | * Indent out; write Object; end current line; indent in. | ||
| 289 | * | ||
| 290 | * This method is useful for generating lines of code that both | ||
| 291 | * end and begin nested blocks, like "} else {". | ||
| 292 | */ | ||
| 293 | public void pOlnI(Object o) throws IOException { | ||
| 294 | pO(o.toString()); | ||
| 295 | pln(); | ||
| 296 | pI(); | ||
| 297 | } | ||
| 298 | |||
| 299 | } | ||