cabal: bump data-default dependency to 0.5.0.
[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    Manifest (..),
8    ManifestEntry (..))
9   where
10
11 import qualified Data.Map as M
12 import Data.Map ((!))
13
14 import Java.META.Types
15 import Java.META.Parser
16 import Java.META.Spec
17
18 -- | JAR MANIFEST.MF
19 data Manifest = Manifest {
20   manifestVersion :: String,
21   createdBy :: String,
22   sealed :: Bool,
23   signatureVersion :: Maybe String,
24   classPath :: [String],
25   mainClass :: Maybe String,
26   manifestEntries :: [ManifestEntry]}
27   deriving (Eq, Show)
28
29 -- | Manifest entry
30 data ManifestEntry = ManifestEntry {
31   meName :: String,
32   meSealed :: Bool,
33   meContentType :: Maybe String,
34   meBean :: Bool }
35   deriving (Eq, Show)
36
37 instance MetaSpec Manifest where
38   loadFirstSection s = Manifest {
39     manifestVersion = s ! "Manifest-Version",
40     createdBy = s ! "Created-By",
41     sealed = case M.lookup "Sealed" s of
42                Nothing -> False
43                Just str -> string2bool str,
44     signatureVersion = M.lookup "Signature-Version" s,
45     classPath = case M.lookup "Class-Path" s of
46                   Nothing -> []
47                   Just str -> words str,
48     mainClass = M.lookup "Main-Class" s,
49     manifestEntries = []}
50
51   loadOtherSection m s = m {manifestEntries = manifestEntries m ++ [entry]}
52     where
53       entry = ManifestEntry {
54                 meName = s ! "Name",
55                 meSealed = case M.lookup "Sealed" s of
56                              Nothing -> sealed m
57                              Just str -> string2bool str,
58                 meContentType = M.lookup "Content-Type" s,
59                 meBean = case M.lookup "Java-Bean" s of
60                            Nothing -> False
61                            Just str -> string2bool str }
62
63   storeMeta m = first: map store (manifestEntries m)
64     where
65       first = M.fromList $ [
66           ("Manifest-Version", manifestVersion m),
67           ("Created-By", createdBy m)] ++
68           lookupList "Signature-Version" (signatureVersion m) ++
69           lookupList "Main-Class" (mainClass m) ++
70           case classPath m of
71             [] -> []
72             list -> [("Class-Path", unwords list)]
73
74       store e = M.fromList $ [
75           ("Name", meName e),
76           ("Sealed", bool2string $ meSealed e)] ++
77           lookupList "Content-Type" (meContentType e) ++
78           if meBean e
79             then [("Java-Bean", "true")]
80             else []
81