MIMETypes.st
author Claus Gittinger <cg@exept.de>
Wed, 27 Jul 2005 10:31:43 +0200
changeset 2055 501740104b75
parent 2048 dbb361dd9c5f
child 2096 0cc1b8dbdd5b
permissions -rw-r--r--
css-comments

"
 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.


"

"{ Package: 'stx:libview2' }"

Object subclass:#MIMETypes
	instanceVariableNames:''
	classVariableNames:'TypeToImageReaderClassMapping TypeToCommentStringMapping
		TypeToFileSuffixMapping TypeToViewerApplicationMapping
		SuffixToCommentStringMapping FileSuffixToTypeMapping
		FilenameToTypeMapping FileSuffixToImageReaderClassMapping
		CharSetToFontMapping LastSuffix LastType
		DefaultCommandPerMIMEPerOS'
	poolDictionaries:''
	category:'Net-Communication-Support'
!

String variableByteSubclass:#MIMEType
	instanceVariableNames:''
	classVariableNames:'CachedTypes'
	poolDictionaries:''
	privateIn:MIMETypes
!

!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
"
!

knownTypes
"
    simply a reminder for me ...

    application         mime                            file extentsions

        powerpoint      application/ms-powerpoint       .pot, .ppz, .ppt, .pps, 
                        application/mspowerpoint        
                        application/vnd.ms-powerpoint   
                        application/x-mspowerpoint

        shockwave       application/x-shockwave-flash   .swf     

        futuresplash    application/futuresplash        .spl
"
! !

!MIMETypes class methodsFor:'initialization'!

initialize
    "initialize wellKnown facts"

    MIMEType initialize.   "must be initialized first"
    LastSuffix := LastType := nil.

    self initializeFileNameToMimeTypeMapping.
    self initializeImageReaderMappings.
    self initializeCommentStringMappings.

    "
     self initialize
    "

    "Modified: / 12-05-2004 / 11:07:20 / cg"
!

initializeCommentStringMappings
    TypeToCommentStringMapping := Dictionary new.
    SuffixToCommentStringMapping := Dictionary new.

    TypeToCommentStringMapping 
        at:'application/x-make'
        put:#('#' (nil nil)).           "/ '#' for EOL comments only

    TypeToCommentStringMapping 
        at:'application/x-sh'
        put:#('#' (nil nil)).           "/ '#' for EOL comments only

    #('text/html' 'text/xml' 'application/xml')
    do:[:eachXMLType |
        TypeToCommentStringMapping 
            at:eachXMLType
            put:#(nil ('<!!-- ' ' -->')). "/ '<!!-- ... -->' delimited comments only
    ].

    TypeToCommentStringMapping 
        at:'text/css'
        put:#('//' ('/*' '*/')).          

    TypeToCommentStringMapping 
        at:'application/x-batch-script'
        put:#('rem ' (nil nil)).         "/ 'rem ' for EOL comments only

    "/ the following is ST/X specific
    TypeToCommentStringMapping 
        at:'application/x-smalltalk-source'
        put:#('"/' ('"' '"')).           "/ '"/ ' for EOL; ".." for delimited comments


    "/ this is for standard smalltalk
"/    TypeToCommentStringMapping 
"/        at:'application/x-smalltalk-source'
"/        put:#(nil ('"' '"')).            "/ ".." for delimited comments only


    TypeToCommentStringMapping 
        at:'application/x-pascal-source'
        put:#(nil ('{' '}')).           "/ '{'..'}' for delimited comments


"/    "/ the following is k&r
"/    TypeToCommentStringMapping 
"/        at:'application/x-c-source'
"/        put:#(nil ('/*' '*/')).          "/ '/*'..'*/' for delimited comments
    "/ this is ANSI-c
    TypeToCommentStringMapping 
        at:'application/x-c-source'
        put:#('//' ('/*' '*/')).          

    TypeToCommentStringMapping 
        at:'application/x-cpp-source'
        put:#('//' ('/*' '*/')).          

    TypeToCommentStringMapping 
        at:'application/x-java-source'
        put:#('//' ('/*' '*/')).          

    TypeToCommentStringMapping 
        at:'application/x-javascript'
        put:#('//' ('/*' '*/')).          

    TypeToCommentStringMapping 
        at:'application/x-asn1-source'
        put:#('--' ('--' '--')).          


    "/ st/x support files
    SuffixToCommentStringMapping 
        at:'style'
        put:#(';' (nil nil)).          

    SuffixToCommentStringMapping 
        at:'rs'
        put:#(';' (nil nil)).          
