Example usage for javax.xml.parsers SAXParser getXMLReader

List of usage examples for javax.xml.parsers SAXParser getXMLReader

Introduction

In this page you can find the example usage for javax.xml.parsers SAXParser getXMLReader.

Prototype


public abstract org.xml.sax.XMLReader getXMLReader() throws SAXException;

Source Link

Document

Returns the org.xml.sax.XMLReader that is encapsulated by the implementation of this class.

Usage

From source file:org.solmix.runtime.support.spring.TunedDocumentLoader.java

@Override
public Document loadDocument(InputSource inputSource, EntityResolver entityResolver, ErrorHandler errorHandler,
        int validationMode, boolean namespaceAware) throws Exception {
    if (validationMode == XmlBeanDefinitionReader.VALIDATION_NONE) {
        SAXParserFactory parserFactory = namespaceAware ? nsasaxParserFactory : saxParserFactory;
        SAXParser parser = parserFactory.newSAXParser();
        XMLReader reader = parser.getXMLReader();
        reader.setEntityResolver(entityResolver);
        reader.setErrorHandler(errorHandler);
        //            SAXSource saxSource = new SAXSource(reader, inputSource);
        //            W3CDOMStreamWriter writer = new W3CDOMStreaXMLStreamWriterImplmWriter();
        //            StaxUtils.copy(saxSource, writer);
        //            return writer.getDocument();
        return null;
    } else {/*from w  w  w  .  j  a  va2  s  .  c om*/
        return super.loadDocument(inputSource, entityResolver, errorHandler, validationMode, namespaceAware);
    }
}

From source file:com.inferiorhumanorgans.WayToGo.Agency.BART.Prediction.XMLTask.java

@Override
protected Void doInBackground(BARTAgency... someAgencies) {
    super.doInBackground(someAgencies);
    theListener.startPredictionFetch();/* w  ww.  ja  va  2s . c  o  m*/
    Log.d(LOG_NAME, "Trying to get Predictions.");

    InputStream content = null;
    ClientConnectionManager connman = new ThreadSafeClientConnManager(params, registry);
    DefaultHttpClient hc = new DefaultHttpClient(connman, params);

    String theURL = BART_URL + BARTAgency.API_KEY + "&orig=" + theStationTag;

    Log.i(LOG_NAME, "Fetching from: " + theURL);
    HttpGet getRequest = new HttpGet(theURL);
    try {
        content = hc.execute(getRequest).getEntity().getContent();
    } catch (ClientProtocolException ex) {
        Logger.getLogger(LOG_NAME).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(LOG_NAME).log(Level.SEVERE, null, ex);
    }
    Log.d(LOG_NAME, "Put the BART predictions in the background.");

    try {
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();

        XMLReader xr = sp.getXMLReader();

        xr.setContentHandler(dataHandler);

        xr.parse(new InputSource(content));

    } catch (ParserConfigurationException pce) {
        Log.e(LOG_NAME + " SAX XML", "sax parse error", pce);
    } catch (AbortXMLParsingException abrt) {
        Log.d(LOG_NAME + " AsyncXML", "Cancelled!!!!!");
    } catch (SAXException se) {
        Log.e(LOG_NAME + " SAX XML", "sax error", se);
    } catch (IOException ioe) {
        Log.e(LOG_NAME + " SAX XML", "sax parse io error", ioe);
    }
    Log.d(LOG_NAME + " SAX XML", "Done parsing BART station XML");
    return null;
}

From source file:com.entertailion.android.overlaynews.rss.RssHandler.java

public RssFeed getFeed(String data) {
    feed = null;//from  w  w  w  . java2s  .  c om
    try {
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();

        InputSource inStream = new org.xml.sax.InputSource();
        inStream.setCharacterStream(new StringReader(data));

        xr.setContentHandler(this);
        feed = new RssFeed();
        xr.parse(inStream);

        xr = null;
        sp = null;
        spf = null;
    } catch (Exception e) {
        Log.e(LOG_CAT, "getFeed", e);
    }

    return feed;
}

From source file:importer.handler.post.annotate.HTMLConverter.java

/**
 * Turn XML note content for Harpur to HTML
 * @param xml the xml note/*  w  w w .j a  v a 2s .  co  m*/
 * @param config the JSON config to control the conversion
 * @return a HTML string
 */
