- private static Node<?> toNodeWithSchema(final Element xmlElement, final DataSchemaNode schema, final XmlCodecProvider codecProvider,final SchemaContext schemaCtx) {
- checkQName(xmlElement, schema.getQName());
- if (schema instanceof DataNodeContainer) {
- return toCompositeNodeWithSchema(xmlElement, schema.getQName(), (DataNodeContainer) schema, schemaCtx);
- } else if (schema instanceof LeafSchemaNode) {
- return toSimpleNodeWithType(xmlElement, (LeafSchemaNode) schema, codecProvider,schemaCtx);
- } else if (schema instanceof LeafListSchemaNode) {
- return toSimpleNodeWithType(xmlElement, (LeafListSchemaNode) schema, codecProvider,schemaCtx);
- }
- return null;
- }
-
-
-
- private static Node<?> toSimpleNodeWithType(final Element xmlElement, final LeafSchemaNode schema,
- final XmlCodecProvider codecProvider,final SchemaContext schemaCtx) {
- TypeDefinitionAwareCodec<? extends Object, ? extends TypeDefinition<?>> codec = codecProvider.codecFor(schema.getType());
- String text = xmlElement.getTextContent();
- Object value = null;
- if (codec != null) {
- logger.debug("toSimpleNodeWithType: found codec, deserializing text {}", text);
- value = codec.deserialize(text);
- }
-
- final TypeDefinition<?> baseType = XmlUtils.resolveBaseTypeFrom(schema.getType());
- if (baseType instanceof InstanceIdentifierType) {
- logger.debug("toSimpleNodeWithType: base type of node is instance identifier, deserializing element", xmlElement);
- value = InstanceIdentifierForXmlCodec.deserialize(xmlElement,schemaCtx);
-
- } else if(baseType instanceof IdentityrefTypeDefinition){
- logger.debug("toSimpleNodeWithType: base type of node is IdentityrefTypeDefinition, deserializing element", xmlElement);
- value = InstanceIdentifierForXmlCodec.toIdentity(xmlElement.getTextContent(), xmlElement, schemaCtx);
-
- }
-
- if (value == null) {
- logger.debug("toSimpleNodeWithType: no type found for element, returning just the text string value of element {}", xmlElement);
- value = xmlElement.getTextContent();
- }
-
- Optional<ModifyAction> modifyAction = getModifyOperationFromAttributes(xmlElement);
- return new SimpleNodeTOImpl<>(schema.getQName(), null, value, modifyAction.orNull());
- }
-
- private static Node<?> toSimpleNodeWithType(final Element xmlElement, final LeafListSchemaNode schema,
- final XmlCodecProvider codecProvider,final SchemaContext schemaCtx) {
- TypeDefinitionAwareCodec<? extends Object, ? extends TypeDefinition<?>> codec = codecProvider.codecFor(schema.getType());
- String text = xmlElement.getTextContent();
- Object value = null;
- if (codec != null) {
- logger.debug("toSimpleNodeWithType: found codec, deserializing text {}", text);
- value = codec.deserialize(text);
- }
-
- final TypeDefinition<?> baseType = XmlUtils.resolveBaseTypeFrom(schema.getType());
- if (baseType instanceof InstanceIdentifierType) {
- logger.debug("toSimpleNodeWithType: base type of node is instance identifier, deserializing element", xmlElement);
- value = InstanceIdentifierForXmlCodec.deserialize(xmlElement,schemaCtx);
- }
-
- if (value == null) {
- logger.debug("toSimpleNodeWithType: no type found for element, returning just the text string value of element {}", xmlElement);
- value = xmlElement.getTextContent();
- }
-
- Optional<ModifyAction> modifyAction = getModifyOperationFromAttributes(xmlElement);
- return new SimpleNodeTOImpl<>(schema.getQName(), null, value, modifyAction.orNull());
- }
-
- private static Node<?> toCompositeNodeWithSchema(final Element xmlElement, final QName qName, final DataNodeContainer schema,
- final SchemaContext schemaCtx) {
- List<Node<?>> values = toDomNodes(xmlElement, Optional.fromNullable(schema.getChildNodes()),schemaCtx);
- Optional<ModifyAction> modifyAction = getModifyOperationFromAttributes(xmlElement);
- return ImmutableCompositeNode.create(qName, values, modifyAction.orNull());
- }
-
- private static Optional<ModifyAction> getModifyOperationFromAttributes(final Element xmlElement) {
- Attr attributeNodeNS = xmlElement.getAttributeNodeNS(OPERATION_ATTRIBUTE_QNAME.getNamespace().toString(), OPERATION_ATTRIBUTE_QNAME.getLocalName());
- if(attributeNodeNS == null) {
- return Optional.absent();
- }
-
- ModifyAction action = ModifyAction.fromXmlValue(attributeNodeNS.getValue());
- Preconditions.checkArgument(action.isOnElementPermitted(), "Unexpected operation %s on %s", action, xmlElement);
-
- return Optional.of(action);
- }
-
- private static void checkQName(final Element xmlElement, final QName qName) {
- checkState(Objects.equal(xmlElement.getNamespaceURI(), qName.getNamespace().toString()));
- checkState(qName.getLocalName().equals(xmlElement.getLocalName()));
- }
-