Source Code Cross Referenced for ContentTypes.java in  » Ajax » zk » org » zkoss » util » media » 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 » Ajax » zk » org.zkoss.util.media 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* ContentTypes.java
002:
003:        {{IS_NOTE
004:        	Purpose:
005:        		
006:        	Description:
007:        		
008:        	History:
009:        		Fri Oct  1 12:32:24     2004, Created by tomyeh
010:        }}IS_NOTE
011:
012:        Copyright (C) 2004 Potix Corporation. All Rights Reserved.
013:
014:        {{IS_RIGHT
015:        	This program is distributed under GPL Version 2.0 in the hope that
016:        	it will be useful, but WITHOUT ANY WARRANTY.
017:        }}IS_RIGHT
018:         */
019:        package org.zkoss.util.media;
020:
021:        import java.util.Map;
022:        import java.util.HashMap;
023:        import java.io.InputStream;
024:        import java.io.IOException;
025:        import java.io.BufferedReader;
026:        import java.io.InputStreamReader;
027:
028:        import org.zkoss.lang.D;
029:        import org.zkoss.lang.Strings;
030:        import org.zkoss.mesg.MCommon;
031:        import org.zkoss.util.logging.Log;
032:
033:        /**
034:         * Utilities relevant to content types.
035:         *
036:         * @author tomyeh
037:         */
038:        public class ContentTypes {
039:            private static final Log log = Log.lookup(ContentTypes.class);
040:
041:            /** A map of (String format, String contentType). */
042:            private static final Map _fmt2ct = new HashMap(17);
043:            /** A map of (String contentType, String format). */
044:            private static final Map _ct2fmt = new HashMap(17);
045:
046:            protected ContentTypes() {
047:            } //prevent from initializing
048:
049:            /** Returns the content type of the specified format,
050:             * such as "html" and "pdf", or null if not found.
051:             */
052:            public static final String getContentType(String format) {
053:                format = format.trim().toLowerCase();
054:                final String ctype;
055:                synchronized (_fmt2ct) {
056:                    ctype = (String) _fmt2ct.get(format);
057:                }
058:
059:                return ctype;
060:            }
061:
062:            /** Returns the format of the specified content type, or null if not found.
063:             */
064:            public static final String getFormat(String ctype) {
065:                ctype = ctype.trim().toLowerCase();
066:                String format;
067:                synchronized (_ct2fmt) {
068:                    format = (String) _ct2fmt.get(ctype);
069:                }
070:                if (format == null) {
071:                    //sometime, content type is "text/html;charset=UTF-8"
072:                    int j = ctype.indexOf(';');
073:                    if (j >= 0) {
074:                        ctype = ctype.substring(0, j);
075:                        synchronized (_ct2fmt) {
076:                            format = (String) _ct2fmt.get(ctype);
077:                        }
078:                    }
079:                    if (format == null) {
080:                        j = ctype.indexOf('/');
081:                        format = j >= 0 ? ctype.substring(j + 1) : ctype;
082:                    }
083:                }
084:                return format;
085:            }
086:
087:            /** Adds additional binding of the format and content type.
088:             *
089:             * <p>You rarely need to invoke this method, unless your format is not
090:             * by the default mapping.
091:             */
092:            public static final void put(String format, String ctype) {
093:                if (format == null || ctype == null)
094:                    throw new NullPointerException("format or ctype");
095:
096:                synchronized (_fmt2ct) {
097:                    _fmt2ct.put(format, ctype);
098:                }
099:                synchronized (_ct2fmt) {
100:                    _ct2fmt.put(ctype, format);
101:                }
102:            }
103:
104:            static {
105:                final String flnm = "/metainfo/org/zkoss/util/media/contentTypes.properties";
106:                if (!load(flnm))
107:                    log.warning(MCommon.FILE_NOT_FOUND, flnm);
108:                load("/contentTypes.properties"); //override!
109:            }
110:
111:            private static final boolean load(String flnm) {
112:                final InputStream strm = ContentTypes.class
113:                        .getResourceAsStream(flnm);
114:                if (strm == null)
115:                    return false;
116:
117:                //NOTE: we cannot use Properties.load because there might be replicated
118:                //mapping (e.g., jpg=images/jpg, jpg=images/pjpeg)
119:                try {
120:                    final BufferedReader in = new BufferedReader(
121:                            new InputStreamReader(strm));
122:
123:                    String line;
124:                    while ((line = in.readLine()) != null) {
125:                        final int j = line.indexOf('=');
126:                        if (j < 0) {
127:                            final int k = Strings.skipWhitespaces(line, 0);
128:                            if (k < line.length() && line.charAt(k) != '#')
129:                                log.warning("Ignored error;  illgal format: "
130:                                        + line);
131:                            continue;
132:                        }
133:
134:                        final String format = line.substring(0, j).trim();
135:                        final String ctype = line.substring(j + 1).trim();
136:                        if (format.length() == 0 || ctype.length() == 0) {
137:                            log.warning("Ignored error;  illgal format: "
138:                                    + line);
139:                            continue;
140:                        }
141:
142:                        _fmt2ct.put(format, ctype);
143:                        _ct2fmt.put(ctype, format);
144:                    }
145:                } catch (IOException ex) {
146:                    log.warning("Ingored error: Unable to read " + flnm, ex);
147:                }
148:                return true;
149:            }
150:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.