String convert(String xml) throws HTMLException {
    try {
        SAXParserFactory spf = SAXParserFactory.newInstance();
        HTMLConverter conv = new HTMLConverter(patterns);
        spf.setNamespaceAware(true);
        SAXParser parser = spf.newSAXParser();
        XMLReader xmlReader = parser.getXMLReader();
        xmlReader.setContentHandler(conv);
        xmlReader.setErrorHandler(new MyErrorHandler(System.err));
        CharArrayReader car = new CharArrayReader(xml.toCharArray());
        InputSource input = new InputSource(car);
        xmlReader.parse(input);
        return conv.body.toString();
    } catch (Exception e) {
        throw new HTMLException(e);
    }
}

From source file:com.joshdrummond.webpasswordsafe.android.GetCurrentPassword.java

private String parseResponse(String responseSOAP) {
    String response = "";
    try {//from  w  ww.  ja va2  s . c om
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();
        GetCurrentPasswordHandler handler = new GetCurrentPasswordHandler();
        xr.setContentHandler(handler);
        xr.parse(new InputSource(new StringReader(responseSOAP)));
        response = handler.getParsedData();
    } catch (Exception e) {
        response = "ERROR parsing: " + Arrays.deepToString(e.getStackTrace());
    }
    return response;
}

From source file:it.openyoureyes.test.OpenCellId.java

private String parseImage(HttpEntity entity, DefaultHandler dd) throws IOException {
    String result = "";
    String line;/*  ww  w  .  j  a  v a2  s.  c om*/

    try {

        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();
        xr.setContentHandler(dd);

        /* Parse the xml-data from our URL. */
        xr.parse(new InputSource(entity.getContent()));

    } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SAXException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        entity.consumeContent();
    }
    return result;
}

From source file:com.entertailion.java.caster.DeviceFinder.java

public void onBroadcastFound(final BroadcastAdvertisement advert) {
    if (advert.getLocation() != null) {
        new Thread(new Runnable() {
            public void run() {
                Log.d(LOG_TAG, "location=" + advert.getLocation());
                HttpResponse response = new HttpRequestHelper().sendHttpGet(advert.getLocation());
                if (response != null) {
                    String appsUrl = null;
                    Header header = response.getLastHeader(HEADER_APPLICATION_URL);
                    if (header != null) {
                        appsUrl = header.getValue();
                        if (!appsUrl.endsWith("/")) {
                            appsUrl = appsUrl + "/";
                        }/* ww  w. j a  va 2 s  . c  o  m*/
                        Log.d(LOG_TAG, "appsUrl=" + appsUrl);
                    }
                    try {
                        InputStream inputStream = response.getEntity().getContent();
                        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

                        InputSource inStream = new org.xml.sax.InputSource();
                        inStream.setCharacterStream(reader);
                        SAXParserFactory spf = SAXParserFactory.newInstance();
                        SAXParser sp = spf.newSAXParser();
                        XMLReader xr = sp.getXMLReader();
                        BroadcastHandler broadcastHandler = new BroadcastHandler();
                        xr.setContentHandler(broadcastHandler);
                        xr.parse(inStream);
                        Log.d(LOG_TAG, "modelName=" + broadcastHandler.getDialServer().getModelName());
                        // Only handle ChromeCast devices; not other DIAL
                        // devices like ChromeCast devices
                        if (broadcastHandler.getDialServer().getModelName().equals(CHROME_CAST_MODEL_NAME)) {
                            Log.d(LOG_TAG,
                                    "ChromeCast device found: " + advert.getIpAddress().getHostAddress());
                            DialServer dialServer = new DialServer(advert.getLocation(), advert.getIpAddress(),
                                    advert.getPort(), appsUrl,
                                    broadcastHandler.getDialServer().getFriendlyName(),
                                    broadcastHandler.getDialServer().getUuid(),
                                    broadcastHandler.getDialServer().getManufacturer(),
                                    broadcastHandler.getDialServer().getModelName());
                            trackedDialServers.add(dialServer);
                        }
                    } catch (Exception e) {
                        Log.e(LOG_TAG, "parse device description", e);
                    }
                }
            }
        }).start();
    }
}

From source file:com.inferiorhumanorgans.WayToGo.Agency.NextBus.RouteConfig.RouteConfigXMLTask.java

