/* @(#)WebTools.java 1.0  1 November 2004 */

import java.io.*;
import java.util.*;
import java.net.*;

/**
 * <p>Class <code>WebTools</code> is a special purpose class for
 * reading data from a web site.</p>
 */

public class WebTools {

    /** Prevent instantiation. */
    private WebTools() { }
    
    
    /**
     * <p>Returns the byte array of data from the web site item
     * with the given URL string.</p>
     * 
     * <p>Assumes the data may be read in one call to read.</p>
     * 
     * <p>If an error occurs, returns <code>new byte[0]</code>.</p>
     */
    public static byte[] readDataFromWeb(String urlString) {
        try {
	        URL url = new URL(urlString);
	        
	        InputStream stream = url.openStream();
	        
	        byte[] data = new byte[stream.available()];
	        
	        stream.read(data);
	        
	        stream.close();
	        
	        return data;
        }
        catch (Exception ex) {
            return new byte[0];
        }
    }
    
    
    /**
     * <p>Returns the string contents of a web site text file
     * with the given URL string.</p>
     * 
     * <p>If an error occurs, returns the empty string.</p>
     */
    public static String readTextFileFromWeb(String urlString) {
        byte[] data = readDataFromWeb(urlString);
        
        return new String(data);
    }
    
    
    /**
     * <p>Returns the non-empty lines from the string contents
     * of a web site text file with the given URL string.</p>
     * 
     * <p>If an error occurs, returns the empty string array.</p>
     */
    public static String[] readTextFileLinesFromWeb(String urlString) {
        String contents = readTextFileFromWeb(urlString);
        
        return extractNonEmptyLines(contents);
    }
    
    
    /** Returns the non-empty lines extracted from a string. */
    public static String[] extractNonEmptyLines(String string) {
        if ((string == null) || (string.length() == 0))
            return new String[0];
        
        StringTokenizer tokenizer =
            new StringTokenizer(string, "\n");
        
        Vector list = new Vector();
        
        while (tokenizer.hasMoreTokens()) {
            String token = tokenizer.nextToken();
            
            if (token.length() > 0)
                list.add(token);
        }
        
        return (String[]) list.toArray(new String[0]);
    }
    
    
    /**
     * <p>Returns the file names in the filelist text file
     * located at the given base url.</p>
     * 
     * <p>Appends slash to the base url string if it is
     * not present.</p>
     * 
     * <p>Assumes that filelist is the name of a text file
     * at the base url that contains the file names.</p>
     *
     * <p>If an error occurs, returns an empty string array.</p>
     */
    public static String[] readFileNamesFromWeb
    	(String baseURL, String filelist)
    {
        String fullURL = appendSlashIfNeeded(baseURL) + filelist;
        
        try {
	        return readTextFileLinesFromWeb(fullURL);
        }
        catch (Exception ex) {
            return new String[0];
        }
    }
    
    
    /** Appends a slash to a URL string if it is not present. */
    public static String appendSlashIfNeeded(String baseURL) {
        int length = baseURL.length();
        
        if (! (baseURL.charAt(length-1) == '/'))
            baseURL += "/";
        
        return baseURL;
    }
}
