Source Code Cross Referenced for MimeType.java in  » 6.0-JDK-Modules » j2me » java » awt » datatransfer » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » 6.0 JDK Modules » j2me » java.awt.datatransfer 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * @(#)MimeType.java	1.9 06/10/10
003:         *
004:         * Copyright  1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006:         * 
007:         * This program is free software; you can redistribute it and/or
008:         * modify it under the terms of the GNU General Public License version
009:         * 2 only, as published by the Free Software Foundation. 
010:         * 
011:         * This program is distributed in the hope that it will be useful, but
012:         * WITHOUT ANY WARRANTY; without even the implied warranty of
013:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014:         * General Public License version 2 for more details (a copy is
015:         * included at /legal/license.txt). 
016:         * 
017:         * You should have received a copy of the GNU General Public License
018:         * version 2 along with this work; if not, write to the Free Software
019:         * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020:         * 02110-1301 USA 
021:         * 
022:         * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023:         * Clara, CA 95054 or visit www.sun.com if you need additional
024:         * information or have any questions. 
025:         *
026:         */
027:
028:        package java.awt.datatransfer;
029:
030:        import java.io.Externalizable;
031:        import java.io.ObjectOutput;
032:        import java.io.ObjectInput;
033:        import java.io.IOException;
034:        import java.util.Enumeration;
035:
036:        /**
037:         * A Multipurpose Internet Mail Extension (MIME) type, as defined
038:         * in RFC 2045 and 2046.
039:         *
040:         * THIS IS *NOT* - REPEAT *NOT* - A PUBLIC CLASS! DataFlavor IS
041:         * THE PUBLIC INTERFACE, AND THIS IS PROVIDED AS A ***PRIVATE***
042:         * (THAT IS AS IN *NOT* PUBLIC) HELPER CLASS!
043:         */
044:        class MimeType implements  Externalizable, Cloneable {
045:            /*
046:             * serialization support
047:             */
048:
049:            static final long serialVersionUID = -6568722458793895906L;
050:
051:            /**
052:             * Constructor for externalization. This constructor should not be
053:             * called directly by an application, since the result will be an
054:             * uninitialized, immutable MimeType object.
055:             */
056:            public MimeType() {
057:            }
058:
059:            /**
060:             * Constructor that builds a MimeType from a String.
061:             */
062:            public MimeType(String rawdata) throws MimeTypeParseException {
063:                parse(rawdata);
064:            }
065:
066:            /**
067:             * Constructor that builds a MimeType with the given primary and sub
068:             type
069:             * but has an empty parameter list.
070:             */
071:            public MimeType(String primary, String sub)
072:                    throws MimeTypeParseException {
073:                this (primary, sub, new MimeTypeParameterList());
074:            }
075:
076:            /**
077:             * Constructor used to initialize MimeType, with a pre-defined 
078:             * and valid (or empty) parameter list.
079:             */
080:
081:            public MimeType(String primary, String sub,
082:                    MimeTypeParameterList mtpl) throws MimeTypeParseException {
083:                //    check to see if primary is valid
084:                if (isValidToken(primary)) {
085:                    primaryType = primary.toLowerCase();
086:                } else {
087:                    throw new MimeTypeParseException("Primary type is invalid.");
088:                }
089:                //    check to see if sub is valid
090:                if (isValidToken(sub)) {
091:                    subType = sub.toLowerCase();
092:                } else {
093:                    throw new MimeTypeParseException("Sub type is invalid.");
094:                }
095:                try {
096:                    mtpl = (MimeTypeParameterList) mtpl.clone();
097:                } catch (CloneNotSupportedException cnse) {
098:                    throw new RuntimeException("failed to clone parameter list");
099:                }
100:                try {
101:                    parameters = (MimeTypeParameterList) mtpl.clone();
102:                } catch (CloneNotSupportedException cnse) {
103:                    throw new RuntimeException("cannot clone parameter list");
104:                }
105:            }
106:
107:            public int hashCode() {
108:                // We sum up the hash codes for all of the strings. This
109:                // way, the order of the strings is irrelevant
110:                int code = 0;
111:                code += primaryType.hashCode();
112:                code += subType.hashCode();
113:                code += parameters.hashCode();
114:                return code;
115:            } // hashCode()
116:
117:            /**
118:             * MimeTypes are equals if their primary types, subtypes, and
119:             * parameters are all equal. No default values are taken into
120:             * account.
121:             */
122:            public boolean equals(Object thatObject) {
123:                if (!(thatObject instanceof  MimeType)) {
124:                    return false;
125:                }
126:                MimeType that = (MimeType) thatObject;
127:                boolean isIt = ((this .primaryType.equals(that.primaryType))
128:                        && (this .subType.equals(that.subType)) && (this .parameters
129:                        .equals(that.parameters)));
130:                return isIt;
131:            } // equals()
132:
133:            /**
134:             * A routine for parsing the MIME type out of a String.
135:             */
136:            private void parse(String rawdata) throws MimeTypeParseException {
137:                //System.out.println("MimeType.parse("+rawdata+")");
138:                int slashIndex = rawdata.indexOf('/');
139:                int semIndex = rawdata.indexOf(';');
140:                if ((slashIndex < 0) && (semIndex < 0)) {
141:                    //    neither character is present, so treat it
142:                    //    as an error
143:                    throw new MimeTypeParseException(
144:                            "Unable to find a sub type.");
145:                } else if ((slashIndex < 0) && (semIndex >= 0)) {
146:                    //    we have a ';' (and therefore a parameter list),
147:                    //    but now '/' indicating a sub type is present
148:                    throw new MimeTypeParseException(
149:                            "Unable to find a sub type.");
150:                } else if ((slashIndex >= 0) && (semIndex < 0)) {
151:                    //    we have a primary and sub type but no parameter list
152:                    primaryType = rawdata.substring(0, slashIndex).trim()
153:                            .toLowerCase();
154:                    subType = rawdata.substring(slashIndex + 1).trim()
155:                            .toLowerCase();
156:                    parameters = new MimeTypeParameterList();
157:                } else if (slashIndex < semIndex) {
158:                    //    we have all three items in the proper sequence
159:                    primaryType = rawdata.substring(0, slashIndex).trim()
160:                            .toLowerCase();
161:                    subType = rawdata.substring(slashIndex + 1, semIndex)
162:                            .trim().toLowerCase();
163:                    parameters = new MimeTypeParameterList(rawdata
164:                            .substring(semIndex));
165:                } else {
166:                    //    we have a ';' lexically before a '/' which means we have a primary type
167:                    //    & a parameter list but no sub type
168:                    throw new MimeTypeParseException(
169:                            "Unable to find a sub type.");
170:                }
171:                //    now validate the primary and sub types
172:
173:                //    check to see if primary is valid
174:                if (!isValidToken(primaryType)) {
175:                    throw new MimeTypeParseException("Primary type is invalid.");
176:                }
177:                //    check to see if sub is valid
178:                if (!isValidToken(subType)) {
179:                    throw new MimeTypeParseException("Sub type is invalid.");
180:                }
181:            }
182:
183:            /**
184:             * Retrieve the primary type of this object.
185:             */
186:            public String getPrimaryType() {
187:                return primaryType;
188:            }
189:
190:            /**
191:             * Retrieve the sub type of this object.
192:             */
193:            public String getSubType() {
194:                return subType;
195:            }
196:
197:            /**
198:             * Retrieve a copy of this object's parameter list.
199:             */
200:            public MimeTypeParameterList getParameters() {
201:                try {
202:                    return (MimeTypeParameterList) parameters.clone();
203:                } catch (CloneNotSupportedException cnse) {
204:                    throw new RuntimeException("cannot clone parameter list");
205:                }
206:            }
207:
208:            /**
209:             * Retrieve the value associated with the given name, or null if there
210:             * is no current association.
211:             */
212:            public String getParameter(String name) {
213:                return parameters.get(name);
214:            }
215:
216:            /**
217:             * Set the value to be associated with the given name, replacing
218:             * any previous association.
219:             *
220:             * @throw IllegalArgumentException if parameter or value is illegal
221:             */
222:            public void setParameter(String name, String value) {
223:                parameters.set(name, value);
224:            }
225:
226:            /**
227:             * Remove any value associated with the given name.
228:             *
229:             * @throw IllegalArgumentExcpetion if parameter may not be deleted
230:             */
231:            public void removeParameter(String name) {
232:                parameters.remove(name);
233:            }
234:
235:            /**
236:             * Return the String representation of this object.
237:             */
238:            public String toString() {
239:                return getBaseType() + parameters.toString();
240:            }
241:
242:            /**
243:             * Return a String representation of this object
244:             * without the parameter list.
245:             */
246:            public String getBaseType() {
247:                return primaryType + "/" + subType;
248:            }
249:
250:            /**
251:             * Determine of the primary and sub type of this object is
252:             * the same as the what is in the given type.
253:             */
254:            public boolean match(MimeType type) {
255:                if (type == null)
256:                    return false;
257:                return primaryType.equals(type.getPrimaryType())
258:                        && (subType.equals("*")
259:                                || type.getSubType().equals("*") || (subType
260:                                .equals(type.getSubType())));
261:            }
262:
263:            /**
264:             * Determine of the primary and sub type of this object is
265:             * the same as the content type described in rawdata.
266:             */
267:            public boolean match(String rawdata) throws MimeTypeParseException {
268:                if (rawdata == null)
269:                    return false;
270:                return match(new MimeType(rawdata));
271:            }
272:
273:            /**
274:             * The object implements the writeExternal method to save its contents
275:             * by calling the methods of DataOutput for its primitive values or
276:             * calling the writeObject method of ObjectOutput for objects, strings
277:             * and arrays.
278:             * @exception IOException Includes any I/O exceptions that may occur
279:             */
280:            public void writeExternal(ObjectOutput out) throws IOException {
281:                out.writeUTF(toString());
282:            }
283:
284:            /**
285:             * The object implements the readExternal method to restore its
286:             * contents by calling the methods of DataInput for primitive
287:             * types and readObject for objects, strings and arrays.  The
288:             * readExternal method must read the values in the same sequence
289:             * and with the same types as were written by writeExternal.
290:             * @exception ClassNotFoundException If the class for an object being
291:             *              restored cannot be found.
292:             */
293:            public void readExternal(ObjectInput in) throws IOException,
294:                    ClassNotFoundException {
295:                try {
296:                    parse(in.readUTF());
297:                } catch (MimeTypeParseException e) {
298:                    throw new IOException(e.toString());
299:                }
300:            }
301:
302:            /**
303:             * @return a clone of this object
304:             */
305:
306:            public Object clone() throws CloneNotSupportedException {
307:                try {
308:                    return new MimeType(primaryType, subType,
309:                            (MimeTypeParameterList) parameters);
310:                } catch (MimeTypeParseException mtpe) { // this should not occur
311:                    throw new CloneNotSupportedException();
312:                }
313:            }
314:
315:            private String primaryType;
316:            private String subType;
317:            private MimeTypeParameterList parameters;
318:
319:            //    below here be scary parsing related things
320:
321:            /**
322:             * Determine whether or not a given character belongs to a legal token.
323:             */
324:            private static boolean isTokenChar(char c) {
325:                return ((c > 040) && (c < 0177)) && (TSPECIALS.indexOf(c) < 0);
326:            }
327:
328:            /**
329:             * Determine whether or not a given string is a legal token.
330:             */
331:            private boolean isValidToken(String s) {
332:                int len = s.length();
333:                if (len > 0) {
334:                    for (int i = 0; i < len; ++i) {
335:                        char c = s.charAt(i);
336:                        if (!isTokenChar(c)) {
337:                            return false;
338:                        }
339:                    }
340:                    return true;
341:                } else {
342:                    return false;
343:                }
344:            }
345:
346:            /**
347:             * A string that holds all the special chars.
348:             */
349:
350:            private static final String TSPECIALS = "()<>@,;:\\\"/[]?=";
351:        } // class MimeType
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.