!

initializeDefaultCommands
    "TODO: move this to OS"

    |unixCommands win32Commands|

    DefaultCommandPerMIMEPerOS := Dictionary new.
    DefaultCommandPerMIMEPerOS at:#unix  put:(unixCommands := Dictionary new).
    DefaultCommandPerMIMEPerOS at:#win32 put:(win32Commands := Dictionary new).

    unixCommands at:'application/x-tar-compressed' put:'gunzip < %1 | tar tvf -'.
    unixCommands at:'application/pdf'              put:'acroread -display %2 %1'.

    OperatingSystem isMSWINDOWSlike ifTrue:[
        |k fkey cmd|

        k := Win32OperatingSystem::RegistryEntry key:'HKEY_CLASSES_ROOT\.pdf'.
        k notNil ifTrue:[
            fkey := (k valueNamed:'').
        ].
        fkey isNil ifTrue:[
            fkey := 'pdf_auto_file'
        ].
        fkey notEmptyOrNil ifTrue:[
            k := Win32OperatingSystem::RegistryEntry key:('HKEY_CLASSES_ROOT\' , fkey , '\shell\open\command').
            k notNil ifTrue:[
                cmd := k valueNamed:''
            ].
        ].

        cmd isNil ifTrue:[
            "assume its in the path"
            cmd := 'acrord32 %1'.
        ].
        win32Commands at:'application/pdf'             put:cmd.
    ].

    "
     self initializeDefaultCommands
    "

    "Modified: / 12-05-2004 / 11:43:23 / cg"
!

initializeFileNameToMimeTypeMapping
    "initialize wellKnown facts"

    |types|

    FileSuffixToTypeMapping isNil ifTrue:[
        FileSuffixToTypeMapping := Dictionary new
    ].
    TypeToFileSuffixMapping isNil ifTrue:[
        TypeToFileSuffixMapping := Dictionary new
    ].
    FilenameToTypeMapping isNil ifTrue:[
        FilenameToTypeMapping := Dictionary new.
    ].

    types := OrderedCollection new.
    types addAll:self textTypeList.
    types addAll:self imageTypeList.
    types addAll:self videoTypeList.
    types addAll:self audioTypeList.
    types addAll:self applicationTypeList.
    types addAll:self osSpecificTypeList.

    types pairWiseDo:[:suff :typeString|
        |type|

        type := MIMEType fromString:typeString.
        suff isArray ifTrue:[
            suff do:[:s | FileSuffixToTypeMapping at:s put:type].
            TypeToFileSuffixMapping at:type put:suff first.
        ] ifFalse:[
            FileSuffixToTypeMapping at:suff put:type.
            TypeToFileSuffixMapping at:type put:suff.
        ].
    ].

    self miscFilenameList pairWiseDo:[:nm :typeString|
        |type|

        type := MIMEType fromString:typeString.
        nm isArray ifTrue:[
            nm do:[:s | FilenameToTypeMapping at:s put:type]
        ] ifFalse:[
            FilenameToTypeMapping at:nm put:type
        ]
    ].

    "
     self initializeFileNameToMimeTypeMapping
    "
!

initializeImageReaderMappings
    FileSuffixToImageReaderClassMapping isNil ifTrue:[
        FileSuffixToImageReaderClassMapping := Dictionary new
    ].
    "MIME" TypeToImageReaderClassMapping isNil ifTrue:[
        TypeToImageReaderClassMapping := Dictionary new
    ].

    "/ 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.
! !

!MIMETypes class methodsFor:'initialization-lists'!

applicationCompressorsAndArchiveTypeList
    ^ #(
        'tar'                       'application/x-tar'
        'gtar'                      'application/x-gtar'
        ('tgz' 'tar.gz')            'application/x-tar-gzip-compressed'   "/ 'application/x-tar-compressed'
        ('tar.bz2')                 'application/x-tar-bzip2-compressed'
        'zip'                       'application/x-zip-compressed'
        'bz2'                       'application/x-bzip2-compressed'
        ('gz' 'z')                  'application/x-gzip-compressed'
        'cpio'                      'application/x-cpio'
        'shar'                      'application/x-shar'
        'jar'                       'application/java-archive'
        'sar'                       'application/x-squeak-archive'
        'sit'                       'application/x-stuffit'
        'hqx'                       'application/mac-binhex40'
        'cpt'                       'application/mac-compactpro'
    )
