- /**
- * Write a value into a XML stream writer. This method assumes the start and end of element is
- * emitted by the caller.
- *
- * @param writer XML Stream writer
- * @param data data node
- * @param schema Schema node
- * @throws XMLStreamException if an encoding problem occurs
- */
- public void writeValue(final XMLStreamWriter writer, final @Nonnull Node<?> data, final SchemaNode schema) throws XMLStreamException {
- if (data instanceof AttributesContainer && ((AttributesContainer) data).getAttributes() != null) {
- RandomPrefix randomPrefix = new RandomPrefix();
- for (Entry<QName, String> attribute : ((AttributesContainer) data).getAttributes().entrySet()) {
- writeAttribute(writer, attribute, randomPrefix);
- }
- }
-
- if (data instanceof 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 {
- // CompositeNode
- for (Node<?> child : ((CompositeNode) data).getValue()) {
- DataSchemaNode childSchema = null;
- if (schema instanceof DataNodeContainer) {
- childSchema = SchemaUtils.findFirstSchema(child.getNodeType(), ((DataNodeContainer) schema).getChildNodes()).orNull();
- if (LOG.isDebugEnabled()) {
- if (childSchema == null) {
- LOG.debug("Probably the data node \"{}\" does not conform to schema", child == null ? "" : child.getNodeType().getLocalName());
- }
- }
- }
-
- writeElement(writer, child, childSchema);
- }
- }