CNTT  Thảo luận  Java  Get list of APPLET tags in one HTML file
Disabled Previous
 
Next Next
New Post 11/22/2007 8:24 AM
  tuankn
2 posts


Get list of APPLET tags in one HTML file  

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Vector;

/**
 * Get list of APPLET tags in one HTML file.
 
 @author Ian Darwin, Darwin Open Systems, www.darwinsys.com. Routine "readTag"
 *         shamelessly stolen from Elliott Rusty Harold's "ImageSizer" program.
 */
public class AppletFinder {

  public static void main(String[] av) {
    String[] tags = new AppletFinder().doPage(av[0]);
    for (int i = 0; i < tags.length; i++)
      System.out.println("APPLET: " + i + " " + tags[i]);
  }

  public String[] doPage(String pageURL) {
    String result[] null;
    URL root;

    if (pageURL != null) {
      //Open the URL for reading
      try {
        if (pageURL.indexOf(":"!= -1) {
          root = new URL(pageURL);
        else {
          root = new URL("http://" + pageURL);
        }
        result = findApplets(root);
      catch (MalformedURLException e) {
        System.err.println(pageURL + " is not a parseable URL");
        System.err.println(e);
      }
    }
    return result;
  }

  public String[] findApplets(URL u) {
    BufferedReader inrdr = null;
    Vector v = new Vector();
    char thisChar = 0;

    try {
      inrdr = new BufferedReader(new InputStreamReader(u.openStream()));
      int i;
      while ((i = inrdr.read()) != -1) {
        thisChar = (chari;
        if (thisChar == '<') {
          String tag = readTag(inrdr);
          // System.out.println("TAG: " + tag);
          if (tag.toUpperCase().startsWith("<APPLET"))
            v.addElement(tag);
        }
      }
      inrdr.close();
    catch (IOException e) {
      System.err.println("Error reading from main URL: " + e);
    }
    String applets[] new String[v.size()];
    v.copyInto(applets);
    return applets;
  }

  public String readTag(BufferedReader is) {
    StringBuffer theTag = new StringBuffer("<");
    char theChar = '<';

    try {
      while (theChar != '>' && ((theChar = (charis.read()) != -1)) {
        theTag.append(theChar);
      // end while
    // end try
    catch (IOException e) {
      System.err.println(e);
    }

    return theTag.toString();
  }
}

 
Disabled Previous
 
Next Next
  CNTT  Thảo luận  Java  Get list of APPLET tags in one HTML file