@Override
protected Void doInBackground(final NextBusAgency... someAgencies) {
    super.doInBackground(someAgencies);

    final String ourNBName = theAgency.getNextBusName();

    Log.i(LOG_NAME, "Trying to get the route config for " + ourNBName + ".");
    Log.i(LOG_NAME, "Fetching from: " + NB_URL_BASE + ourNBName);
    InputStream ourInputStream = null;
    ClientConnectionManager ourConnectionManager = new ThreadSafeClientConnManager(params, registry);
    DefaultHttpClient ourHttpClient = new DefaultHttpClient(ourConnectionManager, params);

    final HttpGet getRequest = new HttpGet(NB_URL_BASE + Uri.encode(ourNBName));
    try {//from  w ww . j a v a  2  s .  com
        ourInputStream = ourHttpClient.execute(getRequest).getEntity().getContent();
    } catch (ClientProtocolException ex) {
        Logger.getLogger(LOG_NAME).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(LOG_NAME).log(Level.SEVERE, null, ex);
    }
    Log.i(LOG_NAME, "Done with the route config for " + ourNBName + ".");

    try {
        SAXParserFactory ourParserFactory = SAXParserFactory.newInstance();
        SAXParser ourParser = ourParserFactory.newSAXParser();

        XMLReader xr = ourParser.getXMLReader();

        RouteConfigXMLHandler ourDataHandler = new RouteConfigXMLHandler(this);
        xr.setContentHandler(ourDataHandler);

        xr.parse(new InputSource(ourInputStream));

    } catch (ParserConfigurationException pce) {
        Log.e(LOG_NAME + "SAX XML", "sax parse error", pce);
    } catch (AbortXMLParsingException abrt) {
        Log.i(LOG_NAME + "AsyncXML", "Cancelled!!!!!");
    } catch (SAXException se) {
        Log.e(LOG_NAME + "SAX XML", "sax error", se);
    } catch (IOException ioe) {
        Log.e(LOG_NAME + "SAX XML", "sax parse io error", ioe);
    }
    Log.i(LOG_NAME + "SAX XML", "Done parsing XML for " + ourNBName);
    return null;
}

From source file:cn.com.loopj.android.http.SaxAsyncHttpResponseHandler.java

/**
 * Deconstructs response into given content handler
 *
 * @param entity returned HttpEntity/*from   w ww  . j ava 2 s  .  com*/
 * @return deconstructed response
 * @throws IOException if there is problem assembling SAX response from stream
 * @see HttpEntity
 */
@Override
protected byte[] getResponseData(HttpEntity entity) throws IOException {
    if (entity != null) {
        InputStream instream = entity.getContent();
        InputStreamReader inputStreamReader = null;
        if (instream != null) {
            try {
                SAXParserFactory sfactory = SAXParserFactory.newInstance();
                SAXParser sparser = sfactory.newSAXParser();
                XMLReader rssReader = sparser.getXMLReader();
                rssReader.setContentHandler(handler);
                inputStreamReader = new InputStreamReader(instream, getCharset());
                rssReader.parse(new InputSource(inputStreamReader));
            } catch (SAXException e) {
                AsyncHttpClient.log.e(LOG_TAG, "getResponseData exception", e);
            } catch (ParserConfigurationException e) {
                AsyncHttpClient.log.e(LOG_TAG, "getResponseData exception", e);
            } finally {
                AsyncHttpClient.silentCloseInputStream(instream);
                if (inputStreamReader != null) {
                    try {
                        inputStreamReader.close();
                    } catch (IOException e) {
                        /*ignore*/ }
                }
            }
        }
    }
    return null;
}

From source file:com.rightscale.service.FeedScraper.java

public void run() {
    int pollPeriod = DEFAULT_POLL_PERIOD;

    while (false == _shouldStop) {
        boolean error = false;
        int interesting = 0;

        try {/*from   w  w  w .  j a v a  2s.  c  o  m*/
            HttpEntity response = getEntity(null, null);

            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            XMLReader xr = sp.getXMLReader();

            AtomParser parser = new AtomParser(this);
            xr.setContentHandler(parser);
            xr.parse(new InputSource(response.getContent()));
            response.consumeContent();
            interesting = parser.getNumInteresting();
        } catch (Exception e) {
            Log.e("FeedScraper", e.getClass().getName());
            e.printStackTrace();
            error = true;
        }

        if (error) {
            pollPeriod = ERROR_POLL_PERIOD;
        } else if (interesting == 0) {
            pollPeriod = (int) (pollPeriod * 1.5);
            pollPeriod = Math.max(pollPeriod, MAX_POLL_PERIOD);
        } else {
            pollPeriod = DEFAULT_POLL_PERIOD;
        }

        try {
            Thread.sleep(pollPeriod);
        } catch (InterruptedException e) {
            //someone may have set _shouldStop on us...
        }
    }
}