|
| 1 | +/* |
| 2 | + * [The "BSD license"] |
| 3 | + * Copyright (c) 2011 Terence Parr |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * Redistribution and use in source and binary forms, with or without |
| 7 | + * modification, are permitted provided that the following conditions |
| 8 | + * are met: |
| 9 | + * 1. Redistributions of source code must retain the above copyright |
| 10 | + * notice, this list of conditions and the following disclaimer. |
| 11 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | + * notice, this list of conditions and the following disclaimer in the |
| 13 | + * documentation and/or other materials provided with the distribution. |
| 14 | + * 3. The name of the author may not be used to endorse or promote products |
| 15 | + * derived from this software without specific prior written permission. |
| 16 | + * |
| 17 | + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 18 | + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 19 | + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 20 | + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 21 | + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 22 | + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 26 | + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | + */ |
| 28 | +package org.stringtemplate.v4; |
| 29 | + |
| 30 | +import java.io.IOException; |
| 31 | +import java.io.Writer; |
| 32 | +import java.util.ArrayList; |
| 33 | +import java.util.List; |
| 34 | +import java.util.Stack; |
| 35 | + |
| 36 | +/** |
| 37 | + * Essentially a char filter that knows how to auto-indent output by maintaining |
| 38 | + * a stack of indent levels. |
| 39 | + * <p> |
| 40 | + * The indent stack is a stack of strings so we can repeat original indent not |
| 41 | + * just the same number of columns (don't have to worry about tabs vs spaces |
| 42 | + * then). Anchors are char positions (tabs won't work) that indicate where all |
| 43 | + * future wraps should justify to. The wrap position is actually the larger of |
| 44 | + * either the last anchor or the indentation level.</p> |
| 45 | + * <p> |
| 46 | + * This is a filter on a {@link Writer}.</p> |
| 47 | + * <p> |
| 48 | + * {@code \n} is the proper way to say newline for options and templates. |
| 49 | + * Templates can mix {@code \r\n} and {@code \n} them, but use {@code \n} in |
| 50 | + * options like {@code wrap="\n"}. This writer will render newline characters |
| 51 | + * according to {@link #newline}. The default value is taken from the |
| 52 | + * {@code line.separator} system property, and can be overridden by passing in a |
| 53 | + * {@code String} to the appropriate constructor.</p> |
| 54 | + */ |
| 55 | +public class AutoIndentWriter implements STWriter { |
| 56 | + /** Stack of indents. Use {@link List} as it's much faster than {@link Stack}. Grows |
| 57 | + * from 0..n-1. |
| 58 | + */ |
| 59 | + public List<String> indents = new ArrayList<String>(); |
| 60 | + |
| 61 | + /** Stack of integer anchors (char positions in line); avoid {@link Integer} |
| 62 | + * creation overhead. |
| 63 | + */ |
| 64 | + public int[] anchors = new int[10]; |
| 65 | + public int anchors_sp = -1; |
| 66 | + |
| 67 | + /** {@code \n} or {@code \r\n}? */ |
| 68 | + public String newline; |
| 69 | + |
| 70 | + public Writer out = null; |
| 71 | + public boolean atStartOfLine = true; |
| 72 | + |
| 73 | + /** |
| 74 | + * Track char position in the line (later we can think about tabs). Indexed |
| 75 | + * from 0. We want to keep {@code charPosition <= }{@link #lineWidth}. |
| 76 | + * This is the position we are <em>about</em> to write, not the position |
| 77 | + * last written to. |
| 78 | + */ |
| 79 | + public int charPosition = 0; |
| 80 | + |
| 81 | + /** The absolute char index into the output of the next char to be written. */ |
| 82 | + public int charIndex = 0; |
| 83 | + |
| 84 | + public int lineWidth = NO_WRAP; |
| 85 | + |
| 86 | + public AutoIndentWriter(Writer out, String newline) { |
| 87 | + this.out = out; |
| 88 | + indents.add(null); // s oftart with no indent |
| 89 | + this.newline = newline; |
| 90 | + } |
| 91 | + |
| 92 | + public AutoIndentWriter(Writer out) { |
| 93 | + this(out, System.getProperty("line.separator")); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public void setLineWidth(int lineWidth) { |
| 98 | + this.lineWidth = lineWidth; |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public void pushIndentation(String indent) { |
| 103 | + indents.add(indent); |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public String popIndentation() { |
| 108 | + return indents.remove(indents.size()-1); |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public void pushAnchorPoint() { |
| 113 | + if ( (anchors_sp +1)>=anchors.length ) { |
| 114 | + int[] a = new int[anchors.length*2]; |
| 115 | + System.arraycopy(anchors, 0, a, 0, anchors.length-1); |
| 116 | + anchors = a; |
| 117 | + } |
| 118 | + anchors_sp++; |
| 119 | + anchors[anchors_sp] = charPosition; |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public void popAnchorPoint() { |
| 124 | + anchors_sp--; |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public int index() { return charIndex; } |
| 129 | + |
| 130 | + /** Write out a string literal or attribute expression or expression element. */ |
| 131 | + @Override |
| 132 | + public int write(String str) throws IOException { |
| 133 | + int n = 0; |
| 134 | + int nll = newline.length(); |
| 135 | + int sl = str.length(); |
| 136 | + for (int i=0; i<sl; i++) { |
| 137 | + char c = str.charAt(i); |
| 138 | + // found \n or \r\n newline? |
| 139 | + if ( c=='\r' ) continue; |
| 140 | + if ( c=='\n' ) { |
| 141 | + atStartOfLine = true; |
| 142 | + charPosition = -nll; // set so the write below sets to 0 |
| 143 | + out.write(newline); |
| 144 | + n += nll; |
| 145 | + charIndex += nll; |
| 146 | + charPosition += n; // wrote n more char |
| 147 | + continue; |
| 148 | + } |
| 149 | + // normal character |
| 150 | + // check to see if we are at the start of a line; need indent if so |
| 151 | + if ( atStartOfLine ) { |
| 152 | + n+=indent(); |
| 153 | + atStartOfLine = false; |
| 154 | + } |
| 155 | + n++; |
| 156 | + out.write(c); |
| 157 | + charPosition++; |
| 158 | + charIndex++; |
| 159 | + } |
| 160 | + return n; |
| 161 | + } |
| 162 | + |
| 163 | + @Override |
| 164 | + public int writeSeparator(String str) throws IOException { |
| 165 | + return write(str); |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * Write out a string literal or attribute expression or expression element. |
| 170 | + * <p> |
| 171 | + * If doing line wrap, then check {@code wrap} before emitting {@code str}. |
| 172 | + * If at or beyond desired line width then emit a {@link #newline} and any |
| 173 | + * indentation before spitting out {@code str}.</p> |
| 174 | + */ |
| 175 | + @Override |
| 176 | + public int write(String str, String wrap) throws IOException { |
| 177 | + int n = writeWrap(wrap); |
| 178 | + return n + write(str); |
| 179 | + } |
| 180 | + |
| 181 | + @Override |
| 182 | + public int writeWrap(String wrap) throws IOException { |
| 183 | + int n = 0; |
| 184 | + // if want wrap and not already at start of line (last char was \n) |
| 185 | + // and we have hit or exceeded the threshold |
| 186 | + if ( lineWidth!=NO_WRAP && wrap!=null && !atStartOfLine && |
| 187 | + charPosition >= lineWidth ) |
| 188 | + { |
| 189 | + // ok to wrap |
| 190 | + // Walk wrap string and look for A\nB. Spit out A\n |
| 191 | + // then spit indent or anchor, whichever is larger |
| 192 | + // then spit out B. |
| 193 | + for (int i=0; i<wrap.length(); i++) { |
| 194 | + char c = wrap.charAt(i); |
| 195 | + if ( c=='\r' ) { |
| 196 | + continue; |
| 197 | + } else if ( c=='\n' ) { |
| 198 | + out.write(newline); |
| 199 | + n += newline.length(); |
| 200 | + charPosition = 0; |
| 201 | + charIndex += newline.length(); |
| 202 | + n += indent(); |
| 203 | + // continue writing any chars out |
| 204 | + } |
| 205 | + else { // write A or B part |
| 206 | + n++; |
| 207 | + out.write(c); |
| 208 | + charPosition++; |
| 209 | + charIndex++; |
| 210 | + } |
| 211 | + } |
| 212 | + } |
| 213 | + return n; |
| 214 | + } |
| 215 | + |
| 216 | + public int indent() throws IOException { |
| 217 | + int n = 0; |
| 218 | + for (String ind : indents) { |
| 219 | + if (ind != null) { |
| 220 | + n += ind.length(); |
| 221 | + out.write(ind); |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + // If current anchor is beyond current indent width, indent to anchor |
| 226 | + // *after* doing indents (might tabs in there or whatever) |
| 227 | + int indentWidth = n; |
| 228 | + if ( anchors_sp>=0 && anchors[anchors_sp]>indentWidth ) { |
| 229 | + int remainder = anchors[anchors_sp]-indentWidth; |
| 230 | + for (int i=1; i<=remainder; i++) out.write(' '); |
| 231 | + n += remainder; |
| 232 | + } |
| 233 | + |
| 234 | + charPosition += n; |
| 235 | + charIndex += n; |
| 236 | + return n; |
| 237 | + } |
| 238 | +} |
0 commit comments