import static com.google.common.base.Preconditions.checkState;
+import com.google.common.base.Function;
+import com.google.common.base.Objects;
+import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
+import com.google.common.base.Strings;
+import com.google.common.collect.ImmutableList;
+
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode;
import org.opendaylight.yangtools.yang.data.impl.SimpleNodeTOImpl;
import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
-import org.opendaylight.yangtools.yang.data.impl.util.CompositeNodeBuilder;
import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
-import com.google.common.base.Function;
-import com.google.common.base.Objects;
-import com.google.common.base.Optional;
-import com.google.common.base.Preconditions;
-import com.google.common.base.Strings;
-import com.google.common.collect.ImmutableList;
-
public class XmlDocumentUtils {
private static class ElementWithSchemaContext {
Element element;
throw new UnsupportedDataTypeException("Schema can be ContainerSchemaNode or ListSchemaNode. Other types are not supported yet.");
}
- final DOMResult result = new DOMResult();
- result.setNode(getDocument());
+ final DOMResult result = new DOMResult(getDocument());
try {
final XMLStreamWriter writer = FACTORY.createXMLStreamWriter(result);
XmlStreamUtils.create(codecProvider).writeDocument(writer, data, (SchemaNode)schema);
* @throws UnsupportedDataTypeException
*/
public static Document toDocument(final CompositeNode data, final XmlCodecProvider codecProvider) {
- final DOMResult result = new DOMResult();
+ final DOMResult result = new DOMResult(getDocument());
try {
final XMLStreamWriter writer = FACTORY.createXMLStreamWriter(result);
XmlStreamUtils.create(codecProvider).writeDocument(writer, data);
public static Node<?> toDomNode(final Element xmlElement, final Optional<DataSchemaNode> schema,
final Optional<XmlCodecProvider> codecProvider) {
+ return toDomNode(xmlElement, schema, codecProvider, Optional.<SchemaContext>absent());
+ }
+
+ public static Node<?> toDomNode(final Element xmlElement, final Optional<DataSchemaNode> schema,
+ final Optional<XmlCodecProvider> codecProvider, final Optional<SchemaContext> schemaContext) {
if (schema.isPresent()) {
- return toNodeWithSchema(xmlElement, schema.get(), codecProvider.or(XmlUtils.DEFAULT_XML_CODEC_PROVIDER));
+ if(schemaContext.isPresent()) {
+ return toNodeWithSchema(xmlElement, schema.get(), codecProvider.or(XmlUtils.DEFAULT_XML_CODEC_PROVIDER), schemaContext.get());
+ } else {
+ return toNodeWithSchema(xmlElement, schema.get(), codecProvider.or(XmlUtils.DEFAULT_XML_CODEC_PROVIDER));
+ }
}
return toDomNode(xmlElement);
}
- public static CompositeNode fromElement(final Element xmlElement) {
- CompositeNodeBuilder<ImmutableCompositeNode> node = ImmutableCompositeNode.builder();
- node.setQName(qNameFromElement(xmlElement));
-
- return node.toInstance();
- }
-
public static QName qNameFromElement(final Element xmlElement) {
String namespace = xmlElement.getNamespaceURI();
String localName = xmlElement.getLocalName();
if (codec != null) {
value = codec.deserialize(text);
}
+ final TypeDefinition<?> baseType = XmlUtils.resolveBaseTypeFrom(schema.getType());
- if (schema.getType() instanceof org.opendaylight.yangtools.yang.model.util.InstanceIdentifier) {
+ if (baseType instanceof org.opendaylight.yangtools.yang.model.util.InstanceIdentifier) {
value = InstanceIdentifierForXmlCodec.deserialize(xmlElement,schemaCtx);
- } else if(schema.getType() instanceof IdentityrefTypeDefinition){
+ } else if(baseType instanceof IdentityrefTypeDefinition){
value = InstanceIdentifierForXmlCodec.toIdentity(xmlElement.getTextContent(), xmlElement, schemaCtx);
}
}
private static void checkQName(final Element xmlElement, final QName qName) {
- checkState(Objects.equal(xmlElement.getNamespaceURI(), qName.getNamespace().toString()));
- checkState(qName.getLocalName().equals(xmlElement.getLocalName()));
+ checkState(Objects.equal(xmlElement.getNamespaceURI(), qName.getNamespace().toString()), "Not equal: %s to: %s for: %s and: %s", qName.getNamespace(), xmlElement.getNamespaceURI(), qName, xmlElement);
+ checkState(qName.getLocalName().equals(xmlElement.getLocalName()), "Not equal: %s to: %s for: %s and: %s", qName.getLocalName(), xmlElement.getLocalName(), qName, xmlElement);
}
- public static final Optional<DataSchemaNode> findFirstSchema(final QName qname, final Set<DataSchemaNode> dataSchemaNode) {
- if (dataSchemaNode != null && !dataSchemaNode.isEmpty() && qname != null) {
+ public static final Optional<DataSchemaNode> findFirstSchema(final QName qname, final Iterable<DataSchemaNode> dataSchemaNode) {
+ if (dataSchemaNode != null && qname != null) {
for (DataSchemaNode dsn : dataSchemaNode) {
if (qname.isEqualWithoutRevision(dsn.getQName())) {
return Optional.<DataSchemaNode> of(dsn);
return ImmutableCompositeNode.create(qname, values.build());
}
- public static List<Node<?>> toDomNodes(final Element element, final Optional<Set<DataSchemaNode>> context,final SchemaContext schemaCtx) {
+ public static List<Node<?>> toDomNodes(final Element element, final Optional<? extends Iterable<DataSchemaNode>> context, final SchemaContext schemaCtx) {
return forEachChild(element.getChildNodes(),schemaCtx, new Function<ElementWithSchemaContext, Optional<Node<?>>>() {
@Override
QName partialQName = qNameFromElement(input.getElement());
Optional<DataSchemaNode> schemaNode = findFirstSchema(partialQName, context.get());
if (schemaNode.isPresent()) {
- return Optional.<Node<?>> fromNullable(//
+ return Optional.<Node<?>> fromNullable(
toNodeWithSchema(input.getElement(), schemaNode.get(), XmlUtils.DEFAULT_XML_CODEC_PROVIDER, input.getSchemaContext()));
}
}
}
- public static List<Node<?>> toDomNodes(final Element element, final Optional<Set<DataSchemaNode>> context) {
- return toDomNodes(element,context,null);
+ public static List<Node<?>> toDomNodes(final Element element, final Optional<? extends Iterable<DataSchemaNode>> context) {
+ return toDomNodes(element, context, null);
}
/**
final Optional<NotificationDefinition> notificationDef = findNotification(partialQName,
notifications.get());
if (notificationDef.isPresent()) {
- final Set<DataSchemaNode> dataNodes = notificationDef.get().getChildNodes();
+ final Iterable<DataSchemaNode> dataNodes = notificationDef.get().getChildNodes();
final List<Node<?>> domNodes = toDomNodes(childElement,
- Optional.<Set<DataSchemaNode>> fromNullable(dataNodes),schemaCtx);
+ Optional.<Iterable<DataSchemaNode>> fromNullable(dataNodes),schemaCtx);
return ImmutableCompositeNode.create(notificationDef.get().getQName(), domNodes);
}
}