Merge "Added documentation for web socket client"
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / codec / xml / XmlDocumentUtils.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.data.impl.codec.xml;
9
10 import static com.google.common.base.Preconditions.checkState;
11
12 import java.net.URI;
13 import java.util.Collection;
14 import java.util.HashMap;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Map.Entry;
18 import java.util.Random;
19 import java.util.Set;
20
21 import javax.activation.UnsupportedDataTypeException;
22 import javax.xml.parsers.DocumentBuilder;
23 import javax.xml.parsers.DocumentBuilderFactory;
24 import javax.xml.parsers.ParserConfigurationException;
25
26 import com.google.common.collect.Maps;
27 import org.opendaylight.yangtools.yang.common.QName;
28 import org.opendaylight.yangtools.yang.data.api.*;
29 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates;
30 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeWithValue;
31 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
32 import org.opendaylight.yangtools.yang.data.api.Node;
33 import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode;
34 import org.opendaylight.yangtools.yang.data.impl.SimpleNodeTOImpl;
35 import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
36 import org.opendaylight.yangtools.yang.data.impl.util.CompositeNodeBuilder;
37 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
38 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
39 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
40 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
41 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
42 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
43 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
44 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
45 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
46 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
47 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
48 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
49 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52 import org.w3c.dom.*;
53
54 import com.google.common.base.Function;
55 import com.google.common.base.Objects;
56 import com.google.common.base.Optional;
57 import com.google.common.base.Preconditions;
58 import com.google.common.base.Strings;
59 import com.google.common.collect.ImmutableList;
60
61 public class XmlDocumentUtils {
62
63     private static final XmlCodecProvider DEFAULT_XML_VALUE_CODEC_PROVIDER = new XmlCodecProvider() {
64
65         @Override
66         public TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> codecFor(TypeDefinition<?> baseType) {
67             return TypeDefinitionAwareCodec.from(baseType);
68         }
69     };
70
71     private static final Logger logger = LoggerFactory.getLogger(XmlDocumentUtils.class);
72
73     /**
74      * Converts Data DOM structure to XML Document for specified XML Codec Provider and corresponding
75      * Data Node Container schema. The CompositeNode data parameter enters as root of Data DOM tree and will
76      * be transformed to root in XML Document. Each element of Data DOM tree is compared against specified Data
77      * Node Container Schema and transformed accordingly.
78      *
79      * @param data Data DOM root element
80      * @param schema Data Node Container Schema
81      * @param codecProvider XML Codec Provider
82      * @return new instance of XML Document
83      * @throws UnsupportedDataTypeException
84      */
85     public static Document toDocument(CompositeNode data, DataNodeContainer schema, XmlCodecProvider codecProvider)
86             throws UnsupportedDataTypeException {
87         Preconditions.checkNotNull(data);
88         Preconditions.checkNotNull(schema);
89
90         Document doc = getDocument();
91
92         if (schema instanceof ContainerSchemaNode || schema instanceof ListSchemaNode) {
93             doc.appendChild(createXmlRootElement(doc, data, (SchemaNode) schema, codecProvider));
94             return doc;
95         } else {
96             throw new UnsupportedDataTypeException(
97                     "Schema can be ContainerSchemaNode or ListSchemaNode. Other types are not supported yet.");
98         }
99     }
100
101     public static Document getDocument() {
102         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
103         Document doc = null;
104         try {
105             DocumentBuilder bob = dbf.newDocumentBuilder();
106             doc = bob.newDocument();
107         } catch (ParserConfigurationException e) {
108             throw new RuntimeException(e);
109         }
110         return doc;
111     }
112
113     /**
114      * Converts Data DOM structure to XML Document for specified XML Codec Provider. The CompositeNode
115      * data parameter enters as root of Data DOM tree and will be transformed to root in XML Document. The child
116      * nodes of Data Tree are transformed accordingly.
117      *
118      * @param data Data DOM root element
119      * @param codecProvider XML Codec Provider
120      * @return new instance of XML Document
121      * @throws UnsupportedDataTypeException
122      */
123     public static Document toDocument(CompositeNode data, XmlCodecProvider codecProvider)
124             throws UnsupportedDataTypeException {
125         Preconditions.checkNotNull(data);
126
127         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
128         dbf.setNamespaceAware(true);
129         Document doc = null;
130         try {
131             DocumentBuilder bob = dbf.newDocumentBuilder();
132             doc = bob.newDocument();
133         } catch (ParserConfigurationException e) {
134             return null;
135         }
136
137         doc.appendChild(createXmlRootElement(doc, data, null, codecProvider));
138         return doc;
139     }
140
141     private static Element createXmlRootElement(Document doc, Node<?> data, SchemaNode schema,
142             XmlCodecProvider codecProvider) throws UnsupportedDataTypeException {
143         Element itemEl = createElementFor(doc, data);
144         if (data instanceof SimpleNode<?>) {
145             if (schema instanceof LeafListSchemaNode) {
146                 writeValueByType(itemEl, (SimpleNode<?>) data, ((LeafListSchemaNode) schema).getType(),
147                         (DataSchemaNode) schema, codecProvider);
148             } else if (schema instanceof LeafSchemaNode) {
149                 writeValueByType(itemEl, (SimpleNode<?>) data, ((LeafSchemaNode) schema).getType(),
150                         (DataSchemaNode) schema, codecProvider);
151             } else {
152                 Object value = data.getValue();
153                 if (value != null) {
154                     itemEl.setTextContent(String.valueOf(value));
155                 }
156             }
157         } else { // CompositeNode
158             for (Node<?> child : ((CompositeNode) data).getChildren()) {
159                 DataSchemaNode childSchema = null;
160                 if (schema != null) {
161                     childSchema = findFirstSchemaForNode(child, ((DataNodeContainer) schema).getChildNodes());
162                     if (logger.isDebugEnabled()) {
163                         if (childSchema == null) {
164                             logger.debug("Probably the data node \""
165                                     + ((child == null) ? "" : child.getNodeType().getLocalName())
166                                     + "\" is not conform to schema");
167                         }
168                     }
169                 }
170                 itemEl.appendChild(createXmlRootElement(doc, child, childSchema, codecProvider));
171             }
172         }
173         return itemEl;
174     }
175
176     public static Element createElementFor(Document doc, Node<?> data) {
177         QName dataType = data.getNodeType();
178         Element ret;
179         if (dataType.getNamespace() != null) {
180             ret = doc.createElementNS(dataType.getNamespace().toString(), dataType.getLocalName());
181         } else {
182             ret = doc.createElementNS(null, dataType.getLocalName());
183         }
184         if (data instanceof AttributesContainer && ((AttributesContainer) data).getAttributes() != null) {
185             for (Entry<QName, String> attribute : ((AttributesContainer) data).getAttributes().entrySet()) {
186                 ret.setAttributeNS(attribute.getKey().getNamespace().toString(), attribute.getKey().getLocalName(),
187                         attribute.getValue());
188             }
189
190         }
191         return ret;
192     }
193
194     public static void writeValueByType(Element element, SimpleNode<?> node, TypeDefinition<?> type,
195             DataSchemaNode schema, XmlCodecProvider codecProvider) {
196
197         Object nodeValue = node.getValue();
198
199         writeValueByType(element, type, codecProvider, nodeValue);
200     }
201
202     public static void writeValueByType(Element element, TypeDefinition<?> type, XmlCodecProvider codecProvider, Object nodeValue) {
203         TypeDefinition<?> baseType = resolveBaseTypeFrom(type);
204         if (baseType instanceof IdentityrefTypeDefinition) {
205             if (nodeValue instanceof QName) {
206                 QName value = (QName) nodeValue;
207                 String prefix = "x";
208                 if (value.getPrefix() != null && !value.getPrefix().isEmpty()) {
209                     prefix = value.getPrefix();
210                 }
211                 element.setAttribute("xmlns:" + prefix, value.getNamespace().toString());
212                 element.setTextContent(prefix + ":" + value.getLocalName());
213             } else {
214                 Object value = nodeValue;
215                 logger.debug("Value of {}:{} is not instance of QName but is {}", baseType.getQName().getNamespace(),
216                         baseType.getQName().getLocalName(), value != null ? value.getClass() : "null");
217                 if (value != null) {
218                     element.setTextContent(String.valueOf(value));
219                 }
220             }
221         } else if (baseType instanceof InstanceIdentifierTypeDefinition) {
222             if (nodeValue instanceof InstanceIdentifier) {
223                 // Map< key = namespace, value = prefix>
224                 Map<String, String> prefixes = new HashMap<>();
225                 InstanceIdentifier instanceIdentifier = (InstanceIdentifier) nodeValue;
226                 StringBuilder textContent = new StringBuilder();
227                 for (PathArgument pathArgument : instanceIdentifier.getPath()) {
228                     textContent.append("/");
229                     writeIdentifierWithNamespacePrefix(element, textContent, pathArgument.getNodeType(), prefixes);
230                     if (pathArgument instanceof NodeIdentifierWithPredicates) {
231                         Map<QName, Object> predicates = ((NodeIdentifierWithPredicates) pathArgument).getKeyValues();
232
233                         for (QName keyValue : predicates.keySet()) {
234                             String predicateValue = String.valueOf(predicates.get(keyValue));
235                             textContent.append("[");
236                             writeIdentifierWithNamespacePrefix(element, textContent, keyValue, prefixes);
237                             textContent.append("='");
238                             textContent.append(predicateValue);
239                             textContent.append("'");
240                             textContent.append("]");
241                         }
242                     } else if (pathArgument instanceof NodeWithValue) {
243                         textContent.append("[.='");
244                         textContent.append(((NodeWithValue)pathArgument).getValue());
245                         textContent.append("'");
246                         textContent.append("]");
247                     }
248                 }
249                 element.setTextContent(textContent.toString());
250
251             } else {
252                 Object value = nodeValue;
253                 logger.debug("Value of {}:{} is not instance of InstanceIdentifier but is {}", baseType.getQName()
254                         .getNamespace(), //
255                         baseType.getQName().getLocalName(), value != null ? value.getClass() : "null");
256                 if (value != null) {
257                     element.setTextContent(String.valueOf(value));
258                 }
259             }
260         } else {
261             if (nodeValue != null) {
262                 final TypeDefinitionAwareCodec<Object, ?> codec = codecProvider.codecFor(baseType);
263                 if (codec != null) {
264                     try {
265                         final String text = codec.serialize(nodeValue);
266                         element.setTextContent(text);
267                     } catch (ClassCastException e) {
268                         logger.error("Provided node value {} did not have type {} required by mapping. Using stream instead.", nodeValue, baseType, e);
269                         element.setTextContent(String.valueOf(nodeValue));
270                     }
271                 } else {
272                     logger.error("Failed to find codec for {}, falling back to using stream", baseType);
273                     element.setTextContent(String.valueOf(nodeValue));
274                 }
275             }
276         }
277     }
278
279     private static void writeIdentifierWithNamespacePrefix(Element element, StringBuilder textContent, QName qName,
280             Map<String, String> prefixes) {
281         String namespace = qName.getNamespace().toString();
282         String prefix = prefixes.get(namespace);
283         if (prefix == null) {
284             prefix = qName.getPrefix();
285             if (prefix == null || prefix.isEmpty() || prefixes.containsValue(prefix)) {
286                 prefix = generateNewPrefix(prefixes.values());
287             }
288         }
289
290         element.setAttribute("xmlns:" + prefix, namespace.toString());
291         textContent.append(prefix);
292         prefixes.put(namespace, prefix);
293
294         textContent.append(":");
295         textContent.append(qName.getLocalName());
296     }
297
298     private static String generateNewPrefix(Collection<String> prefixes) {
299         StringBuilder result = null;
300         Random random = new Random();
301         do {
302             result = new StringBuilder();
303             for (int i = 0; i < 4; i++) {
304                 int randomNumber = 0x61 + (Math.abs(random.nextInt()) % 26);
305                 result.append(Character.toChars(randomNumber));
306             }
307         } while (prefixes.contains(result.toString()));
308
309         return result.toString();
310     }
311
312     public final static TypeDefinition<?> resolveBaseTypeFrom(TypeDefinition<?> type) {
313         TypeDefinition<?> superType = type;
314         while (superType.getBaseType() != null) {
315             superType = superType.getBaseType();
316         }
317         return superType;
318     }
319
320     private static final DataSchemaNode findFirstSchemaForNode(Node<?> node, Set<DataSchemaNode> dataSchemaNode) {
321         if (dataSchemaNode != null && node != null) {
322             for (DataSchemaNode dsn : dataSchemaNode) {
323                 if (node.getNodeType().getLocalName().equals(dsn.getQName().getLocalName())) {
324                     return dsn;
325                 } else if (dsn instanceof ChoiceNode) {
326                     for (ChoiceCaseNode choiceCase : ((ChoiceNode) dsn).getCases()) {
327                         DataSchemaNode foundDsn = findFirstSchemaForNode(node, choiceCase.getChildNodes());
328                         if (foundDsn != null) {
329                             return foundDsn;
330                         }
331                     }
332                 }
333             }
334         }
335         return null;
336     }
337
338     public static Node<?> toDomNode(Element xmlElement, Optional<DataSchemaNode> schema,
339             Optional<XmlCodecProvider> codecProvider) {
340         if (schema.isPresent()) {
341             return toNodeWithSchema(xmlElement, schema.get(), codecProvider.or(DEFAULT_XML_VALUE_CODEC_PROVIDER));
342         }
343         return toDomNode(xmlElement);
344     }
345
346     public static CompositeNode fromElement(Element xmlElement) {
347         CompositeNodeBuilder<ImmutableCompositeNode> node = ImmutableCompositeNode.builder();
348         node.setQName(qNameFromElement(xmlElement));
349
350         return node.toInstance();
351     }
352
353     public static QName qNameFromElement(Element xmlElement) {
354         String namespace = xmlElement.getNamespaceURI();
355         String localName = xmlElement.getLocalName();
356         return QName.create(namespace != null ? URI.create(namespace) : null, null, localName);
357     }
358
359     private static Node<?> toNodeWithSchema(Element xmlElement, DataSchemaNode schema, XmlCodecProvider codecProvider) {
360         checkQName(xmlElement, schema.getQName());
361         if (schema instanceof DataNodeContainer) {
362             return toCompositeNodeWithSchema(xmlElement, schema.getQName(), (DataNodeContainer) schema, codecProvider);
363         } else if (schema instanceof LeafSchemaNode) {
364             return toSimpleNodeWithType(xmlElement, (LeafSchemaNode) schema, codecProvider);
365         } else if (schema instanceof LeafListSchemaNode) {
366             return toSimpleNodeWithType(xmlElement, (LeafListSchemaNode) schema, codecProvider);
367
368         }
369         return null;
370     }
371
372     private static Node<?> toSimpleNodeWithType(Element xmlElement, LeafSchemaNode schema,
373             XmlCodecProvider codecProvider) {
374         TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> codec = codecProvider.codecFor(schema.getType());
375         String text = xmlElement.getTextContent();
376         Object value;
377         if (codec != null) {
378             value = codec.deserialize(text);
379
380         } else {
381             value = xmlElement.getTextContent();
382         }
383
384         Optional<ModifyAction> modifyAction = getModifyOperationFromAttributes(xmlElement);
385         return new SimpleNodeTOImpl<>(schema.getQName(), null, value, modifyAction.orNull());
386     }
387
388     private static Node<?> toSimpleNodeWithType(Element xmlElement, LeafListSchemaNode schema,
389             XmlCodecProvider codecProvider) {
390         TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> codec = codecProvider.codecFor(schema.getType());
391         String text = xmlElement.getTextContent();
392         Object value;
393         if (codec != null) {
394             value = codec.deserialize(text);
395
396         } else {
397             value = xmlElement.getTextContent();
398         }
399
400         Optional<ModifyAction> modifyAction = getModifyOperationFromAttributes(xmlElement);
401         return new SimpleNodeTOImpl<>(schema.getQName(), null, value, modifyAction.orNull());
402     }
403
404     private static Node<?> toCompositeNodeWithSchema(Element xmlElement, QName qName, DataNodeContainer schema,
405             XmlCodecProvider codecProvider) {
406         List<Node<?>> values = toDomNodes(xmlElement, Optional.fromNullable(schema.getChildNodes()));
407         Optional<ModifyAction> modifyAction = getModifyOperationFromAttributes(xmlElement);
408         return ImmutableCompositeNode.create(qName, values, modifyAction.orNull());
409     }
410
411     public static final QName OPERATION_ATTRIBUTE_QNAME = QName.create(URI.create("urn:ietf:params:xml:ns:netconf:base:1.0"), null, "operation");
412
413     public static Optional<ModifyAction> getModifyOperationFromAttributes(Element xmlElement) {
414         Attr attributeNodeNS = xmlElement.getAttributeNodeNS(OPERATION_ATTRIBUTE_QNAME.getNamespace().toString(), OPERATION_ATTRIBUTE_QNAME.getLocalName());
415         if(attributeNodeNS == null)
416             return Optional.absent();
417
418         ModifyAction action = ModifyAction.fromXmlValue(attributeNodeNS.getValue());
419         Preconditions.checkArgument(action.isOnElementPermitted(), "Unexpected operation %s on %s", action, xmlElement);
420
421         return Optional.of(action);
422     }
423
424     private static void checkQName(Element xmlElement, QName qName) {
425         checkState(Objects.equal(xmlElement.getNamespaceURI(), qName.getNamespace().toString()));
426         checkState(qName.getLocalName().equals(xmlElement.getLocalName()));
427     }
428
429     public static final Optional<DataSchemaNode> findFirstSchema(QName qname, Set<DataSchemaNode> dataSchemaNode) {
430         if (dataSchemaNode != null && !dataSchemaNode.isEmpty() && qname != null) {
431             for (DataSchemaNode dsn : dataSchemaNode) {
432                 if (qname.isEqualWithoutRevision(dsn.getQName())) {
433                     return Optional.<DataSchemaNode> of(dsn);
434                 } else if (dsn instanceof ChoiceNode) {
435                     for (ChoiceCaseNode choiceCase : ((ChoiceNode) dsn).getCases()) {
436                         Optional<DataSchemaNode> foundDsn = findFirstSchema(qname, choiceCase.getChildNodes());
437                         if (foundDsn != null && foundDsn.isPresent()) {
438                             return foundDsn;
439                         }
440                     }
441                 }
442             }
443         }
444         return Optional.absent();
445     }
446
447     public static Node<?> toDomNode(Document doc) {
448         return toDomNode(doc.getDocumentElement());
449     }
450
451     private static Node<?> toDomNode(Element element) {
452         QName qname = qNameFromElement(element);
453
454         ImmutableList.Builder<Node<?>> values = ImmutableList.<Node<?>> builder();
455         NodeList nodes = element.getChildNodes();
456         boolean isSimpleObject = true;
457         String value = null;
458         for (int i = 0; i < nodes.getLength(); i++) {
459             org.w3c.dom.Node child = nodes.item(i);
460             if (child instanceof Element) {
461                 isSimpleObject = false;
462                 values.add(toDomNode((Element) child));
463             }
464             if (isSimpleObject && child instanceof org.w3c.dom.Text) {
465                 value = element.getTextContent();
466                 if (!Strings.isNullOrEmpty(value)) {
467                     isSimpleObject = true;
468                 }
469             }
470         }
471         if (isSimpleObject) {
472             return new SimpleNodeTOImpl<>(qname, null, value);
473         }
474         return ImmutableCompositeNode.create(qname, values.build());
475     }
476
477     public static List<Node<?>> toDomNodes(final Element element, final Optional<Set<DataSchemaNode>> context) {
478         return forEachChild(element.getChildNodes(), new Function<Element, Optional<Node<?>>>() {
479
480             @Override
481             public Optional<Node<?>> apply(Element input) {
482                 if (context.isPresent()) {
483                     QName partialQName = qNameFromElement(input);
484                     Optional<DataSchemaNode> schemaNode = findFirstSchema(partialQName, context.get());
485                     if (schemaNode.isPresent()) {
486                         return Optional.<Node<?>> fromNullable(//
487                                 toNodeWithSchema(input, schemaNode.get(), DEFAULT_XML_VALUE_CODEC_PROVIDER));
488                     }
489                 }
490                 return Optional.<Node<?>> fromNullable(toDomNode(input));
491             }
492
493         });
494
495     }
496
497     /**
498      * Converts XML Document containing notification data from Netconf device to
499      * Data DOM Nodes. <br>
500      * By specification defined in <a
501      * href="http://tools.ietf.org/search/rfc6020#section-7.14">RFC 6020</a>
502      * there are xml elements containing notifications metadata, like eventTime
503      * or root notification element which specifies namespace for which is
504      * notification defined in yang model. Those elements MUST be stripped off
505      * notifications body. This method returns pure notification body which
506      * begins in element which is equal to notifications name defined in
507      * corresponding yang model. Rest of notification metadata are obfuscated,
508      * thus Data DOM contains only pure notification body.
509      *
510      * @param document
511      *            XML Document containing notification body
512      * @param notifications
513      *            Notifications Definition Schema
514      * @return Data DOM Nodes containing xml notification body definition or
515      *         <code>null</code> if there is no NotificationDefinition with
516      *         Element with equal notification QName defined in XML Document.
517      */
518     public static CompositeNode notificationToDomNodes(final Document document,
519             final Optional<Set<NotificationDefinition>> notifications) {
520         if (notifications.isPresent() && (document != null) && (document.getDocumentElement() != null)) {
521             final NodeList originChildNodes = document.getDocumentElement().getChildNodes();
522
523             for (int i = 0; i < originChildNodes.getLength(); i++) {
524                 org.w3c.dom.Node child = originChildNodes.item(i);
525                 if (child instanceof Element) {
526                     final Element childElement = (Element) child;
527                     final QName partialQName = qNameFromElement(childElement);
528                     final Optional<NotificationDefinition> notificationDef = findNotification(partialQName,
529                             notifications.get());
530                     if (notificationDef.isPresent()) {
531                         final Set<DataSchemaNode> dataNodes = notificationDef.get().getChildNodes();
532                         final List<Node<?>> domNodes = toDomNodes(childElement,
533                                 Optional.<Set<DataSchemaNode>> fromNullable(dataNodes));
534                         return ImmutableCompositeNode.create(notificationDef.get().getQName(), domNodes);
535                     }
536                 }
537             }
538         }
539         return null;
540     }
541
542     private static Optional<NotificationDefinition> findNotification(final QName notifName,
543             final Set<NotificationDefinition> notifications) {
544         if ((notifName != null) && (notifications != null)) {
545             for (final NotificationDefinition notification : notifications) {
546                 if ((notification != null) && notifName.isEqualWithoutRevision(notification.getQName())) {
547                     return Optional.<NotificationDefinition>fromNullable(notification);
548                 }
549             }
550         }
551         return Optional.<NotificationDefinition>absent();
552     }
553
554     private static final <T> List<T> forEachChild(NodeList nodes, Function<Element, Optional<T>> forBody) {
555         ImmutableList.Builder<T> ret = ImmutableList.<T> builder();
556         for (int i = 0; i < nodes.getLength(); i++) {
557             org.w3c.dom.Node child = nodes.item(i);
558             if (child instanceof Element) {
559                 Optional<T> result = forBody.apply((Element) child);
560                 if (result.isPresent()) {
561                     ret.add(result.get());
562                 }
563             }
564         }
565         return ret.build();
566     }
567
568     public static final XmlCodecProvider defaultValueCodecProvider() {
569         return DEFAULT_XML_VALUE_CODEC_PROVIDER;
570     }
571
572 }