!

applicationMiscTypeList
    "misc applications"

    ^ #(
        ('a' 'o' 'obj' )                        'application/binary'
        'a'                                     'application/library'
        ('dll' 'so')                            'application/shared-library'
    )
!

applicationProgLangTypeList
    "applications for programm languages"

    ^ #(
        ('st' 'cls' 'rc')            'application/x-smalltalk-source'
        'sif'                        'application/x-smalltalk-source-sif'
        'pac'                        'application/x-smalltalk-dolphin-package'

        'sts'                        'application/x-squeak-source'
        'pr'                         'application/x-squeak-project'

        'c'                          'application/x-c-source'
        ('cc' 'cpp')                 'application/x-cpp-source'
        ('h' 'hi')                   'application/x-c-header'
        ('js' 'mocha')               'application/x-javascript'
        ('java' 'jav')               'application/x-java-source'
        'sh'                         'application/x-sh'
        'csh'                        'application/x-csh'
        'tcl'                        'application/x-tcl'
        'pl'                         'application/x-perl'
        'mak'                        'application/x-make'
        'bat'                        'application/x-batch-script'
        ('asn1' 'x409' 'gdmo' 'gdm') 'application/x-asn1-source'
    )
!

applicationTextTypeList
    "text applications"

    ^ #(
        ('ps' 'eps')                 'application/postscript'
        "/ 'ai'                         'application/postscript'
        'pdf'                        'application/pdf'
        'rtf'                        'application/rtf'
        "/ 'rtf'                        'text/rtf'            ????
        "/ 'doc'                        'application/winword'
        'doc'                        'application/ms-word-document'
        'swf'                        'application/x-shockwave-flash'
        ('ppt' 'ppz' 'pot' 'pps')    'application/mspowerpoint'
        'xls'                        'application/x-excel'   
        'slk'                        'application/vnd.ms-excel'
    )
!

applicationTypeList
    "applications"

    |typeList|                            

    typeList := OrderedCollection new.
    typeList addAll:self applicationProgLangTypeList.
    typeList addAll:self applicationTextTypeList.
    typeList addAll:self applicationCompressorsAndArchiveTypeList.
    typeList addAll:self applicationMiscTypeList.

    ^ typeList
!

audioTypeList
    "audio formats ..."

    ^ #(
        ('au' 'snd')            'audio/basic'
        ('ra')                  'audio/x-realaudio'
        ('ram' 'rm')            'audio/x-pn-realaudio'
"/ cg: see unixSpecific / windowsSpecific
"/        'rpm'                   'audio/x-pn-realaudio-plugin'
        ('mpa' 'mpega')         'audio/mpeg'
        "/ 'mp3'                   'audio/x-mp3'
        ('mp3' 'mp2')           'audio/mpeg'
        'wav'                   'audio/x-wav'
        ('aif' 'aiff' 'aifc')   'audio/x-aiff'
        ('midi' 'mid')          'audio/midi'
    )
!

imageTypeList

    ^ #(
        "/ image formats ...

        ('jpg' 'jpeg')  'image/jpeg'
        'gif'           'image/gif'
        ('tif' 'tiff')  'image/tiff'
        'xbm'           'image/x-xbitmap'
        'xpm'           'image/x-xpixmap'
        'png'           'image/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'
        'tga'           'image/x-targa'
        'ico'           'image/x-ico'
    ) 
