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