Android Open Source - mobile-connector-sdk-android Engage Response X M L






From Project

Back to project page mobile-connector-sdk-android.

License

The source code is released under:

Apache License

If you think the Android project mobile-connector-sdk-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.silverpop.engage.response;
/*from w w w . ja  va 2s .com*/
import com.silverpop.engage.exception.XMLResponseParseException;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Stack;

/**
 * Parses the response from XMLAPI requests into usable components.
 *
 * Created by jeremydyer on 5/19/14.
 */
public class EngageResponseXML {

    private String xml = null;
    private XMLAPIResponseNode root = null;

    public EngageResponseXML(String xml) {
        setXml(xml);
        this.parseXMLToTreeStructure(xml);
    }

    private void parseXMLToTreeStructure(String xml) {
        XmlPullParserFactory factory = null;
        try {
            factory = XmlPullParserFactory.newInstance();
            XmlPullParser xpp = factory.newPullParser();

            //Remove unwanted xml Characters like \n
            xml = xml.replace("\n", "");

            xpp.setInput( new StringReader(xml));
            int eventType = xpp.getEventType();

            Stack<XMLAPIResponseNode> contextStack = new Stack<XMLAPIResponseNode>();
            XMLAPIResponseNode currentNode = null;

            while (eventType != XmlPullParser.END_DOCUMENT) {
                if(eventType == XmlPullParser.START_DOCUMENT) {
                    System.out.println("Start document");
                } else if(eventType == XmlPullParser.START_TAG) {
                    currentNode = new XMLAPIResponseNode(xpp.getName());
                    contextStack.push(currentNode);
                } else if(eventType == XmlPullParser.END_TAG) {

                    if (contextStack.size() > 1) {
                        contextStack.pop(); //Must pop because current node is the last element on stack.
                        XMLAPIResponseNode parent = contextStack.pop();
                        parent.addChild(currentNode);
                        currentNode = parent;
                        contextStack.push(currentNode);
                    }

                } else if(eventType == XmlPullParser.TEXT) {
                    currentNode.setValue(xpp.getText());
                }
                eventType = xpp.next();
            }

            root = contextStack.pop();

        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * Locates the value for a "." delimited keypath.
     *
     * @param keyPath
     *  "." delimited string to locate the value.
     *
     * @return
     *      Value for the specified keypath.
     */
    public String valueForKeyPath(String keyPath) throws XMLResponseParseException {
        if (keyPath != null && keyPath.length() > 0) {
            String[] pathComponents = keyPath.split("\\.");

            if (pathComponents != null && pathComponents.length > 0) {
                //Make sure that the first element matches the root name.
                if (root.getName().equalsIgnoreCase(pathComponents[0])) {
                    XMLAPIResponseNode currentNode = root;
                    for (int i = 1; i < pathComponents.length; i++) {
                        currentNode = currentNode.childByName(pathComponents[i]);
                        if (currentNode == null) {
                            throw new XMLResponseParseException("KeyPath " + keyPath
                                    + " does not match response!", getXml(), keyPath);
                        }
                    }

                    //Now get the value from the current node.
                    if (currentNode.isLeaf()) {
                        return currentNode.getValue();
                    } else {
                        throw new XMLResponseParseException("KeyPath " + keyPath
                                + " does not describe a leaf element!", getXml(), keyPath);
                    }

                } else {
                    throw new XMLResponseParseException("KeyPath " + keyPath
                            + " does not match response!", getXml(), keyPath);
                }
            } else {
                return null;
            }
        } else {
            return null;
        }
    }

    public String getXml() {
        return xml;
    }

    public void setXml(String xml) {
        this.xml = xml;
    }
}




Java Source Code List

com.silverpop.engage.EngageApplication.java
com.silverpop.engage.UBFManager.java
com.silverpop.engage.XMLAPIManager.java
com.silverpop.engage.augmentation.UBFAugmentationServiceImpl.java
com.silverpop.engage.augmentation.UBFAugmentationService.java
com.silverpop.engage.augmentation.plugin.UBFAddressAugmentationPlugin.java
com.silverpop.engage.augmentation.plugin.UBFAugmentationPlugin.java
com.silverpop.engage.augmentation.plugin.UBFCoordinatesAugmentationPlugin.java
com.silverpop.engage.augmentation.plugin.UBFLocationNameAugmentationPlugin.java
com.silverpop.engage.config.EngageConfigManagerTests.java
com.silverpop.engage.config.EngageConfigManager.java
com.silverpop.engage.config.EngageConfigTest.java
com.silverpop.engage.config.EngageConfig.java
com.silverpop.engage.deeplinking.EngageDeepLinkManager.java
com.silverpop.engage.demo.engagetest.Application.java
com.silverpop.engage.demo.engagetest.EngageNotificationReceiver.java
com.silverpop.engage.demo.engagetest.MainActivity.java
com.silverpop.engage.demo.engagetest.PushReceiver.java
com.silverpop.engage.demo.engagetest.fragment.EngageConfigFragment.java
com.silverpop.engage.demo.engagetest.fragment.UBFAPIFragment.java
com.silverpop.engage.demo.engagetest.fragment.XMLAPIFragment.java
com.silverpop.engage.domain.EngageEvent.java
com.silverpop.engage.domain.JSONable.java
com.silverpop.engage.domain.UBFTests.java
com.silverpop.engage.domain.UBF.java
com.silverpop.engage.domain.XMLAPIEnum.java
com.silverpop.engage.domain.XMLAPITest.java
com.silverpop.engage.domain.XMLAPI.java
com.silverpop.engage.exception.XMLResponseParseException.java
com.silverpop.engage.location.manager.EngageLocationManager.java
com.silverpop.engage.location.manager.plugin.EngageLocationManagerDefault.java
com.silverpop.engage.location.receiver.EngageLocationReceiver.java
com.silverpop.engage.location.receiver.plugin.EngageLocationReceiverBase.java
com.silverpop.engage.location.receiver.plugin.EngageLocationReceiverGeocode.java
com.silverpop.engage.location.receiver.plugin.EngageLocationReceiverHardcodeTest.java
com.silverpop.engage.network.Credential.java
com.silverpop.engage.network.EngageClient.java
com.silverpop.engage.network.RequestCacheWrapper.java
com.silverpop.engage.network.UBFClient.java
com.silverpop.engage.network.XMLAPIClient.java
com.silverpop.engage.response.EngageResponseXMLTests.java
com.silverpop.engage.response.EngageResponseXML.java
com.silverpop.engage.response.XMLAPIResponseNode.java
com.silverpop.engage.store.EngageLocalEventStoreTest.java
com.silverpop.engage.store.EngageLocalEventStore.java
com.silverpop.engage.store.EngageSQLiteHelper.java
com.silverpop.engage.util.EngageExpirationParserTests.java
com.silverpop.engage.util.EngageExpirationParser.java
com.silverpop.engage.util.TimedAsyncTask.java