AT&T 2.0.19 Code drop, stage 1
[aaf/authz.git] / misc / env / src / main / java / org / onap / aaf / misc / env / util / IndentPrintWriter.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  * ===========================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END====================================================
19  *
20  */
21
22 package org.onap.aaf.misc.env.util;
23
24 import java.io.OutputStream;
25 import java.io.PrintWriter;
26 import java.io.Writer;
27
28 /**
29  * @author Jonathan
30  * 
31  *         Catch \n and indent according to current indent levels of JavaGen
32  */
33 public class IndentPrintWriter extends PrintWriter {
34         public static int INDENT = 2;
35         private boolean addIndent;
36         private int indent;
37         private int col;
38
39         public IndentPrintWriter(Writer out) {
40                 super(out);
41                 addIndent = false;
42                 indent = col = 0;
43         }
44         
45         public IndentPrintWriter(OutputStream out) {
46                 super(out);
47                 addIndent = false;
48                 indent = col = 0;
49         }
50
51
52     public void write(String str) {
53         int len = str.length();
54                 for(int i=0;i<len;++i) {
55                         write((int)str.charAt(i));
56                 }
57     }
58     
59     public void println() {
60         write((int)'\n');
61     }
62         public void write(String str, int off, int len)  {
63                 len = Math.min(str.length(),off+len);
64                 for(int i=off;i<len;++i) {
65                         write((int)str.charAt(i));
66                 }
67         }
68         public void write(int b) {
69                 if (b == '\n') {
70                         addIndent = true;
71                         col = 0;
72                 } else if (addIndent) {
73                         addIndent = false;
74                         toIndent();
75                 } else {
76                         ++col;
77                 }
78                 super.write(b);
79         }
80
81         @Override
82         public void write(char[] buf, int off, int len) {
83                 for (int i = 0; i < len; ++i)
84                         write(buf[i] + off);
85         }
86
87         public void setIndent(int size) {
88                 indent = size;
89         }
90
91         public void inc() {
92                 ++indent;
93         }
94         
95         public void dec() {
96                 --indent;
97         }
98
99         public void toCol(int idx) {
100                 while(idx>col++)super.write((int)' ');
101         }
102
103         public int getIndent() {
104                 return indent;
105         }
106
107         public void toIndent() {
108                 int end = indent * INDENT;
109                 for (int i = 0; i < end; ++i) {
110                         super.write((int) ' ');
111                 }
112                 col = end;
113         }
114 }