2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.xml.codec;
10 import com.google.common.base.Optional;
12 import java.util.Collection;
13 import javax.xml.parsers.DocumentBuilder;
14 import javax.xml.parsers.DocumentBuilderFactory;
15 import javax.xml.parsers.ParserConfigurationException;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.data.impl.codec.xml.XmlCodecProvider;
18 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
19 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
21 import org.w3c.dom.Document;
22 import org.w3c.dom.Element;
24 public class XmlDocumentUtils {
25 public static final QName OPERATION_ATTRIBUTE_QNAME = QName.create(URI.create("urn:ietf:params:xml:ns:netconf:base:1.0"), null, "operation");
27 public static Document getDocument() {
28 final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
31 final DocumentBuilder bob = dbf.newDocumentBuilder();
32 doc = bob.newDocument();
33 } catch (final ParserConfigurationException e) {
34 throw new RuntimeException(e);
40 public static QName qNameFromElement(final Element xmlElement) {
41 final String namespace = xmlElement.getNamespaceURI();
42 final String localName = xmlElement.getLocalName();
43 return QName.create(namespace != null ? URI.create(namespace) : null, null, localName);
46 public static final Optional<DataSchemaNode> findFirstSchema(final QName qname, final Collection<DataSchemaNode> dataSchemaNode) {
47 if (dataSchemaNode != null && !dataSchemaNode.isEmpty() && qname != null) {
48 for (final DataSchemaNode dsn : dataSchemaNode) {
49 if (qname.isEqualWithoutRevision(dsn.getQName())) {
50 return Optional.<DataSchemaNode> of(dsn);
51 } else if (dsn instanceof ChoiceSchemaNode) {
52 for (final ChoiceCaseNode choiceCase : ((ChoiceSchemaNode) dsn).getCases()) {
53 final Optional<DataSchemaNode> foundDsn = findFirstSchema(qname, choiceCase.getChildNodes());
54 if (foundDsn != null && foundDsn.isPresent()) {
61 return Optional.absent();
64 public static final XmlCodecProvider defaultValueCodecProvider() {
65 return XmlUtils.DEFAULT_XML_CODEC_PROVIDER;