HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

WikiCategory class

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
classwikicategorystackoverflow

Problem

This is a whole class I wrote. I am on my third day learning Java, so I think I would benefit a lot from a general review. At first I thought whole class would be too large, but this says it is ok.

```
/**
* WikiCategory
*
* There is no copyright, do whatever you want with this.
* @author Yaşar Arabacı
* @date 10.08.2013
*/
package wikicategory;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

/**
* Given a category like "Category:Psychology" and an endpoint like
* http://en.wikipedia.org/w/api.php this class will attempt to get all articles
* belonging to that category. it can write those to a file, or give you a
* JSONObject of all the articles.
*
* @author yasar
*/
public class WikiCategory {

private static final String EXPLANATION = "Export a category";
private String userAgent;
private String endPoint;
private String category;
private JSONObject pages;
private HashMap params;

/**
* @param name Your name
* @param eMail your e-mail
* @param endPoint meadia wiki api endpoint e.g.
* http://en.wikipedia.org/w/api.php
* @param category The category you would like to get. e.g
* "Category:Psychology"
*/
public WikiCategory(String name, String eMail, String endPoint, String category) {
this(name, eMail, endPoint);
this.category = category;
this.params.put("gcmtitle", this.category);
}

/**
* please check
* {@link #WikiCategory(String, String, String, String) WikiCa

Solution

A few notes (in addition to other answers):

URLEncoder.encode(String) is depreciated. You should be using the other method in the URLEncoder class: URLEncoder.encode(String, String). The first parameter is the String to encode; the second is the name of the character encoding to use (e.g., UTF-8).

Iterator is a raw type (in the for loop). References to generic type Iterator should be parameterized. Better yet, just use a for-each loop (as suggested in other answers).

Your if statements:

if (targetDir.exists() && !targetDir.isDirectory())
{
    return;
}


Could be simplified to one line, since they are exit condition tests:

if (targetDir.exists() && !targetDir.isDirectory()) return;


It makes it more easy to read and takes up less space.

Code Snippets

if (targetDir.exists() && !targetDir.isDirectory())
{
    return;
}
if (targetDir.exists() && !targetDir.isDirectory()) return;

Context

StackExchange Code Review Q#29595, answer score: 8

Revisions (0)

No revisions yet.