Function readMimePackageXML

Read MIME types from xml formatted data with mime-info root element as defined by spec.

mime.xml.XMLPackageRange readMimePackageXML (
  string xmlData
);

Returns

Forward range of MimeType elements parsed from xml definition.

Throws

XMLMimeException on format error.

See Also

readMimePackageFile

Example

string xmlData = `<?xml version="1.0" encoding="utf-8"?>
<mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info">
<mime-type type="image/x-sigma-x3f">
    <comment>Sigma X3F raw image</comment>
    <sub-class-of type="image/x-dcraw"/>
    <magic-deleteall/>
    <magic priority="60">
        <match value="FOVb" type="string" offset="0">
            <match value="0x00FF00FF" type="little32" offset="4" mask="0xFF00FF00"/>
        </match>
    </magic>
    <glob-deleteall/>
    <glob pattern="*.x3f"/>
</mime-type>
    <mime-type type="image/svg+xml">
    <comment>SVG image</comment>
    <sub-class-of type="application/xml"/>
    <magic priority="80">
        <match value="&lt;!DOCTYPE svg" type="string" offset="0:256"/>
        <match value="&lt;svg" type="string" offset="0:256"/>
    </magic>
    <glob pattern="*.svg"/>
    <root-XML namespaceURI="http://www.w3.org/2000/svg" localName="svg"/>
</mime-type>
<mime-type type="x-content/video-bluray">
    <comment>Blu-ray video disc</comment>
    <treemagic>
    <treematch type="directory" path="BDAV" non-empty="true" match-case="true"/>
    <treematch type="directory" path="BDMV" non-empty="true"/>
    </treemagic>
</mime-type>
<mime-type type="application/x-sharedlib">
    <magic priority="50">
        <match value="\177ELF            \003" type="string" offset="0" mask="0xffffffff000000000000000000000000ff"/>
    </magic>
</mime-type>
</mime-info>`;
auto range = readMimePackageXML(xmlData);
assert(!range.empty);
auto mimeType = range.front;
assert(mimeType is range.front);
assert(mimeType.name == "image/x-sigma-x3f");
assert(mimeType.deleteMagic);
assert(mimeType.deleteGlobs);
assert(mimeType.magics.length == 1);
auto magic = mimeType.magics[0];
assert(magic.weight == 60);
assert(magic.matches.length == 1);
auto match = magic.matches[0];
assert(match.type == MagicMatch.Type.string_);
assert(match.value == "FOVb");
assert(match.submatches.length == 1);
auto submatch = match.submatches[0];
assert(submatch.type == MagicMatch.Type.little32);
const uint val = 0x00FF00FF;
const uint mask = 0xFF00FF00;
import std.bitmanip : nativeToLittleEndian;
auto valArr = nativeToLittleEndian(val);
auto maskArr = nativeToLittleEndian(mask);
assert(submatch.value == valArr[]);
assert(submatch.mask == maskArr[]);

range.popFront();
assert(!range.empty);

mimeType = range.front;
assert(mimeType.XMLnamespaces == [XMLnamespace("http://www.w3.org/2000/svg", "svg")]);
assert(mimeType.name == "image/svg+xml");
assert(mimeType.magics.length == 1);
auto magic2 = mimeType.magics[0];
assert(magic2.weight == 80);
assert(magic2.matches.length == 2);
auto match2 = magic2.matches[1];
assert(match2.type == MagicMatch.Type.string_);
assert(match2.value == "<svg");

range.popFront();
assert(!range.empty);

mimeType = range.front;
assert(mimeType.name == "x-content/video-bluray");
assert(mimeType.treeMagics.length == 1);
auto treeMagic = mimeType.treeMagics[0];
assert(treeMagic.matches.length == 2);
auto treeMatch = treeMagic.matches[0];
assert(treeMatch.path == "BDAV");
assert(treeMatch.nonEmpty);
assert(treeMatch.matchCase);

range.popFront();

mimeType = range.front;
assert(mimeType);
assert(mimeType.magics.length == 1);
auto magic3 = mimeType.magics[0];
assert(magic3.matches.length == 1);
auto match3 = magic3.matches[0];
assert(match3.mask == "\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff");

range.popFront();
assert(range.empty);
assert(range.empty);

import std.exception : collectException;
xmlData = `<?xml version="1.0" encoding="utf-8"?>
<mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info">
<mime-type type="application/x-sharedlib">
    <magic priority="50">
        <match value="\177ELF            \003" type="string" offset="0" mask="wrong_format"/>
    </magic>
</mime-type>
</mime-info>`;
range = readMimePackageXML(xmlData);
assert(!range.empty);
auto e = collectException!XMLMimeException(range.front);
assert(e !is null);
assert(e.lineNum == 5);