ITI0011RUS:task 10 Sample

Allikas: Kursused
Redaktsioon seisuga 25. märts 2015, kell 07:29 kasutajalt Aleksandr (arutelu | kaastöö) (Uus lehekülg: 'Общая информация об упражнениях: ITI0011RUS_Practice.<br/> Обратно на страницу предмета. <source lang="ja...')
(erin) ←Vanem redaktsioon | Viimane redaktsiooni (erin) | Uuem redaktsioon→ (erin)
Mine navigeerimisribale Mine otsikasti

Общая информация об упражнениях: ITI0011RUS_Practice.
Обратно на страницу предмета.

<source lang="java"> import java.io.ByteArrayInputStream; import java.io.IOException; import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory;

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

import cs.ttu.ee.BookShopApi; import cs.ttu.ee.BookShopApiException; import cs.ttu.ee.SimpleBookShop;


/**

* BookShop class.
* Simplified implementation of BookShop 
* Does filtering and basic queries.
*/

public class BookShop extends SimpleBookShop {

/** * Instance of BookShopApi, used to access * Google Books */ private BookShopApi bookShopApi;

/** * Last api query result. */ private String lastResponse = "";

public BookShop(BookShopApi _bookShopApi) { bookShopApi = _bookShopApi; }

/** * Make api query. * Uses BookStoreApi functionality to get XML response from Google Books * Every time this function is called, latest XML state is saved. * If previous query failed lastResponse equals to empty string. * Otherwise lastResult is * @param search title of book */ public void query(String searchString) { try { lastResponse = bookShopApi.getBookByTitle(searchString); } catch (BookShopApiException e) { lastResponse = ""; } }

private NodeList getXPathData(String xPathStr) { if(lastResponse.isEmpty()) { return null; } try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = dbf.newDocumentBuilder(); Document xmlDocument = builder.parse( new ByteArrayInputStream(lastResponse.getBytes())); XPathFactory xpf = XPathFactory.newInstance(); XPath xPath = xpf.newXPath(); XPathExpression expr = xPath.compile(xPathStr); return (NodeList) expr.evaluate(xmlDocument, XPathConstants.NODESET); } catch ( ParserConfigurationException | SAXException | IOException | XPathExpressionException e) { return null; }

}

/** * Find total items matched by searchString. * Api uses paging, not all results are * displayed under result, however, total * results is displayed inside <totalItems> element * * If last query was unsuccessful, 0 is returned. * * @return Total items matched by query. */ public int getTotalRecordsInResponse() {

NodeList records = getXPathData("/result/totalItems");

for(int i=0; i<records.getLength(); ++i) { Node node = records.item(i); String v = node.getTextContent(); return Integer.parseInt(v); }

return 0; }


/** * Finds all possbile book titles. * Duplicates may occur. * * Title can be found inside <title> tag. * If last query was unsuccessful, empty ArrayList is returned. * * @return All possible book names matched by searchString */ public ArrayList<String> getPossibleBookTitles() {

ArrayList<String> bookTitles = new ArrayList<String>();

NodeList records = getXPathData("//title");

for(int i=0; i<records.getLength(); ++i) { Node title = records.item(i); bookTitles.add(title.getTextContent()); }

return bookTitles; }

/** * Finds all authors found in last response. * Does not include duplicate authors. * If last query was unsuccessful, empty ArrayList is returned. * * Author can be found inside <authors> element. (Notice non plural form) * NB! Multiple <authors> element may occur. * * @return List of authors found in last response */ public ArrayList<String> getPossibleAuthors() { // TODO: implement return null; }

/** * Finds book with the most number of pages * * Page count can be found inside <pageCount> element * @return Title of book. */ public String getBookWithMostPagesTitle() { // TODO: implement return null; }

/** * Finds all book titles where pageCount < n * * Page count can be found inside <pageCount> element * @param n maximum amount of pages * @return list of books with less than n pages */ public ArrayList<String> getBookWithLessThanNPagesTitles(int n) { // TODO: implement return null; }


} </source>