readMimePackageXML

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

readMimePackageXML
(
string xmlData
)

Return Value

Forward range of mime.type.MimeType elements parsed from xml definition.

Throws

XMLMimeException on format error.

Examples

1     string xmlData = `<?xml version="1.0" encoding="utf-8"?>
2 <mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info">
3     <mime-type type="image/x-sigma-x3f">
4         <comment>Sigma X3F raw image</comment>
5         <sub-class-of type="image/x-dcraw"/>
6         <magic-deleteall/>
7         <magic priority="60">
8             <match value="FOVb" type="string" offset="0">
9                 <match value="0x00FF00FF" type="little32" offset="4" mask="0xFF00FF00"/>
10             </match>
11         </magic>
12         <glob-deleteall/>
13         <glob pattern="*.x3f"/>
14     </mime-type>
15         <mime-type type="image/svg+xml">
16         <comment>SVG image</comment>
17         <sub-class-of type="application/xml"/>
18         <magic priority="80">
19             <match value="&lt;!DOCTYPE svg" type="string" offset="0:256"/>
20             <match value="&lt;svg" type="string" offset="0:256"/>
21         </magic>
22         <glob pattern="*.svg"/>
23         <root-XML namespaceURI="http://www.w3.org/2000/svg" localName="svg"/>
24     </mime-type>
25     <mime-type type="x-content/video-bluray">
26         <comment>Blu-ray video disc</comment>
27         <treemagic>
28         <treematch type="directory" path="BDAV" non-empty="true" match-case="true"/>
29         <treematch type="directory" path="BDMV" non-empty="true"/>
30         </treemagic>
31     </mime-type>
32     <mime-type type="application/x-sharedlib">
33         <magic priority="50">
34             <match value="\177ELF            \003" type="string" offset="0" mask="0xffffffff000000000000000000000000ff"/>
35         </magic>
36     </mime-type>
37 </mime-info>`;
38     auto range = readMimePackageXML(xmlData);
39     assert(!range.empty);
40     auto mimeType = range.front;
41     assert(mimeType is range.front);
42     assert(mimeType.name == "image/x-sigma-x3f");
43     assert(mimeType.deleteMagic);
44     assert(mimeType.deleteGlobs);
45     assert(mimeType.magics.length == 1);
46     auto magic = mimeType.magics[0];
47     assert(magic.weight == 60);
48     assert(magic.matches.length == 1);
49     auto match = magic.matches[0];
50     assert(match.type == MagicMatch.Type.string_);
51     assert(match.value == "FOVb");
52     assert(match.submatches.length == 1);
53     auto submatch = match.submatches[0];
54     assert(submatch.type == MagicMatch.Type.little32);
55     const uint val = 0x00FF00FF;
56     const uint mask = 0xFF00FF00;
57     import std.bitmanip : nativeToLittleEndian;
58     auto valArr = nativeToLittleEndian(val);
59     auto maskArr = nativeToLittleEndian(mask);
60     assert(submatch.value == valArr[]);
61     assert(submatch.mask == maskArr[]);
62 
63     range.popFront();
64     assert(!range.empty);
65 
66     mimeType = range.front;
67     assert(mimeType.XMLnamespaces == [XMLnamespace("http://www.w3.org/2000/svg", "svg")]);
68     assert(mimeType.name == "image/svg+xml");
69     assert(mimeType.magics.length == 1);
70     auto magic2 = mimeType.magics[0];
71     assert(magic2.weight == 80);
72     assert(magic2.matches.length == 2);
73     auto match2 = magic2.matches[1];
74     assert(match2.type == MagicMatch.Type.string_);
75     assert(match2.value == "<svg");
76 
77     range.popFront();
78     assert(!range.empty);
79 
80     mimeType = range.front;
81     assert(mimeType.name == "x-content/video-bluray");
82     assert(mimeType.treeMagics.length == 1);
83     auto treeMagic = mimeType.treeMagics[0];
84     assert(treeMagic.matches.length == 2);
85     auto treeMatch = treeMagic.matches[0];
86     assert(treeMatch.path == "BDAV");
87     assert(treeMatch.nonEmpty);
88     assert(treeMatch.matchCase);
89 
90     range.popFront();
91 
92     mimeType = range.front;
93     assert(mimeType);
94     assert(mimeType.magics.length == 1);
95     auto magic3 = mimeType.magics[0];
96     assert(magic3.matches.length == 1);
97     auto match3 = magic3.matches[0];
98     assert(match3.mask == "\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff");
99 
100     range.popFront();
101     assert(range.empty);
102     assert(range.empty);
103 
104     import std.exception : collectException;
105     xmlData = `<?xml version="1.0" encoding="utf-8"?>
106 <mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info">
107     <mime-type type="application/x-sharedlib">
108         <magic priority="50">
109             <match value="\177ELF            \003" type="string" offset="0" mask="wrong_format"/>
110         </magic>
111     </mime-type>
112 </mime-info>`;
113     range = readMimePackageXML(xmlData);
114     assert(!range.empty);
115     auto e = collectException!XMLMimeException(range.front);
116     assert(e !is null);
117     assert(e.lineNum == 5);

See Also

Meta