ITI0011:praktikum 7 N14

Allikas: Kursused
Redaktsioon seisuga 16. oktoober 2014, kell 11:46 kasutajalt Ago (arutelu | kaastöö)
(erin) ←Vanem redaktsioon | Viimane redaktsiooni (erin) | Uuem redaktsioon→ (erin)
Mine navigeerimisribale Mine otsikasti

Tagasi ITI0011 lehele.

Üldine

Praktikum: 16. oktoober kell 14.00

Praktikumis käsitletavad teemad:

  • käsurea argumendid
  • välise teegi (jar) kasutamine
  • tulemuste parsimine


Kood tuleb üles laadida git'i. Kausta (projekti) nimi peaks olema "prax7".

Koodinäide

<source lang="java"> import java.io.IOException; import java.io.StringReader; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date;

import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; import org.xml.sax.SAXException;

import wiki.WikiAPI;


public class Wiki {

public static void main(String[] args) { args = new String[]{"-c", "20"}; // program -c 20 System.out.println(args.length); for (String s : args) { // foreach System.out.println(s); /*if (s.equals("-c")) { // s == "-c"

}*/ }

int count; for (int i = 0; i < args.length; i++) { String s = args[i];

if (s.equalsIgnoreCase("-c")) { count = Integer.parseInt(args[i + 1]); } }


//String xml = WikiAPI.getWikiInformation("tallinn"); String xml = getXml(); System.out.println(xml);

try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); StringReader sr = new StringReader(xml); InputSource is = new InputSource(sr); Document doc = db.parse(is);

NodeList students = doc.getElementsByTagName("student"); for (int i = 0; i < students.getLength(); i++) { Node student = students.item(i); System.out.println(student.getAttributes().getNamedItem("faculty").getNodeValue()); System.out.println(student.getChildNodes().item(2).getFirstChild().getNodeValue()); }

} catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }


SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy"); String dateString = "01.01.2014"; Date d = new Date(); System.out.println(d); try { Date d2 = sdf.parse(dateString);

System.out.println(d2); Calendar c = Calendar.getInstance(); c.setTime(d2); c.add(Calendar.MINUTE, 44); Date d3 = c.getTime(); System.out.println(d3); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); }

}

public static String getXml() { return "<root><students><student faculty=\"I\"><name>Juhan</name><age>22</age><grade>6</grade></student><student faculty=\"M\"><name>Mari</name><age>19</age><grade>5</grade></student></students></root>"; }

}

</source>

WikiAPI.java

<source lang="java"> package wiki;

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder;

public class WikiAPI {


public static int FORMAT_JSON = 1; public static int FORMAT_XML = 2;

public static void main(String[] args) { // TODO Auto-generated method stub System.out.println(getWikiInformation("Main page", WikiAPI.FORMAT_JSON)); System.out.println(getWikiInformation("Estonia", WikiAPI.FORMAT_JSON)); System.out.println(getWikiInformation("Tallinn", WikiAPI.FORMAT_JSON, "info", "recentchanges"));

}


/** * Uses Wikipedia API to retrieve general information about * a certain page in XML-format. * @param pageName Page title to be searched for * @return Response in XML-format */ public static String getWikiInformation(String pageName) { return getWikiInformation(pageName, FORMAT_XML); }

/** * Uses Wikipedia API to retrieve general information about * a certain page. * @param pageName Page title to be searched for * @param format In which format the response is returned, * use WikiAPI.FORMAT_* constants. * @return Response in the specified format */ public static String getWikiInformation(String pageName, int format) { return getWikiInformation(pageName, format, "info", null); }

/** * Uses Wikipedia API to retrieve information about * a certain page. * @param pageName Page title to be searched for * @param format In which format the response is returned, * use WikiAPI.FORMAT_* constants. * @param prop Value for "prop" parameter (properties) * @param list Value for "list" parameter * @return Response in the specified format */ public static String getWikiInformation(String pageName, int format, String prop, String list) {

String formatString = "json"; if (format == WikiAPI.FORMAT_XML) { formatString = "xml"; } String listStr = ""; if (list != null) { listStr = "&list=" + list; } try { URL url = new URL("http://en.wikipedia.org/w/api.php?format=" + formatString + "&action=query&titles=" + URLEncoder.encode(pageName, "UTF-8") + "&prop=" + prop + listStr); BufferedReader br = new BufferedReader( new InputStreamReader(url.openStream())); String line; StringBuilder sb = new StringBuilder(); while ((line = br.readLine()) != null) { sb.append(line); } return sb.toString();

} catch (MalformedURLException e) { // TODO Auto-generated catch block return null; } catch (IOException e) { // TODO Auto-generated catch block return null; } }

}

</source>

Exercise

Write a program that reads argumends from command line. Every argument is a name. For every name, you should make a query to Wikipedia and retrieve some basic information.

For example, if you run the program with arguments "Tallinn" "Estonia" your program should call wikipedia two times: first with "Tallinn", then with "Estonia".

For getting information from Wikipedia, you can use http://dijkstra.cs.ttu.ee/~ago/iti0011/WikiAPI.jar library (a very simple library to just demonstrate how to use a library). This has a method getWikiInformation(String name) or getWikiInformation(String name, int format). Read javadoc for more. getWikiInformation just reads all the information from Wikiedpia API and returns it as one (huge) String.

Depending on the format you use, you should the the value of "touched" property (when the page is last accessed). If you want to use JSON-format, you can use this library: http://dijkstra.cs.ttu.ee/~ago/iti0011/json-20140107.jar

You should read out "touched" property value and represent it as a date. Try to add 44 days to that date and see, which date it is.

For date parsing, use SimpleDateFormat. To add days, use Calendar object.