Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.internal.util; |
| 18 | |
| 19 | import java.io.PrintWriter; |
| 20 | import java.io.Writer; |
| 21 | |
| 22 | /** |
| 23 | * Lightweight wrapper around {@link PrintWriter} that automatically indents |
Jeff Sharkey | 71cb446 | 2013-01-30 16:35:04 -0800 | [diff] [blame] | 24 | * newlines based on internal state. It also automatically wraps long lines |
| 25 | * based on given line length. |
| 26 | * <p> |
| 27 | * Delays writing indent until first actual write on a newline, enabling indent |
| 28 | * modification after newline. |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 29 | */ |
| 30 | public class IndentingPrintWriter extends PrintWriter { |
Jeff Sharkey | 71cb446 | 2013-01-30 16:35:04 -0800 | [diff] [blame] | 31 | private final String mSingleIndent; |
| 32 | private final int mWrapLength; |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 33 | |
Jeff Sharkey | 71cb446 | 2013-01-30 16:35:04 -0800 | [diff] [blame] | 34 | /** Mutable version of current indent */ |
| 35 | private StringBuilder mIndentBuilder = new StringBuilder(); |
| 36 | /** Cache of current {@link #mIndentBuilder} value */ |
| 37 | private char[] mCurrentIndent; |
| 38 | /** Length of current line being built, excluding any indent */ |
| 39 | private int mCurrentLength; |
| 40 | |
| 41 | /** |
| 42 | * Flag indicating if we're currently sitting on an empty line, and that |
| 43 | * next write should be prefixed with the current indent. |
| 44 | */ |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 45 | private boolean mEmptyLine = true; |
| 46 | |
Jeff Sharkey | 71cb446 | 2013-01-30 16:35:04 -0800 | [diff] [blame] | 47 | public IndentingPrintWriter(Writer writer, String singleIndent) { |
| 48 | this(writer, singleIndent, -1); |
| 49 | } |
| 50 | |
| 51 | public IndentingPrintWriter(Writer writer, String singleIndent, int wrapLength) { |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 52 | super(writer); |
Jeff Sharkey | 71cb446 | 2013-01-30 16:35:04 -0800 | [diff] [blame] | 53 | mSingleIndent = singleIndent; |
| 54 | mWrapLength = wrapLength; |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | public void increaseIndent() { |
Jeff Sharkey | 71cb446 | 2013-01-30 16:35:04 -0800 | [diff] [blame] | 58 | mIndentBuilder.append(mSingleIndent); |
| 59 | mCurrentIndent = null; |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | public void decreaseIndent() { |
Jeff Sharkey | 71cb446 | 2013-01-30 16:35:04 -0800 | [diff] [blame] | 63 | mIndentBuilder.delete(0, mSingleIndent.length()); |
| 64 | mCurrentIndent = null; |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 65 | } |
| 66 | |
Jeff Sharkey | 2f036c5 | 2012-04-13 12:18:13 -0700 | [diff] [blame] | 67 | public void printPair(String key, Object value) { |
| 68 | print(key + "=" + String.valueOf(value) + " "); |
| 69 | } |
| 70 | |
Jeff Sharkey | 328ebf2 | 2013-03-21 18:09:39 -0700 | [diff] [blame] | 71 | public void printHexPair(String key, int value) { |
| 72 | print(key + "=0x" + Integer.toHexString(value) + " "); |
| 73 | } |
| 74 | |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 75 | @Override |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 76 | public void write(char[] buf, int offset, int count) { |
Jeff Sharkey | 71cb446 | 2013-01-30 16:35:04 -0800 | [diff] [blame] | 77 | final int indentLength = mIndentBuilder.length(); |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 78 | final int bufferEnd = offset + count; |
| 79 | int lineStart = offset; |
| 80 | int lineEnd = offset; |
Jeff Sharkey | 71cb446 | 2013-01-30 16:35:04 -0800 | [diff] [blame] | 81 | |
| 82 | // March through incoming buffer looking for newlines |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 83 | while (lineEnd < bufferEnd) { |
| 84 | char ch = buf[lineEnd++]; |
Jeff Sharkey | 71cb446 | 2013-01-30 16:35:04 -0800 | [diff] [blame] | 85 | mCurrentLength++; |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 86 | if (ch == '\n') { |
Jeff Sharkey | 71cb446 | 2013-01-30 16:35:04 -0800 | [diff] [blame] | 87 | maybeWriteIndent(); |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 88 | super.write(buf, lineStart, lineEnd - lineStart); |
| 89 | lineStart = lineEnd; |
| 90 | mEmptyLine = true; |
Jeff Sharkey | 71cb446 | 2013-01-30 16:35:04 -0800 | [diff] [blame] | 91 | mCurrentLength = 0; |
| 92 | } |
| 93 | |
| 94 | // Wrap if we've pushed beyond line length |
| 95 | if (mWrapLength > 0 && mCurrentLength >= mWrapLength - indentLength) { |
| 96 | if (!mEmptyLine) { |
| 97 | // Give ourselves a fresh line to work with |
| 98 | super.write('\n'); |
| 99 | mEmptyLine = true; |
| 100 | mCurrentLength = lineEnd - lineStart; |
| 101 | } else { |
| 102 | // We need more than a dedicated line, slice it hard |
| 103 | maybeWriteIndent(); |
| 104 | super.write(buf, lineStart, lineEnd - lineStart); |
| 105 | super.write('\n'); |
| 106 | mEmptyLine = true; |
| 107 | lineStart = lineEnd; |
| 108 | mCurrentLength = 0; |
| 109 | } |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
| 113 | if (lineStart != lineEnd) { |
Jeff Sharkey | 71cb446 | 2013-01-30 16:35:04 -0800 | [diff] [blame] | 114 | maybeWriteIndent(); |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 115 | super.write(buf, lineStart, lineEnd - lineStart); |
| 116 | } |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 117 | } |
| 118 | |
Jeff Sharkey | 71cb446 | 2013-01-30 16:35:04 -0800 | [diff] [blame] | 119 | private void maybeWriteIndent() { |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 120 | if (mEmptyLine) { |
| 121 | mEmptyLine = false; |
Jeff Sharkey | 71cb446 | 2013-01-30 16:35:04 -0800 | [diff] [blame] | 122 | if (mIndentBuilder.length() != 0) { |
| 123 | if (mCurrentIndent == null) { |
| 124 | mCurrentIndent = mIndentBuilder.toString().toCharArray(); |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 125 | } |
Jeff Sharkey | 71cb446 | 2013-01-30 16:35:04 -0800 | [diff] [blame] | 126 | super.write(mCurrentIndent, 0, mCurrentIndent.length); |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 127 | } |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 128 | } |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 129 | } |
| 130 | } |