home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / APPLETS / MSIEPARS.JAV < prev    next >
Encoding:
Text File  |  1997-02-27  |  3.7 KB  |  161 lines

  1. import java.io.*;
  2.  
  3.  
  4. class MSIEParser  {
  5. //
  6. // MSIEParser.java  -- Parses Internet Explorer Shortcuts
  7. //                     using StreamTokenizer in a state machine
  8. //
  9. // David Moisan, May 9th, 1996
  10. // Version 0.9a
  11. //
  12. // Notes:
  13. //
  14. // Internet Shortcut files are in the form:
  15. //
  16. // [InternetShortcut]
  17. //    URL=http://www.somewhere.com/~someone
  18. //
  19. //
  20. // Note the lack of quotes on the "http://..."
  21. // 
  22.  
  23. //
  24. // Labels for the state-machine parser
  25. //
  26.  
  27. static final int START = 0;    // START: Initial state, nothing read
  28. static final int HEADER = 1;   // HEADER: Header found
  29. static final int URL = 2;      // URL: "URL" found 
  30. static final int LINK = 3;     // LINK: "=" in "URL=" found;
  31. static final int END = 10;     // END: URL found succesfully, end state
  32. static final int ERROR = 1000; // ERROR: Elements not found or parser error
  33.  
  34.  
  35. String MSIEGetURL(String filename) throws IOException {
  36.  
  37. //
  38. // Open Internet Shortcut file and initialize;
  39. //
  40.    
  41.     FileInputStream IEStream = new FileInputStream(filename);    
  42.     StreamTokenizer IEP = new StreamTokenizer(IEStream);
  43.  
  44. //
  45. // Initialize parser
  46. //
  47.  
  48.     IEP.resetSyntax(); // All characters special
  49.     IEP.eolIsSignificant(true); // return end of line (EOL)
  50.     IEP.whitespaceChars(0x11, 0x20); // Whitespace is " "
  51.     IEP.wordChars(0x21,0x7E); // All ASCII printable chars are words
  52.     IEP.ordinaryChar((int)'=');    // Equal sign is ignored
  53.     IEP.ordinaryChar((int)'\"'); // Same with quotes   
  54.    
  55.    int ParseState = START;
  56.     String URL_St = " ";
  57.     int i;
  58.  
  59. //
  60. // Parsing loop
  61. //
  62.  
  63.     while((ParseState != ERROR) && (ParseState != END)) {
  64.       IEP.nextToken();
  65.            
  66.       switch(IEP.ttype) {
  67.             case IEP.TT_WORD:
  68.                 
  69.                 switch (ParseState) {
  70.                    case START: 
  71.                        if (IEP.sval.indexOf("[InternetShortcut]") != -1) 
  72.                             ParseState = HEADER;
  73.                         else
  74.                             ParseState = ERROR;
  75.                         break;
  76.  
  77.                     case HEADER:
  78.                         
  79.                         if (IEP.sval.indexOf("URL")!=-1)
  80.                             ParseState = URL;
  81.                         else 
  82.                             ParseState = ERROR;
  83.                         break;
  84.     
  85.                     case URL:
  86.                                 // no action, waiting for "=";
  87.                         
  88.                        break;
  89.                     
  90.                case LINK:
  91.                         URL_St = IEP.sval; 
  92.                         // We've gotten the URL, we're done
  93.                         ParseState = END;
  94.                         break; 
  95.                     }
  96.                 break;
  97.  
  98.             case IEP.TT_EOF:
  99.                 if (ParseState != END)    
  100.                     ParseState = ERROR;
  101.                 break;            
  102.  
  103.             case (int)'=':
  104.                 if (ParseState == URL) {
  105.                ParseState = LINK;
  106.                     IEP.wordChars((int)'=', (int)'='); 
  107.                // Make '=' a special character so that
  108.                // CGI-type URL's don't get mangled
  109.                   }
  110.               else
  111.                     ParseState = ERROR;
  112.                 break;
  113.  
  114.             case IEP.TT_EOL:
  115.  
  116.                 switch (ParseState) {
  117.                     case URL:
  118.                     case LINK:
  119.                         ParseState=ERROR;  // Handle spurious
  120.                         break;                // end-of-line chars.
  121.  
  122.                     default:              
  123.                         break;
  124.                }
  125.             
  126.          default:
  127.             
  128.             // Ignore all other cases for now
  129.                 break;
  130.          }
  131.         }
  132.     if (ParseState == ERROR) {
  133.         IEStream.close();
  134.         IEStream = null;    
  135.         throw new BadIEShortcut("URL not found in file: "+filename);
  136.         }
  137.     else if (ParseState != END) {
  138.         IEStream.close();
  139.         IEStream = null;            
  140.         throw new BadIEShortcut
  141.             ("Parser error, ParseState= "+ParseState+" File:"+filename);
  142.         }    
  143.         
  144.     IEStream.close();    
  145.     IEStream = null;
  146.     return URL_St;
  147.     }
  148. }
  149.  
  150. //
  151. // BadIEShortcut -- defines exception thrown in MSIEParser
  152. //
  153.  
  154. class BadIEShortcut extends IOException {
  155.     public BadIEShortcut() {
  156.         super();
  157.         }
  158.     public BadIEShortcut(String s) {
  159.         super(s);
  160.         }
  161. }