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 |
| 24 | * newlines based on internal state. Delays writing indent until first actual |
| 25 | * write on a newline, enabling indent modification after newline. |
| 26 | */ |
| 27 | public class IndentingPrintWriter extends PrintWriter { |
| 28 | private final String mIndent; |
| 29 | |
| 30 | private StringBuilder mBuilder = new StringBuilder(); |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame^] | 31 | private char[] mCurrent; |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 32 | private boolean mEmptyLine = true; |
| 33 | |
| 34 | public IndentingPrintWriter(Writer writer, String indent) { |
| 35 | super(writer); |
| 36 | mIndent = indent; |
| 37 | } |
| 38 | |
| 39 | public void increaseIndent() { |
| 40 | mBuilder.append(mIndent); |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame^] | 41 | mCurrent = null; |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | public void decreaseIndent() { |
| 45 | mBuilder.delete(0, mIndent.length()); |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame^] | 46 | mCurrent = null; |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 47 | } |
| 48 | |
Jeff Sharkey | 2f036c5 | 2012-04-13 12:18:13 -0700 | [diff] [blame] | 49 | public void printPair(String key, Object value) { |
| 50 | print(key + "=" + String.valueOf(value) + " "); |
| 51 | } |
| 52 | |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 53 | @Override |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame^] | 54 | public void write(char[] buf, int offset, int count) { |
| 55 | final int bufferEnd = offset + count; |
| 56 | int lineStart = offset; |
| 57 | int lineEnd = offset; |
| 58 | while (lineEnd < bufferEnd) { |
| 59 | char ch = buf[lineEnd++]; |
| 60 | if (ch == '\n') { |
| 61 | writeIndent(); |
| 62 | super.write(buf, lineStart, lineEnd - lineStart); |
| 63 | lineStart = lineEnd; |
| 64 | mEmptyLine = true; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | if (lineStart != lineEnd) { |
| 69 | writeIndent(); |
| 70 | super.write(buf, lineStart, lineEnd - lineStart); |
| 71 | } |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 72 | } |
| 73 | |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame^] | 74 | private void writeIndent() { |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 75 | if (mEmptyLine) { |
| 76 | mEmptyLine = false; |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame^] | 77 | if (mBuilder.length() != 0) { |
| 78 | if (mCurrent == null) { |
| 79 | mCurrent = mBuilder.toString().toCharArray(); |
| 80 | } |
| 81 | super.write(mCurrent, 0, mCurrent.length); |
| 82 | } |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 83 | } |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 84 | } |
| 85 | } |