KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > activation > MIMETypeParser


1 /*
2   GNU-Classpath Extensions: java bean activation framework
3   Copyright (C) 2000 2001 Andrew Selkirk
4
5   For more information on the classpathx please mail:
6   nferrier@tapsellferrier.co.uk
7
8   This program is free software; you can redistribute it and/or
9   modify it under the terms of the GNU Lesser General Public License
10   as published by the Free Software Foundation; either version 2
11   of the License, or (at your option) any later version.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16   GNU General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with this program; if not, write to the Free Software
20   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 */

22 package gnu.activation;
23
24 // Imports
25
import javax.activation.MimeType JavaDoc;
26 import java.io.Reader JavaDoc;
27 import java.io.StreamTokenizer JavaDoc;
28 import java.util.Hashtable JavaDoc;
29
30 /**
31  * MIME Type Parser
32  * @author Andrew Selkirk
33  * @author Nic Ferrier
34  * @version $Revision: 1.1 $
35  */

36 public class MIMETypeParser {
37
38   private static final int STARTLINE = 0;
39   private static final int READTYPE = 1;
40   private static final int READEXT = 2;
41
42   /**
43    * Parse MIME type formatted data stream
44    * @param reader Reader
45    * @return Mapping of extension to MIMEType object
46    */

47   public static Hashtable JavaDoc parseStream(Reader JavaDoc reader) {
48
49     // Variables
50
Hashtable JavaDoc registry;
51     int state; // State Register
52
MimeType JavaDoc mimetype;
53     StreamTokenizer JavaDoc tokens;
54     StringBuffer JavaDoc mimeTypeBuffer;
55     StringBuffer JavaDoc extBuffer;
56
57     // Initialize
58
registry = new Hashtable JavaDoc();
59     state = READTYPE;
60     mimetype = null;
61     mimeTypeBuffer = new StringBuffer JavaDoc();
62     extBuffer = new StringBuffer JavaDoc();
63
64     // Initialize Tokenizer
65
tokens = new StreamTokenizer JavaDoc(reader);
66     tokens.commentChar('#');
67     tokens.eolIsSignificant(true);
68     tokens.wordChars('/', '/');
69     tokens.wordChars('-', '-');
70
71     try
72     {
73       while (true)
74       {
75         switch (tokens.nextToken())
76         {
77         case StreamTokenizer.TT_EOF:
78           return registry;
79         case StreamTokenizer.TT_EOL:
80           switch (state)
81           {
82           case READEXT:
83             //set the state
84
state = READTYPE;
85             continue;
86           default:
87             continue;
88           }
89         case StreamTokenizer.TT_WORD:
90           switch(state)
91           {
92             case READTYPE:
93               //type has been read - create the object
94
mimetype = new MimeType JavaDoc(tokens.sval);
95               state = READEXT;
96               continue;
97             case READEXT:
98               //the extension has been read - store the
99
// mimetype against it
100
registry.put(tokens.sval, mimetype);
101               continue;
102             default:
103               continue;
104           } // switch
105
} // switch
106
} // while
107
}
108     catch (Exception JavaDoc e) {
109       //this is only here for debugging
110
e.printStackTrace();
111     } // try
112

113     // Return registry
114
return registry;
115
116   } // parseStream()
117

118
119 } // MIMETypeParser
120
Popular Tags