- static final XMLOutputFactory XML_FACTORY;
- static {
- XML_FACTORY = XMLOutputFactory.newFactory();
- XML_FACTORY.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
- }
-
- // FIXME similar code is in netconf-notifications-impl , DRY
- private void writeNormalizedNode(final NormalizedNode<?, ?> normalized, final DOMResult result, final SchemaPath schemaPath, final SchemaContext context)
- throws IOException, XMLStreamException {
- NormalizedNodeWriter normalizedNodeWriter = null;
- NormalizedNodeStreamWriter normalizedNodeStreamWriter = null;
- XMLStreamWriter writer = null;
- try {
- writer = XML_FACTORY.createXMLStreamWriter(result);
- normalizedNodeStreamWriter = XMLStreamNormalizedNodeStreamWriter.create(writer, context, schemaPath);
- normalizedNodeWriter = NormalizedNodeWriter.forStreamWriter(normalizedNodeStreamWriter);
-
- normalizedNodeWriter.write(normalized);
-
- normalizedNodeWriter.flush();
- } finally {
- try {
- if(normalizedNodeWriter != null) {
- normalizedNodeWriter.close();
- }
- if(normalizedNodeStreamWriter != null) {
- normalizedNodeStreamWriter.close();
- }
- if(writer != null) {
- writer.close();
- }
- } catch (final Exception e) {
- LOG.warn("Unable to close resource properly", e);
- }
- }
- }
-
- private void writeNormalizedEdit(final ContainerNode normalized, final DOMResult result, final SchemaPath schemaPath, final SchemaContext baseNetconfCtx) throws IOException, XMLStreamException {
- final NormalizedNodeWriter normalizedNodeWriter;
- NormalizedNodeStreamWriter normalizedNodeStreamWriter = null;
- XMLStreamWriter writer = null;
- try {
- writer = XML_FACTORY.createXMLStreamWriter(result);
- normalizedNodeStreamWriter = XMLStreamNormalizedNodeStreamWriter.create(writer, baseNetconfCtx, schemaPath);
- normalizedNodeWriter = NormalizedNodeWriter.forStreamWriter(normalizedNodeStreamWriter);
-
- Optional<Iterable<Element>> editDataElements = Optional.absent();
- for (final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> editElement : normalized.getValue()) {
- if(editElement.getNodeType().getLocalName().equals(EditContent.QNAME.getLocalName())) {
- Preconditions.checkState(editElement instanceof ChoiceNode,
- "Edit content element is expected to be %s, not %s", ChoiceNode.class, editElement);
- final Optional<DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> configContentHolder =
- ((ChoiceNode) editElement).getChild(toId(NETCONF_CONFIG_QNAME));
- // TODO The config element inside the EditContent should be AnyXml not Container, but AnyXml is based on outdated API
- Preconditions.checkState(configContentHolder.isPresent() && configContentHolder.get() instanceof ContainerNode,
- "Edit content/config element is expected to be present as a container node");
- normalizedNodeStreamWriter.startChoiceNode(toId(editElement.getNodeType()), 1);
- normalizedNodeStreamWriter.anyxmlNode(toId(NETCONF_CONFIG_QNAME), null);
- normalizedNodeStreamWriter.endNode();
-
- editDataElements = Optional.of(serializeAnyXmlAccordingToSchema(((ContainerNode) configContentHolder.get()).getValue()));
- } else {
- normalizedNodeWriter.write(editElement);
- }
- }
-
- normalizedNodeWriter.flush();
-
- // FIXME this is a workaround for filter content serialization
- // Any xml is not supported properly by the stream writer
- if(editDataElements.isPresent()) {
- appendEditData(result, editDataElements.get());
- }
- } finally {
- try {
- if(normalizedNodeStreamWriter != null) {
- normalizedNodeStreamWriter.close();
- }
- if(writer != null) {
- writer.close();
- }
- } catch (final Exception e) {
- LOG.warn("Unable to close resource properly", e);
- }
- }
- }
-