com.twilio.sdk.parser.XmlResponseParser Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of twilio-java-sdk Show documentation
Show all versions of twilio-java-sdk Show documentation
Release Candidate for Next-Gen Twilio Java Helper Library
package com.twilio.sdk.parser;
import com.twilio.sdk.TwilioRestResponse;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
// TODO: Auto-generated Javadoc
/**
* The Class XmlResponseParser.
*/
public class XmlResponseParser implements ResponseParser {
public Map parse(TwilioRestResponse response) {
Map xmlMap = this.parseXml(response.getResponseText());
flattenMap(xmlMap, null, null);
return xmlMap;
}
/**
* Flatten map.
*
* @param map the map
* @param parentMap the parent map
* @param parentKey the parent key
*/
@SuppressWarnings("unchecked")
private void flattenMap(Map map,
Map parentMap, String parentKey) {
for (String key : map.keySet()) {
Object data = map.get(key);
if (data instanceof Map) {
flattenMap((Map) data, map, key);
} else if (data instanceof List) {
parentMap.put(parentKey, data);
}
}
}
/**
* Parses the xml.
*
* @param xmlString the xml string
* @return the map
*/
protected Map parseXml(String xmlString) {
Map ret = new HashMap();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
try {
// Configure the DocumentBuilderFactory to prevent XXE Processing
// {@link OWASP Documentation}
factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
factory.setXIncludeAware(false);
factory.setExpandEntityReferences(false);
builder = factory.newDocumentBuilder();
Document d = builder.parse(new InputSource(new StringReader(
xmlString)));
Node resp = d.getFirstChild(); // TwilioResponse
NodeList nodes = resp.getChildNodes();
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
buildNode(node, ret);
}
if (resp.hasChildNodes() && resp.getFirstChild().hasAttributes()) {
// Setup paging
NamedNodeMap attrs = resp.getFirstChild().getAttributes();
for (PagingProperty p : PagingProperty.values()) {
String property = this.getPagingPropertyKey(p);
Node n = attrs.getNamedItem(property);
ret.put(property, n.getNodeValue());
}
}
} catch (ParserConfigurationException e) {
// TODO log this
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ret;
}
/**
* Builds the node.
*
* @param currentNode the current node
* @param containerMap the container map
*/
@SuppressWarnings("unchecked")
private void buildNode(Node currentNode, Map containerMap) {
if (currentNode.hasChildNodes()
&& currentNode.getChildNodes().getLength() == 1
&& currentNode.getFirstChild().getNodeType() == Node.TEXT_NODE) {
Node textNode = currentNode.getFirstChild();
// this should be a list...
if (containerMap.containsKey(currentNode.getNodeName())) {
Object oldObject = containerMap.get(currentNode.getNodeName());
if (oldObject instanceof List) {
List