1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package io.github.tonywasher.joceanus.themis.parser.maven;
19
20 import io.github.tonywasher.joceanus.metis.data.MetisDataItem.MetisDataMap;
21 import io.github.tonywasher.joceanus.metis.field.MetisFieldItem;
22 import io.github.tonywasher.joceanus.metis.field.MetisFieldSet;
23 import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
24 import io.github.tonywasher.joceanus.oceanus.base.OceanusSystem;
25 import io.github.tonywasher.joceanus.oceanus.format.OceanusDataFormatter;
26 import io.github.tonywasher.joceanus.themis.exc.ThemisDataException;
27 import io.github.tonywasher.joceanus.themis.parser.base.ThemisDataResource;
28 import io.github.tonywasher.joceanus.themis.parser.maven.ThemisMavenId.ThemisXElementParser;
29 import org.w3c.dom.Element;
30 import org.w3c.dom.Node;
31
32 import java.util.HashMap;
33 import java.util.Map;
34
35
36
37
38 public class ThemisMavenPropertyCache
39 implements ThemisXElementParser, MetisDataMap<String, String>, MetisFieldItem {
40
41
42
43 private static final MetisFieldSet<ThemisMavenPropertyCache> FIELD_DEFS = MetisFieldSet.newFieldSet(ThemisMavenPropertyCache.class);
44
45
46
47
48 static {
49 FIELD_DEFS.declareLocalField(ThemisDataResource.DATA_PARENT, ThemisMavenPropertyCache::getParent);
50 }
51
52
53
54
55 private static final String PROP_START = "${";
56
57
58
59
60 private static final String PROP_END = "}";
61
62
63
64
65 private final Map<String, String> theMap;
66
67
68
69
70 private ThemisMavenPropertyCache theParent;
71
72
73
74
75 ThemisMavenPropertyCache() {
76 theMap = new HashMap<>();
77 setProperty("javafx.platform", OceanusSystem.determineSystem().getClassifier());
78 }
79
80 @Override
81 public MetisFieldSet<ThemisMavenPropertyCache> getDataFieldSet() {
82 return FIELD_DEFS;
83 }
84
85 @Override
86 public String formatObject(final OceanusDataFormatter pFormatter) {
87 return getClass().getSimpleName();
88 }
89
90 @Override
91 public Map<String, String> getUnderlyingMap() {
92 return theMap;
93 }
94
95
96
97
98
99
100 private ThemisMavenPropertyCache getParent() {
101 return theParent;
102 }
103
104
105
106
107
108
109 void setParent(final ThemisMavenPropertyCache pParent) {
110 theParent = pParent;
111 }
112
113
114
115
116
117
118
119 void setProperty(final String pName,
120 final String pValue) {
121 final String myProperty = getPropertyKey(pName);
122 theMap.put(myProperty, pValue);
123 }
124
125
126
127
128
129
130
131 String getProperty(final String pName) {
132 final String myProperty = getPropertyKey(pName);
133 return theMap.get(myProperty);
134 }
135
136
137
138
139
140
141
142 private String getPropertyKey(final String pName) {
143 return PROP_START + pName + PROP_END;
144 }
145
146 @Override
147 public String getElementValue(final Element pElement,
148 final String pValue) throws OceanusException {
149
150 if (pElement == null) {
151 return null;
152 }
153
154
155 for (Node myChild = pElement.getFirstChild();
156 myChild != null;
157 myChild = myChild.getNextSibling()) {
158
159 if (myChild instanceof Element
160 && pValue.equals(myChild.getNodeName())) {
161 return replaceProperty(myChild.getTextContent());
162 }
163 }
164
165
166 return null;
167 }
168
169
170
171
172
173
174
175 private String replaceProperty(final String pValue) throws ThemisDataException {
176
177 String myCurrent = pValue;
178 while (containsProperty(myCurrent)) {
179
180 final String myResult = replaceNextProperty(myCurrent);
181
182
183 if (myResult.equals(myCurrent)) {
184 throw new ThemisDataException("Unknown embedded property - " + myCurrent);
185 }
186
187
188 myCurrent = myResult;
189 }
190 return myCurrent;
191 }
192
193
194
195
196
197
198
199 private boolean containsProperty(final String pValue) {
200 final int myIndex = pValue.indexOf(PROP_START);
201 return myIndex != -1;
202 }
203
204
205
206
207
208
209
210 private String replaceNextProperty(final String pValue) {
211 for (Map.Entry<String, String> myEntry : theMap.entrySet()) {
212 if (pValue.contains(myEntry.getKey())) {
213 return pValue.replace(myEntry.getKey(), myEntry.getValue());
214 }
215 }
216 return theParent != null ? theParent.replaceNextProperty(pValue) : pValue;
217 }
218 }