b04b30af547c4be6e19ca2f098632cc44332c0cf
[hs-java.git] / Java / META.hs
1 -- | This module declares functions and data types for
2 -- JAR meta-information classes, such as MANIFEST.MF etc.
3 module Java.META
4   (module Java.META.Types,
5    module Java.META.Parser,
6    module Java.META.Spec)
7   where
8
9 import Java.META.Types
10 import Java.META.Parser
11 import Java.META.Spec
12
13 -- | JAR MANIFEST.MF
14 data Manifest = Manifest {
15   manifestVersion :: String,
16   createdBy :: String,
17   sealed :: Bool,
18   signatureVersion :: Maybe String,
19   classPath :: [String],
20   mainClass :: Maybe String,
21   manifestEntries :: [ManifestEntry]}
22   deriving (Eq, Show)
23
24 -- | Manifest entry
25 data ManifestEntry = ManifestEntry {
26   meName :: String,
27   meSealed :: Bool,
28   meContentType :: Maybe String,
29   meBean :: Bool }
30   deriving (Eq, Show)
31
32 instance MetaSpec Manifest where
33   loadFirstSection s = Manifest {
34     manifestVersion = s ! "Manifest-Version",
35     createdBy = s ! "Created-By",
36     sealed = case M.lookup "Sealed" s of
37                Nothing -> False
38                Just str -> string2bool str,
39     signatureVersion = M.lookup "Signature-Version" s,
40     classPath = case M.lookup "Class-Path" s of
41                   Nothing -> []
42                   Just str -> words str,
43     mainClass = M.lookup "Main-Class" s,
44     manifestEntries = []}
45
46   loadOtherSection m s = m {manifestEntries = manifestEntries m ++ [entry]}
47     where
48       entry = ManifestEntry {
49                 meName = s ! "Name",
50                 meSealed = case M.lookup "Sealed" s of
51                              Nothing -> sealed m
52                              Just str -> string2bool str,
53                 meContentType = M.lookup "Content-Type" s,
54                 meBean = case M.lookup "Java-Bean" s of
55                            Nothing -> False
56                            Just str -> string2bool str }
57
58   storeMeta m = first: map store (manifestEntries m)
59     where
60       first = M.fromList $ [
61           ("Manifest-Version", manifestVersion m),
62           ("Created-By", createdBy m)] ++
63           lookupList "Signature-Version" (signatureVersion m) ++
64           lookupList "Main-Class" (mainClass m) ++
65           case classPath m of
66             [] -> []
67             list -> [("Class-Path", unwords list)]
68
69       store e = M.fromList $ [
70           ("Name", meName e),
71           ("Sealed", bool2string $ meSealed e)] ++
72           lookupList "Content-Type" (meContentType e) ++
73           if meBean e
74             then [("Java-Bean", "true")]
75             else []
76