List of usage examples for javax.xml.parsers SAXParser getXMLReader
public abstract org.xml.sax.XMLReader getXMLReader() throws SAXException;
From source file:com.android.yijiang.kzx.http.SaxAsyncHttpResponseHandler.java
/** * Deconstructs response into given content handler * * @param entity returned HttpEntity/*from w ww . j a v a2 s . co m*/ * @return deconstructed response * @throws java.io.IOException * @see org.apache.http.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, DEFAULT_CHARSET); rssReader.parse(new InputSource(inputStreamReader)); } catch (SAXException e) { Log.e(LOG_TAG, "getResponseData exception", e); } catch (ParserConfigurationException e) { 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.fdwills.external.http.SaxAsyncHttpResponseHandler.java
/** * Deconstructs response into given content handler * * @param entity returned HttpEntity/* w w w .ja va 2 s .c om*/ * @return deconstructed response * @throws java.io.IOException * @see org.apache.http.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, DEFAULT_CHARSET); rssReader.parse(new InputSource(inputStreamReader)); } catch (SAXException e) { Log.e(LOG_TAG, "getResponseData exception", e); } catch (ParserConfigurationException e) { 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:ca.nines.ise.util.XMLDriver.java
/** * Construct a driver.// w w w . j av a2 s.c o m * * @throws ParserConfigurationException * @throws TransformerConfigurationException * @throws SAXException * @throws TransformerException */ public XMLDriver() throws ParserConfigurationException, TransformerConfigurationException, SAXException, TransformerException { docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); nullTransformer = TransformerFactory.newInstance().newTransformer(); SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); saxParserFactory.setNamespaceAware(true); SAXParser saxParser = saxParserFactory.newSAXParser(); xmlReader = saxParser.getXMLReader(); }
From source file:com.amytech.android.library.utils.asynchttp.SaxAsyncHttpResponseHandler.java
/** * Deconstructs response into given content handler * * @param entity/*from w ww.j a v a 2s. c o m*/ * returned HttpEntity * @return deconstructed response * @throws java.io.IOException * if there is problem assembling SAX response from stream * @see org.apache.http.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, DEFAULT_CHARSET); rssReader.parse(new InputSource(inputStreamReader)); } catch (SAXException e) { Log.e(LOG_TAG, "getResponseData exception", e); } catch (ParserConfigurationException e) { 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.textocat.textokit.morph.ruscorpora.RusCorporaCollectionReader.java
@Override public void initialize(UimaContext ctx) throws ResourceInitializationException { super.initialize(ctx); tagMapper = InitializableFactory.create(ctx, tagMapperClassName, RusCorporaTagMapper.class); if (!inputDir.isDirectory()) { throw new IllegalArgumentException(String.format("%s is not existing directory", inputDir)); }//from w w w . j av a 2 s . co m relativeURIFunc = CorpusUtils.relativeURIFunction(inputDir); relativePathFunc = CorpusUtils.relativePathFunction(inputDir); String inputFileExt = DEFAULT_INPUT_FILE_EXT; inputFiles = ImmutableList .copyOf(FileUtils.listFiles(inputDir, suffixFileFilter(inputFileExt), TrueFileFilter.INSTANCE)); getLogger().info(String.format("Detected *%s files in %s: %s", inputFileExt, inputDir, inputFiles.size())); try { SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); xmlReader = saxParser.getXMLReader(); } catch (Exception e) { throw new ResourceInitializationException(e); } }
From source file:io.onedecision.engine.decisions.model.dmn.validators.SchemaValidator.java
public void validate(InputStream obj, Errors errors) { InputStream is = (InputStream) obj; SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); schemaFactory.setResourceResolver(new LocalResourceResolver()); try {//from www. j a va 2 s .com schemaFactory.newSchema(new StreamSource(getResourceAsStreamWrapper("schema/dmn.xsd"))); } catch (SAXException e1) { errors.reject("Cannot find / read schema", "Exception: " + e1.getMessage()); } SAXParserFactory parserFactory = SAXParserFactory.newInstance(); // parserFactory.setValidating(true); parserFactory.setNamespaceAware(true); // parserFactory.setSchema(schema); SAXParser parser = null; try { parser = parserFactory.newSAXParser(); XMLReader reader = parser.getXMLReader(); reader.setErrorHandler(this); try { parser.parse(new InputSource(is), (DefaultHandler) null); } catch (Exception e) { String msg = "Schema validation failed"; LOGGER.error(msg, e); errors.reject(msg, "Exception: " + e.getMessage()); } if (this.errors.size() > 0) { errors.reject("Schema validation failed", this.errors.toString()); } } catch (ParserConfigurationException e1) { errors.reject("Cannot create parser", "Exception: " + e1.getMessage()); } catch (SAXException e1) { errors.reject("Parser cannot be created", "Exception: " + e1.getMessage()); } }
From source file:com.inferiorhumanorgans.WayToGo.Agency.BART.Station.StationTask.java
@Override protected Void doInBackground(BARTAgency... someAgencies) { Assert.assertEquals(1, someAgencies.length); super.doInBackground(someAgencies); Log.i(LOG_NAME, "Trying to get BART station list."); InputStream content = null;//from w ww . java 2 s . c o m ClientConnectionManager connman = new ThreadSafeClientConnManager(params, registry); DefaultHttpClient hc = new DefaultHttpClient(connman, params); Log.i(LOG_NAME, "Fetching basic station info from: " + BART_URL + BARTAgency.API_KEY); HttpGet getRequest = new HttpGet(BART_URL + BARTAgency.API_KEY); 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.i(LOG_NAME, "Put the station list 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.i(LOG_NAME + " AsyncXML", "Cancelled!!!!!"); return null; // Pass on some exception to the caller? } 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 BART station XML"); final DetailXMLHandler detailDataHandler = new DetailXMLHandler(this); for (String aStationTag : theStations.keySet()) { getRequest = new HttpGet(BART_DETAIL_URL + BARTAgency.API_KEY + "&orig=" + aStationTag); 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.i(LOG_NAME, "Put the station detail for " + aStationTag + " in the background."); try { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); XMLReader xr = sp.getXMLReader(); xr.setContentHandler(detailDataHandler); xr.parse(new InputSource(content)); } 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 BART station XML"); } return null; }
From source file:com.zegoggles.smssync.XOAuthConsumer.java
protected String getUsernameFromContacts() { final HttpClient httpClient = new DefaultHttpClient(); final String url = "https://www.google.com/m8/feeds/contacts/default/thin?max-results=1"; final StringBuilder email = new StringBuilder(); try {/* ww w .j a va 2 s . c o m*/ HttpGet get = new HttpGet(sign(url)); HttpResponse resp = httpClient.execute(get); SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); XMLReader xr = sp.getXMLReader(); xr.setContentHandler(new DefaultHandler() { boolean inEmail; @Override public void startElement(String uri, String localName, String qName, Attributes atts) { inEmail = "email".equals(localName); } @Override public void characters(char[] c, int start, int length) { if (inEmail) { email.append(c, start, length); } } }); xr.parse(new InputSource(resp.getEntity().getContent())); return email.toString(); } catch (oauth.signpost.exception.OAuthException e) { Log.e(TAG, "error", e); return null; } catch (org.xml.sax.SAXException e) { Log.e(TAG, "error", e); return null; } catch (java.io.IOException e) { Log.e(TAG, "error", e); return null; } catch (javax.xml.parsers.ParserConfigurationException e) { Log.e(TAG, "error", e); return null; } }
From source file:Validate.java
void parse(String dir, String filename) throws FileNotFoundException, IOException, ParserConfigurationException, SAXException { try {//from w w w . j a va2 s .co m File f = new File(dir, filename); StringBuffer errorBuff = new StringBuffer(); InputSource input = new InputSource(new FileInputStream(f)); // Set systemID so parser can find the dtd with a relative URL in the source document. input.setSystemId(f.toString()); SAXParserFactory spfact = SAXParserFactory.newInstance(); spfact.setValidating(true); spfact.setNamespaceAware(true); SAXParser parser = spfact.newSAXParser(); XMLReader reader = parser.getXMLReader(); //Instantiate inner-class error and lexical handler. Handler handler = new Handler(filename, errorBuff); reader.setProperty("http://xml.org/sax/properties/lexical-handler", handler); parser.parse(input, handler); if (handler.containsDTD && !handler.errorOrWarning) // valid { buff.append("VALID " + filename + "\n"); numValidFiles++; } else if (handler.containsDTD) // not valid { buff.append("NOT VALID " + filename + "\n"); buff.append(errorBuff.toString()); numInvalidFiles++; } else // no DOCTYPE to use for validation { buff.append("NO DOCTYPE DECLARATION " + filename + "\n"); numFilesMissingDoctype++; } } catch (Exception e) // Serious problem! { buff.append("NOT WELL-FORMED " + filename + ". " + e.getMessage() + "\n"); numMalformedFiles++; } finally { numXMLFiles++; } }
From source file:com.binomed.showtime.android.util.CineShowtimeFactory.java
private XMLReader getReader() throws Exception { if (reader == null) { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); reader = sp.getXMLReader(); }// w w w . j ava2 s. c o m return reader; }