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.  
  5.  
  6.  
  7. class MSIEParser  {
  8.  
  9. //
  10.  
  11. // MSIEParser.java  -- Parses Internet Explorer Shortcuts
  12.  
  13. //                     using StreamTokenizer in a state machine
  14.  
  15. //
  16.  
  17. // David Moisan, May 9th, 1996
  18.  
  19. // Version 0.9a
  20.  
  21. //
  22.  
  23. // Notes:
  24.  
  25. //
  26.  
  27. // Internet Shortcut files are in the form:
  28.  
  29. //
  30.  
  31. // [InternetShortcut]
  32.  
  33. //    URL=http://www.somewhere.com/~someone
  34.  
  35. //
  36.  
  37. //
  38.  
  39. // Note the lack of quotes on the "http://..."
  40.  
  41. // 
  42.  
  43.  
  44.  
  45. //
  46.  
  47. // Labels for the state-machine parser
  48.  
  49. //
  50.  
  51.  
  52.  
  53. static final int START = 0;    // START: Initial state, nothing read
  54.  
  55. static final int HEADER = 1;   // HEADER: Header found
  56.  
  57. static final int URL = 2;      // URL: "URL" found 
  58.  
  59. static final int LINK = 3;     // LINK: "=" in "URL=" found;
  60.  
  61. static final int END = 10;     // END: URL found succesfully, end state
  62.  
  63. static final int ERROR = 1000; // ERROR: Elements not found or parser error
  64.  
  65.  
  66.  
  67.  
  68.  
  69. String MSIEGetURL(String filename) throws IOException {
  70.  
  71.  
  72.  
  73. //
  74.  
  75. // Open Internet Shortcut file and initialize;
  76.  
  77. //
  78.  
  79.    
  80.  
  81.     FileInputStream IEStream = new FileInputStream(filename);    
  82.  
  83.     StreamTokenizer IEP = new StreamTokenizer(IEStream);
  84.  
  85.  
  86.  
  87. //
  88.  
  89. // Initialize parser
  90.  
  91. //
  92.  
  93.  
  94.  
  95.     IEP.resetSyntax(); // All characters special
  96.  
  97.     IEP.eolIsSignificant(true); // return end of line (EOL)
  98.  
  99.     IEP.whitespaceChars(0x11, 0x20); // Whitespace is " "
  100.  
  101.     IEP.wordChars(0x21,0x7E); // All ASCII printable chars are words
  102.  
  103.     IEP.ordinaryChar((int)'=');    // Equal sign is ignored
  104.  
  105.     IEP.ordinaryChar((int)'\"'); // Same with quotes   
  106.  
  107.    
  108.  
  109.    int ParseState = START;
  110.  
  111.     String URL_St = " ";
  112.  
  113.     int i;
  114.  
  115.  
  116.  
  117. //
  118.  
  119. // Parsing loop
  120.  
  121. //
  122.  
  123.  
  124.  
  125.     while((ParseState != ERROR) && (ParseState != END)) {
  126.  
  127.       IEP.nextToken();
  128.  
  129.            
  130.  
  131.       switch(IEP.ttype) {
  132.  
  133.             case IEP.TT_WORD:
  134.  
  135.                 
  136.  
  137.                 switch (ParseState) {
  138.  
  139.                    case START: 
  140.  
  141.                        if (IEP.sval.indexOf("[InternetShortcut]") != -1) 
  142.  
  143.                             ParseState = HEADER;
  144.  
  145.                         else
  146.  
  147.                             ParseState = ERROR;
  148.  
  149.                         break;
  150.  
  151.  
  152.  
  153.                     case HEADER:
  154.  
  155.                         
  156.  
  157.                         if (IEP.sval.indexOf("URL")!=-1)
  158.  
  159.                             ParseState = URL;
  160.  
  161.                         else 
  162.  
  163.                             ParseState = ERROR;
  164.  
  165.                         break;
  166.  
  167.     
  168.  
  169.                     case URL:
  170.  
  171.                                 // no action, waiting for "=";
  172.  
  173.                         
  174.  
  175.                        break;
  176.  
  177.                     
  178.  
  179.                case LINK:
  180.  
  181.                         URL_St = IEP.sval; 
  182.  
  183.                         // We've gotten the URL, we're done
  184.  
  185.                         ParseState = END;
  186.  
  187.                         break; 
  188.  
  189.                     }
  190.  
  191.                 break;
  192.  
  193.  
  194.  
  195.             case IEP.TT_EOF:
  196.  
  197.                 if (ParseState != END)    
  198.  
  199.                     ParseState = ERROR;
  200.  
  201.                 break;            
  202.  
  203.  
  204.  
  205.             case (int)'=':
  206.  
  207.                 if (ParseState == URL) {
  208.  
  209.                ParseState = LINK;
  210.  
  211.                     IEP.wordChars((int)'=', (int)'='); 
  212.  
  213.                // Make '=' a special character so that
  214.  
  215.                // CGI-type URL's don't get mangled
  216.  
  217.                   }
  218.  
  219.               else
  220.  
  221.                     ParseState = ERROR;
  222.  
  223.                 break;
  224.  
  225.  
  226.  
  227.             case IEP.TT_EOL:
  228.  
  229.  
  230.  
  231.                 switch (ParseState) {
  232.  
  233.                     case URL:
  234.  
  235.                     case LINK:
  236.  
  237.                         ParseState=ERROR;  // Handle spurious
  238.  
  239.                         break;                // end-of-line chars.
  240.  
  241.  
  242.  
  243.                     default:              
  244.  
  245.                         break;
  246.  
  247.                }
  248.  
  249.             
  250.  
  251.          default:
  252.  
  253.             
  254.  
  255.             // Ignore all other cases for now
  256.  
  257.                 break;
  258.  
  259.          }
  260.  
  261.         }
  262.  
  263.     if (ParseState == ERROR) {
  264.  
  265.         IEStream.close();
  266.  
  267.         IEStream = null;    
  268.  
  269.         throw new BadIEShortcut("URL not found in file: "+filename);
  270.  
  271.         }
  272.  
  273.     else if (ParseState != END) {
  274.  
  275.         IEStream.close();
  276.  
  277.         IEStream = null;            
  278.  
  279.         throw new BadIEShortcut
  280.  
  281.             ("Parser error, ParseState= "+ParseState+" File:"+filename);
  282.  
  283.         }    
  284.  
  285.         
  286.  
  287.     IEStream.close();    
  288.  
  289.     IEStream = null;
  290.  
  291.     return URL_St;
  292.  
  293.     }
  294.  
  295. }
  296.  
  297.  
  298.  
  299. //
  300.  
  301. // BadIEShortcut -- defines exception thrown in MSIEParser
  302.  
  303. //
  304.  
  305.  
  306.  
  307. class BadIEShortcut extends IOException {
  308.  
  309.     public BadIEShortcut() {
  310.  
  311.         super();
  312.  
  313.         }
  314.  
  315.     public BadIEShortcut(String s) {
  316.  
  317.         super(s);
  318.  
  319.         }
  320.  
  321. }