!

miscFilenameList
    "other formats (not by suffix, but by fileName isnstead) ..."

    ^ #(
        ('makefile' 'make.proto')  'application/x-make'
        ('exe' 'bin')              'application/octet-stream'
        ('class')                  'application/octet-stream'
    )
!

osSpecificTypeList
    OperatingSystem isMSWINDOWSlike ifTrue:[
        ^ self windowsSpecificTypeList
    ].
    ^ self unixSpecificTypeList

"
    self osSpecificTypeList
"
!

textTypeList
   "/ misc text ...

    ^ #(
        ('html' 'htm' 'shtml')  'text/html'
        ('txt' 'text')          'text/plain'
        'xml'                   'text/xml'
        'css'                   'text/css'
    )
!

unixSpecificTypeList

    ^ #(
"/ cg: RPM is 'audio/x-pn-realaudio-plugin' for WIN32
"/     and 'application/x-rpm' for Unix
        'rpm'                   'application/x-rpm'
    )
!

videoTypeList
    "/ video formats ...

    ^ #(
        ('qt' 'mov' 'moov')                'video/quicktime'
        ('mpv' 'mpegv' 'mpg' 'mpeg' 'mpe') 'video/mpeg'
        'movie'                            'video/x-sgi-movie'
        ('avi' 'wmv' 'asf')                'video/x-msvideo'
        ('mpv2' 'mp2v' 'mp2' 'mpeg2')      'video/x-mpeg2'
        'rm'                               'application/vnd.rn-realmedia'
        'rv'                               'video/x-pn-realvideo'
   )
!

windowsSpecificTypeList
    ^ #(
"/ cg: RPM is 'audio/x-pn-realaudio-plugin' for WIN32
"/     and 'application/x-rpm' for Unix
        'rpm'                   'audio/x-pn-realaudio-plugin'
    )
! !

!MIMETypes class methodsFor:'obsolete'!

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

    <resource:#obsolete>
    self obsoleteMethodWarning:'use FontDescription >> fontNamePatternForCharset:'.
    ^ FontDescription fontNamePatternForCharset:aCharSetName.

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

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

!MIMETypes class methodsFor:'queries'!

defaultCommandForMimeType:mimeType
    ^ self defaultCommandPerMIME at:mimeType ifAbsent:nil.

    "
     MIMETypes defaultCommandForMimeType:'application/pdf'
    "

    "Created: / 12-05-2004 / 11:09:22 / cg"
    "Modified: / 12-05-2004 / 11:16:53 / cg"
!

defaultCommandPerMIME
    "TODO: move this to OS/UserPreferences"

    DefaultCommandPerMIMEPerOS isNil ifTrue:[
        self initializeDefaultCommands
    ].
    OperatingSystem isUNIXlike ifTrue:[
        ^ DefaultCommandPerMIMEPerOS at:#unix
    ].
    ^ DefaultCommandPerMIMEPerOS at:#win32
!

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"
!

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"
!

mimeTypeForFilename:filenameArg
    "given a filename, return the mime-type or nil, if unknown"

    |type filename lcFilename suff suff2|

    filename := filenameArg asFilename.

    lcFilename := filename baseName asLowercase.
    type := FilenameToTypeMapping at:lcFilename ifAbsent:nil.
    type isNil ifTrue:[
        "/ allow for fallback ...
        type := OperatingSystem mimeTypeForFilename:lcFilename.
        "/ the special value #unknown is returned as nil;
        "/ this avoids constant retry if a mimeType is not known in
        "/ the OS.
"/        type isNil ifTrue:[
"/            FilenameToTypeMapping at:lcFilename put:#unknown
"/        ].
    ].

    type == #unknown ifTrue:[
        type := nil.
    ].

    type isNil ifTrue:[
        suff := filename suffix.
        suff size ~~ 0 ifTrue:[
            "/ sigh - special code for multiple-suffices...
            "/ .tar.gz -> tgz
            suff2 := filename withoutSuffix suffix.
            suff2 size > 0 ifTrue:[
                type := self mimeTypeForSuffix:(suff2 , '.' , suff).
            ].
            type isNil ifTrue:[
                type := self mimeTypeForSuffix:suff
            ].
        ]
    ].

    ^ type

    "
     self mimeTypeForFilename:'typeinst.dvi'      
     self mimeTypeForFilename:'foo.tar'
     self mimeTypeForFilename:'foo.tgz'
     self mimeTypeForFilename:'foo.tar.gz'
    "
