IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
Manifest.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Immutable;
3using System.IO;
4
5namespace IKVM.CoreLib.Jar
6{
7
13 internal class Manifest
14 {
15
16 static readonly char[] ManifestNameValueSeparatorChars = [':'];
17
18 readonly ImmutableDictionary<string, string> _mainAttributes;
19 readonly ImmutableDictionary<string, ImmutableDictionary<string, string>> _attributes;
20
25 public Manifest(TextReader reader)
26 {
27 var attributes = ImmutableDictionary.CreateBuilder<string, ImmutableDictionary<string, string>>();
28
29 // read the remaining sections
30 while (ReadSection(reader) is { } section)
31 {
32 if (_mainAttributes is null)
33 {
34 section = section.Remove("Name");
35 _mainAttributes = section;
36 }
37 else
38 {
39 if (section.TryGetValue("Name", out var name))
40 {
41 section = section.Remove("Name");
42 attributes[name] = section;
43 }
44 else
45 {
46 throw new FormatException("Non-main manifest section missing Name attribute.");
47 }
48 }
49 }
50
51 if (_mainAttributes is null)
52 throw new FormatException("Could not find main manifest section.");
53
54 // finalize non-main sections
55 _attributes = attributes.ToImmutable();
56 }
57
62 public Manifest(Stream stream) :
63 this(new StreamReader(stream))
64 {
65
66 }
67
72 public Manifest(string value) :
73 this(new StringReader(value))
74 {
75
76 }
77
82 ImmutableDictionary<string, string>? ReadSection(TextReader reader)
83 {
84 var attributes = ImmutableDictionary.CreateBuilder<string, string>(StringComparer.OrdinalIgnoreCase);
85
86 // read until we hit an empty line or the end of the file
87 while (reader.ReadLine() is string s && s.Trim() != "")
88 if (s.Split(ManifestNameValueSeparatorChars, 2) is string[] p && p.Length == 2)
89 attributes[p[0].Trim()] = p[1].Trim();
90
91 // only return attributes if there is any
92 return attributes.Count > 0 ? attributes.ToImmutable() : null;
93 }
94
98 public ImmutableDictionary<string, string> MainAttributes => _mainAttributes;
99
103 public ImmutableDictionary<string, ImmutableDictionary<string, string>> Attributes => _attributes;
104
105 }
106
107}