1 /* 2 * Copyright 2016, Google Inc. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: 8 * 9 * Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * Redistributions in binary form must reproduce the above 12 * copyright notice, this list of conditions and the following disclaimer 13 * in the documentation and/or other materials provided with the 14 * distribution. 15 * Neither the name of Google Inc. nor the names of its 16 * contributors may be used to endorse or promote products derived from 17 * this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 package org.jf.util; 33 34 import com.google.common.collect.Lists; 35 36 import java.io.FilterWriter; 37 import java.io.IOException; 38 import java.io.Writer; 39 import java.util.List; 40 41 public class WrappedIndentingWriter extends FilterWriter { 42 43 private final int maxIndent; 44 private final int maxWidth; 45 46 private int currentIndent = 0; 47 private final StringBuilder line = new StringBuilder(); 48 WrappedIndentingWriter(Writer out, int maxIndent, int maxWidth)49 public WrappedIndentingWriter(Writer out, int maxIndent, int maxWidth) { 50 super(out); 51 this.maxIndent = maxIndent; 52 this.maxWidth = maxWidth; 53 } 54 writeIndent()55 private void writeIndent() throws IOException { 56 for (int i=0; i<getIndent(); i++) { 57 write(' '); 58 } 59 } 60 getIndent()61 private int getIndent() { 62 if (currentIndent < 0) { 63 return 0; 64 } 65 if (currentIndent > maxIndent) { 66 return maxIndent; 67 } 68 return currentIndent; 69 } 70 indent(int indent)71 public void indent(int indent) { 72 currentIndent += indent; 73 } 74 deindent(int indent)75 public void deindent(int indent) { 76 currentIndent -= indent; 77 } 78 wrapLine()79 private void wrapLine() throws IOException { 80 List<String> wrapped = Lists.newArrayList(StringWrapper.wrapStringOnBreaks(line.toString(), maxWidth)); 81 out.write(wrapped.get(0), 0, wrapped.get(0).length()); 82 out.write('\n'); 83 84 line.replace(0, line.length(), ""); 85 writeIndent(); 86 for (int i=1; i<wrapped.size(); i++) { 87 if (i > 1) { 88 write('\n'); 89 } 90 write(wrapped.get(i)); 91 } 92 } 93 write(int c)94 @Override public void write(int c) throws IOException { 95 if (c == '\n') { 96 out.write(line.toString()); 97 out.write(c); 98 line.replace(0, line.length(), ""); 99 writeIndent(); 100 } else { 101 line.append((char)c); 102 if (line.length() > maxWidth) { 103 wrapLine(); 104 } 105 } 106 } 107 write(char[] cbuf, int off, int len)108 @Override public void write(char[] cbuf, int off, int len) throws IOException { 109 for (int i=0; i<len; i++) { 110 write(cbuf[i+off]); 111 } 112 } 113 write(String str, int off, int len)114 @Override public void write(String str, int off, int len) throws IOException { 115 for (int i=0; i<len; i++) { 116 write(str.charAt(i+off)); 117 } 118 } 119 flush()120 @Override public void flush() throws IOException { 121 out.write(line.toString()); 122 line.replace(0, line.length(), ""); 123 } 124 } 125