From b6869036b7839b4540cee1114f44bc5c9b33d9c4 Mon Sep 17 00:00:00 2001 From: Josh Walters Date: Tue, 13 Dec 2011 17:35:57 -0800 Subject: Initial commit of code. --- .../bookcatalog/isbnlookup/GDataISBNLookup.java | 158 ++++++++++++ .../bookcatalog/isbnlookup/ISBNLookup.java | 286 +++++++++++++++++++++ .../bookcatalog/isbnlookup/InvalidSearchTerms.java | 34 +++ .../bookcatalog/isbnlookup/NoBookFound.java | 34 +++ 4 files changed, 512 insertions(+) create mode 100644 src/com/joshwalters/bookcatalog/isbnlookup/GDataISBNLookup.java create mode 100644 src/com/joshwalters/bookcatalog/isbnlookup/ISBNLookup.java create mode 100644 src/com/joshwalters/bookcatalog/isbnlookup/InvalidSearchTerms.java create mode 100644 src/com/joshwalters/bookcatalog/isbnlookup/NoBookFound.java (limited to 'src/com/joshwalters/bookcatalog/isbnlookup') diff --git a/src/com/joshwalters/bookcatalog/isbnlookup/GDataISBNLookup.java b/src/com/joshwalters/bookcatalog/isbnlookup/GDataISBNLookup.java new file mode 100644 index 0000000..dc670b3 --- /dev/null +++ b/src/com/joshwalters/bookcatalog/isbnlookup/GDataISBNLookup.java @@ -0,0 +1,158 @@ +/* + * Book Catalog - Catalog your book collection. + * + * Copyright (C) 2009 Joshua Walters + * URL: http://joshwalters.com + * + * This file is part of Book Catalog. + * + * Book Catalog is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Book Catalog is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Book Catalog. If not, see . + */ + +package com.joshwalters.bookcatalog.isbnlookup; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.List; + +import com.google.gdata.client.books.BooksService; +import com.google.gdata.client.books.VolumeQuery; +import com.google.gdata.data.books.VolumeEntry; +import com.google.gdata.data.books.VolumeFeed; +import com.google.gdata.data.dublincore.Creator; +import com.google.gdata.data.dublincore.Subject; +import com.google.gdata.data.dublincore.Title; +import com.google.gdata.util.ServiceException; + +/** + * Performs book lookup with GData. + * + * @author Josh Walters + */ +public class GDataISBNLookup extends ISBNLookup { + + /** + * Used for looking up the book. + */ + private BooksService service; + /** + * The URL for the Google volumes feed. + */ + public final String FEED_URL = "http://books.google.com/books/feeds/volumes"; + + /** + * Sets up the book service. + */ + public GDataISBNLookup() { + service = new BooksService("BookCatalog-1.0"); + } + + /** + * Searches for a book. + */ + public void searchBooks(String barcode) throws NoBookFound, + InvalidSearchTerms { + // Clear all the book data fields + emptyDataFields(); + // Purify the barcode, get the price if possible, and return just the + // ISBN + ISBN = purifyBarcode(barcode); + // Perform the query + VolumeQuery query; + try { + query = new VolumeQuery(new URL(FEED_URL)); + query.setFullTextQuery(ISBN); + // Only return one result + query.setMaxResults(1); + VolumeFeed volumeFeed = service.query(query, VolumeFeed.class); + // Process the result + processVolumeFeed(volumeFeed); + } catch (MalformedURLException e) { + // Print the error to the error stream + System.err.println(e.getMessage()); + } catch (IOException e) { + // Print the error to the error stream + System.err.println(e.getMessage()); + } catch (ServiceException e) { + // Print the error to the error stream + System.err.println(e.getMessage()); + } + } + + /** + * Processes the result feed to get the book data. + * + * @param volumeFeed + * @throws IOException + * @throws ServiceException + * @throws NoBookFound + */ + private void processVolumeFeed(VolumeFeed volumeFeed) throws IOException, + ServiceException, NoBookFound { + List volumeEntries = volumeFeed.getEntries(); + if (volumeEntries.size() == 0) { + throw new NoBookFound(); + } + // Send the data to be stored in the class + storeVolumeEntry(volumeEntries.get(0)); + } + + /** + * Store the data in the class variables. + * + * @param entry + * @throws IOException + * @throws ServiceException + */ + private void storeVolumeEntry(VolumeEntry entry) throws IOException, + ServiceException { + // Get the book title(s). + for (Title t : entry.getTitles()) { + if (title == null) { + title = t.getValue(); + } else { + title += ", " + t.getValue(); + } + } + // Get the book creator(s). + for (Creator c : entry.getCreators()) { + if (author == null) { + author = c.getValue(); + } else { + author += ", " + c.getValue(); + } + } + // Get the book date. + if (entry.getDates().size() > 0) { + date = entry.getDates().get(0).getValue(); + } + // Get the book description. + if (entry.getDescriptions().size() > 0) { + description = entry.getDescriptions().get(0).getValue(); + } + // Get the book subject(s). + for (Subject s : entry.getSubjects()) { + if (subject == null) { + subject = s.getValue(); + } else { + subject += ", " + s.getValue(); + } + } + // Get the book publisher. + if (entry.getPublishers().size() > 0) { + publisher = entry.getPublishers().get(0).getValue(); + } + } +} \ No newline at end of file diff --git a/src/com/joshwalters/bookcatalog/isbnlookup/ISBNLookup.java b/src/com/joshwalters/bookcatalog/isbnlookup/ISBNLookup.java new file mode 100644 index 0000000..92d31ff --- /dev/null +++ b/src/com/joshwalters/bookcatalog/isbnlookup/ISBNLookup.java @@ -0,0 +1,286 @@ +/* + * Book Catalog - Catalog your book collection. + * + * Copyright (C) 2009 Joshua Walters + * URL: http://joshwalters.com + * + * This file is part of Book Catalog. + * + * Book Catalog is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Book Catalog is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Book Catalog. If not, see . + */ + +package com.joshwalters.bookcatalog.isbnlookup; + +import java.util.regex.Pattern; + +/** + * Abstract class for performing an ISBN lookup for a book. + * + * @author Josh Walters + */ +public abstract class ISBNLookup { + + /** + * The title of the book. + */ + protected String title; + /** + * The author of the book. + */ + protected String author; + /** + * The publisher of the book. + */ + protected String publisher; + /** + * The date the book was published. + */ + protected String date; + /** + * The description of the book. + */ + protected String description; + /** + * The subject of the book. + */ + protected String subject; + /** + * The books ISBN number. + */ + protected String ISBN; + /** + * The price of the book. + */ + protected String price; + + /** + * Purifies the input barcode to make it suitable for use in lookup. Also + * grabs the price from the barcode if it is present. + * + * @param barcode + * @return A purified barcode. + * @throws InvalidSearchTerms + */ + protected String purifyBarcode(String barcode) throws InvalidSearchTerms { + // Remove the extra characters that some scanners include + barcode = removeCharInString(barcode, ' '); + barcode = removeCharInString(barcode, '-'); + // Check to see if the barcode is made up of numbers. + if (!Pattern.matches("^\\d*$", barcode)) { + throw new InvalidSearchTerms(); + } + // Check to see that the barcode is at least 4 digits long. + if (barcode.length() < 4) { + throw new InvalidSearchTerms(); + } + // ISBN-13 + if (barcode.substring(0, 3).compareTo("978") == 0 + || barcode.substring(0, 3).compareTo("979") == 0) { + if (barcode.length() == 15) { + barcode = barcode.substring(0, 13); + } + if (barcode.length() == 18) { + price = barcode.substring(13); + barcode = barcode.substring(0, 13); + } + } + // ISBN-10 + else { + if (barcode.length() == 12) { + barcode = barcode.substring(0, 10); + } + if (barcode.length() == 15) { + price = barcode.substring(10); + barcode = barcode.substring(0, 10); + } + } + if (price != null) { + // Format price information + if (price.charAt(1) == '0') { + price = price.substring(2); + price = "$" + price.substring(0, 1) + "." + price.substring(1); + } else { + price = price.substring(1); + price = "$" + price.substring(0, 2) + "." + price.substring(2); + } + } + // Check to make sure that this is the proper length for an ISBN number. + if (barcode.length() != 10 & barcode.length() != 13) { + throw new InvalidSearchTerms(); + } + return barcode; + } + + /** + * Clear out all the data fields. + */ + protected void emptyDataFields() { + title = null; + author = null; + publisher = null; + date = null; + description = null; + subject = null; + ISBN = null; + price = null; + return; + } + + /** + * Removes character from string. + * + * @param s + * @param c + * @return The new string. + */ + private String removeCharInString(String s, char c) { + String result = ""; + for (int i = 0; i < s.length(); i++) { + if (s.charAt(i) != c) { + result += s.charAt(i); + } + } + return result; + } + + /** + * Searches for a book. + * + * @param barcode + * @throws NoBookFound + * @throws InvalidSearchTerms + */ + public abstract void searchBooks(String barcode) throws NoBookFound, + InvalidSearchTerms; + + /** + * @return the title + */ + public String getTitle() { + return title; + } + + /** + * @param title + * the title to set + */ + public void setTitle(String title) { + this.title = title; + } + + /** + * @return the author + */ + public String getAuthor() { + return author; + } + + /** + * @param author + * the author to set + */ + public void setAuthor(String author) { + this.author = author; + } + + /** + * @return the publisher + */ + public String getPublisher() { + return publisher; + } + + /** + * @param publisher + * the publisher to set + */ + public void setPublisher(String publisher) { + this.publisher = publisher; + } + + /** + * @return the date + */ + public String getDate() { + return date; + } + + /** + * @param date + * the date to set + */ + public void setDate(String date) { + this.date = date; + } + + /** + * @return the description + */ + public String getDescription() { + return description; + } + + /** + * @param description + * the description to set + */ + public void setDescription(String description) { + this.description = description; + } + + /** + * @return the subject + */ + public String getSubject() { + return subject; + } + + /** + * @param subject + * the subject to set + */ + public void setSubject(String subject) { + this.subject = subject; + } + + /** + * @return the ISBN + */ + public String getISBN() { + return ISBN; + } + + /** + * @param ISBN + * the ISBN to set + */ + public void setISBN(String ISBN) { + this.ISBN = ISBN; + } + + /** + * @return the price + */ + public String getPrice() { + return price; + } + + /** + * @param price + * the price to set + */ + public void setPrice(String price) { + this.price = price; + } +} \ No newline at end of file diff --git a/src/com/joshwalters/bookcatalog/isbnlookup/InvalidSearchTerms.java b/src/com/joshwalters/bookcatalog/isbnlookup/InvalidSearchTerms.java new file mode 100644 index 0000000..558cde1 --- /dev/null +++ b/src/com/joshwalters/bookcatalog/isbnlookup/InvalidSearchTerms.java @@ -0,0 +1,34 @@ +/* + * Book Catalog - Catalog your book collection. + * + * Copyright (C) 2009 Joshua Walters + * URL: http://joshwalters.com + * + * This file is part of Book Catalog. + * + * Book Catalog is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Book Catalog is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Book Catalog. If not, see . + */ + +package com.joshwalters.bookcatalog.isbnlookup; + +/** + * Thrown if the search terms are invalid. + * + * @author Josh Walters + */ +public class InvalidSearchTerms extends Exception { + + /** The serial ID */ + private static final long serialVersionUID = 8598966809541800761L; +} \ No newline at end of file diff --git a/src/com/joshwalters/bookcatalog/isbnlookup/NoBookFound.java b/src/com/joshwalters/bookcatalog/isbnlookup/NoBookFound.java new file mode 100644 index 0000000..1c909e0 --- /dev/null +++ b/src/com/joshwalters/bookcatalog/isbnlookup/NoBookFound.java @@ -0,0 +1,34 @@ +/* + * Book Catalog - Catalog your book collection. + * + * Copyright (C) 2009 Joshua Walters + * URL: http://joshwalters.com + * + * This file is part of Book Catalog. + * + * Book Catalog is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Book Catalog is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Book Catalog. If not, see . + */ + +package com.joshwalters.bookcatalog.isbnlookup; + +/** + * Thrown if the book is not found. + * + * @author Josh Walters + */ +public class NoBookFound extends Exception { + + /** The serial ID */ + private static final long serialVersionUID = -7276230896519983235L; +} \ No newline at end of file -- cgit v1.2.3-24-g4f1b