static {
final Document document = XmlUtil.newDocument();
- final Element filterElem = XmlUtil.createElement(document, "filter", Optional.of(NamespaceURN.BASE));
+ final Element filterElem = document.createElementNS(NamespaceURN.BASE, "filter");
filterElem.setAttribute("type", "subtree");
- final Element stateElem = XmlUtil.createElement(document, NetconfState.QNAME.getLocalName(),
- Optional.of(NetconfState.QNAME.getNamespace().toString()));
- stateElem.appendChild(XmlUtil.createElement(document, Schemas.QNAME.getLocalName(),
- Optional.of(Schemas.QNAME.getNamespace().toString())));
+ final Element stateElem = document.createElementNS(NetconfState.QNAME.getNamespace().toString(),
+ NetconfState.QNAME.getLocalName());
+ stateElem.appendChild(document.createElementNS(Schemas.QNAME.getNamespace().toString(),
+ Schemas.QNAME.getLocalName()));
filterElem.appendChild(stateElem);
GET_SCHEMAS_RPC = Builders.containerBuilder()
}
private static Element newFilterElement() {
- final var element = XmlUtil.createElement(BLANK_DOCUMENT, "filter", Optional.of(NamespaceURN.BASE));
+ final var element = BLANK_DOCUMENT.createElementNS(NamespaceURN.BASE, "filter");
element.setAttribute("type", "subtree");
return element;
}
+ "but was: %s", override);
}
- final var element = XmlUtil.createElement(BLANK_DOCUMENT, NETCONF_CONFIG_QNAME.getLocalName(),
- Optional.of(NETCONF_CONFIG_QNAME.getNamespace().toString()));
+ final var element = BLANK_DOCUMENT.createElementNS(NETCONF_CONFIG_QNAME.getNamespace().toString(),
+ NETCONF_CONFIG_QNAME.getLocalName());
final var metadata = operation.map(o -> leafMetadata(dataPath, o)).orElse(null);
try {
if (lastChildOverride.isPresent()) {
import org.opendaylight.netconf.api.DocumentedException;
import org.opendaylight.netconf.api.xml.XmlElement;
import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
-import org.opendaylight.netconf.api.xml.XmlUtil;
import org.opendaylight.netconf.server.api.monitoring.NetconfMonitoringService;
import org.opendaylight.netconf.server.api.operations.AbstractSingletonNetconfOperation;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.SessionIdType;
Map.of(ErrorTag.OPERATION_FAILED.elementBody(), e.getMessage()));
}
- final var getSchemaResult = XmlUtil.createTextElement(document, XmlNetconfConstants.DATA_KEY, schema,
- Optional.of(URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING));
+ final var ret = document.createElementNS(URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING,
+ XmlNetconfConstants.DATA_KEY);
+ ret.appendChild(document.createTextNode(schema));
LOG.trace("{} operation successful", GET_SCHEMA);
- return getSchemaResult;
+ return ret;
}
private static final class GetSchemaEntry {
throw DocumentedException.wrap(e);
}
if (result.value() == null) {
- return XmlUtil.createElement(document, XmlNetconfConstants.OK, Optional.of(NamespaceURN.BASE));
+ return document.createElementNS(NamespaceURN.BASE, XmlNetconfConstants.OK);
}
return transformNormalizedNode(document, result.value(),
Absolute.of(rpcDefinition.getQName(), rpcDefinition.getOutput().getQName()));
final Map<String, Attr> attributes = requestElement.getAttributes();
final Element response = handle(document, operationElement, subsequentOperation);
- final Element rpcReply = XmlUtil.createElement(document, RpcReplyMessage.ELEMENT_NAME,
- Optional.of(NamespaceURN.BASE));
+ final Element rpcReply = document.createElementNS(NamespaceURN.BASE, RpcReplyMessage.ELEMENT_NAME);
if (XmlElement.fromDomElement(response).hasNamespace()) {
rpcReply.appendChild(response);
*/
package org.opendaylight.netconf.api.xml;
-import static javax.xml.XMLConstants.XMLNS_ATTRIBUTE;
-import static javax.xml.XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
-
import com.google.common.io.Resources;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
-import java.util.Optional;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
return DEFAULT_DOM_BUILDER.get().newDocument();
}
- public static Element createElement(final Document document, final String qname,
- final Optional<String> namespaceURI) {
- if (namespaceURI.isEmpty()) {
- return document.createElement(qname);
- }
-
- final String uri = namespaceURI.orElseThrow();
- final Element element = document.createElementNS(uri, qname);
- String name = XMLNS_ATTRIBUTE;
- if (element.getPrefix() != null) {
- name += ":" + element.getPrefix();
- }
- element.setAttributeNS(XMLNS_ATTRIBUTE_NS_URI, name, uri);
- return element;
- }
-
- public static Element createTextElement(final Document document, final String qname, final String content,
- final Optional<String> namespaceURI) {
- Element typeElement = createElement(document, qname, namespaceURI);
- typeElement.appendChild(document.createTextNode(content));
- return typeElement;
- }
-
- public static Element createTextElementWithNamespacedContent(final Document document, final String qname,
- final String prefix, final String namespace, final String contentWithoutPrefix) {
-
- return createTextElementWithNamespacedContent(document, qname, prefix, namespace, contentWithoutPrefix,
- Optional.empty());
- }
-
- public static Element createTextElementWithNamespacedContent(final Document document, final String qname,
- final String prefix, final String namespace, final String contentWithoutPrefix,
- final Optional<String> namespaceURI) {
-
- String content = createPrefixedValue(XmlNetconfConstants.PREFIX, contentWithoutPrefix);
- Element element = createTextElement(document, qname, content, namespaceURI);
- String prefixedNamespaceAttr = createPrefixedValue(XMLNS_ATTRIBUTE, prefix);
- element.setAttributeNS(XMLNS_ATTRIBUTE_NS_URI, prefixedNamespaceAttr, namespace);
- return element;
- }
-
- public static String createPrefixedValue(final String prefix, final String value) {
- return prefix + ":" + value;
- }
-
/**
* Return a new {@link Transformer} which performs indentation.
*
package org.opendaylight.netconf.api.xml;
import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThrows;
-import java.util.Optional;
import org.junit.Test;
-import org.w3c.dom.Document;
-import org.w3c.dom.Element;
import org.xml.sax.SAXParseException;
-import org.xmlunit.builder.DiffBuilder;
import org.xmlunit.builder.Input;
import org.xmlunit.builder.Transform;
-import org.xmlunit.diff.DefaultNodeMatcher;
-import org.xmlunit.diff.ElementSelectors;
public class XmlUtilTest {
- private static final String XML_SNIPPET = """
- <top xmlns="namespace">
- <innerText>value</innerText>
- <innerPrefixedText xmlns:pref="prefixNamespace">prefix:value</innerPrefixedText>
- <innerPrefixedText xmlns="randomNamespace" xmlns:pref="prefixNamespace">prefix:value</innerPrefixedText>
- </top>""";
-
- @Test
- public void testCreateElement() throws Exception {
- final Document document = XmlUtil.newDocument();
- final Element top = XmlUtil.createElement(document, "top", Optional.of("namespace"));
-
- top.appendChild(XmlUtil.createTextElement(document, "innerText", "value", Optional.of("namespace")));
- top.appendChild(XmlUtil.createTextElementWithNamespacedContent(document, "innerPrefixedText", "pref",
- "prefixNamespace", "value", Optional.of("namespace")));
- top.appendChild(XmlUtil.createTextElementWithNamespacedContent(document, "innerPrefixedText", "pref",
- "prefixNamespace", "value", Optional.of("randomNamespace")));
-
- document.appendChild(top);
- assertEquals("top", XmlUtil.createDocumentCopy(document).getDocumentElement().getTagName());
-
- final var diff = DiffBuilder.compare(document)
- .withTest(XML_SNIPPET)
- .ignoreWhitespace()
- .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText))
- .checkForSimilar()
- .build();
-
- assertFalse(diff.toString(), diff.hasDifferences());
- }
-
@Test
public void testXXEFlaw() {
assertThrows(SAXParseException.class, () -> XmlUtil.readXmlToDocument("""
import static java.util.Objects.requireNonNull;
-import java.util.Optional;
import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.netconf.api.DocumentedException;
import org.opendaylight.netconf.api.NamespaceURN;
final var attributes = requestElement.getAttributes();
final var response = handle(document, operationElement, subsequentOperation);
- final var rpcReply = XmlUtil.createElement(document, RpcReplyMessage.ELEMENT_NAME,
- Optional.of(NamespaceURN.BASE));
+ final var rpcReply = document.createElementNS(NamespaceURN.BASE, RpcReplyMessage.ELEMENT_NAME);
if (XmlElement.fromDomElement(response).hasNamespace()) {
rpcReply.appendChild(response);
} else {
- final var responseNS = XmlUtil.createElement(document, response.getNodeName(),
- Optional.of(NamespaceURN.BASE));
+ // FIXME: use getLocalName() instead
+ final var responseNS = document.createElementNS(NamespaceURN.BASE, response.getNodeName());
final var list = response.getChildNodes();
while (list.getLength() != 0) {
responseNS.appendChild(list.item(0));