/*
 * @(#)WebTools.java    2.3.3  1 January 2005
 *
 * Copyright 2005
 * College of Computer and Information Science
 * Northeastern University
 * Boston, MA  02115
 *
 * The Java Power Tools software may be used for educational
 * purposes as long as this copyright notice is retained intact
 * at the top of all source files.
 *
 * To discuss possible commercial use of this software, 
 * contact Richard Rasala at Northeastern University, 
 * College of Computer and Information Science,
 * 617-373-2462 or rasala@ccs.neu.edu.
 *
 * The Java Power Tools software has been designed and built
 * in collaboration with Viera Proulx and Jeff Raab.
 *
 * Should this software be modified, the words "Modified from 
 * Original" must be included as a comment below this notice.
 *
 * All publication rights are retained.  This software or its 
 * documentation may not be published in any media either
 * in whole or in part without explicit permission.
 *
 * This software was created with support from Northeastern 
 * University and from NSF grant DUE-9950829.
 */

package edu.neu.ccs.util;

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>
 *
 * @author  Richard Rasala
 * @version 2.3.3
 * @since   2.3.3
 */
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 the input
     * stream reader associated with the URL.</p>
     * 
     * <p>Throws <code>RuntimeException</code> if an error occurs.</p>
     *
     * @param urlString the URL of a web site item as a string
     * @return the contents of the web site item as byte array data
     */
    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) {
            throw new RuntimeException(ex.getMessage());
        }
    }
    
    
    /**
     * <p>Returns the string contents of a web site text file with
     * the given URL string.</p>
     * 
     * <p>Assumes the data may be read in one call to the input
     * stream reader associated with the URL.</p>
     * 
     * <p>Throws <code>RuntimeException</code> if an error occurs.</p>
     *
     * @param urlString the URL of a web site text file as a string
     * @return the contents of the web site text file
     */
    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>Assumes the data may be read in one call to the input
     * stream reader associated with the URL.</p>
     * 
     * <p>Throws <code>RuntimeException</code> if an error occurs.</p>
     *
     * @param urlString the URL of a web site text file as a string
     * @return the non-empty lines of the web site text file
     */
    public static String[] readNonEmptyLinesFromWeb(String urlString) {
        String contents = readTextFileFromWeb(urlString);
        
        return TextTools.extractNonEmptyLines(contents);
    }
    
    
    /**
     * <p>Returns the file names in the filelist text file
     * located at the given filelist url.</p>
     * 
     * <p>Appends slash to the filelist url string if it is not
     * present.</p>
     * 
     * <p>Assumes that filelist is the name of a text file
     * at the filelist url that contains a list of file names.</p>
     *
     * <p>Assumes the data may be read in one call to the input
     * stream reader associated with the URL.</p>
     * 
     * <p>If an error occurs, returns a zero-length string array
     * rather than throw an exception.</p>
     *
     * @param filelistURL the URL of a text file with file names
     * @param filelist the name of a text file with file names
     * @return the array of file names
     */
    public static String[] readFileNamesFromWeb
    	(String filelistURL, String filelist)
    {
        try {
            String fullURL = appendSlashIfNeeded(filelistURL) + filelist;
            
    	    return readNonEmptyLinesFromWeb(fullURL);
        }
        catch (Exception ex) {
            return new String[0];
        }
    }
    
    
    /**
     * Appends a slash to a URL string if it is not present.
     *
     * @param baseURL a URL string that may need a trailing slash
     * @return the base URL with possibly a trailing slash appended
     */
    public static String appendSlashIfNeeded(String baseURL) {
        if (baseURL == null)
            return baseURL;
        
        int length = baseURL.length();
        
        if (length == 0)
            return baseURL;
        
        if (! (baseURL.charAt(length-1) == '/'))
            baseURL += "/";
        
        return baseURL;
    }
    
}

