- /**
- * Check if a particular data element can be emitted as an empty element, bypassing value encoding. This
- * functionality is optional, as valid XML stream is produced even if start/end element is produced unconditionally.
- *
- * @param data Data node
- * @return True if the data node will result in empty element body.
- */
- public static boolean isEmptyElement(final Node<?> data) {
- if (data == null) {
- return true;
- }
-
- if (data instanceof CompositeNode) {
- return ((CompositeNode) data).getValue().isEmpty();
- }
- if (data instanceof SimpleNode) {
- return data.getValue() == null;
- }
-
- // Safe default
- return false;
- }
-
- /**
- * Write an InstanceIdentifier into the output stream. Calling corresponding {@link XMLStreamWriter#writeStartElement(String)}
- * and {@link XMLStreamWriter#writeEndElement()} is the responsibility of the caller.
- *
- * @param writer XML Stream writer
- * @param id InstanceIdentifier
- * @throws XMLStreamException
- */
- public static void write(final @Nonnull XMLStreamWriter writer, final @Nonnull YangInstanceIdentifier id) throws XMLStreamException {
- Preconditions.checkNotNull(writer, "Writer may not be null");
- Preconditions.checkNotNull(id, "Variable should contain instance of instance identifier and can't be null");
-
- final RandomPrefix prefixes = new RandomPrefix();
- final String str = XmlUtils.encodeIdentifier(prefixes, id);
-
- for (Entry<URI, String> e: prefixes.getPrefixes()) {
- final String ns = e.getKey().toString();
- final String p = e.getValue();
-
- writer.writeNamespace(p, ns);
- }
- writer.writeCharacters(str);
- }
-
- /**
- * Write a full XML document corresponding to a CompositeNode into an XML stream writer.
- *
- * @param writer XML Stream writer
- * @param data data node
- * @param schema corresponding schema node, may be null
- * @throws XMLStreamException if an encoding problem occurs
- */
- public void writeDocument(final @Nonnull XMLStreamWriter writer, final @Nonnull CompositeNode data, final @Nullable SchemaNode schema) throws XMLStreamException {
- // final Boolean repairing = (Boolean) writer.getProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES);
- // Preconditions.checkArgument(repairing == true, "XML Stream Writer has to be repairing namespaces");
-
- writer.writeStartDocument();
- writeElement(writer, data, schema);
- writer.writeEndDocument();
- writer.flush();
- }
-
- /**
- * Short-hand for {@link #writeDocument(XMLStreamWriter, CompositeNode, SchemaNode)})} with
- * null SchemaNode.
- *
- * @param writer XML Stream writer
- * @param data data node
- * @throws XMLStreamException if an encoding problem occurs
- */
- public void writeDocument(final XMLStreamWriter writer, final CompositeNode data) throws XMLStreamException {
- writeDocument(writer, data, null);
- }
-
- /**
- * Write an element into a XML stream writer. This includes the element start/end tags and
- * the value of the element.
- *
- * @param writer XML Stream writer
- * @param data data node
- * @param schema Schema node
- * @throws XMLStreamException if an encoding problem occurs
- */
- public void writeElement(final XMLStreamWriter writer, final @Nonnull Node<?> data, final SchemaNode schema) throws XMLStreamException {
- final QName qname = data.getNodeType();
- final String pfx = qname.getPrefix() != null ? qname.getPrefix() : "";
- final String ns = qname.getNamespace() != null ? qname.getNamespace().toString() : "";
-
- if (isEmptyElement(data)) {
- if (hasAttributes(data)) {
- writer.writeStartElement(pfx, qname.getLocalName(), ns);
- final RandomPrefix randomPrefix = new RandomPrefix();
- writeAttributes(writer, (AttributesContainer) data, randomPrefix);
- writer.writeEndElement();
- } else {
- writer.writeEmptyElement(pfx, qname.getLocalName(), ns);
- }
- return;
- }
-
- writer.writeStartElement(pfx, qname.getLocalName(), ns);
- writeValue(writer, data, schema);
- writer.writeEndElement();