1 package org.apache.onami.configuration.variables;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import static java.lang.String.format;
23 import static java.util.logging.Level.FINEST;
24 import static java.util.logging.Logger.getLogger;
25
26 import java.util.Map;
27 import java.util.logging.Logger;
28
29
30
31
32
33
34 abstract class AbstractAppender
35 implements Appender
36 {
37
38
39 private static final Logger logger = getLogger( AbstractAppender.class.getName() );
40
41
42 protected final String chunk;
43
44
45
46
47
48
49 protected AbstractAppender( String chunk )
50 {
51 this.chunk = chunk;
52 }
53
54
55
56
57
58
59
60
61 public final void append( StringBuilder buffer, Map<String, String> configuration, Tree<Appender> context )
62 {
63
64 Tree<Appender> currentContext = context == null ? new Tree<Appender>( this ) : context.addLeaf( this );
65
66
67 if ( currentContext.inAncestors( this ) )
68 {
69
70 buffer.append( chunk );
71
72 logger.warning( format( "Recursion detected within variable resolving:%n%s", currentContext.getRoot() ) );
73 }
74
75 else
76 {
77 doAppend( buffer, configuration, currentContext );
78
79 if ( currentContext.isRoot() && logger.isLoggable( FINEST ) )
80 {
81 logger.finest( format( "Resolving variables:%n%s", currentContext ) );
82 }
83 }
84 }
85
86
87
88
89 public String resolve( Map<String, String> configuration )
90 {
91 StringBuilder buffer = new StringBuilder();
92 append( buffer, configuration, null );
93 return buffer.toString();
94 }
95
96
97
98
99
100
101
102
103 protected abstract void doAppend( StringBuilder buffer, Map<String, String> configuration, Tree<Appender> context );
104
105
106
107
108 @Override
109 public abstract boolean equals( Object obj );
110
111
112
113
114 @Override
115 public abstract int hashCode();
116
117
118
119
120 @Override
121 public final String toString()
122 {
123 return chunk;
124 }
125
126 }