Jump to content

Java Programming Help


Shehezaada

Recommended Posts

Hey guys! Got an assignment due on sunday. It's a metasearch project. First we make an XML File and parse it. We ask the user to identify the media they want: book, music, dvd. If it is found, then we display results from our XML file. If it isn't, we search and parse Chapters.ca for it. I've implemented both the XMLParse and the Chapters Parse and I get the results just fine. Now the prof wants two more things. He wants me to update my XML file by updating my Title and Author indices. my updateTitleIndices takes in two arguements, (String word, int index). I need to find a way to send those arguements in. I have an idea though. The way I parsed my results from chapters was a huge gigantic string with all the titles, authors, date, listprice, saleprice. I simply seperated them with new lines and when I show to the user it's all in order. I realize it's very inefficient. I could put the titleString into the array list and then access them one by one, but i have tried and it failed. Any idea guys? I'm copying my main code here. (let me know if u need everything) import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; import java.util.Vector; import java.util.Iterator; import java.util.List; import java.util.StringTokenizer; import java.util.Hashtable; import javax.xml.bind.JAXBException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; import com.sun.xml.internal.bind.v2.runtime.reflect.ListIterator; import java.util.Scanner; public class MetaSearch implements Extractor { /** * @param args */ private Vector items; private Hashtable> titleIndex; private Hashtable> authorIndex; private Document document; public static String result; public static boolean found; public static String useTitle; public static String chapTitle; public MetaSearch(){ //create a list to hold the employee objects items = new Vector(); titleIndex = new Hashtable>(); authorIndex = new Hashtable>(); } public void load(String filename) { //parse an xml file and get the dom object parseXmlFile(filename); //get each element and create a relevant object parseDocument(); //create title and auther indices createIndices(); } private void parseXmlFile(String filename){ //get the factory DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); try { //Using factory get an instance of document builder DocumentBuilder db = dbf.newDocumentBuilder(); //parse using builder to get DOM representation of the XML file document = db.parse(filename); } catch(ParserConfigurationException pce) { pce.printStackTrace(); } catch(SAXException se) { se.printStackTrace(); } catch(IOException ioe) { ioe.printStackTrace(); } } private void parseDocument(){ //get the root elememt Element docEle = document.getDocumentElement(); //get a nodelist of elements NodeList nl = docEle.getElementsByTagName("Item"); if (nl != null && nl.getLength() > 0) { for(int i = 0 ; i < nl.getLength(); i++) { //get the item element Element el = (Element)nl.item(i); //extract the item type String type = el.getAttribute("type"); //get the relevant object Object item = null; if (type.equals("book")) item = getBook(el); else if (type.equals("music")) item = getMusicCD(el); else if (type.equals("dvd")) item = getDVDMovie(el); else { System.err.println("Unknown item type: " + type); System.exit(0); } //add it to list items.add((Item) item); } } } /** * Extract all field values from an XML element and create a Book object * @param empEl * @return */ private Book getBook(Element el) { String title = getTextValue(el,"Title"); String author = getTextValue(el, "Author"); Date publishDate = getDateObject(el, "PublishDate"); String supplier = getTextValue(el, "Supplier"); double listPrice = getDoubleValue(el, "ListPrice"); double salePrice = getDoubleValue(el, "SalePrice"); return new Book(title, author, publishDate, supplier, listPrice, salePrice); } /** * Extract all field values from an XML element and create a MusicCD object * @param empEl * @return */ private MusicCD getMusicCD(Element el) { String title = getTextValue(el,"Title"); String performer = getTextValue(el, "Performer"); String publisher = getTextValue(el, "Publisher"); Date publishDate = getDateObject(el, "PublishDate"); String supplier = getTextValue(el, "Supplier"); double listPrice = getDoubleValue(el, "ListPrice"); double salePrice = getDoubleValue(el, "SalePrice"); return new MusicCD(title, performer, publisher, publishDate, supplier, listPrice, salePrice); } /** * Extract all field values from an XML element and create a DVDMovie object * @param empEl * @return */ private DVDMovie getDVDMovie(Element el) { String title = getTextValue(el,"Title"); String director = getTextValue(el, "Director"); Date publishDate = getDateObject(el, "PublishDate"); String supplier = getTextValue(el, "Supplier"); double listPrice = getDoubleValue(el, "ListPrice"); double salePrice = getDoubleValue(el, "SalePrice"); return new DVDMovie(title, director, publishDate, supplier, listPrice, salePrice); } /** * Extract a field value as a string from an XML element * @param ele * @param tagName * @return */ private String getTextValue(Element ele, String tagName) { String textVal = null; NodeList nl = ele.getElementsByTagName(tagName); if (nl != null && nl.getLength() > 0) { Element el = (Element)nl.item(0); textVal = el.getFirstChild().getNodeValue(); } return textVal; } /** * Calls getTextValue and returns a int value * @param ele * @param tagName * @return */ private int getIntValue(Element ele, String tagName) { String value = getTextValue(ele, tagName); return (value == null) ? 0 : Integer.parseInt(value); } /** * Calls getTextValue and returns a double value * @param ele * @param tagName * @return */ private double getDoubleValue(Element ele, String tagName) { String value = getTextValue(ele, tagName); return (value == null) ? 0.0 : Double.parseDouble(value); } /** * Calls getTextValue and returns a Date object * @param ele * @param tagName * @return */ private Date getDateObject(Element ele, String tagName) { String value = getTextValue(ele, tagName); StringTokenizer tokens = new StringTokenizer(value, " ,"); int counts = tokens.countTokens(); if (counts == 0) return null; else { int month = 0, day = 0; if (counts > 1) { month = Date.monthInt(tokens.nextToken()); if (counts > 2 ) day = Integer.parseInt(tokens.nextToken()); } int year = Integer.parseInt(tokens.nextToken()); return new Date(month, day, year); } } private void updateTitleIndex(String word, int index) { String normalized = word.toLowerCase(); if (titleIndex.containsKey(normalized)) titleIndex.get(normalized).add(index); else { Vector indices = new Vector(); indices.add(index); titleIndex.put(normalized, indices); } } private void updateAuthorIndex(String word, int index) { String normalized = word.toLowerCase(); if (authorIndex.containsKey(normalized)) authorIndex.get(normalized).add(index); else { Vector indices = new Vector(); indices.add(index); authorIndex.put(normalized, indices); } } private Vector getTitleIndices(String word) { String normalized = word.toLowerCase(); if (titleIndex.containsKey(normalized)) return titleIndex.get(normalized); else return new Vector(); } private Vector getAuthorIndices(String word) { String normalized = word.toLowerCase(); if (authorIndex.containsKey(normalized)) return authorIndex.get(normalized); else return new Vector(); } private Vector matchIndices(Vector index1, Vector index2) { if (index1 == null){ return index2; } if (index2 == null) return index1; Vector matched = new Vector(); int i = 0, j = 0; while (i < index1.size() && j < index2.size()) { if (index1.get(i) < index2.get(j)) i++; else if (index1.get(i) > index2.get(j)) j++; else{ matched.add(index1.get(i)); i++; j++; } } return matched; } private void createIndices(){ for (int i = 0; i < items.size(); i++){ Item item = items.get(i); //Default for books because titles are in base class String title = item.getTitle(); String author = item.getAuthor(); StringTokenizer titles = new StringTokenizer(title, " ,:()"); while (titles.hasMoreTokens()) updateTitleIndex(titles.nextToken(), i); if (item instanceof Book){ StringTokenizer authors = new StringTokenizer(author, " ,:()"); while(authors.hasMoreTokens()) updateAuthorIndex(authors.nextToken(),i); } if (item instanceof MusicCD){ MusicCD mcd = (MusicCD)item; StringTokenizer performers = new StringTokenizer(mcd.getPerformer(), " ,"); while (performers.hasMoreTokens()) updateAuthorIndex(performers.nextToken(), i); } if (item instanceof DVDMovie){ DVDMovie dvd = (DVDMovie)item; StringTokenizer directors = new StringTokenizer(dvd.getDirector(), " ,"); while (directors.hasMoreTokens()) updateAuthorIndex(directors.nextToken(), i); } } } public String search(String media, String title, String author) { if (!media.equals("book") && !media.equals("music") && !media.equals("dvd")) { System.err.println("media type is not specified"); return ""; } StringTokenizer titles = new StringTokenizer(title, " ,:()"); StringTokenizer authors = new StringTokenizer(author, " ,"); if (titles.countTokens() == 0 && authors.countTokens() == 0 ) { System.err.println("Both title and author are empty"); return ""; } Vector results = null; while (titles.hasMoreTokens()) results = matchIndices(results, getTitleIndices(titles.nextToken())); while (authors.hasMoreTokens()) results = matchIndices(results, getAuthorIndices(authors.nextToken())); String output = ""; for (int i = 0; i < results.size(); i++) { Object item = items.get(results.get(i)); if (item instanceof Book && media.equals("book")) output += item + "\n"; else if (item instanceof MusicCD && media.equals("music")) output += item + "\n"; else if (item instanceof DVDMovie && media.equals("dvd")) output += item + "\n"; } return output; } /** * Iterate through the list and print the * content to console */ public void printItems(){ System.out.println("Number of Items '" + items.size() + "'."); Iterator it = items.iterator(); while(it.hasNext()) { System.out.println(it.next().toString()); } } public static void main(String[] args){ //create an instance MetaSearch ms = new MetaSearch(); //load in the records from an XML file ms.load("items.xml"); //display the contents of the collection //ms.printItems(); //search for a specific media type with title/author information Scanner keyboard = new Scanner(System.in); System.out.println("Enter the media you want to search for"); String media = keyboard.nextLine(); System.out.println("Enter Title Name"); String title = keyboard.nextLine(); System.out.println("Enter Author/Director/Performer"); String author = keyboard.nextLine(); String result = ms.search(media, title, author); System.out.println(result); if( result.equals("")){ String searchRequest = ms.getSearchRequest(); String link =ms.getHTMLPage(searchRequest); String infoz =ms.getSearchResults(link); String info = infoz.substring(4,infoz.length()); System.out.println(info); } } public String getHTMLPage(String searchRequest) { String request=searchRequest.replaceAll(" ", "%20"); String cutz1 = "http://www.chapters.indigo.ca/books/search?keywords="; String cutz2 = "&pageSize=10"; String link = cutz1+request+cutz2; return link; } public String getSearchRequest() { Scanner keyboard = new Scanner(System.in); System.out.println("What type of media would you like to search for?"); String user = keyboard.nextLine(); String searchRequest =null; if(user.equalsIgnoreCase("book")){ System.out.println("What Book Would You Like To Search For?"); String userBook=keyboard.nextLine(); searchRequest = userBook; } else if(user.equalsIgnoreCase("music") || user.equalsIgnoreCase("CD")){ System.out.println("I'm only going to search for books"); } else if(user.equalsIgnoreCase("DVD") || user.equalsIgnoreCase("movie")){ System.out.println("I'm only going to search for books"); } return searchRequest; } public String getSearchResults(String link) { try { URL chapter = new URL(link); URLConnection chapterConnection = chapter.openConnection(); chapterConnection.connect(); Scanner page = new Scanner(chapter.openStream()); //String Processing for Title while (page.hasNextLine()){ String wholePage = page.nextLine(); if(wholePage.indexOf("

")!= -1){ //String head = page.nextLine(); //System.out.println(wholePage); if(wholePage.indexOf("Search+Books") != -1){ //System.out.println(wholePage); int cut1 = wholePage.indexOf("d\">"); //System.out.println(cut1); int cut2=wholePage.indexOf(""); //System.out.println(cut2); String titlez = wholePage.substring(cut1+3,cut2); String title = "Title: " +titlez; // System.out.println(chapTitle); //chapTitle.add(titlez); //System.out.println(title); result = result + title + "\n" ; } } else if(wholePage.indexOf("OTSTemplateSpecs") != -1){ //System.out.println(wholePage); if(wholePage.indexOf("author")== -1){ //System.out.println(wholePage); if(wholePage.indexOf("href") != -1){ int cut1=wholePage.indexOf("d\">"); //System.out.println(cut1); int cut2=wholePage.indexOf(""); //System.out.println(cut2); String authorz=wholePage.substring(cut1+3,cut2); String author = ("Author: " + authorz); // System.out.println(author); result = result + author + "\n"; } } } // String processing for pubDate if(wholePage.indexOf("OTSTemplateSpecs") != -1){ if(wholePage.indexOf("strong")== -1){ //System.out.println(wholePage); int cut1=wholePage.lastIndexOf("|"); int cut2=wholePage.indexOf(""); String pubDatez=wholePage.substring(cut1+2,cut2); String pubDate = ("Publish Date: " + pubDatez); // System.out.println(pubDate); result = result + pubDate + "\n" + "Supplier: Chapters.ca" + "\n"; } } if(wholePage.indexOf("priceOld")!= -1){ if(wholePage.indexOf("span") != -1){ int cut1=wholePage.indexOf("$"); int cut2=wholePage.indexOf(""); String listPricez=wholePage.substring(cut1,cut2); String listPrice = ("List Price: " + listPricez); // System.out.println(listPrice); result = result + listPrice + "\n"; } } if(wholePage.indexOf("")!= -1){ //System.out.println(wholePage); int cut1=wholePage.indexOf("$"); int cut2=wholePage.indexOf(""); String salePricez=wholePage.substring(cut1,cut2); String salePrice = ("Sale Price: " + salePricez); // System.out.println(salePrice); result = result + salePrice + "\n"; } } page.close(); }catch(MalformedURLException e){ System.out.println("MalformedURLException "+e); }catch(IOException e){ System.out.println("IOException "+e); } return result; } }

Link to comment
Share on other sites

if you want an efficient way of storing the title by index , you can put them in a hash map with the index as the key. you can then iterate through the map and get the index and title values to call your update method.

Link to comment
Share on other sites

×
×
  • Create New...