try {
writeStartElement(qname);
if (value != null) {
- streamUtils.writeValue(writer, type, value);
+ streamUtils.writeValue(writer, type, value, qname.getModule());
}
writer.writeEndElement();
} catch (XMLStreamException e) {
try {
writeStartElement(qname);
if (value != null) {
- streamUtils.writeValue(writer, schemaNode, value);
+ streamUtils.writeValue(writer, schemaNode, value, qname.getModule());
}
writer.writeEndElement();
} catch (XMLStreamException e) {
private void writeElement(final QName qname, final SchemaNode schemaNode, final Object value, final Map<QName, String> attributes) throws IOException {
try {
writeStartElement(qname);
+
writeAttributes(attributes);
if (value != null) {
- streamUtils.writeValue(writer, schemaNode, value);
+ streamUtils.writeValue(writer, schemaNode, value, qname.getModule());
}
writer.writeEndElement();
} catch (XMLStreamException e) {
for (final Map.Entry<QName, String> qNameStringEntry : attributes.entrySet()) {
try {
final String namespace = qNameStringEntry.getKey().getNamespace().toString();
+
if(Strings.isNullOrEmpty(namespace)) {
writer.writeAttribute(qNameStringEntry.getKey().getLocalName(), qNameStringEntry.getValue());
} else {
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.common.QNameModule;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
final RandomPrefix prefixes = new RandomPrefix();
final String str = XmlUtils.encodeIdentifier(prefixes, id);
- writeNamespaceDeclarations(writer,prefixes.getPrefixes());
+ writeNamespaceDeclarations(writer, prefixes.getPrefixes());
writer.writeCharacters(str);
}
* @param writer XML Stream writer
* @param schemaNode Schema node that describes the value
* @param value data value
+ * @param parent optional parameter of a module QName owning the leaf definition
* @throws XMLStreamException if an encoding problem occurs
*/
- public void writeValue(@Nonnull final XMLStreamWriter writer, @Nonnull final SchemaNode schemaNode, final Object value) throws XMLStreamException {
+ public void writeValue(@Nonnull final XMLStreamWriter writer, @Nonnull final SchemaNode schemaNode, final Object value, final Optional<QNameModule> parent) throws XMLStreamException {
if (value == null) {
LOG.debug("Value of {}:{} is null, not encoding it", schemaNode.getQName().getNamespace(), schemaNode.getQName().getLocalName());
return;
baseType = SchemaContextUtil.getBaseTypeForLeafRef(leafrefTypeDefinition, schemaContext.get(), schemaNode);
}
- writeValue(writer, baseType, value);
+ writeValue(writer, baseType, value, parent);
+ }
+
+ public void writeValue(@Nonnull final XMLStreamWriter writer, @Nonnull final SchemaNode schemaNode, final Object value) throws XMLStreamException {
+ writeValue(writer, schemaNode, value, Optional.<QNameModule>absent());
+ }
+
+ public void writeValue(@Nonnull final XMLStreamWriter writer, @Nonnull final SchemaNode schemaNode, final Object value, final QNameModule parent) throws XMLStreamException {
+ writeValue(writer, schemaNode, value, Optional.of(parent));
}
/**
* @param writer XML Stream writer
* @param type data type. In case of leaf ref this should be the type of leaf being referenced
* @param value data value
+ * @param parent optional parameter of a module QName owning the leaf definition
* @throws XMLStreamException if an encoding problem occurs
*/
- public void writeValue(@Nonnull final XMLStreamWriter writer, @Nonnull final TypeDefinition<?> type, final Object value) throws XMLStreamException {
+ public void writeValue(@Nonnull final XMLStreamWriter writer, @Nonnull final TypeDefinition<?> type, final Object value, final Optional<QNameModule> parent) throws XMLStreamException {
if (value == null) {
LOG.debug("Value of {}:{} is null, not encoding it", type.getQName().getNamespace(), type.getQName().getLocalName());
return;
TypeDefinition<?> baseType = XmlUtils.resolveBaseTypeFrom(type);
if (baseType instanceof IdentityrefTypeDefinition) {
- write(writer, (IdentityrefTypeDefinition) baseType, value);
+ if (parent.isPresent()) {
+ write(writer, (IdentityrefTypeDefinition) baseType, value, parent);
+ } else {
+ write(writer, (IdentityrefTypeDefinition) baseType, value, Optional.<QNameModule>absent());
+ }
} else if (baseType instanceof InstanceIdentifierTypeDefinition) {
write(writer, (InstanceIdentifierTypeDefinition) baseType, value);
} else {
- final TypeDefinitionAwareCodec<Object, ?> codec = codecProvider.codecFor(baseType);
+ final TypeDefinitionAwareCodec<Object, ?> codec = codecProvider.codecFor(type);
String text;
if (codec != null) {
try {
text = codec.serialize(value);
} catch (ClassCastException e) {
- LOG.error("Provided node value {} did not have type {} required by mapping. Using stream instead.", value, baseType, e);
+ LOG.error("Provided node value {} did not have type {} required by mapping. Using stream instead.", value, type, e);
text = String.valueOf(value);
}
} else {
- LOG.error("Failed to find codec for {}, falling back to using stream", baseType);
+ LOG.error("Failed to find codec for {}, falling back to using stream", type);
text = String.valueOf(value);
}
writer.writeCharacters(text);
}
}
- private static void write(@Nonnull final XMLStreamWriter writer, @Nonnull final IdentityrefTypeDefinition type, @Nonnull final Object value) throws XMLStreamException {
+ public void writeValue(@Nonnull final XMLStreamWriter writer, @Nonnull final TypeDefinition<?> type, final Object value, final QNameModule parent) throws XMLStreamException {
+ writeValue(writer, type, value, Optional.of(parent));
+ }
+
+ public void writeValue(@Nonnull final XMLStreamWriter writer, @Nonnull final TypeDefinition<?> type, final Object value) throws XMLStreamException {
+ writeValue(writer, type, value, Optional.<QNameModule>absent());
+ }
+
+ @VisibleForTesting
+ static void write(@Nonnull final XMLStreamWriter writer, @Nonnull final IdentityrefTypeDefinition type, @Nonnull final Object value, final Optional<QNameModule> parent) throws XMLStreamException {
if (value instanceof QName) {
final QName qname = (QName) value;
final String prefix = "x";
- final String ns = qname.getNamespace().toString();
- writer.writeNamespace(prefix, ns);
- writer.writeCharacters(prefix + ':' + qname.getLocalName());
+ //in case parent is present and same as element namespace write value without namespace
+ if (parent.isPresent() && qname.getNamespace().equals(parent.get().getNamespace())){
+ writer.writeCharacters(qname.getLocalName());
+ } else {
+ final String ns = qname.getNamespace().toString();
+ writer.writeNamespace(prefix, ns);
+ writer.writeCharacters(prefix + ':' + qname.getLocalName());
+ }
+
} else {
LOG.debug("Value of {}:{} is not a QName but {}", type.getQName().getNamespace(), type.getQName().getLocalName(), value.getClass());
writer.writeCharacters(String.valueOf(value));
package org.opendaylight.yangtools.yang.data.impl.codec.xml;
+import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import com.google.common.base.Optional;
import com.google.common.collect.Maps;
import java.net.URISyntaxException;
import java.util.AbstractMap;
import java.util.Arrays;
+import java.util.Date;
import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamWriter;
import org.custommonkey.xmlunit.Diff;
assertTrue("Xml differs: " + diff.toString(), identical);
}
+ @Test
+ public void testWriteIdentityRef() throws Exception {
+ final ByteArrayOutputStream out = new ByteArrayOutputStream();
+ final XMLStreamWriter writer = XML_OUTPUT_FACTORY.createXMLStreamWriter(out);
+
+ writer.writeStartElement("element");
+ final QNameModule parent = QNameModule.create(URI.create("parent:uri"), new Date());
+ XmlStreamUtils.write(writer, null, QName.create(parent, "identity"), Optional.of(parent));
+ writer.writeEndElement();
+
+ writer.writeStartElement("elementDifferent");
+ XmlStreamUtils.write(writer, null, QName.create("different:namespace", "identity"), Optional.of(parent));
+ writer.writeEndElement();
+
+ writer.close();
+ out.close();
+
+ final String xmlAsString = new String(out.toByteArray()).replaceAll("\\s*", "");
+ assertThat(xmlAsString, containsString("element>identity"));
+
+ final Pattern prefixedIdentityPattern = Pattern.compile(".*\"different:namespace\">(.*):identity.*");
+ final Matcher matcher = prefixedIdentityPattern.matcher(xmlAsString);
+ assertTrue("Xml: " + xmlAsString + " should match: " + prefixedIdentityPattern, matcher.matches());
+ }
+
/**
* One leafref reference to other leafref via relative references
*/