- LOG.debug("Instance identifier with Random prefix is now {}", str);
- 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 javax.xml.stream.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();
- }
-
-
- /**
- * 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 javax.xml.stream.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)) {
- writer.writeEmptyElement(pfx, qname.getLocalName(), ns);
- return;
- }
-
- writer.writeStartElement(pfx, qname.getLocalName(), ns);
- if (data instanceof AttributesContainer && ((AttributesContainer) data).getAttributes() != null) {
- for (Entry<QName, String> attribute : ((AttributesContainer) data).getAttributes().entrySet()) {
- writer.writeAttribute(attribute.getKey().getNamespace().toString(), attribute.getKey().getLocalName(), attribute.getValue());
- }
- }
-
- if (data instanceof SimpleNode<?>) {
- LOG.debug("writeElement : node is of type SimpleNode");
- // Simple node
- if (schema instanceof LeafListSchemaNode) {
- writeValue(writer, ((LeafListSchemaNode) schema).getType(), data.getValue());
- } else if (schema instanceof LeafSchemaNode) {
- writeValue(writer, ((LeafSchemaNode) schema).getType(), data.getValue());
- } else {
- Object value = data.getValue();
- if (value != null) {
- writer.writeCharacters(String.valueOf(value));
- }
- }
- } else {
- LOG.debug("writeElement : node is of type CompositeNode");
- // CompositeNode
- for (Node<?> child : ((CompositeNode) data).getValue()) {
- DataSchemaNode childSchema = null;
- if (schema instanceof DataNodeContainer) {
- childSchema = SchemaUtils.findFirstSchema(child.getNodeType(), ((DataNodeContainer) schema).getChildNodes()).orNull();
- if (childSchema == null) {
- LOG.debug("Probably the data node \"{}\" does not conform to schema", child == null ? "" : child.getNodeType().getLocalName());
- }
- }
-
- writeElement(writer, child, childSchema);
- }