MIMETypes.st
author Claus Gittinger <cg@exept.de>
Tue, 27 Jul 1999 08:43:21 +0200
changeset 1209 7ed72799d0ee
parent 1188 bea6424548e3
child 1216 26fe79e724d0
permissions -rw-r--r--
checkin from browser

"
 COPYRIGHT (c) 1997 by eXept Software AG 
	      All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.


"

Object subclass:#MIMETypes
	instanceVariableNames:''
	classVariableNames:'TypeToImageReaderClassMapping FileSuffixToTypeMapping
		FileSuffixToImageReaderClassMapping CharSetToFontMapping'
	poolDictionaries:''
	category:'System-Documentation'
!

!MIMETypes class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1997 by eXept Software AG 
	      All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.


"
!

documentation
"
    just a place to keep MIME information
    (avoid spreading things at many places)

    MIMETypes is abstract and functional.
    Actually, in some OperatingSystems, this information may
    be found in some config files (or registries).
    Therefore, this class may have to be extended to support this
    and fetch the information from there eventually.

    [author:]
        Claus Gittinger
"
! !

!MIMETypes class methodsFor:'initialization'!

initialize
    "initialize wellKnown facts"

    |typeToImageReaderClassMapping fileSuffixToTypeMapping
     fileSuffixToImageReaderClassMapping charSetToFontMapping|

    (fileSuffixToImageReaderClassMapping := FileSuffixToImageReaderClassMapping) isNil ifTrue:[
        FileSuffixToImageReaderClassMapping := fileSuffixToImageReaderClassMapping := Dictionary new
    ].
    (typeToImageReaderClassMapping := TypeToImageReaderClassMapping) isNil ifTrue:[
        TypeToImageReaderClassMapping := typeToImageReaderClassMapping := Dictionary new
    ].
    (fileSuffixToTypeMapping := FileSuffixToTypeMapping) isNil ifTrue:[
        FileSuffixToTypeMapping := fileSuffixToTypeMapping := Dictionary new
    ].

    "/ setup file-suffix to mimeType mapping ...

    #(
        "/ image formats ...

        'jpg'           'image/jpeg'
        'gif'           'image/gif'
        ('tif' 'tiff')  'image/tiff'
        'xbm'           'image/x-xbitmap'
        'xpm'           'image/x-xpixmap'
        'png'           'image/x-png'
        'pcd'           'image/x-photo-cd'
        'bmp'           'image/x-MS-bmp'
        'rgb'           'image/x-rgb'
        'ppm'           'image/x-portable-pixmap'
        'pgm'           'image/x-portable-graymap'
        'pbm'           'image/x-portable-bitmap'
        'pnm'           'image/x-portable-anymap'
        'xwd'           'image/x-xwindowdump'
        'ras'           'image/x-cmu-raster'

        "/ misc text ...

        ('htm' 'html')  'text/html'
        ('txt' 'text')  'text/plain'

        'ps'            'application/postscript'
        'pdf'           'application/pdf'
        'rtf'           'application/rtf'

        "/ video formats ...

        'movie'                         'video/x-sgi-movie'
        'avi'                           'video/x-msvideo'
        ('qt' 'mov' 'moov')             'video/quicktime'
        ('mpv2' 'mp2' 'mpeg2')          'video/x-mpeg2'
        ('mpv' 'mpegv' 'mpg' 'mpeg')    'video/mpeg'

        "/ audio formats ...

        'ra'                    'audio/x-pn-realaudio'
        ('mpa' 'mpega')         'audio/x-mpeg'
        'wav'                   'audio/x-wav'
        ('aif' 'aiff' 'aifc')   'audio/x-aiff'
        ('au' 'snd')            'audio/basic'

        "/ misc stuff

        'st'     'application/x-smalltalk-source'
        'js'     'application/x-javascript'
        'java'   'application/x-java-source'

        'tar'  'application/x-tar'
        'tgz'  'application/x-tar-compressed'
        'zip'  'application/x-zip-compressed'
        'sh'   'application/x-sh'
        'tcl'  'application/x-tcl'
        'pl'   'application/x-perl'

    ) pairWiseDo:[:suff :type|
        suff isArray ifTrue:[
            suff do:[:s | fileSuffixToTypeMapping at:s put:type]
        ] ifFalse:[
            fileSuffixToTypeMapping at:suff put:type
        ]
    ].

    "/ setup mimeType to image reader class mapping ...

    typeToImageReaderClassMapping at:'image/jpeg' put:JPEGReader.
    typeToImageReaderClassMapping at:'image/gif'  put:GIFReader.
    typeToImageReaderClassMapping at:'image/tiff' put:TIFFReader.

    "/ setup suffix to image reader class mapping ...

    fileSuffixToImageReaderClassMapping at:'jpg'  put:JPEGReader.
    fileSuffixToImageReaderClassMapping at:'gif'  put:GIFReader.
    fileSuffixToImageReaderClassMapping at:'tif'  put:TIFFReader.


    "/ character sets (not really mime stuff, but also placed here)

    (charSetToFontMapping := CharSetToFontMapping) isNil ifTrue:[
        CharSetToFontMapping := charSetToFontMapping := Dictionary new
    ].

    #(
        'iso2022-jp'   'jis*0208*'
        'x-iso2022-jp' 'jis*0208*'
        'x-euc-jp'     'jis*0208*'
        'x-shift-jis'  'jis*0208*'
        'x-sjis'       'jis*0208*'
        'x-jis7'       'jis*0208*'
        'jis7'         'jis*0208*'
        'euc'          'jis*0208*'
        'euc-jp'       'jis*0208*'
        'sjis'         'jis*0208*'

        'big5'         'big5*'

        'gb2312'       'gb*'
        'hz-gb-2312'   'gb*'
        'x-gbk'        'gb*'

        'iso2022-kr'   'ksc*'
        'x-euc-kr'     'ksc*'
    ) pairWiseDo:[:charSet :fontEncoding|
         charSetToFontMapping at:charSet put:fontEncoding
    ].

    "
     self initialize
    "

    "Modified: / 1.8.1998 / 17:50:57 / cg"
