List of usage examples for javax.xml.parsers SAXParser getXMLReader
public abstract org.xml.sax.XMLReader getXMLReader() throws SAXException;
From source file:com.entertailion.java.caster.RampClient.java
private void parseXml(Reader reader) { try {/*from w w w . ja v a2 s. c o m*/ InputSource inStream = new org.xml.sax.InputSource(); inStream.setCharacterStream(reader); SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); XMLReader xr = sp.getXMLReader(); AppHandler appHandler = new AppHandler(); xr.setContentHandler(appHandler); xr.parse(inStream); connectionServiceUrl = appHandler.getConnectionServiceUrl(); state = appHandler.getState(); protocol = appHandler.getProtocol(); } catch (Exception e) { Log.e(LOG_TAG, "parse device description", e); } }
From source file:com.commonsware.android.EMusicDownloader.SingleBook.java
private void getInfoFromXML() { final ProgressDialog dialog = ProgressDialog.show(this, "", getString(R.string.loading), true, true); setProgressBarIndeterminateVisibility(true); Thread t3 = new Thread() { public void run() { waiting(200);/*from ww w . j a va2 s . co m*/ try { URL url = new URL(urlAddress); SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); XMLReader xr = sp.getXMLReader(); XMLHandlerSingleBook myXMLHandler = new XMLHandlerSingleBook(); xr.setContentHandler(myXMLHandler); xr.parse(new InputSource(url.openStream())); genre = myXMLHandler.genre; publisher = myXMLHandler.publisher; narrator = myXMLHandler.narrator; edition = myXMLHandler.edition; artist = myXMLHandler.author; authorId = myXMLHandler.authorId; if (edition == null) { edition = ""; } date = myXMLHandler.releaseDate; rating = myXMLHandler.rating; sampleURL = myXMLHandler.sampleURL; imageURL = myXMLHandler.imageURL; statuscode = myXMLHandler.statuscode; if (statuscode != 200 && statuscode != 206) { throw new Exception(); } blurb = myXMLHandler.blurb; blurb = blurb.replace("<br> ", "<br>"); blurbSource = myXMLHandler.blurbSource; handlerSetContent.sendEmptyMessage(0); dialog.dismiss(); updateImage(); } catch (Exception e) { final Exception ef = e; nameTextView.post(new Runnable() { public void run() { nameTextView.setText(R.string.couldnt_get_book_info); } }); dialog.dismiss(); } handlerDoneLoading.sendEmptyMessage(0); } }; t3.start(); }
From source file:org.gege.caldavsyncadapter.caldav.CaldavFacade.java
private void parseXML(HttpResponse response, ContentHandler contentHandler) throws IOException, CaldavProtocolException { InputStream is = response.getEntity().getContent(); /*BufferedReader bReader = new BufferedReader(new InputStreamReader(is, "UTF-8")); String Content = "";// w ww . j av a2s . c o m String Line = bReader.readLine(); while (Line != null) { Content += Line; Line = bReader.readLine(); }*/ SAXParserFactory factory = SAXParserFactory.newInstance(); try { SAXParser parser = factory.newSAXParser(); XMLReader reader = parser.getXMLReader(); reader.setContentHandler(contentHandler); reader.parse(new InputSource(is)); } catch (ParserConfigurationException e) { throw new AssertionError("ParserConfigurationException " + e.getMessage()); } catch (IllegalStateException e) { throw new CaldavProtocolException(e.getMessage()); } catch (SAXException e) { throw new CaldavProtocolException(e.getMessage()); } }
From source file:com.centeractive.ws.builder.soap.XmlUtils.java
/** * XmlOptions configuration used in preventing XML Bomb * /*from w w w .j ava 2 s . c o m*/ * @return XmlOptions */ public static XmlOptions createDefaultXmlOptions() { XmlOptions xmlOptions; try { SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); SecurityManager securityManager = new SecurityManager(); // Default seems to be 64000! // TODO // securityManager.setEntityExpansionLimit( 16 ); saxParser.setProperty("http://apache.org/xml/properties/security-manager", securityManager); XMLReader xmlReader = saxParser.getXMLReader(); xmlOptions = new XmlOptions().setLoadUseXMLReader(xmlReader); } catch (Exception e) { xmlOptions = new XmlOptions(); log.error("Error creating XmlOptions; " + e.getMessage(), e); } return xmlOptions; }
From source file:nl.armatiek.xslweb.configuration.WebApp.java
public XsltExecutable tryTemplatesCache(String transformationPath, ErrorListener errorListener) throws Exception { String key = FilenameUtils.normalize(transformationPath); XsltExecutable templates = templatesCache.get(key); if (templates == null) { logger.info("Compiling and caching stylesheet \"" + transformationPath + "\" ..."); try {/*w w w.ja v a2s . c o m*/ SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); spf.setXIncludeAware(true); spf.setValidating(false); SAXParser parser = spf.newSAXParser(); XMLReader reader = parser.getXMLReader(); Source source = new SAXSource(reader, new InputSource(transformationPath)); XsltCompiler comp = processor.newXsltCompiler(); comp.setErrorListener(errorListener); templates = comp.compile(source); } catch (Exception e) { logger.error("Could not compile stylesheet \"" + transformationPath + "\"", e); throw e; } if (!developmentMode) { templatesCache.put(key, templates); } } return templates; }
From source file:com.larvalabs.svgandroid.SVGParser.java
private static SVG parse(InputStream in, Integer searchColor, Integer replaceColor, boolean whiteMode) throws SVGParseException { // Util.debug("Parsing SVG..."); SVGHandler svgHandler = null;/*from w w w . j a v a2 s . c o m*/ try { // long start = System.currentTimeMillis(); SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); XMLReader xr = sp.getXMLReader(); final Picture picture = new Picture(); svgHandler = new SVGHandler(picture); svgHandler.setColorSwap(searchColor, replaceColor); svgHandler.setWhiteMode(whiteMode); CopyInputStream cin = new CopyInputStream(in); IDHandler idHandler = new IDHandler(); xr.setContentHandler(idHandler); xr.parse(new InputSource(cin.getCopy())); svgHandler.idXml = idHandler.idXml; xr.setContentHandler(svgHandler); xr.parse(new InputSource(cin.getCopy())); // Util.debug("Parsing complete in " + (System.currentTimeMillis() - start) + " millis."); SVG result = new SVG(picture, svgHandler.bounds); // Skip bounds if it was an empty pic if (!Float.isInfinite(svgHandler.limits.top)) { result.setLimits(svgHandler.limits); } return result; } catch (Exception e) { //for (String s : handler.parsed.toString().replace(">", ">\n").split("\n")) // Log.d(TAG, "Parsed: " + s); throw new SVGParseException(e); } }
From source file:com.flipzu.flipzu.FlipInterface.java
private List<BroadcastDataSet> sendRequest(String url, String data) throws IOException { DefaultHttpClient hc = new DefaultHttpClient(); ResponseHandler<String> res = new ResponseHandler<String>() { public String handleResponse(final HttpResponse response) throws HttpResponseException, IOException { StatusLine statusLine = response.getStatusLine(); if (statusLine.getStatusCode() >= 300) { throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase()); }//from w w w . j av a 2 s . c o m HttpEntity entity = response.getEntity(); return entity == null ? null : EntityUtils.toString(entity, "UTF-8"); } }; HttpPost postMethod = new HttpPost(url); postMethod.getParams().setParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, Boolean.FALSE); if (data != null) { StringEntity tmp = null; try { tmp = new StringEntity(data, "UTF-8"); } catch (UnsupportedEncodingException e) { debug.logE(TAG, "sendRequest ERROR", e.getCause()); return null; } postMethod.setEntity(tmp); } String response = hc.execute(postMethod, res); SAXParserFactory spf = SAXParserFactory.newInstance(); try { SAXParser sp = spf.newSAXParser(); XMLReader xr = sp.getXMLReader(); TimelineHandler myTimelineHandler = new TimelineHandler(); xr.setContentHandler(myTimelineHandler); InputSource inputSource = new InputSource(); inputSource.setEncoding("UTF-8"); inputSource.setCharacterStream(new StringReader(response)); xr.parse(inputSource); List<BroadcastDataSet> parsedDataSet = myTimelineHandler.getParsedData(); return parsedDataSet; } catch (ParserConfigurationException e) { return null; } catch (SAXException e) { return null; } }
From source file:com.flipzu.flipzu.FlipInterface.java
public FlipUser getUser(String username, String token) throws IOException { String data = "username=" + username + "&access_token=" + token; String url = WSServer + "/api/get_user.xml"; debug.logV(TAG, "getUser for username " + username); DefaultHttpClient hc = new DefaultHttpClient(); ResponseHandler<String> res = new ResponseHandler<String>() { public String handleResponse(final HttpResponse response) throws HttpResponseException, IOException { StatusLine statusLine = response.getStatusLine(); if (statusLine.getStatusCode() >= 300) { throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase()); }// w w w.java 2 s .c om HttpEntity entity = response.getEntity(); return entity == null ? null : EntityUtils.toString(entity, "UTF-8"); } }; HttpPost postMethod = new HttpPost(url); postMethod.getParams().setParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, Boolean.FALSE); if (data != null) { StringEntity tmp = null; try { tmp = new StringEntity(data, "UTF-8"); } catch (UnsupportedEncodingException e) { debug.logE(TAG, "getUser ERROR", e.getCause()); return null; } postMethod.setEntity(tmp); } String response = hc.execute(postMethod, res); SAXParserFactory spf = SAXParserFactory.newInstance(); try { SAXParser sp = spf.newSAXParser(); XMLReader xr = sp.getXMLReader(); UserHandler myUserHandler = new UserHandler(); xr.setContentHandler(myUserHandler); InputSource inputSource = new InputSource(); inputSource.setEncoding("UTF-8"); inputSource.setCharacterStream(new StringReader(response)); xr.parse(inputSource); FlipUser parsedData = myUserHandler.getParsedData(); return parsedData; } catch (ParserConfigurationException e) { return null; } catch (SAXException e) { return null; } }
From source file:com.flipzu.flipzu.FlipInterface.java
private FlipUser setFollowUnfollow(String username, String token, boolean follow) throws IOException { String data = "username=" + username + "&access_token=" + token; String url;/*from w w w. j a va 2 s .co m*/ if (follow) { url = WSServer + "/api/set_follow.xml"; } else { url = WSServer + "/api/set_unfollow.xml"; } debug.logV(TAG, "setFollow for username " + username); DefaultHttpClient hc = new DefaultHttpClient(); ResponseHandler<String> res = new ResponseHandler<String>() { public String handleResponse(final HttpResponse response) throws HttpResponseException, IOException { StatusLine statusLine = response.getStatusLine(); if (statusLine.getStatusCode() >= 300) { throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase()); } HttpEntity entity = response.getEntity(); return entity == null ? null : EntityUtils.toString(entity, "UTF-8"); } }; HttpPost postMethod = new HttpPost(url); postMethod.getParams().setParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, Boolean.FALSE); if (data != null) { StringEntity tmp = null; try { tmp = new StringEntity(data, "UTF-8"); } catch (UnsupportedEncodingException e) { debug.logE(TAG, "getUser ERROR", e.getCause()); return null; } postMethod.setEntity(tmp); } String response = hc.execute(postMethod, res); SAXParserFactory spf = SAXParserFactory.newInstance(); try { SAXParser sp = spf.newSAXParser(); XMLReader xr = sp.getXMLReader(); UserHandler myUserHandler = new UserHandler(); xr.setContentHandler(myUserHandler); InputSource inputSource = new InputSource(); inputSource.setEncoding("UTF-8"); inputSource.setCharacterStream(new StringReader(response)); xr.parse(inputSource); FlipUser parsedData = myUserHandler.getParsedData(); return parsedData; } catch (ParserConfigurationException e) { return null; } catch (SAXException e) { return null; } }
From source file:com.xmobileapp.rockplayer.LastFmEventImporter.java
/********************************* * //from w w w . j a v a 2s. c o m * Get Artist Events * @throws SAXException * @throws ParserConfigurationException * *********************************/ public void getArtistEvents() throws SAXException, ParserConfigurationException { /* * Initialize Artist Cursor */ artistCursor = ((RockPlayer) context).contentResolver.query(MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI, ((RockPlayer) context).ARTIST_COLS, // we should minimize the number of columns null, // all albums null, // parameters to the previous parameter - which is null also null // sort order, SQLite-like ); /* * Declare & Initialize some vars */ String artistName = null; String artistNameFiltered = null; String artistConcertFileName = null; SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); SAXParser saxParser = saxParserFactory.newSAXParser(); XMLReader xmlReader = saxParser.getXMLReader(); XMLArtistEventHandler xmlHandler = new XMLArtistEventHandler(); xmlHandler.myLocation = this.myLocation; xmlReader.setContentHandler(xmlHandler); /* * Set Distance Limit */ xmlHandler.MAX_DISTANCE = this.concertRadius; /* * Loop through the artists */ artistCursor.moveToFirst(); for (int i = 0; i < artistCursor.getCount(); i++) { /* * Get artist name */ artistName = artistCursor.getString(artistCursor.getColumnIndex(MediaStore.Audio.Artists.ARTIST)); if (artistName.equals("<unknown>")) { artistCursor.moveToNext(); continue; } artistNameFiltered = filterString(artistName); artistConcertFileName = ((RockPlayer) context).FILEX_CONCERT_PATH + validateFileName(artistName); /* * UI feedback */ Bundle data = new Bundle(); data.putString("info", artistName); Message msg = new Message(); msg.setData(data); ((RockPlayer) context).analyseConcertInfoHandler.sendMessage(msg); /* * If we dont have yet or info is too old, update the concert info of this artist */ if (hasConcertInfo(artistName) == false || concertInfoNeedsUpdate(artistName) == true) { Log.i("INET", "Getting concert info from LastFM"); if (hasConcertInfo(artistName) == false) Log.i("INET", "Because there is no concert info yet"); if (concertInfoNeedsUpdate(artistName) == true) Log.i("INET", "Because Info is too old"); File artistConcertFile = new File(artistConcertFileName); if (!artistConcertFile.exists()) { try { artistConcertFile.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } URL lastFmApiRequest; try { lastFmApiRequest = new URL( this.LAST_FM_API_URL + "&artist=" + URLEncoder.encode(artistNameFiltered)); BufferedInputStream bufferedURLStream = new BufferedInputStream(lastFmApiRequest.openStream()); BufferedOutputStream bufferedFileWriter = new BufferedOutputStream( new FileOutputStream(artistConcertFile)); byte[] buf = new byte[1024]; int len; while ((len = bufferedURLStream.read(buf)) >= 0) bufferedFileWriter.write(buf, 0, len); bufferedURLStream.close(); bufferedFileWriter.close(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /* * get event list from cached XML files */ File artistConcertFile = new File(artistConcertFileName); if (artistConcertFile.exists() && artistConcertFile.length() > 0) { try { BufferedReader xmlFileReader = new BufferedReader( new InputStreamReader(new FileInputStream(artistConcertFile))); xmlHandler.resetList(); xmlHandler.artist = artistName; xmlReader.parse(new InputSource(xmlFileReader)); insertListInListByDate(xmlHandler.artistEventList); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } artistCursor.moveToNext(); } /* * Debug */ ArtistEvent artistEventDebug = null; if (!artistEventList.isEmpty()) { artistEventList.getFirst(); ListIterator<ArtistEvent> listIterator = artistEventList.listIterator(0); for (artistEventDebug = listIterator.next(); listIterator .hasNext() == true; artistEventDebug = listIterator.next()) { if (artistEventDebug != null) Log.i("DBG", artistEventDebug.date + " " + artistEventDebug.city); // Log.i("DBG", artistEventDebug.date+" "+artistEventDebug.city+" "+artistEventDebug.artist); else Log.i("DBG", "NULL"); } } /* * Update Adapter */ eventLinkedListAdapter = new EventLinkedListAdapter(context, R.layout.eventlist_item, artistEventList); if (eventLinkedListAdapter == null) Log.i("NULL", "NULL"); ((RockPlayer) context).updateEventListHandler.sendEmptyMessage(0); /* * Give feedback to the user */ Bundle data = new Bundle(); Message msg = new Message(); data.putString("info", "Done!"); msg.setData(data); ((RockPlayer) context).analyseConcertInfoHandler.sendMessage(msg); }