+ private static NormalizedNode parse(Document doc, DataSchemaNode schemaNode){
+ if(schemaNode instanceof ContainerSchemaNode){
+ return parserFactory
+ .getContainerNodeParser()
+ .parse(Collections.singletonList(doc.getDocumentElement()),
+ (ContainerSchemaNode) schemaNode);
+
+ } else if(schemaNode instanceof ChoiceNode){
+ return parserFactory
+ .getChoiceNodeParser()
+ .parse(Collections.singletonList(doc.getDocumentElement()),
+ (ChoiceNode) schemaNode);
+ } else if(schemaNode instanceof LeafNode){
+ return parserFactory
+ .getLeafNodeParser()
+ .parse(Collections.singletonList(doc.getDocumentElement()),
+ (LeafSchemaNode) schemaNode);
+ } else if(schemaNode instanceof ListSchemaNode){
+ return parserFactory
+ .getMapNodeParser()
+ .parse(Collections.singletonList(doc.getDocumentElement()),
+ (ListSchemaNode) schemaNode);
+ } else if(schemaNode instanceof LeafListSchemaNode){
+ return parserFactory
+ .getLeafSetNodeParser()
+ .parse(Collections.singletonList(doc.getDocumentElement()),
+ (LeafListSchemaNode) schemaNode);
+ } else if(schemaNode instanceof AugmentationSchema){
+ return parserFactory
+ .getAugmentationNodeParser()
+ .parse(Collections.singletonList(doc.getDocumentElement()),
+ (AugmentationSchema) schemaNode);
+ } else if(schemaNode instanceof ListSchemaNode){
+ return parserFactory
+ .getMapEntryNodeParser()
+ .parse(Collections.singletonList(doc.getDocumentElement()),
+ (ListSchemaNode) schemaNode);
+
+ }
+
+ throw new UnsupportedOperationException(schemaNode.getClass().toString());
+ }
+
+
+ /**
+ * Helps in generation of NormalizedNodeXml message for the supplied NormalizedNode
+ *
+ * @param sc --SchemaContext
+ * @param normalizedNode -- Normalized Node to be encoded
+ * @return SimpleNormalizedNodeMessage.NormalizedNodeXml
+ */
+ public static SimpleNormalizedNodeMessage.NormalizedNodeXml encode(
+ SchemaContext sc, NormalizedNode<?, ?> normalizedNode) {
+
+ Preconditions.checkArgument(sc != null, "Schema context found null");
+
+ Preconditions.checkArgument(normalizedNode != null,
+ "normalized node found null");
+
+ DataSchemaNode schemaNode = getSchemaNode(sc,
+ normalizedNode.getIdentifier()
+ .getNodeType()
+ );
+
+ Preconditions.checkState(schemaNode != null,
+ "Couldn't find schema node for " + normalizedNode.getIdentifier());
+
+ Iterable<Element> els = serialize(schemaNode, normalizedNode);
+
+ String xmlString = toString(els.iterator().next());
+ SimpleNormalizedNodeMessage.NormalizedNodeXml.Builder builder =
+ SimpleNormalizedNodeMessage.NormalizedNodeXml.newBuilder();
+ builder.setXmlString(xmlString);
+ builder
+ .setNodeIdentifier(normalizedNode.getIdentifier()
+ .getNodeType().toString());
+ return builder.build();
+
+ }
+
+ /**
+ * Utilizes the SimpleNormalizedNodeMessage.NormalizedNodeXml to convert into NormalizedNode
+ *
+ * @param sc -- schema context
+ * @param normalizedNodeXml -- containing the normalized Node XML
+ * @return NormalizedNode return
+ * @throws Exception
+ */
+
+ public static NormalizedNode decode(SchemaContext sc,
+ SimpleNormalizedNodeMessage.NormalizedNodeXml normalizedNodeXml)
+ throws Exception {
+
+ Preconditions
+ .checkArgument(sc != null, "schema context seems to be null");
+
+ Preconditions.checkArgument(normalizedNodeXml != null,
+ "SimpleNormalizedNodeMessage.NormalizedNodeXml found to be null");
+ QName qname = QName.create(normalizedNodeXml.getNodeIdentifier());
+
+ // here we will try to get back the NormalizedNode
+ DataSchemaNode schemaNode = getSchemaNode(sc, qname);
+
+ // now we need to read the XML
+ Document doc =
+ factory.newDocumentBuilder().parse(
+ new ByteArrayInputStream(
+ normalizedNodeXml.getXmlString().getBytes(
+ "utf-8"))
+ );
+
+ doc.getDocumentElement().normalize();
+
+
+ return parse(doc, schemaNode);
+ }