svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.expressionevaluator / org.gvsig.expressionevaluator.lib / org.gvsig.expressionevaluator.lib.api / src / main / java / org / gvsig / expressionevaluator / spi / PrettyFormaterImpl.java @ 47734
History | View | Annotate | Download (2.98 KB)
1 |
/*
|
---|---|
2 |
* To change this license header, choose License Headers in Project Properties.
|
3 |
* To change this template file, choose Tools | Templates
|
4 |
* and open the template in the editor.
|
5 |
*/
|
6 |
package org.gvsig.expressionevaluator.spi; |
7 |
|
8 |
import java.util.ArrayList; |
9 |
import java.util.List; |
10 |
import java.util.Stack; |
11 |
import org.apache.commons.lang3.StringUtils; |
12 |
import org.gvsig.expressionevaluator.PrettyFormatter; |
13 |
|
14 |
/**
|
15 |
*
|
16 |
* @author jjdelcerro
|
17 |
*/
|
18 |
public class PrettyFormaterImpl implements PrettyFormatter { |
19 |
|
20 |
private class PrettyFormaterConfig { |
21 |
int indentSize;
|
22 |
int indentation;
|
23 |
boolean useNl;
|
24 |
StringBuilder builder;
|
25 |
|
26 |
public PrettyFormaterConfig() {
|
27 |
this.indentSize = 4; |
28 |
this.indentation = 0; |
29 |
this.useNl = false; |
30 |
this.builder = null; |
31 |
} |
32 |
|
33 |
public PrettyFormaterConfig(int indentation, boolean useNl) { |
34 |
this.indentation = indentation;
|
35 |
this.useNl = useNl;
|
36 |
this.builder = null; |
37 |
} |
38 |
|
39 |
public StringBuilder builder() { |
40 |
if( this.builder == null ) { |
41 |
this.builder = new StringBuilder(); |
42 |
} |
43 |
return this.builder; |
44 |
} |
45 |
} |
46 |
|
47 |
private Stack<PrettyFormaterConfig> config; |
48 |
|
49 |
public PrettyFormaterImpl() {
|
50 |
this.config = new Stack<>(); |
51 |
this.config.push(new PrettyFormaterConfig()); |
52 |
} |
53 |
|
54 |
@Override
|
55 |
public void setIndentSize(int indentSize) { |
56 |
PrettyFormaterConfig c = this.config.peek();
|
57 |
c.indentSize = indentSize; |
58 |
} |
59 |
|
60 |
@Override
|
61 |
public void setUseNl(boolean useNl) { |
62 |
PrettyFormaterConfig c = this.config.peek();
|
63 |
c.useNl = useNl; |
64 |
} |
65 |
|
66 |
@Override
|
67 |
public void indent() { |
68 |
PrettyFormaterConfig c = this.config.peek();
|
69 |
c.indentation += 4;
|
70 |
} |
71 |
|
72 |
@Override
|
73 |
public void unindent() { |
74 |
PrettyFormaterConfig c = this.config.peek();
|
75 |
c.indentation -= 4;
|
76 |
if( c.indentation<0 ) { |
77 |
c.indentation = 0;
|
78 |
} |
79 |
} |
80 |
|
81 |
@Override
|
82 |
public void nl() { |
83 |
PrettyFormaterConfig c = this.config.peek();
|
84 |
if( c.useNl ) {
|
85 |
c.builder().append("\n");
|
86 |
c.builder().append(StringUtils.repeat(' ', c.indentation));
|
87 |
} |
88 |
} |
89 |
|
90 |
@Override
|
91 |
public void push() { |
92 |
PrettyFormaterConfig c = this.config.peek();
|
93 |
this.config.push(new PrettyFormaterConfig(c.indentation,c.useNl)); |
94 |
} |
95 |
|
96 |
@Override
|
97 |
public void pop() { |
98 |
PrettyFormaterConfig c = this.config.pop();
|
99 |
if( this.config.isEmpty() ) { |
100 |
this.config.push(new PrettyFormaterConfig()); |
101 |
} |
102 |
} |
103 |
|
104 |
@Override
|
105 |
public void append(Object o) { |
106 |
PrettyFormaterConfig c = this.config.peek();
|
107 |
c.builder().append(o); |
108 |
} |
109 |
|
110 |
@Override
|
111 |
public String build() { |
112 |
PrettyFormaterConfig c = this.config.peek();
|
113 |
if( c.builder==null ) { |
114 |
return ""; |
115 |
} |
116 |
return c.builder.toString();
|
117 |
} |
118 |
|
119 |
} |