!

mimeTypeForFilename:filename put:mimeType
    "register a mime type for a filename"

    FilenameToTypeMapping isNil ifTrue:[
        FilenameToTypeMapping := Dictionary new
    ].
    FilenameToTypeMapping at:filename put:(MIMEType fromString:mimeType asLowercase)
!

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

    |type lcSuffix|

    suffix = LastSuffix ifTrue:[
        ^ LastType
    ].

    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 := type ? #unknown.
        FileSuffixToTypeMapping at:lcSuffix put:type.
    ].

    type == #unknown ifTrue:[
        type := nil.
    ].
    type notNil ifTrue:[
        LastSuffix := suffix.
        LastType := MIMEType fromString:type.
        ^ LastType
    ].
    ^ type

    "
     self mimeTypeForSuffix:'gif'     
     self mimeTypeForSuffix:'rpm'     
    "

    "Created: / 30.6.1997 / 21:55:51 / cg"
    "Modified: / 23.12.1999 / 22:30:55 / 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"
!

mimeTypeForSuffixMapping

    ^ FileSuffixToTypeMapping
!

mimeTypeFromString:mimeTypeString
    "given a mime-type for a string"

     ^ MIMEType fromString:mimeTypeString

    "
     self mimeTypeFromString:'image/gif' 
    "
!

mimeTypeOfContents:filename
    "given a filename, scan the contents, return the mime-type or nil, if unknown"

    |typeString|

    typeString := filename mimeTypeOfContents.
    typeString isNil ifTrue:[^ nil].
    ^ MIMEType fromString:typeString.
!

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

    ^ TypeToFileSuffixMapping at:mimeType ifAbsent:nil

    "
     self suffixForMimeType:(MIMEType fromString:'image/gif')     
    "

    "Created: / 30.6.1997 / 21:55:51 / cg"
    "Modified: / 23.12.1999 / 22:30:55 / cg"
!

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

     ^ self suffixForMimeType:(MIMEType fromString:mimeTypeString) 

    "
     self suffixForMimeTypeString:'image/gif' 
    "

    "Created: / 30.6.1997 / 21:55:51 / cg"
    "Modified: / 23.12.1999 / 22:30:55 / cg"
! !

!MIMETypes class methodsFor:'queries-image formats'!

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"
! !

!MIMETypes class methodsFor:'queries-language syntax'!

commentStringsForFilename:aFilename
    "return a useful comment definition; heuristics for now.
     The returned value is an array of 2 elements;
     the first is the end-of-line comment string (or nil);
     the second an array of opening/closing delimiters (or an array of nils)"

     |mime|

     mime := MIMETypes mimeTypeForFilename:aFilename.
     ^ self commentStringsForMimeType:mime suffix:(aFilename asFilename suffix)

    "
     MIMETypes commentStringsForFilename:'Makefile'.
     MIMETypes commentStringsForFilename:'Object.st'. 
     MIMETypes commentStringsForFilename:'Foo.java'. 
     MIMETypes commentStringsForFilename:'Foo.html'. 
    "
!

commentStringsForFilename:aFilename ifUnknown:alternativeBlockReturningCommentString
    "return a useful comment definition; heuristics for now.
     The returned value is an array of 2 elements;
     the first is the end-of-line comment string (or nil);
     the second an array of opening/closing delimiters (or an array of nils)"

     |mime|

     mime := MIMETypes mimeTypeForFilename:aFilename.
     ^ self commentStringsForMimeType:mime suffix:(aFilename asFilename suffix) ifUnknown:alternativeBlockReturningCommentString

    "
     MIMETypes commentStringsForFilename:'Makefile'.
     MIMETypes commentStringsForFilename:'Object.st'. 
     MIMETypes commentStringsForFilename:'Foo.java'. 
    "
