|
15 | 15 | package org.eclipse.winery.model.tosca; |
16 | 16 |
|
17 | 17 | import java.util.ArrayList; |
| 18 | +import java.util.LinkedHashMap; |
18 | 19 | import java.util.List; |
| 20 | +import java.util.Map; |
19 | 21 | import java.util.Objects; |
20 | 22 |
|
21 | 23 | import javax.xml.bind.annotation.XmlAccessType; |
22 | 24 | import javax.xml.bind.annotation.XmlAccessorType; |
23 | 25 | import javax.xml.bind.annotation.XmlAnyElement; |
24 | 26 | import javax.xml.bind.annotation.XmlElement; |
25 | 27 | import javax.xml.bind.annotation.XmlType; |
| 28 | +import javax.xml.parsers.DocumentBuilder; |
| 29 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 30 | +import javax.xml.parsers.ParserConfigurationException; |
26 | 31 |
|
| 32 | +import org.eclipse.winery.model.tosca.constants.Namespaces; |
| 33 | + |
| 34 | +import io.github.adr.embedded.ADR; |
27 | 35 | import org.eclipse.jdt.annotation.NonNull; |
28 | 36 | import org.eclipse.jdt.annotation.Nullable; |
| 37 | +import org.w3c.dom.Comment; |
| 38 | +import org.w3c.dom.Document; |
| 39 | +import org.w3c.dom.Element; |
| 40 | +import org.w3c.dom.Node; |
| 41 | +import org.w3c.dom.NodeList; |
| 42 | +import org.w3c.dom.Text; |
29 | 43 |
|
30 | 44 | @XmlAccessorType(XmlAccessType.FIELD) |
31 | 45 | @XmlType(name = "tBoundaryDefinitions", propOrder = { |
@@ -197,13 +211,151 @@ public static class Properties { |
197 | 211 |
|
198 | 212 | @Nullable |
199 | 213 | public Object getAny() { |
200 | | - return any; |
| 214 | + if (this.getKVProperties() == null) { |
| 215 | + return any; |
| 216 | + } else { |
| 217 | + return null; |
| 218 | + } |
201 | 219 | } |
202 | 220 |
|
203 | 221 | public void setAny(@Nullable Object value) { |
204 | 222 | this.any = value; |
205 | 223 | } |
206 | 224 |
|
| 225 | + /** |
| 226 | + * This is a special method for Winery. Winery allows to define a property by specifying name/value values. |
| 227 | + * Instead of parsing the XML contained in TNodeType, this method is a convenience method to access this |
| 228 | + * information assumes the properties are key/value pairs (see WinerysPropertiesDefinition), all other cases are |
| 229 | + * return null. |
| 230 | + * <p> |
| 231 | + * Returns a map of key/values of this template based on the information of WinerysPropertiesDefinition. In case |
| 232 | + * no value is set, the empty string is used. The map is implemented as {@link LinkedHashMap} to ensure that the |
| 233 | + * order of the elements is the same as in the XML. We return the type {@link LinkedHashMap}, because there is |
| 234 | + * no appropriate Java interface for "sorted" Maps |
| 235 | + * <p> |
| 236 | + * In case the element is not of the form k/v, null is returned |
| 237 | + * <p> |
| 238 | + * This method assumes that the any field is always populated. |
| 239 | + * |
| 240 | + * @return null if not k/v, a map of k/v properties otherwise |
| 241 | + */ |
| 242 | + @ADR(12) |
| 243 | + public LinkedHashMap<String, String> getKVProperties() { |
| 244 | + // we use the internal variable "any", because getAny() returns null, if we have KVProperties |
| 245 | + if (any == null || !(any instanceof Element)) { |
| 246 | + return null; |
| 247 | + } |
| 248 | + |
| 249 | + Element el = (Element) any; |
| 250 | + if (el == null) { |
| 251 | + return null; |
| 252 | + } |
| 253 | + |
| 254 | + // we have no type information in this place |
| 255 | + // we could inject a repository, but if Winery is used with multiple repositories, this could cause race conditions |
| 256 | + // therefore, we guess at the instance of the properties definition (i.e., here) if it is key/value or not. |
| 257 | + |
| 258 | + boolean isKv = true; |
| 259 | + |
| 260 | + LinkedHashMap<String, String> properties = new LinkedHashMap<>(); |
| 261 | + |
| 262 | + NodeList childNodes = el.getChildNodes(); |
| 263 | + |
| 264 | + if (childNodes.getLength() == 0) { |
| 265 | + // somehow invalid XML - do not treat it as k/v |
| 266 | + return null; |
| 267 | + } |
| 268 | + |
| 269 | + for (int i = 0; i < childNodes.getLength(); i++) { |
| 270 | + Node item = childNodes.item(i); |
| 271 | + if (item instanceof Element) { |
| 272 | + String key = item.getLocalName(); |
| 273 | + String value; |
| 274 | + |
| 275 | + Element kvElement = (Element) item; |
| 276 | + NodeList kvElementChildNodes = kvElement.getChildNodes(); |
| 277 | + if (kvElementChildNodes.getLength() == 0) { |
| 278 | + value = ""; |
| 279 | + } else if (kvElementChildNodes.getLength() > 1) { |
| 280 | + // This is a wrong guess if comments are used, but this is prototype |
| 281 | + isKv = false; |
| 282 | + break; |
| 283 | + } else { |
| 284 | + // one child - just get the text. |
| 285 | + value = item.getTextContent(); |
| 286 | + } |
| 287 | + properties.put(key, value); |
| 288 | + } else if (item instanceof Text || item instanceof Comment) { |
| 289 | + // these kinds of nodes are OK |
| 290 | + } |
| 291 | + } |
| 292 | + |
| 293 | + if (isKv) { |
| 294 | + return properties; |
| 295 | + } else { |
| 296 | + return null; |
| 297 | + } |
| 298 | + } |
| 299 | + |
| 300 | + @ADR(12) |
| 301 | + public void setKVProperties(Map<String, String> properties) { |
| 302 | + Objects.requireNonNull(properties); |
| 303 | + Element el = (Element) any; |
| 304 | + |
| 305 | + if (el == null) { |
| 306 | + // special case if JSON is parsed without updating an existing element |
| 307 | + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); |
| 308 | + DocumentBuilder db; |
| 309 | + try { |
| 310 | + db = dbf.newDocumentBuilder(); |
| 311 | + } catch (ParserConfigurationException e) { |
| 312 | + throw new IllegalStateException("Could not instantiate document builder", e); |
| 313 | + } |
| 314 | + Document doc = db.newDocument(); |
| 315 | + |
| 316 | + // We cannot access the wrapper definitions, because we don't have access to the type |
| 317 | + // Element root = doc.createElementNS(wpd.getNamespace(), wpd.getElementName()); |
| 318 | + // Therefore, we create a dummy wrapper element: |
| 319 | + |
| 320 | + Element root = doc.createElementNS(Namespaces.EXAMPLE_NAMESPACE_URI, "Properties"); |
| 321 | + doc.appendChild(root); |
| 322 | + |
| 323 | + // No wpd - so this is not possible: |
| 324 | + // we produce the serialization in the same order the XSD would be generated (because of the usage of xsd:sequence) |
| 325 | + // for (PropertyDefinitionKV prop : wpd.getPropertyDefinitionKVList()) { |
| 326 | + |
| 327 | + for (String key : properties.keySet()) { |
| 328 | + // wpd.getNamespace() |
| 329 | + Element element = doc.createElementNS(Namespaces.EXAMPLE_NAMESPACE_URI, key); |
| 330 | + root.appendChild(element); |
| 331 | + String value = properties.get(key); |
| 332 | + if (value != null) { |
| 333 | + Text text = doc.createTextNode(value); |
| 334 | + element.appendChild(text); |
| 335 | + } |
| 336 | + } |
| 337 | + |
| 338 | + this.setAny(doc.getDocumentElement()); |
| 339 | + } else { |
| 340 | + // straight-forward copy over to existing property structure |
| 341 | + NodeList childNodes = el.getChildNodes(); |
| 342 | + |
| 343 | + for (int i = 0; i < childNodes.getLength(); i++) { |
| 344 | + Node item = childNodes.item(i); |
| 345 | + if (item instanceof Element) { |
| 346 | + final Element element = (Element) item; |
| 347 | + final String key = element.getLocalName(); |
| 348 | + final String value = properties.get(key); |
| 349 | + if (value != null) { |
| 350 | + element.setTextContent(value); |
| 351 | + } |
| 352 | + } else if (item instanceof Text || item instanceof Comment) { |
| 353 | + // these kinds of nodes are OK |
| 354 | + } |
| 355 | + } |
| 356 | + } |
| 357 | + } |
| 358 | + |
207 | 359 | public TBoundaryDefinitions.Properties.@Nullable PropertyMappings getPropertyMappings() { |
208 | 360 | return propertyMappings; |
209 | 361 | } |
|
0 commit comments