soapresultparser.java

来自「国外的j2me播放器软件」· Java 代码 · 共 133 行

JAVA
133
字号
package no.auc.one.portableplayer.communication.soap;

import java.io.*;
import java.util.*;
import javax.microedition.io.*;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;

import no.auc.one.portableplayer.communication.*;
import no.auc.one.portableplayer.communication.soap.*;
import no.auc.one.portableplayer.utils.*;
import no.auc.one.portableplayer.librarymanager.*;

import org.apache.log4j.*;

public class SoapResultParser {
    private Logger LOG = Logger.getLogger("SRP");
    
    private InputStream is;
    private SAXParser parser;

    private boolean isParsed = false;
    private String result = null;

    public SoapResultParser(InputStream is) {
        try {
            SAXParserFactory pfactory = SAXParserFactory.newInstance();
            parser = pfactory.newSAXParser();

            this.is = is;
        } catch (SAXException se) {
            LOG.fatal("SAXException caught in SoapResultParser::ctor");
            LOG.fatal(se);
        } catch (ParserConfigurationException pce) {
            LOG.fatal("PCE in SoapResultParser::ctor");
            LOG.fatal(pce);
        }
    }

    public void parseSoapResult(String resultTag) {
        try {
            SoapResultParserHandler srph = new SoapResultParserHandler(resultTag);
            parser.parse(is, srph);
            result = srph.getResult();
            isParsed = true;
        } catch (SAXException se) {
            LOG.fatal("Error in soap result parser");
            LOG.fatal(se);
        } catch (IOException ioe) {
            LOG.fatal("IOException in SRP");
            LOG.fatal(ioe);
        }
    }

    public boolean getBooleanResult() throws Exception {
        if (!isParsed) {
            // XXX Need some better exception to throw here!
            throw new Exception("Result has not been parsed yet. Please parse it first");
        }

        boolean res = false;
        
        int iRes = getIntegerResult();

        if (iRes == 0) {
            res = false;
        } else if (iRes == 1) {
            res = true;
        } else {
            LOG.debug("Strange result: " + iRes);
        }

        return res;
    }

    public int getIntegerResult() throws Exception {
        if (!isParsed) {
            // XXX Need some better exception to throw here!
            throw new Exception("Result has not been parsed yet. Please parse it first");
        }

        int res = -1;

        res = Integer.parseInt(result);

        return res;
    }

    public String getStringResult() throws Exception {
        if (!isParsed) {
            // XXX Need some better exception to throw here!
            throw new Exception("Result has not been parsed yet. Please parse it first");
        }

        return result;
    }

    private class SoapResultParserHandler extends DefaultHandler {
        private String resTag = null;
        private boolean isResTag = false;
        private String result = null;
        
        public SoapResultParserHandler(String resultTag) {
            resTag = resultTag;
        }

        public void startElement(
            String uri,
            String localName,
            String qName,
            Attributes attr) throws SAXException
        {
            if (qName.equals(resTag)) {
                isResTag = true;
            }
        }

        public void characters(
            char[] ch, int start, int length) throws SAXException
        {
            if (isResTag) {
                result = new String(ch, start, length);
                isResTag = false;
            }
        }

        public String getResult() {
            return result;
        }   
    }   
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?