Is there a way for parsing Document to VoiceXMLDocument? [Java - Android]

740 views Asked by At

I wonder if there is a good way for parsing org.w3c.dom.Document to org.enhydra.wireless.voicexml.dom.VoiceXMLDocument?

I'm using Java in Android Application. My application is reading a VoiceXML file into InputStream. From InputStream, I can get Document using DocumentBuilderFactory. But this time I stuff at parsing Document to VoiceXMLDocument. Below is my soft code.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    btnLoadFile = (Button) findViewById(R.id.btnLoadFile);

    btnLoadFile.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            InputStream input = downloadFile(fileUrl);
            try {
                Document dom = getVXML(input);
                /**
                 * I'm stuff at here.
                 * Cannot parsing Document to VoiceXMLDocument.
                 * It will make an error
                 * */
                VoiceXMLDocument voiceXMLDocument = (VoiceXMLDocument) dom;
            } catch (Exception e) {
                Log.e("Exception = ", e.getMessage());
            }
        }
    });
}

/**
 * Convert InputStream to Document
 * @param inputStream (InputStream)
 * @return dom (Document)
 * */
public Document getVXML(InputStream inputStream)
        throws FileNotFoundException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    Document dom = null;
    try {
        DocumentBuilder db = dbf.newDocumentBuilder();
        dom = db.parse(inputStream);
    } catch (ParserConfigurationException pce) {
        pce.printStackTrace();
    } catch (SAXException se) {
        se.printStackTrace();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
    return dom;
}
1

There are 1 answers

0
user964409 On

I have spend a little time for this topic and have found a good way to get VoiceXMLDocument from vxml file without parsing Document. Below is the way how I get this thing done.

First Step: Import these jar files: jvxml-jsapi1.0.jar - jvxml-xml.jar - jvxml.jar - log4j-1.2.13.jar. I think you can find these jar files on internet.

Second Step: Download file vxml and read it as InputStream

/**
 * Download file from Internet
 * 
 * @param fileUrl
 *            - file location from Internet
 * @return InputStream - file input stream
 * */
public static InputStream downloadFile(String fileUrl) {
    InputStream input = null;
    if (checkUrl(fileUrl)) {
        try {
            HttpUriRequest request = new HttpGet(fileUrl.toString());
            DefaultHttpClient defaultHttpClient = new DefaultHttpClient();
            setProxy(defaultHttpClient);
            HttpClient httpClient = defaultHttpClient;

            HttpResponse response = httpClient.execute(request);

            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            if (statusCode == 200) {
                HttpEntity entity = response.getEntity();
                BufferedHttpEntity buf = new BufferedHttpEntity(entity);
                input = buf.getContent();
                return input;
            } else {
                throw new IOException(
                        "Download failed, HTTP response code " + statusCode
                                + " - " + statusLine.getReasonPhrase());
            }
        } catch (Exception e) {
            return input;
        }
    } else {
        return input;
    }
}

/**
 * Check URL
 * 
 * @param fileUrl
 *            - file location from Internet
 * @return boolean - URL link is valid or invalid
 * **/
private static boolean checkUrl(String fileUrl) {
    URL u;
    try {
        u = new URL(fileUrl);// this would check for the protocol
        u.toURI();// does the extra checking required for validation of URI
        return true;
    } catch (MalformedURLException e) {
        return false;
    } catch (URISyntaxException e) {
        return false;
    }
}

public static void setProxy(DefaultHttpClient httpclient) {
    final String PROXY_IP = "10.10.10.10";
    final int PROXY_PORT = 8080;
    final String username = "";
    final String password = "";

    httpclient.getCredentialsProvider().setCredentials(
            new AuthScope(PROXY_IP, PROXY_PORT),
            new UsernamePasswordCredentials(username, password));

    HttpHost proxy = new HttpHost(PROXY_IP, PROXY_PORT);

    httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,
            proxy);
}

Third Step: Parsing Input Stream to VoiceXmlDocument

private static VoiceXmlDocument getVoiceXMLDocument(InputStream input) {
    VoiceXmlDocument vdom = null;
    try {
        vdom = new VoiceXmlDocument(new InputSource(input));
    } catch (ParserConfigurationException ex) {
        Log.e("ParserConfigurationException = ", ex.getMessage());
    } catch (SAXException ex) {
        Log.e("SAXException = ", ex.getMessage());
    } catch (IOException ex) {
        Log.e("IOException = ", ex.getMessage());
    }
    return vdom;
}

Final Step: Get element from VoiceXMLDocument as you want

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button download = (Button)findViewById(R.id.button1);
    download.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            String fileUrl = "http://www.ampersand.com/vxml_tests/ex.vxml";
            InputStream input = downloadFile(fileUrl);
            Log.e("input = ", input.toString());
            try {
                VoiceXmlDocument voiceXmlDocument = getVoiceXMLDocument(input);
                Log.e("prompt = ", voiceXmlDocument.getElementsByTagName("prompt").item(0).toString());
            } catch (Exception e) {
                return;
            }
        }

    });
}

This way will print information of prompt tag from vxml file. You can see it in Android Log.

Good Luck!