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