!

commentStringsForMimeType:mime suffix:suff
    "return a useful comment definition; heuristics for now.
     The returned value is an array of 2 elements;
     the first is the end-of-line comment string (or nil);
     the second an array of opening/closing delimiters (or an array of nils)"

    ^ self
        commentStringsForMimeType:mime suffix:suff 
        ifUnknown: #(';' (nil nil))  

    "
     |mime|

     mime := MIMETypes mimeTypeForFilename:'Makefile'.
     MIMETypes commentStringsForMimeType:mime suffix:nil.     
    "

    "
     |mime|

     mime := MIMETypes mimeTypeForFilename:'Object.st'.
     MIMETypes commentStringsForMimeType:mime suffix:nil.    
    "
!

commentStringsForMimeType:mime suffix:suff ifUnknown:alternativeBlockReturningCommentString
    "return a useful comment definition; heuristics for now.
     The returned value is an array of 2 elements;
     the first is the end-of-line comment string (or nil);
     the second an array of opening/closing delimiters (or an array of nils)"

    |commentSpec|

    commentSpec := TypeToCommentStringMapping at:mime ifAbsent:nil.
    commentSpec notNil ifTrue:[
        ^ commentSpec
    ].

    commentSpec := SuffixToCommentStringMapping at:suff ifAbsent:nil.
    commentSpec notNil ifTrue:[
        ^ commentSpec
    ].

    ^ alternativeBlockReturningCommentString value

    "
     |mime|

     mime := MIMETypes mimeTypeForFilename:'Makefile'.
     MIMETypes commentStringsForMimeType:mime suffix:nil.     
    "

    "
     |mime|

     mime := MIMETypes mimeTypeForFilename:'Object.st'.
     MIMETypes commentStringsForMimeType:mime suffix:nil.    
    "
! !

!MIMETypes::MIMEType class methodsFor:'documentation'!

documentation
"
    like a string, but knows that it represents a mimeType.
"
! !

!MIMETypes::MIMEType class methodsFor:'initialization'!

initialize
    CachedTypes := CacheDictionary new:30.

    "
     self initialize
    "
! !

!MIMETypes::MIMEType class methodsFor:'instance creation'!

fromString:aString
    |cachedType newType|

    aString class == self ifTrue:[^ aString].

    cachedType := CachedTypes at:aString ifAbsent:nil.
    cachedType notNil ifTrue:[^ cachedType].
    newType := aString copy changeClassTo:self.
    CachedTypes at:aString put:newType.
    ^ newType

    "
     self fromString:'text/html'
    "
! !

!MIMETypes::MIMEType methodsFor:'queries'!

isArchive
    "return true, if I represent an archive type (such as zip or tar)"

    |archivTypes|

    archivTypes := MIMETypes applicationCompressorsAndArchiveTypeList.
    archivTypes pairWiseDo:[: ext : type |
        self = type ifTrue:[ ^ true].
    ].
    ^ false
!

isBinary
    "return true, if I represent a binary (non-text) type"

    ^ self isTextType not
!

isHtml
    "return true, if I represent the html text type"

    ^ (self = 'text/html')
!

isImage
    "return true, if I represent an image type (such as gif or jpg)"

    ^ (self startsWith:'image/')
!

isPdf
    "return true, if I represent the pdf type"

    ^ (self = 'application/pdf')
!

isSmalltalkSource
    "return true, if I represent the smalltalk sourcecode type"

    ^ (self = 'application/x-smalltalk-source')
!

isTextType
    "return true, if I represent some text type"

    ^ self startsWith:'text/'
!

isXml
    "return true, if I represent the xml text type"

    ^ (self = 'text/xml')
! !

!MIMETypes class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libview2/MIMETypes.st,v 1.80 2005-07-27 08:31:43 cg Exp $'
! !

MIMETypes initialize!
MIMETypes::MIMEType initialize!