- public static Node<?> toDomNode(final Document doc) {
- return toDomNode(doc.getDocumentElement());
- }
-
- private static Node<?> toDomNode(final Element element) {
- QName qname = qNameFromElement(element);
-
- ImmutableList.Builder<Node<?>> values = ImmutableList.<Node<?>> builder();
- NodeList nodes = element.getChildNodes();
- boolean isSimpleObject = true;
- String value = null;
- for (int i = 0; i < nodes.getLength(); i++) {
- org.w3c.dom.Node child = nodes.item(i);
- if (child instanceof Element) {
- isSimpleObject = false;
- values.add(toDomNode((Element) child));
- }
- if (isSimpleObject && child instanceof org.w3c.dom.Text) {
- value = element.getTextContent();
- if (!Strings.isNullOrEmpty(value)) {
- isSimpleObject = true;
- }
- }
- }
- if (isSimpleObject) {
- return new SimpleNodeTOImpl<>(qname, null, value);
- }
- return ImmutableCompositeNode.create(qname, values.build());
- }
-
- public static List<Node<?>> toDomNodes(final Element element, final Optional<Set<DataSchemaNode>> context,final SchemaContext schemaCtx) {
- return forEachChild(element.getChildNodes(),schemaCtx, new Function<ElementWithSchemaContext, Optional<Node<?>>>() {
-
- @Override
- public Optional<Node<?>> apply(final ElementWithSchemaContext input) {
- if (context.isPresent()) {
- QName partialQName = qNameFromElement(input.getElement());
- Optional<DataSchemaNode> schemaNode = findFirstSchema(partialQName, context.get());
- if (schemaNode.isPresent()) {
- return Optional.<Node<?>> fromNullable(//
- toNodeWithSchema(input.getElement(), schemaNode.get(), XmlUtils.DEFAULT_XML_CODEC_PROVIDER, input.getSchemaContext()));
- }
- }
- return Optional.<Node<?>> fromNullable(toDomNode(input.getElement()));
- }
-
- });
-
- }
-
- public static List<Node<?>> toDomNodes(final Element element, final Optional<Set<DataSchemaNode>> context) {
- return toDomNodes(element,context,null);
- }
-
- /**
- * Converts XML Document containing notification data from Netconf device to
- * Data DOM Nodes. <br>
- * By specification defined in <a
- * href="http://tools.ietf.org/search/rfc6020#section-7.14">RFC 6020</a>
- * there are xml elements containing notifications metadata, like eventTime
- * or root notification element which specifies namespace for which is
- * notification defined in yang model. Those elements MUST be stripped off
- * notifications body. This method returns pure notification body which
- * begins in element which is equal to notifications name defined in
- * corresponding yang model. Rest of notification metadata are obfuscated,
- * thus Data DOM contains only pure notification body.
- *
- * @param document
- * XML Document containing notification body
- * @param notifications
- * Notifications Definition Schema
- * @return Data DOM Nodes containing xml notification body definition or
- * <code>null</code> if there is no NotificationDefinition with
- * Element with equal notification QName defined in XML Document.
- */
- public static CompositeNode notificationToDomNodes(final Document document,
- final Optional<Set<NotificationDefinition>> notifications, final SchemaContext schemaCtx) {
- if (notifications.isPresent() && (document != null) && (document.getDocumentElement() != null)) {
- final NodeList originChildNodes = document.getDocumentElement().getChildNodes();
- for (int i = 0; i < originChildNodes.getLength(); i++) {
- org.w3c.dom.Node child = originChildNodes.item(i);
- if (child instanceof Element) {
- final Element childElement = (Element) child;
- final QName partialQName = qNameFromElement(childElement);
- final Optional<NotificationDefinition> notificationDef = findNotification(partialQName,
- notifications.get());
- if (notificationDef.isPresent()) {
- final Set<DataSchemaNode> dataNodes = notificationDef.get().getChildNodes();
- final List<Node<?>> domNodes = toDomNodes(childElement,
- Optional.<Set<DataSchemaNode>> fromNullable(dataNodes),schemaCtx);
- return ImmutableCompositeNode.create(notificationDef.get().getQName(), domNodes);
- }
- }
- }
- }
- return null;
- }
-
- public static CompositeNode notificationToDomNodes(final Document document,
- final Optional<Set<NotificationDefinition>> notifications) {
- return notificationToDomNodes(document, notifications,null);
- }
-
- private static Optional<NotificationDefinition> findNotification(final QName notifName,
- final Set<NotificationDefinition> notifications) {
- if ((notifName != null) && (notifications != null)) {
- for (final NotificationDefinition notification : notifications) {
- if ((notification != null) && notifName.isEqualWithoutRevision(notification.getQName())) {
- return Optional.<NotificationDefinition>fromNullable(notification);
- }
- }
- }
- return Optional.<NotificationDefinition>absent();
- }
-
- private static final <T> List<T> forEachChild(final NodeList nodes, final SchemaContext schemaContext, final Function<ElementWithSchemaContext, Optional<T>> forBody) {
- final int l = nodes.getLength();
- if (l == 0) {
- return ImmutableList.of();
- }
-
- final List<T> list = new ArrayList<>(l);
- for (int i = 0; i < l; i++) {
- org.w3c.dom.Node child = nodes.item(i);
- if (child instanceof Element) {
- Optional<T> result = forBody.apply(new ElementWithSchemaContext((Element) child,schemaContext));
- if (result.isPresent()) {
- list.add(result.get());
- }
- }
- }
- return ImmutableList.copyOf(list);
- }
-
- public static final XmlCodecProvider defaultValueCodecProvider() {