www.gusucode.com > external 工具箱matlab源码程序 > external/interfaces/webservices/http/+matlab/+net/+internal/getCharsetForMediaType.m

    function charset = getCharsetForMediaType(mediaType)
% Given the mediaType (a type/subtype string) or MediaType, return the charset we
% should use to decode or encode it, if it's character data, or '' if not.
%
% For a MediaType that has an explicit charset parameter, return that. For a
% type/subtype string or a MediaType that has no charset parameter, return the
% default charset for that type/subtype if we know it.  If we don't recognize the
% type/subtype as being character data, return ''.
%
% Some document types like text/xml have the charset specified in the document, not
% in a Content-Type field.  For these we'll return the most likely charset typically
% used for that media type.  This leads us to return utf-8 for many
% text/* types such as text/html and text/xml, even though the default is us-ascii.
% This is fine, because utf-8 is a superset of us-ascii.

% Copyright 2015-2016 The MathWorks, Inc.

    if ischar(mediaType) || isstring(mediaType)
        % if a string, assume it's 'type/subtype' with no explicit charset
        mtype = char(mediaType);
    else
        charset = mediaType.getParameter('charset');
        if ~isempty(charset)
            return
        else
            mtype = char(mediaType.Type + '/' + mediaType.Subtype);
        end
    end
    % No explicit charset; return default for the MediaType, if any.
    % The following content types are character data for which we can guess a default
    % encoding.
    switch (lower(mtype))
        case {'text/plain', 'text/csv'}
            charset = 'us-ascii';  % RFC 1341
        % The default type for html was changed from iso-8859-1 in HTML 4 to utf-8 in
        % HTML 5.  Since we don't have access to the data, assume UTF-8.
        % For x-www-form-urlencoded, there is no default charset, but if one is
        % missing from the mediaType, the payload decode algorithm in 
        % http://www.w3.org/TR/html5/forms.html#application/x-www-form-urlencoded-decoding-algorithm
        % "suggests" UTF-8.  For text/xml, we specify utf-8 because that's the
        % charset that xmlwrite uses, which is what we'll call to convert a user's DOM
        % to a string.
        case {'application/json', 'text/html', 'text/xml', 'text/javascript', ...
              'application/xhtml+xml', 'application/xml', 'text/css', 'text/calendar', ...
              'application/x-www-form-urlencoded', 'application/javascript', 'application/css'}
            charset = 'utf-8';
        % TBD More?
        otherwise
            if strcmpi(mediaType.Type, 'text')
                % Unknown text type.  Return utf-8 even though the default for text is ASCII,
                % since utf-8 is superset.
                charset = 'utf-8';
            else
                % Return an empty charset.  For character data, the caller will need to derive
                % the charset based on the data.
                charset = '';
            end
    end
end