home *** CD-ROM | disk | FTP | other *** search
- import java.io.*;
-
-
- class MSIEParser {
- //
- // MSIEParser.java -- Parses Internet Explorer Shortcuts
- // using StreamTokenizer in a state machine
- //
- // David Moisan, May 9th, 1996
- // Version 0.9a
- //
- // Notes:
- //
- // Internet Shortcut files are in the form:
- //
- // [InternetShortcut]
- // URL=http://www.somewhere.com/~someone
- //
- //
- // Note the lack of quotes on the "http://..."
- //
-
- //
- // Labels for the state-machine parser
- //
-
- static final int START = 0; // START: Initial state, nothing read
- static final int HEADER = 1; // HEADER: Header found
- static final int URL = 2; // URL: "URL" found
- static final int LINK = 3; // LINK: "=" in "URL=" found;
- static final int END = 10; // END: URL found succesfully, end state
- static final int ERROR = 1000; // ERROR: Elements not found or parser error
-
-
- String MSIEGetURL(String filename) throws IOException {
-
- //
- // Open Internet Shortcut file and initialize;
- //
-
- FileInputStream IEStream = new FileInputStream(filename);
- StreamTokenizer IEP = new StreamTokenizer(IEStream);
-
- //
- // Initialize parser
- //
-
- IEP.resetSyntax(); // All characters special
- IEP.eolIsSignificant(true); // return end of line (EOL)
- IEP.whitespaceChars(0x11, 0x20); // Whitespace is " "
- IEP.wordChars(0x21,0x7E); // All ASCII printable chars are words
- IEP.ordinaryChar((int)'='); // Equal sign is ignored
- IEP.ordinaryChar((int)'\"'); // Same with quotes
-
- int ParseState = START;
- String URL_St = " ";
- int i;
-
- //
- // Parsing loop
- //
-
- while((ParseState != ERROR) && (ParseState != END)) {
- IEP.nextToken();
-
- switch(IEP.ttype) {
- case IEP.TT_WORD:
-
- switch (ParseState) {
- case START:
- if (IEP.sval.indexOf("[InternetShortcut]") != -1)
- ParseState = HEADER;
- else
- ParseState = ERROR;
- break;
-
- case HEADER:
-
- if (IEP.sval.indexOf("URL")!=-1)
- ParseState = URL;
- else
- ParseState = ERROR;
- break;
-
- case URL:
- // no action, waiting for "=";
-
- break;
-
- case LINK:
- URL_St = IEP.sval;
- // We've gotten the URL, we're done
- ParseState = END;
- break;
- }
- break;
-
- case IEP.TT_EOF:
- if (ParseState != END)
- ParseState = ERROR;
- break;
-
- case (int)'=':
- if (ParseState == URL) {
- ParseState = LINK;
- IEP.wordChars((int)'=', (int)'=');
- // Make '=' a special character so that
- // CGI-type URL's don't get mangled
- }
- else
- ParseState = ERROR;
- break;
-
- case IEP.TT_EOL:
-
- switch (ParseState) {
- case URL:
- case LINK:
- ParseState=ERROR; // Handle spurious
- break; // end-of-line chars.
-
- default:
- break;
- }
-
- default:
-
- // Ignore all other cases for now
- break;
- }
- }
- if (ParseState == ERROR) {
- IEStream.close();
- IEStream = null;
- throw new BadIEShortcut("URL not found in file: "+filename);
- }
- else if (ParseState != END) {
- IEStream.close();
- IEStream = null;
- throw new BadIEShortcut
- ("Parser error, ParseState= "+ParseState+" File:"+filename);
- }
-
- IEStream.close();
- IEStream = null;
- return URL_St;
- }
- }
-
- //
- // BadIEShortcut -- defines exception thrown in MSIEParser
- //
-
- class BadIEShortcut extends IOException {
- public BadIEShortcut() {
- super();
- }
- public BadIEShortcut(String s) {
- super(s);
- }
- }