List of usage examples for javax.xml.parsers SAXParserFactory newSAXParser
public abstract SAXParser newSAXParser() throws ParserConfigurationException, SAXException;
From source file:edwardawebb.queueman.classes.NetFlix.java
/** * moveInQueue This will post to netflix with the new ddesired position. * Disc q only 1 based index//from w w w . j a va2 s . c o m * * @param disc * @param oldPosition * @param newPosition * @param queueType * @return subcode, statuscode, or httpresponse code (NF_ERROR_BAD_DEFAULT on exception) */ public int moveInQueue(Disc disc, int oldPosition, int newPosition, int queueType) { int result = NF_ERROR_BAD_DEFAULT; //getNewETag(queueType,newPosition); // 2 choirs, send request to netflix, and if successful update local q. OAuthConsumer postConsumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET, SignatureMethod.HMAC_SHA1); postConsumer.setTokenWithSecret(user.getAccessToken(), user.getAccessTokenSecret()); InputStream xml = null; URL url = null; try { url = new URL("http://api.netflix.com/users/" + user.getUserId() + "/queues/" + NetFlixQueue.queueTypeText[queueType]); Log.d("NetFlix", "Moving: " + url.toString()); HttpClient httpclient = new DefaultHttpClient(); // Your URL HttpPost httppost = new HttpPost(url.toString()); List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); // Your DATA nameValuePairs.add(new BasicNameValuePair("title_ref", disc.getId())); nameValuePairs.add(new BasicNameValuePair("position", String.valueOf(newPosition))); nameValuePairs.add(new BasicNameValuePair("etag", NetFlix.discQueue.getETag())); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); postConsumer.sign(httppost); HttpResponse response; response = httpclient.execute(httppost); xml = response.getEntity().getContent(); result = response.getStatusLine().getStatusCode(); /* BufferedReader in = new BufferedReader(new InputStreamReader(xml)); String linein = null; while ((linein = in.readLine()) != null) { Log.d("NetFlix", "Move Movie: " + linein); }*/ lastResponseMessage = "HTTP:" + response.getStatusLine().getStatusCode() + ", " + response.getStatusLine().getReasonPhrase(); if (result == 502) { HashMap<String, String> parameters = new HashMap<String, String>(); parameters.put("Queue Type:", "" + NetFlixQueue.queueTypeText[queueType]); parameters.put("HTTP Result:", "" + lastResponseMessage); parameters.put("User ID:", "" + user.getUserId()); parameters.put("Disc ID:", "" + disc.getId()); parameters.put("Positions:", "" + disc.getPosition() + " -> " + String.valueOf(newPosition)); parameters.put("URL:", "" + url); FlurryAgent.onEvent("MoveInQueue502", parameters); return result; } SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp; sp = spf.newSAXParser(); XMLReader xr = sp.getXMLReader(); MoveQueueHandler myHandler = new MoveQueueHandler(oldPosition); xr.setContentHandler(myHandler); xr.parse(new InputSource(xml)); // result=response.getStatusLine().getStatusCode(); result = myHandler.getSubCode(result); if (myHandler.getMessage() != null) { //we may have an error from netflix, check it lastResponseMessage += " NF: " + result + ", " + myHandler.getMessage(); } else { if (queueType == NetFlixQueue.QUEUE_TYPE_DISC && newPosition > (discQueue.getStartIndex() + discQueue.getPerPage())) { // a disc queue and we have moved past our current viewing // so we will remove it from viewing to prevnt confusion and removing mishpas (position will be lost) discQueue.delete(disc); result = MOVED_OUTSIDE_CURRENT_VIEW; } } } catch (IOException e) { reportError(e, lastResponseMessage); // Log.i("NetFlix", "IO Error connecting to NetFlix queue") } catch (OAuthMessageSignerException e) { reportError(e, lastResponseMessage); } catch (OAuthExpectationFailedException e) { reportError(e, lastResponseMessage); } catch (SAXException e) { reportError(e, lastResponseMessage); } catch (ParserConfigurationException e) { reportError(e, lastResponseMessage); } return result; }
From source file:edwardawebb.queueman.classes.NetFlix.java
/** * Post a rating to specificed title//from w ww . ja v a 2 s.c om * @param modifiedDisc * @return SubCode, httpResponseCode or NF_ERROR_BAD_DEFAULT on exception */ public int setRating(Disc modifiedDisc) { int result = NF_ERROR_BAD_DEFAULT; // 2 choirs, send request to netflix, and if successful update local q. OAuthConsumer postConsumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET, SignatureMethod.HMAC_SHA1); postConsumer.setTokenWithSecret(user.getAccessToken(), user.getAccessTokenSecret()); // postProvider.setOAuth10a(false); InputStream xml = null; try { // Construct data /* * Log.d("NetFlix", "title_ref=" + URLEncoder.encode(disc.getId(), * "UTF-8")); Log.d("NetFlix", "etag=" + * URLEncoder.encode(NetFlixQueue.getETag(), "UTF-8")); */ URL url = new URL("https://api.netflix.com/users/" + user.getUserId() + "/ratings/title/actual"); // Log.d("NetFlix", "@URL: " + url.toString()) HttpClient httpclient = new DefaultHttpClient(); // Your URL HttpPost httppost = new HttpPost(url.toString()); postConsumer.setTokenWithSecret(user.getAccessToken(), user.getAccessTokenSecret()); String rating = (modifiedDisc.getUserRating() == 0) ? NF_RATING_NO_INTEREST : String.valueOf(modifiedDisc.getUserRating().intValue()); List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); // Your DATA nameValuePairs.add(new BasicNameValuePair("title_ref", modifiedDisc.getId())); nameValuePairs.add(new BasicNameValuePair("rating", rating)); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); postConsumer.sign(httppost); HttpResponse response; response = httpclient.execute(httppost); xml = response.getEntity().getContent(); lastResponseMessage = response.getStatusLine().getStatusCode() + ": " + response.getStatusLine().getReasonPhrase(); result = response.getStatusLine().getStatusCode(); /* Log.d("NetFlix", "" + response.getEntity().getContentType().toString()); BufferedReader in = new BufferedReader(new InputStreamReader(xml)); String linein = null; while ((linein = in.readLine()) != null) { Log.d("NetFlix", "SetRating: " + linein); }*/ // Log.i("NetFlix", "Parsing XML Response") SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp; sp = spf.newSAXParser(); XMLReader xr = sp.getXMLReader(); QueueHandler myHandler = new QueueHandler(); xr.setContentHandler(myHandler); xr.parse(new InputSource(xml)); if (result == 201 || result == 422) { switch (modifiedDisc.getQueueType()) { //could be rating froms earch, recommends, instant, discm, or at home case NetFlixQueue.QUEUE_TYPE_RECOMMEND: ((Disc) recomemendedQueue.getDiscs().get(recomemendedQueue.indexOf(modifiedDisc))) .setUserRating(modifiedDisc.getUserRating()); break; case NetFlixQueue.QUEUE_TYPE_INSTANT: ((Disc) instantQueue.getDiscs().get(instantQueue.indexOf(modifiedDisc))) .setUserRating(modifiedDisc.getUserRating()); break; case NetFlixQueue.QUEUE_TYPE_DISC: ((Disc) discQueue.getDiscs().get(discQueue.indexOf(modifiedDisc))) .setUserRating(modifiedDisc.getUserRating()); break; } } lastNFResponseMessage = "NF: " + myHandler.getMessage(); result = myHandler.getSubCode(result); } catch (IOException e) { reportError(e, lastResponseMessage); // Log.i("NetFlix", "IO Error connecting to NetFlix queue") } catch (OAuthMessageSignerException e) { reportError(e, lastResponseMessage); } catch (OAuthExpectationFailedException e) { reportError(e, lastResponseMessage); } catch (ParserConfigurationException e) { // TODO Auto-generated catch block reportError(e, lastResponseMessage); } catch (SAXException e) { // TODO Auto-generated catch block reportError(e, lastResponseMessage); } return result; }
From source file:edwardawebb.queueman.classes.NetFlix.java
/** * /*from ww w .j a v a 2s . co m*/ * @param queueType * @param maxResults * @return HttpStatusCOde or NF_ERROR_BAD_DEFAULT for exceptions */ public int getQueue(int queueType, String maxResults) { Log.d("NetFlix", "getQueue()>>>"); URL QueueUrl = null; QueueHandler myQueueHandler = null; int result = NF_ERROR_BAD_DEFAULT; if (maxResults.equals(QueueMan.ALL_TITLES_STRING)) { maxResults = "500"; } // addtional info to return String expanders = "?expand=synopsis,formats&max_results=" + maxResults; InputStream xml = null; try { switch (queueType) { case NetFlixQueue.QUEUE_TYPE_INSTANT: if (!NetFlix.instantQueue.isEmpty() && instantQueue.isDownloaded()) return SUCCESS_FROM_CACHE; QueueUrl = new URL( "http://api.netflix.com/users/" + user.getUserId() + "/queues/instant" + expanders); myQueueHandler = new InstantQueueHandler(); break; case NetFlixQueue.QUEUE_TYPE_DISC: if (!NetFlix.discQueue.isEmpty() && discQueue.isDownloaded()) return SUCCESS_FROM_CACHE; QueueUrl = new URL( "http://api.netflix.com/users/" + user.getUserId() + "/queues/disc/available" + expanders); myQueueHandler = new DiscQueueHandler(); break; } //Log.d("NetFlix", "" + QueueUrl.toString()); setSignPost(user.getAccessToken(), user.getAccessTokenSecret()); HttpURLConnection request = (HttpURLConnection) QueueUrl.openConnection(); Log.d("NetFlix", "getQueue() | ready"); NetFlix.oaconsumer.sign(request); Log.d("NetFlix", "getQueue() | signed"); request.connect(); Log.d("NetFlix", "getQueue() | response"); lastResponseMessage = request.getResponseCode() + ": " + request.getResponseMessage(); result = request.getResponseCode(); xml = request.getInputStream(); /* BufferedReader in = new BufferedReader(new InputStreamReader(xml)); String linein = null; while ((linein = in.readLine()) != null) { Log.d("NetFlix", "GetQueue: " + linein); }*/ SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp; sp = spf.newSAXParser(); XMLReader xr = sp.getXMLReader(); xr.setContentHandler(myQueueHandler); Log.d("NetFlix", "getQueue() | parse ready"); xr.parse(new InputSource(xml)); Log.d("NetFlix", "getQueue() | parse complete"); result = myQueueHandler.getSubCode(result); if (myQueueHandler.getMessage() != null) { //we may have an error from netflix, check it lastResponseMessage += " NF:" + result + ", " + myQueueHandler.getMessage(); lastNFResponseMessage = myQueueHandler.getMessage(); } else { lastNFResponseMessage = "No Message"; } if (result == 200) { switch (queueType) { case NetFlixQueue.QUEUE_TYPE_INSTANT: instantQueue.setDownloaded(true); break; case NetFlixQueue.QUEUE_TYPE_DISC: discQueue.setDownloaded(true); break; } } else if (result == 502) { HashMap<String, String> parameters = new HashMap<String, String>(); parameters.put("Queue Type:", "" + NetFlixQueue.queueTypeText[queueType]); parameters.put("HTTP Result:", "" + lastResponseMessage); parameters.put("User ID:", "" + user.getUserId()); parameters.put("NF Message:", "" + myQueueHandler.getMessage()); FlurryAgent.onEvent("getQueue502", parameters); } } catch (ParserConfigurationException e) { reportError(e, lastResponseMessage); } catch (SAXException e) { reportError(e, lastResponseMessage); } catch (IOException e) { reportError(e, lastResponseMessage); // Log.i("NetFlix", "IO Error connecting to NetFlix queue") } catch (OAuthMessageSignerException e) { reportError(e, lastResponseMessage); // Log.i("NetFlix", "Unable to Sign request - token invalid") } catch (OAuthExpectationFailedException e) { reportError(e, lastResponseMessage); // Log.i("NetFlix", "Expectation failed") } Log.d("NetFlix", "getQueue()<<<"); return result; }
From source file:com.silverpeas.importExport.control.ImportExport.java
/** * Mthode retournant l'arbre des objets mapps sur le fichier xml pass en paramtre. * * @param xmlFileName le fichier xml interprt par Castor * @return Un objet SilverPeasExchangeType contenant le mapping d'un fichier XML Castor * @throws ImportExportException/*from w w w. j a v a 2s . c o m*/ */ SilverPeasExchangeType loadSilverpeasExchange(String xmlFileName) throws ImportExportException { SilverTrace.debug("importExport", "ImportExportSessionController.loadSilverpeasExchange", "root.MSG_GEN_ENTER_METHOD", "xmlFileName = " + xmlFileName); try { InputSource xmlInputSource = new InputSource(xmlFileName); String xsdPublicId = settings.getString("xsdPublicId"); String xsdSystemId = settings.getString("xsdDefaultSystemId"); // Load and parse default XML schema for import/export SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema", "com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory", null); Schema schema = schemaFactory.newSchema(new StreamSource(xsdSystemId)); // Create an XML parser for loading XML import file SAXParserFactory factory = SAXParserFactory .newInstance("com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl", null); factory.setValidating(false); factory.setNamespaceAware(true); factory.setSchema(schema); SAXParser parser = factory.newSAXParser(); // First try to determine to load the XML file using the default // XML-Schema ImportExportErrorHandler errorHandler = new ImportExportErrorHandler(); XMLReader xmlReader = parser.getXMLReader(); xmlReader.setErrorHandler(errorHandler); try { xmlReader.parse(xmlInputSource); } catch (SAXException ex) { SilverTrace.debug("importExport", "ImportExportSessionController.loadSilverpeasExchange", "root.MSG_GEN_PARAM_VALUE", (new StringBuilder("XML File ")).append(xmlFileName) .append(" is not valid according to default schema").toString()); // If case the default schema is not the one specified by the // XML import file, try to get the right XML-schema and // namespace (this is done by parsing without validation) ImportExportNamespaceHandler nsHandler = new ImportExportNamespaceHandler(); factory.setSchema(null); parser = factory.newSAXParser(); xmlReader = parser.getXMLReader(); xmlReader.setContentHandler(nsHandler); xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true); xmlReader.parse(xmlInputSource); // If OK, extract the name and location of the schema String nsSpec = nsHandler.getNsSpec(); if (nsSpec == null || xsdPublicId.equals(nsSpec)) { throw ex; } String nsVersion = extractUriNameIndex(nsSpec); if (nsVersion.length() == 0) { throw ex; } String altXsdSystemId = settings.getStringWithParam("xsdSystemId", nsVersion); if ((altXsdSystemId == null) || (altXsdSystemId.equals(xsdSystemId))) { throw ex; } SilverTrace.debug("importExport", "ImportExportSessionController.loadSilverpeasExchange", "root.MSG_GEN_PARAM_VALUE", (new StringBuilder("Trying again using schema specification located at ")) .append(altXsdSystemId).toString()); // Try again to load, parse and validate the XML import file, // using the new schema specification schema = schemaFactory.newSchema(new StreamSource(altXsdSystemId)); factory.setSchema(schema); parser = factory.newSAXParser(); xmlReader = parser.getXMLReader(); xmlReader.setErrorHandler(errorHandler); xmlReader.parse(xmlInputSource); } SilverTrace.debug("importExport", "ImportExportSessionController.loadSilverpeasExchange", "root.MSG_GEN_PARAM_VALUE", "XML Validation complete"); // Mapping file for Castor String mappingDir = settings.getString("mappingDir"); String mappingFileName = settings.getString("importExportMapping"); String mappingFile = mappingDir + mappingFileName; Mapping mapping = new Mapping(); // Load mapping and instantiate a Unmarshaller mapping.loadMapping(mappingFile); Unmarshaller unmar = new Unmarshaller(SilverPeasExchangeType.class); unmar.setMapping(mapping); unmar.setValidation(false); // Unmarshall the process model SilverPeasExchangeType silverpeasExchange = (SilverPeasExchangeType) unmar.unmarshal(xmlInputSource); SilverTrace.debug("importExport", "ImportExportSessionController.loadSilverpeasExchange", "root.MSG_GEN_PARAM_VALUE", "Unmarshalling complete"); return silverpeasExchange; } catch (MappingException me) { throw new ImportExportException("ImportExport.loadSilverpeasExchange", "importExport.EX_LOADING_XML_MAPPING_FAILED", "XML Filename " + xmlFileName + ": " + me.getLocalizedMessage(), me); } catch (MarshalException me) { throw new ImportExportException("ImportExport.loadSilverpeasExchange", "importExport.EX_UNMARSHALLING_FAILED", "XML Filename " + xmlFileName + ": " + me.getLocalizedMessage(), me); } catch (ValidationException ve) { throw new ImportExportException("ImportExport.loadSilverpeasExchange", "importExport.EX_PARSING_FAILED", "XML Filename " + xmlFileName + ": " + ve.getLocalizedMessage(), ve); } catch (IOException ioe) { throw new ImportExportException("ImportExport.loadSilverpeasExchange", "importExport.EX_LOADING_XML_MAPPING_FAILED", "XML Filename " + xmlFileName + ": " + ioe.getLocalizedMessage(), ioe); } catch (ParserConfigurationException ex) { throw new ImportExportException("ImportExport.loadSilverpeasExchange", "importExport.EX_PARSING_FAILED", "XML Filename " + xmlFileName + ": " + ex.getLocalizedMessage(), ex); } catch (SAXNotRecognizedException snre) { throw new ImportExportException("ImportExport.loadSilverpeasExchange", "importExport.EX_PARSING_FAILED", "XML Filename " + xmlFileName + ": " + snre.getLocalizedMessage(), snre); } catch (SAXNotSupportedException snse) { throw new ImportExportException("ImportExport.loadSilverpeasExchange", "importExport.EX_PARSING_FAILED", "XML Filename " + xmlFileName + ": " + snse.getLocalizedMessage(), snse); } catch (SAXException se) { throw new ImportExportException("ImportExport.loadSilverpeasExchange", "importExport.EX_PARSING_FAILED", "XML Filename " + xmlFileName + ": " + se.getLocalizedMessage(), se); } }
From source file:com.xmobileapp.rockplayer.LastFmAlbumArtImporter.java
/********************************* * //from w ww. j a va2s. c o m * Get AlbumArt * @throws SAXException * @throws ParserConfigurationException * *********************************/ public void getAlbumArt() throws SAXException, ParserConfigurationException { /* * Initialize Album Cursor */ albumCursor = ((RockPlayer) context).contentResolver.query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, ((RockPlayer) context).ALBUM_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 albumName = null; SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); SAXParser saxParser = saxParserFactory.newSAXParser(); XMLReader xmlReader = saxParser.getXMLReader(); XMLAlbumArtHandler xmlHandler = new XMLAlbumArtHandler(); // XMLGoogleAlbumArtHandler xmlGoogleHandler = new XMLGoogleAlbumArtHandler(); /* * Give feedback to the user */ Bundle data = new Bundle(); Message msg = new Message(); data.putString("info", "Looking for missing art..."); msg.setData(data); ((RockPlayer) context).getAlbumArtHandler.sendMessage(msg); /* * Loop through the albums */ albumCursor.moveToFirst(); for (int i = 0; i < albumCursor.getCount(); i++) { System.gc(); /* * Get Album Details */ artistName = albumCursor.getString(albumCursor.getColumnIndex(MediaStore.Audio.Albums.ARTIST)); albumName = albumCursor.getString(albumCursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM)); /* * If no Art is available fetch it */ if (getAlbumArtPath(artistName, albumName) == null) { Log.i("LastFM", "Album with no Art " + albumName); try { if ((artistName.equals("<unknown>") && albumName.equals("<unknown>"))) { Log.i("ALBUMART", "Unknown album"); albumCursor.moveToNext(); continue; } } catch (Exception e) { Log.i("ALBUMART", "Null album or artist"); albumCursor.moveToNext(); continue; } /* * Give feedback to the user */ data = new Bundle(); msg = new Message(); data.putString("info", albumName); msg.setData(data); ((RockPlayer) context).getAlbumArtHandler.sendMessage(msg); String albumArtURL = null; try { /* * Get album URL from Last.FM */ String artistNameFiltered = filterString(artistName); String albumNameFiltered = filterString(albumName); if (USE_GOOGLE_IMAGES) { // xmlReader.setContentHandler(xmlGoogleHandler); URL googleImagesRequest = new URL( this.GOOGLE_IMAGES_SEARCH_URL + URLEncoder.encode(artistNameFiltered) + "+" + URLEncoder.encode(albumNameFiltered)); // Log.i("GOOGLEIMAGES", googleImagesRequest.toString()); // DefaultHttpClientConnection httpCon = createGoogleImageConnection( // googleImagesRequest.toString()); /* * retreive URL */ BasicHttpParams params = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(params, 10000); DefaultHttpClient httpClient = new DefaultHttpClient(); // Get cookies from the login page (not the address same of the form post) HttpGet httpGet = new HttpGet(googleImagesRequest.toString()); HttpResponse response; try { /* * Get the page */ response = httpClient.execute(httpGet); HttpEntity entity = response.getEntity(); BufferedReader in = new BufferedReader(new InputStreamReader(entity.getContent())); /* * Parse 1st existing image on the result page */ String line; int idxStart = 0; int idxStop; do { line = in.readLine(); if (line != null) { // Log.i("GIMAGES", line); if (line.startsWith("<table")) { boolean found = false; int tries = 0; while (!found) { tries++; if (tries > 12) break; idxStart = line.indexOf("<a href=", idxStart); if (idxStart == -1) { line = in.readLine(); if (line == null) break; continue; } idxStart = line.indexOf("http://", idxStart); idxStop = line.indexOf("&imgrefurl=", idxStart); albumArtURL = line.substring(idxStart, idxStop); Log.i("GIMAGE", line.substring(idxStart, idxStop)); try { //URL albumArt = new URL(URLEncoder.encode(albumArtURL)); // URL albumArt = new URL(albumArtURL); // InputStream albumArtURLStream = albumArt.openStream(); // albumArtURLStream.close(); if (albumArtURL != null) { if (createAlbumArt(artistName, albumName, albumArtURL) == null) { albumArtURL = null; found = false; Log.i("GIMAGES", "createAlbumArt FAIL"); } else { found = true; Log.i("GIMAGES", "createAlbumArt WIN"); } } else { albumArtURL = null; found = false; Log.i("GIMAGES", "albumArt URL FAIL!"); } } catch (Exception e) { e.printStackTrace(); albumArtURL = null; found = false; } } break; } } } while (line != null); // xmlReader.parse(new InputSource(in)); entity.consumeContent(); // for(int j = 0; j < xmlGoogleHandler.MAX_IMAGES; j++){ // if(xmlGoogleHandler.albumArtUrl[j] != null){ // albumArtURL = xmlGoogleHandler.albumArtUrl[j]; // break; // } // } /* * No luck with the duck */ // if(albumArtURL == null){ // Log.i("GOOGLEIMAGES", "Absolutely no luck"); // // mark this as a problematic album... // // so we dont refetch it all the time // createSmallAlbumArt(artistName, albumName, false); // albumCursor.moveToNext(); // continue; // } else { // Log.i("GOOGLEIMAGES", albumArtURL); // } /* * Clear up the Handler */ // xmlGoogleHandler.clear(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } // // /* // * No Album Art available // * 1. try going by the album name // * 2. get some artist pic and thats it // */ // if(albumArtURL == null){ // Log.i("LASTFM", "Could not get album art immediately"); // Log.i("LASTFM", "Trying sole album search"); // // albumArtURL = getAlbumArtByAlbumName(albumName, artistName); // if(albumArtURL == null){ // Log.i("LASTFM", "Trying to get artist Art"); // albumArtURL = getArtistArt(artistName); // } // /* // * No luck with the duck // */ // if(albumArtURL == null){ // Log.i("LASTFM", "Absolutely no luck"); // // mark this as a problematic album... // // so we dont refetch it all the time // createSmallAlbumArt(artistName, albumName, false); // albumCursor.moveToNext(); // continue; // } // } } /* * If google images failed try last.fm */ if (albumArtURL == null) { xmlReader.setContentHandler(xmlHandler); URL lastFmApiRequest = new URL( this.LAST_FM_ALBUM_GETINFO_URL + "&artist=" + URLEncoder.encode(artistNameFiltered) + "&album=" + URLEncoder.encode(albumNameFiltered)); try { BufferedReader in = new BufferedReader( new InputStreamReader(lastFmApiRequest.openStream())); xmlReader.parse(new InputSource(in)); if (xmlHandler.xlargeAlbumArt != null) { albumArtURL = xmlHandler.xlargeAlbumArt; } else if (xmlHandler.largeAlbumArt != null) { albumArtURL = xmlHandler.largeAlbumArt; } else if (xmlHandler.mediumAlbumArt != null) { albumArtURL = xmlHandler.mediumAlbumArt; } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } /* * No Album Art available * 1. try going by the album name * 2. get some artist pic and thats it */ if (albumArtURL == null) { Log.i("LASTFM", "Could not get album art immediately"); Log.i("LASTFM", "Trying sole album search"); albumArtURL = getAlbumArtByAlbumName(albumName, artistName); if (albumArtURL == null) { Log.i("LASTFM", "Trying to get artist Art"); albumArtURL = getArtistArt(artistName); } /* * No luck with the duck */ if (albumArtURL == null) { Log.i("LASTFM", "Absolutely no luck"); // mark this as a problematic album... // so we dont refetch it all the time createSmallAlbumArt(artistName, albumName, false); albumCursor.moveToNext(); continue; } } /* only reaches here if not FAIL */ createAlbumArt(artistName, albumName, albumArtURL); } /* * reset xml handler */ xmlHandler.smallAlbumArt = null; xmlHandler.mediumAlbumArt = null; xmlHandler.largeAlbumArt = null; xmlHandler.xlargeAlbumArt = null; } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NullPointerException e) { e.printStackTrace(); } } /* * Create small album art */ createSmallAlbumArt(artistName, albumName, true); /* * Give feedback to the user */ //Bundle data = new Bundle(); //Message msg = new Message(); //data.putString("info", "Creating Thumbnail"); //msg.setData(data); //((Filex) context).getAlbumArtHandler.sendMessage(msg); albumCursor.moveToNext(); } /* * Give feedback to the user */ data = new Bundle(); msg = new Message(); data.putString("info", "Done!"); msg.setData(data); ((RockPlayer) context).getAlbumArtHandler.sendMessage(msg); /* * Set the last import date on preferences */ // SharedPreferences settings = ((Filex) this.context).getSharedPreferences(((Filex) this.context).PREFS_NAME, 0); // Editor editor = settings.edit(); // editor.putLong("artImportDate", System.currentTimeMillis()); // editor.commit(); RockOnPreferenceManager settings = new RockOnPreferenceManager( ((RockPlayer) context).FILEX_PREFERENCES_PATH); settings.putLong("artImportDate", System.currentTimeMillis()); //settings. // long lastAlbumArtImportDate = settings.getLong("artImportDate", 0); }
From source file:edwardawebb.queueman.classes.NetFlix.java
/** * /*from w ww .j av a 2 s . c om*/ * @param disc * @param queueType * @return SubCode, httpResponseCode or NF_ERROR_BAD_DEFAULT on exception */ public int addToQueue(Disc disc, int queueType) { lastResponseMessage = ""; lastNFResponseMessage = ""; int result = NF_ERROR_BAD_DEFAULT; // 2 choirs, send request to netflix, and if successful update local q. OAuthConsumer postConsumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET, SignatureMethod.HMAC_SHA1); postConsumer.setTokenWithSecret(user.getAccessToken(), user.getAccessTokenSecret()); OAuthProvider postProvider = new DefaultOAuthProvider(postConsumer, REQUEST_TOKEN_ENDPOINT_URL, ACCESS_TOKEN_ENDPOINT_URL, AUTHORIZE_WEBSITE_URL); String expanders = "?expand=synopsis,formats"; InputStream xml = null; NetFlixQueue queue = null; URL QueueUrl = null; String eTag = null; URL url = null; try { // Construct data int queueSize = 0; switch (queueType) { case NetFlixQueue.QUEUE_TYPE_DISC: queueSize = NetFlix.discQueue.getTotalTitles(); if (queueSize == 0) getNewETag(queueType); // @ TODO This is for issue 41 if (disc.getPosition() > NetFlix.discQueue.getTotalTitles()) { disc.setPosition(NetFlix.discQueue.getTotalTitles()); } // @ TODO Move this to instnat once it works eTag = NetFlix.discQueue.getETag(); url = new URL("https://api.netflix.com/users/" + user.getUserId() + "/queues/disc" + expanders); break; case NetFlixQueue.QUEUE_TYPE_INSTANT: eTag = NetFlix.instantQueue.getETag(); url = new URL("https://api.netflix.com/users/" + user.getUserId() + "/queues/instant" + expanders); break; } // Log.d("NetFlix", "@URL: " + url.toString()) HttpClient httpclient = new DefaultHttpClient(); // Your URL HttpPost httppost = new HttpPost(url.toString()); postConsumer.setTokenWithSecret(user.getAccessToken(), user.getAccessTokenSecret()); List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); // Your DATA nameValuePairs.add(new BasicNameValuePair("title_ref", disc.getId())); nameValuePairs.add(new BasicNameValuePair("position", "" + disc.getPosition())); nameValuePairs.add(new BasicNameValuePair("etag", eTag)); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); postConsumer.sign(httppost); HttpResponse response; response = httpclient.execute(httppost); result = response.getStatusLine().getStatusCode(); xml = response.getEntity().getContent(); lastResponseMessage = response.getStatusLine().getStatusCode() + ": " + response.getStatusLine().getReasonPhrase(); /* Log.d("NetFlix", "" + response.getEntity().getContentType().toString()); BufferedReader in = new BufferedReader(new InputStreamReader(xml)); String linein = null; while ((linein = in.readLine()) != null) { Log.d("NetFlix", "AddMovie: " + linein); } if(true) return 200; //^ avoids the parser since we consumed xml for debug */ // Log.i("NetFlix", "Parsing XML Response") SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp; sp = spf.newSAXParser(); XMLReader xr = sp.getXMLReader(); QueueHandler myHandler = null; switch (queueType) { case NetFlixQueue.QUEUE_TYPE_DISC: myHandler = (AddDiscQueueHandler) new AddDiscQueueHandler(); break; case NetFlixQueue.QUEUE_TYPE_INSTANT: myHandler = (AddInstantQueueHandler) new AddInstantQueueHandler(); break; } xr.setContentHandler(myHandler); xr.parse(new InputSource(xml)); result = myHandler.getSubCode(result); if (myHandler.getMessage() != null) { //we may have an error from netflix, check it lastResponseMessage += " NF:" + result + ", " + myHandler.getMessage(); lastNFResponseMessage = myHandler.getMessage(); } else { lastNFResponseMessage = "No Message"; } //extra code to catch 502 } catch (IOException e) { reportError(e, lastResponseMessage); // Log.i("NetFlix", "IO Error connecting to NetFlix queue") } catch (OAuthMessageSignerException e) { reportError(e, lastResponseMessage); } catch (OAuthExpectationFailedException e) { reportError(e, lastResponseMessage); } catch (ParserConfigurationException e) { reportError(e, lastResponseMessage); } catch (SAXException e) { reportError(e, lastResponseMessage); } finally { if (result == 502) { HashMap<String, String> parameters = new HashMap<String, String>(); parameters.put("Queue Type:", "" + NetFlixQueue.queueTypeText[queueType]); parameters.put("HTTP Result:", "" + lastResponseMessage); parameters.put("User ID:", "" + user.getUserId()); parameters.put("Disc ID:", "" + disc.getId()); parameters.put("Position:", "" + disc.getPosition()); parameters.put("Availability:", "" + disc.isAvailable() + ", " + disc.getAvailibilityText()); parameters.put("URL:", "" + url); FlurryAgent.onEvent("AddToQueue502", parameters); } } return result; }
From source file:com.g2inc.scap.library.domain.SCAPContentManager.java
public List<File> getOvalFiles(File dir) { ArrayList<File> ovalFileList = new ArrayList<File>(); // get list of all xml files in dir File[] xmlFiles = dir.listFiles(new FilenameFilter() { @Override/* w ww. j av a2 s . co m*/ public boolean accept(File dir, String name) { return name.toLowerCase().endsWith(".xml"); } }); if (xmlFiles != null && xmlFiles.length > 0) { try { SAXParserFactory saxfactory = SAXParserFactory.newInstance(); saxfactory.setValidating(false); saxfactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); saxfactory.setFeature("http://xml.org/sax/features/validation", false); for (int i = 0; i < xmlFiles.length; i++) { SAXParser saxparser = saxfactory.newSAXParser(); XMLReader xmlReader = saxparser.getXMLReader(); OvalParser ovalParser = new OvalParser(); xmlReader.setContentHandler(ovalParser); FileInputStream inStream = new FileInputStream(xmlFiles[i]); xmlReader.parse(new InputSource(inStream)); if (ovalParser.isOval()) { ovalFileList.add(xmlFiles[i]); } } } catch (Exception e) { throw new IllegalStateException( "Caught an error trying list oval files in directory " + dir.getAbsolutePath(), e); } } return ovalFileList; }
From source file:self.philbrown.droidQuery.Ajax.java
protected TaskResponse doInBackground(Void... arg0) { if (this.isCancelled) return null; //if synchronous, block on the background thread until ready. Then call beforeSend, etc, before resuming. if (!beforeSendIsAsync) { try {/*from w w w .j a v a 2s. co m*/ mutex.acquire(); } catch (InterruptedException e) { Log.w("AjaxTask", "Synchronization Error. Running Task Async"); } final Thread asyncThread = Thread.currentThread(); isLocked = true; mHandler.post(new Runnable() { @Override public void run() { if (options.beforeSend() != null) { if (options.context() != null) options.beforeSend().invoke($.with(options.context()), options); else options.beforeSend().invoke(null, options); } if (options.isAborted()) { cancel(true); return; } if (options.global()) { synchronized (globalTasks) { if (globalTasks.isEmpty()) { $.ajaxStart(); } globalTasks.add(Ajax.this); } $.ajaxSend(); } else { synchronized (localTasks) { localTasks.add(Ajax.this); } } isLocked = false; LockSupport.unpark(asyncThread); } }); if (isLocked) LockSupport.park(); } //here is where to use the mutex //handle cached responses Object cachedResponse = AjaxCache.sharedCache().getCachedResponse(options); //handle ajax caching option if (cachedResponse != null && options.cache()) { Success s = new Success(cachedResponse); s.reason = "cached response"; s.allHeaders = null; return s; } if (connection == null) { try { String type = options.type(); URL url = new URL(options.url()); if (type == null) { type = "GET"; } if (type.equalsIgnoreCase("CUSTOM")) { try { connection = options.customConnection(); } catch (Exception e) { connection = null; } if (connection == null) { Log.w("droidQuery.ajax", "CUSTOM type set, but AjaxOptions.customRequest is invalid. Defaulting to GET."); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); } } else { connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(type); if (type.equalsIgnoreCase("POST") || type.equalsIgnoreCase("PUT")) { connection.setDoOutput(true); } } } catch (Throwable t) { if (options.debug()) t.printStackTrace(); Error e = new Error(null); AjaxError error = new AjaxError(); error.connection = connection; error.options = options; e.status = 0; e.reason = "Bad Configuration"; error.status = e.status; error.reason = e.reason; error.response = e.response; e.allHeaders = new Headers(); e.error = error; return e; } } Map<String, Object> args = new HashMap<String, Object>(); args.put("options", options); args.put("request", null); args.put("connection", connection); EventCenter.trigger("ajaxPrefilter", args, null); if (options.headers() != null) { if (options.headers().authorization() != null) { options.headers() .authorization(options.headers().authorization() + " " + options.getEncodedCredentials()); } else if (options.username() != null) { //guessing that authentication is basic options.headers().authorization("Basic " + options.getEncodedCredentials()); } for (Entry<String, String> entry : options.headers().map().entrySet()) { connection.setRequestProperty(entry.getKey(), entry.getValue()); } } if (options.data() != null) { try { OutputStream os = connection.getOutputStream(); os.write(options.data().toString().getBytes()); os.close(); } catch (Throwable t) { Log.w("Ajax", "Could not post data"); } } if (options.timeout() != 0) { connection.setConnectTimeout(options.timeout()); connection.setReadTimeout(options.timeout()); } if (options.trustedCertificate() != null) { Certificate ca = options.trustedCertificate(); String keyStoreType = KeyStore.getDefaultType(); KeyStore keyStore = null; try { keyStore = KeyStore.getInstance(keyStoreType); keyStore.load(null, null); keyStore.setCertificateEntry("ca", ca); } catch (KeyStoreException e) { if (options.debug()) e.printStackTrace(); } catch (NoSuchAlgorithmException e) { if (options.debug()) e.printStackTrace(); } catch (CertificateException e) { if (options.debug()) e.printStackTrace(); } catch (IOException e) { if (options.debug()) e.printStackTrace(); } if (keyStore == null) { Log.w("Ajax", "Could not configure trusted certificate"); } else { try { //Create a TrustManager that trusts the CAs in our KeyStore String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm(); TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm); tmf.init(keyStore); //Create an SSLContext that uses our TrustManager SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(null, tmf.getTrustManagers(), null); ((HttpsURLConnection) connection).setSSLSocketFactory(sslContext.getSocketFactory()); } catch (KeyManagementException e) { if (options.debug()) e.printStackTrace(); } catch (NoSuchAlgorithmException e) { if (options.debug()) e.printStackTrace(); } catch (KeyStoreException e) { if (options.debug()) e.printStackTrace(); } } } try { if (options.cookies() != null) { CookieManager cm = new CookieManager(); CookieStore cookies = cm.getCookieStore(); URI uri = URI.create(options.url()); for (Entry<String, String> entry : options.cookies().entrySet()) { HttpCookie cookie = new HttpCookie(entry.getKey(), entry.getValue()); cookies.add(uri, cookie); } connection.setRequestProperty("Cookie", TextUtils.join(",", cookies.getCookies())); } connection.connect(); final int statusCode = connection.getResponseCode(); final String message = connection.getResponseMessage(); if (options.dataFilter() != null) { if (options.context() != null) options.dataFilter().invoke($.with(options.context()), connection, options.dataType()); else options.dataFilter().invoke(null, connection, options.dataType()); } final Function function = options.statusCode().get(statusCode); if (function != null) { mHandler.post(new Runnable() { @Override public void run() { if (options.context() != null) function.invoke($.with(options.context()), statusCode, options.clone()); else function.invoke(null, statusCode, options.clone()); } }); } //handle dataType String dataType = options.dataType(); if (dataType == null) dataType = "text"; if (options.debug()) Log.i("Ajax", "dataType = " + dataType); Object parsedResponse = null; InputStream stream = null; try { if (dataType.equalsIgnoreCase("text") || dataType.equalsIgnoreCase("html")) { if (options.debug()) Log.i("Ajax", "parsing text"); stream = AjaxUtil.getInputStream(connection); parsedResponse = parseText(stream); } else if (dataType.equalsIgnoreCase("xml")) { if (options.debug()) Log.i("Ajax", "parsing xml"); if (options.customXMLParser() != null) { stream = AjaxUtil.getInputStream(connection); if (options.SAXContentHandler() != null) options.customXMLParser().parse(stream, options.SAXContentHandler()); else options.customXMLParser().parse(stream, new DefaultHandler()); parsedResponse = "Response handled by custom SAX parser"; } else if (options.SAXContentHandler() != null) { stream = AjaxUtil.getInputStream(connection); SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setFeature("http://xml.org/sax/features/namespaces", false); factory.setFeature("http://xml.org/sax/features/namespace-prefixes", true); SAXParser parser = factory.newSAXParser(); XMLReader reader = parser.getXMLReader(); reader.setContentHandler(options.SAXContentHandler()); reader.parse(new InputSource(stream)); parsedResponse = "Response handled by custom SAX content handler"; } else { parsedResponse = parseXML(connection); } } else if (dataType.equalsIgnoreCase("json")) { if (options.debug()) Log.i("Ajax", "parsing json"); parsedResponse = parseJSON(connection); } else if (dataType.equalsIgnoreCase("script")) { if (options.debug()) Log.i("Ajax", "parsing script"); parsedResponse = parseScript(connection); } else if (dataType.equalsIgnoreCase("image")) { if (options.debug()) Log.i("Ajax", "parsing image"); stream = AjaxUtil.getInputStream(connection); parsedResponse = parseImage(stream); } else if (dataType.equalsIgnoreCase("raw")) { if (options.debug()) Log.i("Ajax", "parsing raw data"); parsedResponse = parseRawContent(connection); } } catch (ClientProtocolException cpe) { if (options.debug()) cpe.printStackTrace(); Error e = new Error(parsedResponse); AjaxError error = new AjaxError(); error.connection = connection; error.options = options; e.status = statusCode; e.reason = message; error.status = e.status; error.reason = e.reason; error.response = e.response; e.allHeaders = Headers.createHeaders(connection.getHeaderFields()); e.error = error; return e; } catch (Exception ioe) { if (options.debug()) ioe.printStackTrace(); Error e = new Error(parsedResponse); AjaxError error = new AjaxError(); error.connection = connection; error.options = options; e.status = statusCode; e.reason = message; error.status = e.status; error.reason = e.reason; error.response = e.response; e.allHeaders = Headers.createHeaders(connection.getHeaderFields()); e.error = error; return e; } finally { connection.disconnect(); try { if (stream != null) { stream.close(); } } catch (IOException e) { } } if (statusCode >= 300) { //an error occurred Error e = new Error(parsedResponse); Log.e("Ajax Test", parsedResponse.toString()); //AjaxError error = new AjaxError(); //error.request = request; //error.options = options; e.status = e.status; e.reason = e.reason; //error.status = e.status; //error.reason = e.reason; //error.response = e.response; e.allHeaders = Headers.createHeaders(connection.getHeaderFields()); //e.error = error; if (options.debug()) Log.i("Ajax", "Error " + e.status + ": " + e.reason); return e; } else { //handle ajax ifModified option List<String> lastModifiedHeaders = connection.getHeaderFields().get("last-modified"); if (lastModifiedHeaders.size() >= 1) { try { String h = lastModifiedHeaders.get(0); SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US); Date lastModified = format.parse(h); if (options.ifModified() && lastModified != null) { Date lastModifiedDate; synchronized (lastModifiedUrls) { lastModifiedDate = lastModifiedUrls.get(options.url()); } if (lastModifiedDate != null && lastModifiedDate.compareTo(lastModified) == 0) { //request response has not been modified. //Causes an error instead of a success. Error e = new Error(parsedResponse); AjaxError error = new AjaxError(); error.connection = connection; error.options = options; e.status = e.status; e.reason = e.reason; error.status = e.status; error.reason = e.reason; error.response = e.response; e.allHeaders = Headers.createHeaders(connection.getHeaderFields()); e.error = error; Function func = options.statusCode().get(304); if (func != null) { if (options.context() != null) func.invoke($.with(options.context())); else func.invoke(null); } return e; } else { synchronized (lastModifiedUrls) { lastModifiedUrls.put(options.url(), lastModified); } } } } catch (Throwable t) { Log.e("Ajax", "Could not parse Last-Modified Header", t); } } //Now handle a successful request Success s = new Success(parsedResponse); s.reason = message; s.allHeaders = Headers.createHeaders(connection.getHeaderFields()); return s; } } catch (Throwable t) { if (options.debug()) t.printStackTrace(); if (t instanceof java.net.SocketTimeoutException) { Error e = new Error(null); AjaxError error = new AjaxError(); error.connection = connection; error.options = options; error.response = e.response; e.status = 0; String reason = t.getMessage(); if (reason == null) reason = "Socket Timeout"; e.reason = reason; error.status = e.status; error.reason = e.reason; if (connection != null) e.allHeaders = Headers.createHeaders(connection.getHeaderFields()); else e.allHeaders = new Headers(); e.error = error; return e; } return null; } }
From source file:org.openbmap.soapclient.CheckServerTask.java
@Override protected Object[] doInBackground(final String... params) { try {//from w w w. j a v a2s. c om final Object[] result = new Object[2]; result[0] = ServerAnswer.UNKNOWN_ERROR; result[1] = "Uninitialized"; //check whether we have a connection to openbmap.org if (!isOnline()) { // if not, check whether connecting, if so wait Log.i(TAG, "No reply from server! Device might just been switched on, so wait a bit"); waitForConnect(); if (!isOnline()) { Log.i(TAG, "Waiting didn't help. Still no connection"); result[0] = ServerAnswer.NO_REPLY; result[1] = "No online connection!"; return result; } } final SAXParserFactory factory = SAXParserFactory.newInstance(); final DefaultHandler handler = new DefaultHandler() { private boolean versionElement = false; public void startElement(final String uri, final String localName, final String qName, final Attributes attributes) throws SAXException { if (qName.equalsIgnoreCase("ALLOWED")) { versionElement = true; } } public void endElement(final String uri, final String localName, final String qName) throws SAXException { // Log.d(TAG, "End Element :" + qName); } public void characters(final char[] ch, final int start, final int length) throws SAXException { if (versionElement) { serverVersion = new String(ch, start, length); versionElement = false; } } }; Log.i(TAG, "Verifying client version at" + Preferences.VERSION_CHECK_URL); // version file is opened as stream, thus preventing immediate timeout issues final URL url = new URL(Preferences.VERSION_CHECK_URL); final InputStream stream = url.openStream(); final SAXParser saxParser = factory.newSAXParser(); saxParser.parse(stream, handler); stream.close(); if (serverVersion.equals(params[0])) { Log.i(TAG, "Client version is up-to-date: " + params[0]); final boolean anonymousUpload = PreferenceManager.getDefaultSharedPreferences(mContext) .getBoolean(Preferences.KEY_ANONYMOUS_UPLOAD, false); if (!anonymousUpload && credentialsAccepted(params[1], params[2])) { result[0] = ServerAnswer.OK; result[1] = "Everything fine! You're using the most up-to-date version!"; } else if (!anonymousUpload && !credentialsAccepted(params[1], params[2])) { result[0] = ServerAnswer.BAD_PASSWORD; result[1] = "Server reports bad user or password!"; } else { result[0] = ServerAnswer.OK; result[1] = "Password validation skipped, anonymous upload!"; } return result; } else { Log.i(TAG, "Client version is outdated: server " + serverVersion + " client " + params[0]); result[0] = ServerAnswer.OUTDATED; result[1] = "New version available:" + serverVersion; return result; } } catch (final IOException e) { Log.e(TAG, "Error while checking version. Are you online?"); return new Object[] { ServerAnswer.NO_REPLY, "Couldn't contact server" }; } catch (final Exception e) { Log.e(TAG, "Error while checking version: " + e.toString(), e); return new Object[] { ServerAnswer.UNKNOWN_ERROR, "Error: " + e.toString() }; } }
From source file:it.unimi.di.big.mg4j.document.WikipediaDocumentSequence.java
@Override public DocumentIterator iterator() throws IOException { final SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); saxParserFactory.setNamespaceAware(true); final MutableString nameSpaceAccumulator = new MutableString(); final ObjectOpenHashSet<MutableString> nameSpacesAccumulator = new ObjectOpenHashSet<MutableString>(); final ArrayBlockingQueue<DocumentFactory> freeFactories = new ArrayBlockingQueue<DocumentFactory>(16); for (int i = freeFactories.remainingCapacity(); i-- != 0;) freeFactories.add(this.factory.copy()); final ArrayBlockingQueue<DocumentAndFactory> readyDocumentsAndFactories = new ArrayBlockingQueue<DocumentAndFactory>( freeFactories.size());/* w ww . j av a 2s . c o m*/ final SAXParser parser; try { parser = saxParserFactory.newSAXParser(); } catch (Exception e) { throw new RuntimeException(e.getMessage(), e); } final DefaultHandler handler = new DefaultHandler() { private final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); private boolean inText; private boolean inTitle; private boolean inId; private boolean inTimestamp; private boolean inNamespaceDef; private boolean redirect; private MutableString text = new MutableString(); private MutableString title = new MutableString(); private MutableString id = new MutableString(); private MutableString timestamp = new MutableString(); private final Reference2ObjectMap<Enum<?>, Object> metadata = new Reference2ObjectOpenHashMap<Enum<?>, Object>(); { metadata.put(PropertyBasedDocumentFactory.MetadataKeys.ENCODING, "UTF-8"); metadata.put(MetadataKeys.REDIRECT, redirectAnchors); } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if ("page".equals(localName)) { redirect = inText = inTitle = inId = inTimestamp = false; text.length(0); title.length(0); id.length(0); timestamp.length(0); } else if ("text".equals(localName)) inText = true; else if ("title".equals(localName) && title.length() == 0) inTitle = true; // We catch only the first id/title elements. else if ("id".equals(localName) && id.length() == 0) inId = true; else if ("timestamp".equals(localName) && timestamp.length() == 0) inTimestamp = true; else if ("redirect".equals(localName)) { redirect = true; if (attributes.getValue("title") != null) // Accumulate the title of the page as virtual text of the redirect page. synchronized (redirectAnchors) { final String link = Encoder.encodeTitleToUrl(attributes.getValue("title"), true); redirectAnchors.add( new AnchorExtractor.Anchor(new MutableString(baseURL.length() + link.length()) .append(baseURL).append(link), title.copy())); } } else if ("namespace".equals(localName)) { // Found a new namespace inNamespaceDef = true; nameSpaceAccumulator.length(0); } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { if ("namespace".equals(localName)) { // Collecting a namespace if (nameSpaceAccumulator.length() != 0) nameSpacesAccumulator.add(nameSpaceAccumulator.copy().toLowerCase()); return; } if ("namespaces".equals(localName)) { // All namespaces collected nameSpaces = ImmutableSet.copyOf(nameSpacesAccumulator); return; } if (!redirect) { if ("title".equals(localName)) { // Set basic metadata for the page metadata.put(PropertyBasedDocumentFactory.MetadataKeys.TITLE, title.copy()); String link = Encoder.encodeTitleToUrl(title.toString(), true); metadata.put(PropertyBasedDocumentFactory.MetadataKeys.URI, new MutableString(baseURL.length() + link.length()).append(baseURL).append(link)); inTitle = false; } else if ("id".equals(localName)) { metadata.put(MetadataKeys.ID, Long.valueOf(id.toString())); inId = false; } else if ("timestamp".equals(localName)) { try { metadata.put(MetadataKeys.LASTEDIT, dateFormat.parse(timestamp.toString())); } catch (ParseException e) { throw new RuntimeException(e.getMessage(), e); } inTimestamp = false; } else if ("text".equals(localName)) { inText = false; if (!keepNamespaced) { // Namespaces are case-insensitive and language-dependent final int pos = title.indexOf(':'); if (pos != -1 && nameSpaces.contains(title.substring(0, pos).toLowerCase())) return; } try { final MutableString html = new MutableString(); DocumentFactory freeFactory; try { freeFactory = freeFactories.take(); } catch (InterruptedException e) { throw new RuntimeException(e.getMessage(), e); } if (parseText) { if (DISAMBIGUATION.search(text) != -1) { // It's a disambiguation page. /* Roi's hack: duplicate links using the page title, so the generic name will end up as anchor text. */ final MutableString newLinks = new MutableString(); for (int start = 0, end; (start = BRACKETS_OPEN.search(text, start)) != -1; start = end) { end = start; final int endOfLink = text.indexOfAnyOf(END_OF_DISAMBIGUATION_LINK, start); // Note that we don't escape title because we are working at the Wikipedia raw text level. if (endOfLink != -1) { newLinks.append(text.array(), start, endOfLink - start).append('|') .append(title).append("]]\n"); end = endOfLink; } end++; } text.append(newLinks); } // We separate categories by OXOXO, so we don't get overflowing phrases. final MutableString category = new MutableString(); for (int start = 0, end; (start = CATEGORY_START.search(text, start)) != -1; start = end) { end = BRACKETS_CLOSED.search(text, start += CATEGORY_START.length()); if (end != -1) category.append(text.subSequence(start, end)).append(" OXOXO "); else break; } metadata.put(MetadataKeys.CATEGORY, category); // Heuristics to get the first paragraph metadata.put(MetadataKeys.FIRSTPAR, new MutableString()); String plainText = wikiModel.render(new PlainTextConverter(true), text.toString()); for (int start = 0; start < plainText.length(); start++) { //System.err.println("Examining " + plainText.charAt( start ) ); if (Character.isWhitespace(plainText.charAt(start))) continue; if (plainText.charAt(start) == '{') { //System.err.print( "Braces " + start + " text: \"" + plainText.subSequence( start, start + 10 ) + "\" -> " ); start = BRACES_CLOSED.search(plainText, start); //System.err.println( start + " text: \"" + plainText.subSequence( start, start + 10 ) + "\"" ); if (start == -1) break; start++; } else if (plainText.charAt(start) == '[') { start = BRACKETS_CLOSED.search(plainText, start); if (start == -1) break; start++; } else { final int end = plainText.indexOf('\n', start); if (end != -1) metadata.put(MetadataKeys.FIRSTPAR, new MutableString(plainText.substring(start, end))); break; } } try { wikiModel.render(new HTMLConverter(), text.toString(), html, false, true); final Map<String, String> categories = wikiModel.getCategories(); // Put back category links in the page (they have been parsed by bliki and to not appear anymore in the HTML rendering) for (Entry<String, String> entry : categories.entrySet()) { final String key = entry.getKey(); final String value = entry.getValue().trim(); if (value.length() != 0) // There are empty such things html.append("\n<a href=\"").append(baseURL).append("Category:") .append(Encoder.encodeTitleToUrl(key, true)).append("\">") .append(HtmlEscapers.htmlEscaper().escape(key)) .append("</a>\n"); } } catch (Exception e) { LOGGER.error("Unexpected exception while parsing " + title, e); } } readyDocumentsAndFactories.put(new DocumentAndFactory( freeFactory.getDocument(IOUtils.toInputStream(html, Charsets.UTF_8), new Reference2ObjectOpenHashMap<Enum<?>, Object>(metadata)), freeFactory)); } catch (InterruptedException e) { throw new RuntimeException(e.getMessage(), e); } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } } } } @Override public void characters(char[] ch, int start, int length) throws SAXException { if (inText && parseText) text.append(ch, start, length); if (inTitle) title.append(ch, start, length); if (inId) id.append(ch, start, length); if (inTimestamp) timestamp.append(ch, start, length); if (inNamespaceDef) { nameSpaceAccumulator.append(ch, start, length); inNamespaceDef = false; // Dirty, but it works } } @Override public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException { if (inText && parseText) text.append(ch, start, length); if (inTitle) title.append(ch, start, length); } }; final Thread parsingThread = new Thread() { public void run() { try { InputStream in = new FileInputStream(wikipediaXmlDump); if (bzipped) in = new BZip2CompressorInputStream(in); parser.parse( new InputSource(new InputStreamReader(new FastBufferedInputStream(in), Charsets.UTF_8)), handler); readyDocumentsAndFactories.put(END); } catch (Exception e) { throw new RuntimeException(e.getMessage(), e); } } }; parsingThread.start(); return new AbstractDocumentIterator() { private DocumentFactory lastFactory; @Override public Document nextDocument() throws IOException { try { final DocumentAndFactory documentAndFactory = readyDocumentsAndFactories.take(); if (lastFactory != null) freeFactories.put(lastFactory); if (documentAndFactory == END) return null; lastFactory = documentAndFactory.factory; return documentAndFactory.document; } catch (InterruptedException e) { throw new RuntimeException(e.getMessage(), e); } } }; }