1
2
3
4 package de.powerstat.phplib.templateengine.intern;
5
6
7 import java.util.Objects;
8 import java.util.regex.Pattern;
9
10 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
11
12
13
14
15
16 public final class BlockManager
17 {
18
19
20
21
22
23
24
25
26 private final VariableManager variableManager;
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41 @SuppressFBWarnings("EI_EXPOSE_REP2")
42 public BlockManager(final VariableManager vManager, final BlockManager bManager)
43 {
44 super();
45 Objects.requireNonNull(vManager, "vManager");
46 Objects.requireNonNull(bManager, "bManager");
47 this.variableManager = vManager;
48
49
50
51
52
53
54 }
55
56
57
58
59
60
61
62 @SuppressFBWarnings("EI_EXPOSE_REP2")
63 public BlockManager(final VariableManager vManager)
64 {
65 super();
66 this.variableManager = vManager;
67 }
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83 @SuppressWarnings({"PMD.LinguisticNaming", "PMD.AvoidLiteralsInIfCondition"})
84 public boolean setBlock(final String parent, final String varname, final String name)
85 {
86
87 String internName = name;
88 if ((internName == null) || "".equals(internName))
89 {
90 internName = varname;
91 }
92 final var pattern = Pattern.compile("<!--\\s+BEGIN " + varname + "\\s+-->(.*)<!--\\s+END " + varname + "\\s+-->", Pattern.DOTALL | Pattern.MULTILINE);
93 final var matcher = pattern.matcher(this.variableManager.getVar(parent));
94 final String str = matcher.replaceFirst("{" + internName + "}");
95 this.variableManager.setVar(varname, matcher.group(1));
96 this.variableManager.setVar(parent, str);
97 return true;
98 }
99
100
101
102
103
104
105
106
107
108
109
110
111 @Override
112 public String toString()
113 {
114 return new StringBuilder().append("BlockManager[vars=").append(this.variableManager.getVars()).append(']').toString();
115 }
116
117
118
119
120
121
122
123
124 @Override
125 public int hashCode()
126 {
127 return Objects.hash(this.variableManager);
128 }
129
130
131
132
133
134
135
136
137
138 @Override
139 public boolean equals(final Object obj)
140 {
141 if (this == obj)
142 {
143 return true;
144 }
145 if (!(obj instanceof BlockManager))
146 {
147 return false;
148 }
149 final BlockManager other = (BlockManager)obj;
150 return this.variableManager.equals(other.variableManager);
151 }
152
153 }