1 | /** |
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one |
---|
3 | * or more contributor license agreements. See the NOTICE file |
---|
4 | * distributed with this work for additional information |
---|
5 | * regarding copyright ownership. The ASF licenses this file |
---|
6 | * to you under the Apache License, Version 2.0 (the |
---|
7 | * "License"); you may not use this file except in compliance |
---|
8 | * with the License. You may obtain a copy of the License at |
---|
9 | * |
---|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
11 | * |
---|
12 | * Unless required by applicable law or agreed to in writing, software |
---|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
15 | * See the License for the specific language governing permissions and |
---|
16 | * limitations under the License. |
---|
17 | */ |
---|
18 | package org.apache.hadoop.record.compiler; |
---|
19 | |
---|
20 | import java.util.ArrayList; |
---|
21 | |
---|
22 | /** |
---|
23 | * A wrapper around StringBuffer that automatically does indentation |
---|
24 | */ |
---|
25 | public class CodeBuffer { |
---|
26 | |
---|
27 | static private ArrayList<Character> startMarkers = new ArrayList<Character>(); |
---|
28 | static private ArrayList<Character> endMarkers = new ArrayList<Character>(); |
---|
29 | |
---|
30 | static { |
---|
31 | addMarkers('{', '}'); |
---|
32 | addMarkers('(', ')'); |
---|
33 | } |
---|
34 | |
---|
35 | static void addMarkers(char ch1, char ch2) { |
---|
36 | startMarkers.add(ch1); |
---|
37 | endMarkers.add(ch2); |
---|
38 | } |
---|
39 | |
---|
40 | private int level = 0; |
---|
41 | private int numSpaces = 2; |
---|
42 | private boolean firstChar = true; |
---|
43 | private StringBuffer sb; |
---|
44 | |
---|
45 | /** Creates a new instance of CodeBuffer */ |
---|
46 | CodeBuffer() { |
---|
47 | this(2, ""); |
---|
48 | } |
---|
49 | |
---|
50 | CodeBuffer(String s) { |
---|
51 | this(2, s); |
---|
52 | } |
---|
53 | |
---|
54 | CodeBuffer(int numSpaces, String s) { |
---|
55 | sb = new StringBuffer(); |
---|
56 | this.numSpaces = numSpaces; |
---|
57 | this.append(s); |
---|
58 | } |
---|
59 | |
---|
60 | void append(String s) { |
---|
61 | int length = s.length(); |
---|
62 | for (int idx = 0; idx < length; idx++) { |
---|
63 | char ch = s.charAt(idx); |
---|
64 | append(ch); |
---|
65 | } |
---|
66 | } |
---|
67 | |
---|
68 | void append(char ch) { |
---|
69 | if (endMarkers.contains(ch)) { |
---|
70 | level--; |
---|
71 | } |
---|
72 | if (firstChar) { |
---|
73 | for (int idx = 0; idx < level; idx++) { |
---|
74 | for (int num = 0; num < numSpaces; num++) { |
---|
75 | rawAppend(' '); |
---|
76 | } |
---|
77 | } |
---|
78 | } |
---|
79 | rawAppend(ch); |
---|
80 | firstChar = false; |
---|
81 | if (startMarkers.contains(ch)) { |
---|
82 | level++; |
---|
83 | } |
---|
84 | if (ch == '\n') { |
---|
85 | firstChar = true; |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | private void rawAppend(char ch) { |
---|
90 | sb.append(ch); |
---|
91 | } |
---|
92 | |
---|
93 | public String toString() { |
---|
94 | return sb.toString(); |
---|
95 | } |
---|
96 | } |
---|