ITI0011:praktikum 7 N8

Allikas: Kursused
Mine navigeerimisribale Mine otsikasti

Tagasi ITI0011 lehele.

Üldine

Praktikum: 16. oktoober kell 8.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.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.StringReader; import java.text.ParseException; import java.text.SimpleDateFormat; 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.SAXException;

import wiki.WikiAPI;


public class Wiki {

public static void main(String[] args) { //args = new String[]{"tere", "123", "new york"}; System.out.println(args.length); for (String s : args) { System.out.println(s); }

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

String xml = getXml(); System.out.println(xml);

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db; try { db = dbf.newDocumentBuilder();

Document doc = db.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));

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


NodeList students2 = doc.getElementsByTagName("student"); System.out.println(students2.getLength()); for (int i = 0; i < students2.getLength(); i++) { Node student = students2.item(i); NodeList studentAttributes = student.getChildNodes(); for (int j = 0; j < studentAttributes.getLength(); j++) {

} System.out.println(student.getTextContent()); }

} 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(); }

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

Date d2 = new Date(); System.out.println(sdf.format(d2)); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } //Date d = new Date();


}

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>

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.