You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tika.apache.org by "Tim Allison (JIRA)" <ji...@apache.org> on 2015/07/22 16:13:04 UTC

[jira] [Comment Edited] (TIKA-1692) Enable getExtension() for mime strings with parameters

    [ https://issues.apache.org/jira/browse/TIKA-1692?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14636901#comment-14636901 ] 

Tim Allison edited comment on TIKA-1692 at 7/22/15 2:12 PM:
------------------------------------------------------------

Hmmm....
If we modify {{getRegisteredMimeType}} to this:
{noformat}
        if (type != null) {
            MediaType normalisedType = registry.normalize(type);
            MimeType candidate = types.get(normalisedType);
            if (candidate != null) {
                return candidate;
            }
            if (normalisedType.hasParameters()) {
                return types.get(normalisedType.getBaseType());
            }
            return null;
        } else {
            throw new MimeTypeException("Invalid media type name: " + name);
        }
{noformat}

then we lose the parameters in the returned value:
{noformat}
    @Test
    public void testGetExtensionForMimesWithParameters() throws Exception {
        MimeType mt = this.mimeTypes.getRegisteredMimeType("text/html; charset=UTF-8");
        assertEquals("text/html", mt.toString());
        assertEquals("text/html", mt.getName());
        assertEquals(".html", mt.getExtension());
{noformat}

I don't think this is what you were expecting in your test above, however, I guess it could make sense.  If you want the one that is actually registered, it often isn't the one with parameters.  However if you want the full MimeType from a string, use {{parse}}. 

Another option is to move this logic into a static getExtension(String) and/or getExtension(MediaType)...


was (Author: tallison@mitre.org):
Hmmm....
If we modify {{getRegisteredMimeType}} to this:
{noformat}
        if (type != null) {
            MediaType normalisedType = registry.normalize(type);
            MimeType candidate = types.get(normalisedType);
            if (candidate != null) {
                return candidate;
            }
            if (normalisedType.hasParameters()) {
                return types.get(normalisedType.getBaseType());
            }
            return null;
        } else {
            throw new MimeTypeException("Invalid media type name: " + name);
        }
{noformat}

then we lose the parameters in the returned value:
{noformat}
    @Test
    public void testGetExtensionForMimesWithParameters() throws Exception {
        MimeType mt = this.mimeTypes.getRegisteredMimeType("text/html; charset=UTF-8");
        assertEquals("text/html", mt.toString());
        assertEquals("text/html", mt.getName());
        assertEquals(".html", mt.getExtension());
{noformat}

I don't think this is what you were expecting in your test above, however, I guess it could make sense.  If you want the one that is actually registered, it often isn't the one with parameters.  However if you want the full MimeType from a string, use {{forName}}. 

Another option is to move this logic into a static getExtension(String) and/or getExtension(MediaType)...

> Enable getExtension() for mime strings with parameters
> ------------------------------------------------------
>
>                 Key: TIKA-1692
>                 URL: https://issues.apache.org/jira/browse/TIKA-1692
>             Project: Tika
>          Issue Type: Improvement
>          Components: core
>            Reporter: Tim Allison
>            Priority: Trivial
>             Fix For: 1.10
>
>         Attachments: MimeUtilTest.java
>
>
> {{getExtension()}} offers a handy way to add a "detected" extension from a {{MimeType}} for a file that didn't come with an extension.  However, this functionality doesn't work with texty files: html, xml, css, csv, etc.  
> Let's add a static helper class (or build it into {{MimeType}}?) that will output an extension for all mime types including texty mime types. 



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)