/* Some simple code to show what you can do with java in xsl (c) Casey Durfee 8.6.2004 no legal culpability taken by the author, and no warranty given. By using this software you agree to abide by the terms of Amazon.com's web services usage rules, and not sue me even if something tragically horrible goes wrong. */ package org.spl; import java.io.*; import java.net.*; public class XSLUtil{ private static String devToken; // you need to get your own from amazon. private static String assocID = "webservices-20"; private static int useCount = 0; private static long lastUseTime = System.currentTimeMillis(); private static String cacheDir = "./AmazonCache/"; private static long startTime; public static void setDevID (String s){ devToken = s; } public static void setAssocID (String s){ assocID = s; } public static synchronized boolean checkTime(){ // returns true if it's been more than a second since the last time it was used and updates the timestamp. // otherwise returns false. if(System.currentTimeMillis() - lastUseTime > 1000 || useCount == 0){ lastUseTime = System.currentTimeMillis(); useCount += 1; return true; } else { return false; } } public static synchronized long getlastUseTime() { return lastUseTime; } /* start cache functions TODO: -better error handling - split off cache functions so they are in their own class and improve file locking. To just lock a single file, you need to use the java.nio.channels package. See java almanac e181 for the basics. */ public static boolean isInCache( String ISBN) { File fileOn = new File ( cacheDir + ISBN + ".xml"); if( fileOn.exists() ){ return true; } else { return false; } } public static synchronized void writeCache( String ISBN, String data) { try { FileWriter fw = new FileWriter( cacheDir + ISBN + ".xml"); fw.write( data ) ; fw.flush(); fw.close(); } catch(IOException e){ //do nothing for now } } public static String readCache( String ISBN) { try{ File fileOn = new File(cacheDir + ISBN + ".xml"); FileReader fr = new FileReader( fileOn ); int fileLength = (int) fileOn.length(); char[] acTmp = new char[fileLength]; for(int k = 0; k < fileLength; k += fr.read(acTmp, k, fileLength-k) ); fr.close(); return new String(acTmp); } catch(FileNotFoundException e) { return ""; } catch (IOException ee) { return ""; } } /* end cache functions */ public static String getURL(String URLName){ StringBuffer response = new StringBuffer(); BufferedReader br = null; try{ URL u = new java.net.URL(URLName); HttpURLConnection hu = (HttpURLConnection) u.openConnection(); hu.setFollowRedirects(true); br = new BufferedReader(new InputStreamReader( hu.getInputStream())); String lineOn = ""; while((lineOn = br.readLine()) != null){ response.append(lineOn); } } catch(Exception e){ e.printStackTrace(); return ""; } finally{ try{ if(br != null) br.close(); } catch(Exception e){ e.printStackTrace(); } } return response.toString(); } public static String extractTagInfo(String rawXML, String tagName){ int startOfOpen = rawXML.indexOf( tagName ); int endOfOpen = -1; if(startOfOpen > -1) { startOfOpen += tagName.length(); endOfOpen = rawXML.indexOf(">", startOfOpen) + 1; // make sure you get any attributes assoc. with tag as well. if( rawXML.substring(endOfOpen -1, endOfOpen).equals("/") ){ return ""; } } else { // tag not present return ""; } int beginOfClose = rawXML.indexOf( " -1){ return rawXML.substring(endOfOpen, beginOfClose); } else { return ""; } } public static String getAmazonXMLbyISBN( String ISBN ) { // tries to get from the cache, if not in cache it gets from the internet. // checks last use time before getting it from internet. ISBN = ISBN.trim(); if (ISBN.length() == 0) { return ""; } if (isInCache(ISBN) ) { return readCache(ISBN); } else { if( checkTime() ){ String XMLURL = "http://xml.amazon.com/onca/xml3?t=" + assocID + "&dev-t=" + devToken + "&AsinSearch=" + ISBN + "&mode=books&type=heavy&page=1&f=xml"; String tempContent = getURL(XMLURL); writeCache(ISBN, tempContent); return tempContent; } else { return ""; } } } /* begin convenience functions to get specific Amazon attributes */ public static String getAmazonSalesRank(String ISBN){ //returns the sales rank from the amazon.com site using their xml api return extractTagInfo( getAmazonXMLbyISBN(ISBN), "SalesRank"); } public static String getAmazonDescription(String ISBN){ //returns the ProductDescription from the amazon.com site using their xml api // for dynix's xslprocessor, if you want something to be treated as HTML, you have to wrap it in $html$ dealies. // that's all it does to a dictionary or a marc map whose type you display as HTML. The XSL processor will strip those off... return "$html$" + extractTagInfo( getAmazonXMLbyISBN( ISBN ), "ProductDescription") + "$html$"; } public static String getAmazonCustomerRating(String ISBN){ return extractTagInfo( getAmazonXMLbyISBN( ISBN ), "AvgCustomerRating"); } public static String getAmazonBookJacketURLSmall( String ISBN ){ return extractTagInfo( getAmazonXMLbyISBN( ISBN ), "ImageUrlSmall"); } public static String getAmazonBookJacketURLMedium( String ISBN ){ return extractTagInfo( getAmazonXMLbyISBN( ISBN ), "ImageUrlMedium"); } public static String getAmazonBookJacketURLLarge( String ISBN ){ return extractTagInfo( getAmazonXMLbyISBN( ISBN ), "ImageUrlLarge"); } public static String getAmazonReleaseDate( String ISBN ){ return extractTagInfo( getAmazonXMLbyISBN( ISBN ), "ReleaseDate"); } /* get an attribute that you specify. */ public static String getAmazonAttribute(String ISBN, String attribute){ return extractTagInfo( getAmazonXMLbyISBN( ISBN ), attribute); } public static String getAmazonAttributeHTML(String ISBN, String attribute) { // use this version of the function if the content you are getting might have HTML markup in it. return "$html$" + extractTagInfo( getAmazonXMLbyISBN( ISBN ), attribute) + "$html$"; } /* get the weather from the national weather service based on airport code. For instance KSEA is the code for Sea-Tac airport. see http://www.nws.noaa.gov/data/current_obs/ */ public static String getWeather( String airportCode) { String theURL = "http://www.nws.noaa.gov/data/current_obs/" + airportCode; String rawXML = getURL( theURL); return extractTagInfo( rawXML, "weather"); } public static String getTemperature( String airportCode) { String theURL = "http://www.nws.noaa.gov/data/current_obs/" + airportCode; String rawXML = getURL( theURL); return extractTagInfo( rawXML, "temperature_string"); } public static void main(String[] args){ // call with an ISBN as an argument to test. startTime = System.currentTimeMillis(); System.out.println( getAmazonDescription( args[0] ) ); System.out.println( "Elapsed time in milliseconds: " + (System.currentTimeMillis() - startTime) ); System.out.println( getWeather( "KSEA") ); } }