- private static final String BLANK = "";
- private static final Logger LOG = LoggerFactory.getLogger(XmlUtils.class);
-
- /**
- * Converts the composite node to xml using rpc input schema node
- * @param cNode
- * @param schemaContext
- * @return xml String
- */
- public static String inputCompositeNodeToXml(CompositeNode cNode, SchemaContext schemaContext){
- if(LOG.isDebugEnabled()) {
- LOG.debug("Converting input composite node to xml {}", cNode);
- }
- if (cNode == null) {
- return BLANK;
- }
-
- if(schemaContext == null) {
- return BLANK;
- }
-
- Document domTree = null;
- try {
- Set<RpcDefinition> rpcs = schemaContext.getOperations();
- for(RpcDefinition rpc : rpcs) {
- if(rpc.getQName().equals(cNode.getNodeType())){
- if(LOG.isDebugEnabled()) {
- LOG.debug("Found the rpc definition from schema context matching with input composite node {}", rpc.getQName());
- }
- CompositeNode inputContainer = cNode.getFirstCompositeByName(QName.create(cNode.getNodeType(), "input"));
- domTree = XmlDocumentUtils.toDocument(inputContainer, rpc.getInput(), XmlDocumentUtils.defaultValueCodecProvider());
- if(LOG.isDebugEnabled()) {
- LOG.debug("input composite node to document conversion complete, document is {}", domTree);
- }
- break;
- }
- }
-
- } catch (UnsupportedDataTypeException e) {
- LOG.error("Error during translation of CompositeNode to Document", e);
- }
- return domTransformer(domTree);
- }
-
- /**
- * Converts the composite node to xml String using rpc output schema node
- * @param cNode
- * @param schemaContext
- * @return xml string
- */
- public static String outputCompositeNodeToXml(CompositeNode cNode, SchemaContext schemaContext){
- if(LOG.isDebugEnabled()) {
- LOG.debug("Converting output composite node to xml {}", cNode);
- }
- if (cNode == null) {
- return BLANK;
- }
-
- if(schemaContext == null) {
- return BLANK;
- }
-
- Document domTree = null;
- try {
- Set<RpcDefinition> rpcs = schemaContext.getOperations();
- for(RpcDefinition rpc : rpcs) {
- if(rpc.getQName().equals(cNode.getNodeType())){
- if(LOG.isDebugEnabled()) {
- LOG.debug("Found the rpc definition from schema context matching with output composite node {}", rpc.getQName());
- }
- CompositeNode outputContainer = cNode.getFirstCompositeByName(QName.create(cNode.getNodeType(), "output"));
- domTree = XmlDocumentUtils.toDocument(outputContainer, rpc.getOutput(), XmlDocumentUtils.defaultValueCodecProvider());
- if(LOG.isDebugEnabled()) {
- LOG.debug("output composite node to document conversion complete, document is {}", domTree);
- }
- break;
- }
- }
-
- } catch (UnsupportedDataTypeException e) {
- LOG.error("Error during translation of CompositeNode to Document", e);
- }
- return domTransformer(domTree);
- }
-
- private static String domTransformer(Document domTree) {
- StringWriter writer = new StringWriter();
- try {
- TransformerFactory tf = TransformerFactory.newInstance();
- Transformer transformer = tf.newTransformer();
- transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
- transformer.transform(new DOMSource(domTree), new StreamResult(writer));
- } catch (TransformerException e) {
-
- LOG.error("Error during translation of Document to OutputStream", e);
- }
- if(LOG.isDebugEnabled()) {
- LOG.debug("Document to string conversion complete, xml string is {} ", writer.toString());
- }
- return writer.toString();
- }
-
- public static CompositeNode xmlToCompositeNode(String xml){
- if (xml==null || xml.length()==0) {
- return null;
- }
-
- Node<?> dataTree;
- try {
- dataTree = XmlTreeBuilder.buildDataTree(new ByteArrayInputStream(xml.getBytes()));
- } catch (XMLStreamException e) {
- LOG.error("Error during building data tree from XML", e);
- return null;
- }
- if (dataTree == null) {
- LOG.error("data tree is null");
- return null;
- }
- if (dataTree instanceof SimpleNode) {
- LOG.error("RPC XML was resolved as SimpleNode");
- return null;
- }
- return (CompositeNode) dataTree;
- }
-
- /**
- * Converts the xml to composite node using rpc input schema node
- * @param rpc
- * @param xml
- * @param schemaContext
- * @return CompositeNode object based on the input, if any of the input parameter is null, a null object is returned
- */
- public static CompositeNode inputXmlToCompositeNode(QName rpc, String xml, SchemaContext schemaContext){
- if(LOG.isDebugEnabled()) {
- LOG.debug("Converting input xml to composite node {}", xml);
- }
- if (xml==null || xml.length()==0) {
- return null;
- }
-
- if(rpc == null) {
- return null;
- }
-
- if(schemaContext == null) {
- return null;
- }
-
- CompositeNode compositeNode = null;
- try {
-
- Document doc = XmlUtil.readXmlToDocument(xml);
- Set<RpcDefinition> rpcs = schemaContext.getOperations();
- for(RpcDefinition rpcDef : rpcs) {
- if(rpcDef.getQName().equals(rpc)){
- if(LOG.isDebugEnabled()) {
- LOG.debug("found the rpc definition from schema context matching rpc {}", rpc);
- }
- if(rpcDef.getInput() == null) {
- LOG.warn("found rpc definition's input is null");
- return null;
- }
-
- QName input = rpcDef.getInput().getQName();
- NodeList nodeList = doc.getElementsByTagNameNS(input.getNamespace().toString(), "input");
- if(nodeList == null || nodeList.getLength() < 1) {
- LOG.warn("xml does not have input entry. {}", xml);
- return null;
- }
- Element xmlData = (Element)nodeList.item(0);
-
- List<Node<?>> dataNodes = XmlDocumentUtils.toDomNodes(xmlData,
- Optional.of(rpcDef.getInput().getChildNodes()), schemaContext);
- if(LOG.isDebugEnabled()) {
- LOG.debug("Converted xml input to list of nodes {}", dataNodes);
- }
- final CompositeNodeBuilder<ImmutableCompositeNode> it = ImmutableCompositeNode.builder();
- it.setQName(rpc);
- it.add(ImmutableCompositeNode.create(input, dataNodes));
- compositeNode = it.toInstance();
- break;
- }
- }
- } catch (SAXException e) {
- LOG.error("Error during building data tree from XML", e);
- } catch (IOException e) {
- LOG.error("Error during building data tree from XML", e);
- }
- if(LOG.isDebugEnabled()) {
- LOG.debug("Xml to composite node conversion complete {} ", compositeNode);
- }
- return compositeNode;
- }
-