Rosetta Code/Count examples: Difference between revisions

no edit summary
m (Some fixes.)
No edit summary
Line 30:
print
print "Total: " + str(sum(tasks.values())) + " examples."</lang>
 
=={{header|Java}}==
 
<lang java>
import java.util.ArrayList;
import java.util.Iterator;
import ScreenScrape;
 
public class CountProgramExamples {
private static final String baseURL = "http://rosettacode.org/wiki/";
private static final String rootURL = "http://www.rosettacode.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:Programming_Tasks&cmlimit=500&format=xml";
private static final String taskBegin = "title=\"";
private static final String taskEnd = "\"";
private static final String exmplBegin = "<span class=\"tocnumber\">";
private static final String exmplEnd = "</span>";
private static final String editBegin = "<span class=\"editsection\">";
/**
* @param args
*/
public static void main(String[] args) {
// Setup variables
int exTotal = 0;
int exSubTot = 0;
String title = "";
String taskPage = "";
int startPos = 0;
String countStr = "";
try {
// Get root query results
ArrayList<String> tasks = new ArrayList<String>();
ScreenScrape ss = new ScreenScrape();
String rootPage = ss.read(rootURL);
while(rootPage.contains(taskBegin)){
rootPage = rootPage.substring(rootPage.indexOf(taskBegin)+taskBegin.length());
title = rootPage.substring(0, rootPage.indexOf(taskEnd));
if (!title.contains("Category:")) {
tasks.add(title);
}
rootPage = rootPage.substring(rootPage.indexOf(taskEnd));
}
// Loop through each task and print count
Iterator<String> itr = tasks.iterator();
while(itr.hasNext()) {
title = itr.next().replaceAll("&#039;","'");
taskPage = ss.read(baseURL+title.replaceAll(" ", "_"));
if (taskPage.contains(exmplBegin)) {
startPos = taskPage.lastIndexOf(exmplBegin)+exmplBegin.length();
countStr = taskPage.substring(startPos, taskPage.indexOf(exmplEnd, startPos));
exSubTot = Integer.parseInt(countStr.contains(".") ?
countStr.substring(0,countStr.indexOf(".")) : countStr);
}else{
exSubTot = 0;
while(taskPage.contains(editBegin)) {
taskPage = taskPage.substring(taskPage.indexOf(editBegin)+editBegin.length());
exSubTot++;
}
}
exTotal += exSubTot;
System.out.println(title+": "+exSubTot+" examples.");
}
// Print total
System.out.println("\nTotal: "+exTotal+" examples.");
}catch(Exception e){
System.out.println(title);
System.out.println(startPos+":"+taskPage.indexOf(exmplEnd, startPos));
System.out.println(taskPage);
e.printStackTrace(System.out);
}
}
}
</lang>
 
This is the ScreenScrape class imported in the above class.
 
<lang java>
import java.io.*;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
 
public class ScreenScrape {
 
public String read(String sUrl) throws Exception {
String lastHtml = "";
BufferedReader reader;
if (sUrl.startsWith("https://")) { // The URL class doesn't like HTTPS so we have to check for it.
reader = new BufferedReader(
new InputStreamReader(
getSSLOutputCon(sUrl).getInputStream()));
} else if (sUrl.startsWith("http://")) { // for nonsecure http
reader = new BufferedReader(
new InputStreamReader(
new URL(sUrl).openStream()));
} else {
return "Protocol not supported. Please verify that your URL starts with "
+ "\"http://\" or \"https://\" and try again.";
}
String line = reader.readLine();
while (line != null) {
lastHtml += line;
line = reader.readLine();
}
return lastHtml;
}
private HttpsURLConnection httpsUrlCon;
private HttpsURLConnection getSSLOutputCon(String sUrl) throws Exception {
try {
URL url = new URL(sUrl);
httpsUrlCon = (HttpsURLConnection) url.openConnection();
httpsUrlCon.setHostnameVerifier(new javax.net.ssl.HostnameVerifier() {
public boolean verify(String hostname, javax.net.ssl.SSLSession certHostName) {
return true;
}
});
} catch(Exception e) {
throw e;
}
return httpsUrlCon;
}
}
</lang>