Advertisement
CR7CR7

Walden

May 20th, 2022
1,008
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.86 KB | None | 0 0
  1. public class APIResponseParser {
  2.  
  3.     /**
  4.      * Parses the input text and returns a Book instance containing
  5.      * the parsed data.
  6.      * @param response text to be parsed
  7.      * @return Book instance containing parsed data
  8.      */
  9.     public static Book parse(String response) {
  10.         // -- Start: Provided by instructor --
  11.         Book book = new Book();
  12.         String endRule = "<";
  13.  
  14.         String startRule = "<title>";
  15.  
  16.         APIResponseParser arp = new APIResponseParser();
  17.         String title = arp.parse(response, startRule, endRule);
  18.         book.setTitle(title);
  19.         // -- End: Provided by instructor --
  20.  
  21.         startRule = "<name>";
  22.         String name = arp.parse(response, startRule, endRule);
  23.         book.setAuthor(name);
  24.  
  25.         startRule = "<original_publication_year type=\"integer\">";
  26.         String publicationYear = arp.parse(response, startRule, endRule);
  27.         book.setPublicationYear(new Integer(publicationYear));
  28.  
  29.         startRule = "<average_rating>";
  30.         String averageRating = arp.parse(response, startRule, endRule);
  31.         book.setAverageRating(new Double(averageRating));
  32.  
  33.         startRule = "<ratings_count type=\"integer\">";
  34.         String ratingsCount = arp.parse(response, startRule, endRule);
  35.         book.setRatingsCount(new Integer(ratingsCount.replaceAll(",", "")));
  36.  
  37.         startRule = "<image_url>";
  38.         String imageUrl = arp.parse(response, startRule, endRule);
  39.         book.setImageUrl(imageUrl);
  40.  
  41.         return book;
  42.     }
  43.  
  44.     private String parse(String response, String startRule, String endRule) {
  45.         int pos = response.indexOf(startRule);
  46.         int start = pos + startRule.length();
  47.         int end = response.indexOf(endRule, start);
  48.  
  49.         return response.substring(start, end);
  50.     }
  51.  
  52.     // write overloaded parse method with the 3 parameters response, startRule, endRule
  53.     public static void main(String[] args) {
  54.         String response = "<work>" +
  55.                                 "<id type=\"integer\">2361393</id>" +
  56.                                 "<books_count type=\"integer\">813</books_count>" +
  57.                                 "<ratings_count type=\"integer\">1,16,315</ratings_count>" +
  58.                                 "<text_reviews_count type=\"integer\">3439</text_reviews_count>" +
  59.                                 "<original_publication_year type=\"integer\">1854</original_publication_year>" +
  60.                                 "<original_publication_month type=\"integer\" nil=\"true\"/>" +
  61.                                 "<original_publication_day type=\"integer\" nil=\"true\"/>" +
  62.                                 "<average_rating>3.79</average_rating>" +
  63.                                 "<best_book type=\"Book\">" +
  64.                                     "<id type=\"integer\">16902</id>" +
  65.                                     "<title>Walden</title>" +
  66.                                     "<author>" +
  67.                                         "<id type=\"integer\">10264</id>" +
  68.                                         "<name>Henry David Thoreau</name>" +
  69.                                     "</author>" +
  70.                                     "<image_url>" +
  71.                                         "http://images.gr-assets.com/books/1465675526m/16902.jpg" +
  72.                                     "</image_url>" +
  73.                                     "<small_image_url>" +
  74.                                         "http://images.gr-assets.com/books/1465675526s/16902.jpg" +
  75.                                     "</small_image_url>" +
  76.                                 "</best_book>" +
  77.                             "</work>";
  78.  
  79.         Book book = APIResponseParser.parse(response);
  80.  
  81.         out.println(book.getAuthor());
  82.         out.println(book.getTitle());
  83.         out.println(book.getAverageRating());
  84.         out.println(book.getImageUrl());
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement