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