! !

!MIMETypes class methodsFor:'accessing'!

defineImageType:mimeType suffix:aSuffix reader:aReaderClass
    "register an image reader."

    aSuffix notNil ifTrue:[
        self imageReaderForSuffix:aSuffix put:aReaderClass.
    ].

    mimeType notNil ifTrue:[
        self imageReaderForType:mimeType put:aReaderClass
    ].

    (aSuffix notNil and:[mimeType notNil]) ifTrue:[
        self mimeTypeForSuffix:aSuffix put:mimeType
    ].
!

fileSuffixToImageReaderMapping
    "return the suffix-to-imageReader mapping"

    ^ FileSuffixToImageReaderClassMapping ? #()

    "Modified: / 1.8.1998 / 17:00:11 / cg"
!

fontForCharset:aCharSet
    "return the font-encoding for an iso-charset"

    ^ CharSetToFontMapping at:aCharSet ifAbsent:nil

    "
     MIMETypes fontForCharset:'iso2022-jp'       
     MIMETypes fontForCharset:'euc-jp'     
    "

    "Modified: / 1.8.1998 / 17:00:57 / cg"
!

imageFileSuffixes
    "return a collection with known file suffixes"

    FileSuffixToImageReaderClassMapping isNil ifTrue:[^ #()].
    ^ FileSuffixToImageReaderClassMapping keys

    "Created: / 30.6.1997 / 22:04:48 / cg"
    "Modified: / 1.8.1998 / 17:01:26 / cg"
!

imageReaderClasses
    "return a collection of registered image reader classes"

    |setOfClasses|

    setOfClasses := IdentitySet new.
    FileSuffixToImageReaderClassMapping notNil ifTrue:[
        FileSuffixToImageReaderClassMapping keysAndValuesDo:[:suff :cls |
            setOfClasses add:cls
        ].
    ].
    TypeToImageReaderClassMapping notNil ifTrue:[
        TypeToImageReaderClassMapping keysAndValuesDo:[:suff :cls |
            setOfClasses add:cls
        ].
    ].
    ^ setOfClasses

    "Created: / 30.6.1997 / 22:03:42 / cg"
    "Modified: / 1.8.1998 / 16:59:52 / cg"
!

imageReaderForSuffix:aSuffix
    "given a file suffix, return an approriate image reader class"

    FileSuffixToImageReaderClassMapping isNil ifTrue:[^ nil].
    ^ FileSuffixToImageReaderClassMapping at:aSuffix asLowercase ifAbsent:nil

    "Created: / 30.6.1997 / 21:59:11 / cg"
    "Modified: / 1.8.1998 / 17:01:58 / cg"
!

imageReaderForSuffix:aSuffix put:aReaderClass
    "register an image reader for a file suffix"

    FileSuffixToImageReaderClassMapping isNil ifTrue:[
        FileSuffixToImageReaderClassMapping := Dictionary new.
    ].
    FileSuffixToImageReaderClassMapping at:aSuffix asLowercase put:aReaderClass

    "Created: / 30.6.1997 / 21:59:43 / cg"
    "Modified: / 1.8.1998 / 17:02:14 / cg"
!

imageReaderForType:mimeTypeString
    "given a mime-type, return an approriate image reader class"

    TypeToImageReaderClassMapping isNil ifTrue:[^ nil].
    ^ TypeToImageReaderClassMapping at:mimeTypeString asLowercase ifAbsent:nil

    "Created: / 30.6.1997 / 21:56:01 / cg"
    "Modified: / 1.8.1998 / 17:02:28 / cg"
!

imageReaderForType:mimeTypeString put:aReaderClass
    "register an image reader for a mime-type"

    TypeToImageReaderClassMapping isNil ifTrue:[
        TypeToImageReaderClassMapping := Dictionary new.
    ].
    TypeToImageReaderClassMapping at:mimeTypeString asLowercase put:aReaderClass

    "Created: / 30.6.1997 / 21:56:11 / cg"
    "Modified: / 1.8.1998 / 17:02:40 / cg"
!

mimeTypeForSuffix:suffix
    "given a file suffix, return the mime-type"

    |type lcSuffix|

    lcSuffix := suffix asLowercase.
    type := FileSuffixToTypeMapping at:lcSuffix ifAbsent:nil.
    type isNil ifTrue:[
        "/ allow for fallback ...
        type := OperatingSystem mimeTypeForSuffix:lcSuffix.
        "/ the special value #unknown is returned as nil;
        "/ this avoids constant retry if a mimeType is not known in
        "/ the OS.
        type isNil ifTrue:[
            FileSuffixToTypeMapping at:lcSuffix put:#unknown
        ].
    ].
    type == #unknown ifTrue:[
        ^ nil
    ].
    ^ type

    "Created: / 30.6.1997 / 21:55:51 / cg"
    "Modified: / 1.8.1998 / 17:02:59 / cg"
!

mimeTypeForSuffix:suffix put:mimeType
    "register a mime type for a file suffix"

    FileSuffixToTypeMapping isNil ifTrue:[
        FileSuffixToTypeMapping := Dictionary new
    ].
    FileSuffixToTypeMapping at:suffix put:mimeType asLowercase

    "Created: / 30.6.1997 / 21:56:20 / cg"
    "Modified: / 1.8.1998 / 17:03:18 / cg"
! !

!MIMETypes class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libview2/MIMETypes.st,v 1.15 1999-06-18 22:44:59 cg Exp $'
! !
MIMETypes initialize!