Bug 484: Fix XmlDocumentUtils.toDocument to handle anyxml
[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(final Element element,final 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(final 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(final CompositeNode data, final DataNodeContainer schema, final 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(final CompositeNode data, final 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(final Document doc, final Node<?> data, final SchemaNode schema,
161             final 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).getValue()) {
178                 DataSchemaNode childSchema = null;
179                 if (schema instanceof DataNodeContainer) {
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(final Document doc, final 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(final Element element, final SimpleNode<?> node, final TypeDefinition<?> type,
214             final DataSchemaNode schema, final XmlCodecProvider codecProvider) {
215
216         Object nodeValue = node.getValue();
217
218         writeValueByType(element, type, codecProvider, nodeValue);
219     }
220
221     public static void writeValueByType(final Element element, final TypeDefinition<?> type, final XmlCodecProvider codecProvider, final 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(final 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(final Node<?> node, final 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(final Element xmlElement, final Optional<DataSchemaNode> schema,
299             final 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(final 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(final 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(final Element xmlElement, final DataSchemaNode schema, final XmlCodecProvider codecProvider,final 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(final Element xmlElement, final DataSchemaNode schema, final XmlCodecProvider codecProvider) {
332         return toNodeWithSchema(xmlElement, schema, codecProvider, null);
333     }
334
335     protected static Node<?> toSimpleNodeWithType(final Element xmlElement, final LeafSchemaNode schema,
336             final XmlCodecProvider codecProvider,final 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
344         if (schema.getType() instanceof org.opendaylight.yangtools.yang.model.util.InstanceIdentifier) {
345             value = INSTANCE_IDENTIFIER_FOR_XML_CODEC.deserialize(xmlElement,schemaCtx);
346         } else if(schema.getType() instanceof IdentityrefTypeDefinition){
347             value = INSTANCE_IDENTIFIER_FOR_XML_CODEC.toIdentity(xmlElement.getTextContent(), xmlElement, schemaCtx);
348         }
349
350         if (value == null) {
351             value = xmlElement.getTextContent();
352         }
353
354         Optional<ModifyAction> modifyAction = getModifyOperationFromAttributes(xmlElement);
355         return new SimpleNodeTOImpl<>(schema.getQName(), null, value, modifyAction.orNull());
356     }
357
358     private static Node<?> toSimpleNodeWithType(final Element xmlElement, final LeafListSchemaNode schema,
359             final XmlCodecProvider codecProvider,final SchemaContext schemaCtx) {
360         TypeDefinitionAwareCodec<? extends Object, ? extends TypeDefinition<?>> codec = codecProvider.codecFor(schema.getType());
361         String text = xmlElement.getTextContent();
362         Object value = null;
363         if (codec != null) {
364             value = codec.deserialize(text);
365         }
366         if (schema.getType() instanceof org.opendaylight.yangtools.yang.model.util.InstanceIdentifier) {
367             value = INSTANCE_IDENTIFIER_FOR_XML_CODEC.deserialize(xmlElement,schemaCtx);
368         }
369         if (value == null) {
370             value = xmlElement.getTextContent();
371         }
372
373         Optional<ModifyAction> modifyAction = getModifyOperationFromAttributes(xmlElement);
374         return new SimpleNodeTOImpl<>(schema.getQName(), null, value, modifyAction.orNull());
375     }
376
377     private static Node<?> toCompositeNodeWithSchema(final Element xmlElement, final QName qName, final DataNodeContainer schema,
378             final XmlCodecProvider codecProvider,final SchemaContext schemaCtx) {
379         List<Node<?>> values = toDomNodes(xmlElement, Optional.fromNullable(schema.getChildNodes()),schemaCtx);
380         Optional<ModifyAction> modifyAction = getModifyOperationFromAttributes(xmlElement);
381         return ImmutableCompositeNode.create(qName, values, modifyAction.orNull());
382     }
383
384     public static final QName OPERATION_ATTRIBUTE_QNAME = QName.create(URI.create("urn:ietf:params:xml:ns:netconf:base:1.0"), null, "operation");
385
386     public static Optional<ModifyAction> getModifyOperationFromAttributes(final Element xmlElement) {
387         Attr attributeNodeNS = xmlElement.getAttributeNodeNS(OPERATION_ATTRIBUTE_QNAME.getNamespace().toString(), OPERATION_ATTRIBUTE_QNAME.getLocalName());
388         if(attributeNodeNS == null) {
389                         return Optional.absent();
390                 }
391
392         ModifyAction action = ModifyAction.fromXmlValue(attributeNodeNS.getValue());
393         Preconditions.checkArgument(action.isOnElementPermitted(), "Unexpected operation %s on %s", action, xmlElement);
394
395         return Optional.of(action);
396     }
397
398     private static void checkQName(final Element xmlElement, final QName qName) {
399         checkState(Objects.equal(xmlElement.getNamespaceURI(), qName.getNamespace().toString()));
400         checkState(qName.getLocalName().equals(xmlElement.getLocalName()));
401     }
402
403     public static final Optional<DataSchemaNode> findFirstSchema(final QName qname, final Set<DataSchemaNode> dataSchemaNode) {
404         if (dataSchemaNode != null && !dataSchemaNode.isEmpty() && qname != null) {
405             for (DataSchemaNode dsn : dataSchemaNode) {
406                 if (qname.isEqualWithoutRevision(dsn.getQName())) {
407                     return Optional.<DataSchemaNode> of(dsn);
408                 } else if (dsn instanceof ChoiceNode) {
409                     for (ChoiceCaseNode choiceCase : ((ChoiceNode) dsn).getCases()) {
410                         Optional<DataSchemaNode> foundDsn = findFirstSchema(qname, choiceCase.getChildNodes());
411                         if (foundDsn != null && foundDsn.isPresent()) {
412                             return foundDsn;
413                         }
414                     }
415                 }
416             }
417         }
418         return Optional.absent();
419     }
420
421     public static Node<?> toDomNode(final Document doc) {
422         return toDomNode(doc.getDocumentElement());
423     }
424
425     private static Node<?> toDomNode(final Element element) {
426         QName qname = qNameFromElement(element);
427
428         ImmutableList.Builder<Node<?>> values = ImmutableList.<Node<?>> builder();
429         NodeList nodes = element.getChildNodes();
430         boolean isSimpleObject = true;
431         String value = null;
432         for (int i = 0; i < nodes.getLength(); i++) {
433             org.w3c.dom.Node child = nodes.item(i);
434             if (child instanceof Element) {
435                 isSimpleObject = false;
436                 values.add(toDomNode((Element) child));
437             }
438             if (isSimpleObject && child instanceof org.w3c.dom.Text) {
439                 value = element.getTextContent();
440                 if (!Strings.isNullOrEmpty(value)) {
441                     isSimpleObject = true;
442                 }
443             }
444         }
445         if (isSimpleObject) {
446             return new SimpleNodeTOImpl<>(qname, null, value);
447         }
448         return ImmutableCompositeNode.create(qname, values.build());
449     }
450
451     public static List<Node<?>> toDomNodes(final Element element, final Optional<Set<DataSchemaNode>> context,final SchemaContext schemaCtx) {
452         return forEachChild(element.getChildNodes(),schemaCtx, new Function<ElementWithSchemaContext, Optional<Node<?>>>() {
453
454             @Override
455             public Optional<Node<?>> apply(final ElementWithSchemaContext input) {
456                 if (context.isPresent()) {
457                     QName partialQName = qNameFromElement(input.getElement());
458                     Optional<DataSchemaNode> schemaNode = findFirstSchema(partialQName, context.get());
459                     if (schemaNode.isPresent()) {
460                         return Optional.<Node<?>> fromNullable(//
461                                 toNodeWithSchema(input.getElement(), schemaNode.get(), DEFAULT_XML_VALUE_CODEC_PROVIDER,input.getSchemaContext()));
462                     }
463                 }
464                 return Optional.<Node<?>> fromNullable(toDomNode(input.getElement()));
465             }
466
467         });
468
469     }
470
471     public static List<Node<?>> toDomNodes(final Element element, final Optional<Set<DataSchemaNode>> context) {
472         return toDomNodes(element,context,null);
473     }
474
475     /**
476      * Converts XML Document containing notification data from Netconf device to
477      * Data DOM Nodes. <br>
478      * By specification defined in <a
479      * href="http://tools.ietf.org/search/rfc6020#section-7.14">RFC 6020</a>
480      * there are xml elements containing notifications metadata, like eventTime
481      * or root notification element which specifies namespace for which is
482      * notification defined in yang model. Those elements MUST be stripped off
483      * notifications body. This method returns pure notification body which
484      * begins in element which is equal to notifications name defined in
485      * corresponding yang model. Rest of notification metadata are obfuscated,
486      * thus Data DOM contains only pure notification body.
487      *
488      * @param document
489      *            XML Document containing notification body
490      * @param notifications
491      *            Notifications Definition Schema
492      * @return Data DOM Nodes containing xml notification body definition or
493      *         <code>null</code> if there is no NotificationDefinition with
494      *         Element with equal notification QName defined in XML Document.
495      */
496     public static CompositeNode notificationToDomNodes(final Document document,
497             final Optional<Set<NotificationDefinition>> notifications, final SchemaContext schemaCtx) {
498         if (notifications.isPresent() && (document != null) && (document.getDocumentElement() != null)) {
499             final NodeList originChildNodes = document.getDocumentElement().getChildNodes();
500             for (int i = 0; i < originChildNodes.getLength(); i++) {
501                 org.w3c.dom.Node child = originChildNodes.item(i);
502                 if (child instanceof Element) {
503                     final Element childElement = (Element) child;
504                     final QName partialQName = qNameFromElement(childElement);
505                     final Optional<NotificationDefinition> notificationDef = findNotification(partialQName,
506                             notifications.get());
507                     if (notificationDef.isPresent()) {
508                         final Set<DataSchemaNode> dataNodes = notificationDef.get().getChildNodes();
509                         final List<Node<?>> domNodes = toDomNodes(childElement,
510                                 Optional.<Set<DataSchemaNode>> fromNullable(dataNodes),schemaCtx);
511                         return ImmutableCompositeNode.create(notificationDef.get().getQName(), domNodes);
512                     }
513                 }
514             }
515         }
516         return null;
517     }
518
519     public static CompositeNode notificationToDomNodes(final Document document,
520             final Optional<Set<NotificationDefinition>> notifications) {
521         return notificationToDomNodes(document, notifications,null);
522     }
523
524     private static Optional<NotificationDefinition> findNotification(final QName notifName,
525             final Set<NotificationDefinition> notifications) {
526         if ((notifName != null) && (notifications != null)) {
527             for (final NotificationDefinition notification : notifications) {
528                 if ((notification != null) && notifName.isEqualWithoutRevision(notification.getQName())) {
529                     return Optional.<NotificationDefinition>fromNullable(notification);
530                 }
531             }
532         }
533         return Optional.<NotificationDefinition>absent();
534     }
535
536     private static final <T> List<T> forEachChild(final NodeList nodes, final SchemaContext schemaContext, final Function<ElementWithSchemaContext, Optional<T>> forBody) {
537         ImmutableList.Builder<T> ret = ImmutableList.<T> builder();
538         for (int i = 0; i < nodes.getLength(); i++) {
539             org.w3c.dom.Node child = nodes.item(i);
540             if (child instanceof Element) {
541                 Optional<T> result = forBody.apply(new ElementWithSchemaContext((Element) child,schemaContext));
542                 if (result.isPresent()) {
543                     ret.add(result.get());
544                 }
545             }
546         }
547         return ret.build();
548     }
549
550     public static final XmlCodecProvider defaultValueCodecProvider() {
551         return DEFAULT_XML_VALUE_CODEC_PROVIDER;
552     }
553 }