Cleanup, minor updates.
authorIlya V. Portnov <i.portnov@compassplus.ru>
Tue, 4 Oct 2011 08:26:05 +0000 (14:26 +0600)
committerIlya Portnov <portnov84@rambler.ru>
Wed, 5 Oct 2011 17:43:44 +0000 (23:43 +0600)
JVM/Common.hs
JVM/Converter.hs
Java/JAR.hs [new file with mode: 0644]
Java/JAR/Archive.hs
Java/JAR/Tree.hs [deleted file]
Java/META.hs
Java/META/Parser.hs
Java/META/Spec.hs
Java/META/Types.hs
hs-java.cabal

index 65a6c6fcf8425eb1f75e70ed2991c998e495e322..1b0e5eb022e012076f6605712d9e2538aefe0b57 100644 (file)
@@ -8,12 +8,9 @@ module JVM.Common
   byteString
   ) where
 
   byteString
   ) where
 
-import Codec.Binary.UTF8.String hiding (encode, decode)
 import Data.Binary
 import Data.Binary.Put
 import qualified Data.ByteString.Lazy as B
 import Data.Binary
 import Data.Binary.Put
 import qualified Data.ByteString.Lazy as B
-import Data.Char
-import Data.String
 import qualified Data.Map as M
 import Data.Default
 
 import qualified Data.Map as M
 import Data.Default
 
@@ -25,9 +22,6 @@ instance Default B.ByteString where
 instance Default Word16 where
   def = 0
 
 instance Default Word16 where
   def = 0
 
-instance IsString B.ByteString where
-  fromString s = B.pack $ map (fromIntegral . ord) $ encodeString s
-
 toCharList :: B.ByteString -> [Int]
 toCharList bstr = map fromIntegral $ B.unpack bstr
 
 toCharList :: B.ByteString -> [Int]
 toCharList bstr = map fromIntegral $ B.unpack bstr
 
index 52b3483943995dd2e6ddb44e5a14bc2dfd36c18d..0c26dd156c0e91afcf6c638106fc651ea9a53933 100644 (file)
@@ -18,6 +18,7 @@ import Data.Bits
 import Data.Binary
 import Data.Default () -- import instances only
 import qualified Data.ByteString.Lazy as B
 import Data.Binary
 import Data.Default () -- import instances only
 import qualified Data.ByteString.Lazy as B
+import qualified Data.ByteString.Lazy.Char8 ()
 import qualified Data.Set as S
 import qualified Data.Map as M
 
 import qualified Data.Set as S
 import qualified Data.Map as M
 
diff --git a/Java/JAR.hs b/Java/JAR.hs
new file mode 100644 (file)
index 0000000..1c92f50
--- /dev/null
@@ -0,0 +1,58 @@
+
+module Java.JAR 
+  (readManifest,
+   readJAR,
+   addJAR
+  ) where
+
+import Control.Monad.Trans (liftIO)
+import qualified Control.Monad.State as St
+import Data.List
+import qualified Codec.Archive.LibZip as Zip
+
+import Java.ClassPath
+import Java.JAR.Archive
+import Java.META
+
+readManifest :: Zip.Archive (Maybe Manifest)
+readManifest = do
+  let manifestPath = "META-INF/MANIFEST.MF"
+  files <- Zip.fileNames []
+  if manifestPath `elem` files
+    then do
+         content <- Zip.fileContents [] manifestPath
+         case parseMeta content of
+           Left e -> fail $ show e
+           Right meta -> return $ Just (loadSpec meta)
+    else return Nothing
+
+readOne :: FilePath -> String -> Zip.Archive [Tree CPEntry]
+readOne jarfile str = do
+    files <- Zip.fileNames []
+    return $ mapF (NotLoadedJAR jarfile) (buildTree $ filter good files)
+  where
+    good name = str `isPrefixOf` name
+
+-- | Read entries from JAR file, using MANIFEST.MF if it exists.
+readJAR :: FilePath -> IO [Tree CPEntry]
+readJAR jarfile = do
+  r <- Zip.withArchive [] jarfile $ do
+         m <- readManifest
+         case m of
+           Nothing -> return Nothing
+           Just mf -> do
+                      trees <- mapM (readOne jarfile) (map meName $ manifestEntries mf)
+                      let forest = merge (concat trees)
+                      return (Just forest)
+  case r of
+    Nothing -> readAllJAR jarfile
+    Just f  -> return f
+
+-- | Add given JAR file to ClassPath
+addJAR :: FilePath -> ClassPath ()
+addJAR jarfile = do
+  classes <- liftIO $ readJAR jarfile
+  cp <- St.get
+  let cp' = merge $ cp ++ classes
+  St.put cp'
+
index d84be49c35034b2b05d7c19e13bb7b3df8f627c3..0963a156eb61250a0cdfdd19c91fb7f45ec7bcc3 100644 (file)
@@ -1,8 +1,6 @@
 -- | This module defines functions to read Java JAR files.
 module Java.JAR.Archive where
 
 -- | This module defines functions to read Java JAR files.
 module Java.JAR.Archive where
 
-import Control.Monad.Trans
-import qualified Control.Monad.State as St
 import qualified Codec.Archive.LibZip as Zip
 import Data.Binary
 import qualified Data.ByteString.Lazy as B
 import qualified Codec.Archive.LibZip as Zip
 import Data.Binary
 import qualified Data.ByteString.Lazy as B
@@ -12,9 +10,14 @@ import Java.ClassPath.Common
 import JVM.ClassFile
 import JVM.Converter
 
 import JVM.ClassFile
 import JVM.Converter
 
+readJAREntry :: (Enum a) => FilePath -> String -> IO (Maybe [a])
+readJAREntry jarfile path = do
+  Zip.catchZipError (Just `fmap` (Zip.withArchive [] jarfile $ Zip.fileContents [] path))
+                    (\_ -> return Nothing)
+
 -- | Read all entires from JAR file
 -- | Read all entires from JAR file
-readJAR :: FilePath -> IO [Tree CPEntry]
-readJAR jarfile = do
+readAllJAR :: FilePath -> IO [Tree CPEntry]
+readAllJAR jarfile = do
   files <- Zip.withArchive [] jarfile $ Zip.fileNames []
   return $ mapF (NotLoadedJAR jarfile) (buildTree files)
 
   files <- Zip.withArchive [] jarfile $ Zip.fileNames []
   return $ mapF (NotLoadedJAR jarfile) (buildTree files)
 
@@ -25,11 +28,3 @@ readFromJAR jarfile path = do
   let bstr = B.pack content
   return $ classFile2Direct (decode bstr)
 
   let bstr = B.pack content
   return $ classFile2Direct (decode bstr)
 
--- | Add given JAR file to ClassPath
-addJAR :: FilePath -> ClassPath ()
-addJAR jarfile = do
-  classes <- liftIO $ readJAR jarfile
-  cp <- St.get
-  let cp' = merge $ cp ++ classes
-  St.put cp'
-
diff --git a/Java/JAR/Tree.hs b/Java/JAR/Tree.hs
deleted file mode 100644 (file)
index 7057aa2..0000000
+++ /dev/null
@@ -1,3 +0,0 @@
-
-module JAR.Tree where
-
index b04b30af547c4be6e19ca2f098632cc44332c0cf..b945a3152e490d66c0c77925a30fc078af0095b1 100644 (file)
@@ -3,9 +3,14 @@
 module Java.META
   (module Java.META.Types,
    module Java.META.Parser,
 module Java.META
   (module Java.META.Types,
    module Java.META.Parser,
-   module Java.META.Spec)
+   module Java.META.Spec,
+   Manifest (..),
+   ManifestEntry (..))
   where
 
   where
 
+import qualified Data.Map as M
+import Data.Map ((!))
+
 import Java.META.Types
 import Java.META.Parser
 import Java.META.Spec
 import Java.META.Types
 import Java.META.Parser
 import Java.META.Spec
index 14c14073ecfe469ec67d549cc8ee45057ea9b0b6..f92f49bedf70b725f463b7a9cea0250441f7a015 100644 (file)
@@ -1,6 +1,7 @@
 
 module Java.META.Parser
 
 module Java.META.Parser
-  (parseMetaFile) where
+  (parseMeta,
+   parseMetaFile) where
 
 import qualified Data.Map as M
 import Text.Parsec
 
 import qualified Data.Map as M
 import Text.Parsec
@@ -59,3 +60,7 @@ parseMetaFile :: FilePath -> IO (Either ParseError META)
 parseMetaFile path = do
   str <- readFile path
   return $ parse pMETA path (str ++ "\n\n")
 parseMetaFile path = do
   str <- readFile path
   return $ parse pMETA path (str ++ "\n\n")
+
+parseMeta :: String -> Either ParseError META
+parseMeta str = parse pMETA "<META>" (str ++ "\n\n")
+
index 1e296fba177a36ba945bdd1eafd99d7bd1491afc..afda1434b95ac349d20929f3dcf27d55f52b33da 100644 (file)
@@ -1,9 +1,5 @@
 module Java.META.Spec where
 
 module Java.META.Spec where
 
-import Control.Monad
-import Control.Monad.Error
-import qualified Data.Map as M
-import Data.Map ((!))
 import Data.Char (toLower)
 
 import Java.META.Types
 import Data.Char (toLower)
 
 import Java.META.Types
index f058f5a7c62665bcf9f5ebfde4e0fa390f897056..c21129184ff9291f95a7a3baa3c16e4f80f4abac 100644 (file)
@@ -2,8 +2,6 @@
 module Java.META.Types where
 
 import qualified Data.Map as M
 module Java.META.Types where
 
 import qualified Data.Map as M
-import Text.Parsec
-import Text.Parsec.String
 
 type Section = M.Map String String
 type META = [Section]
 
 type Section = M.Map String String
 type META = [Section]
index 50b5f5723b4cad166520ca3004a1b229370b5d28..a3b918260201f78afc3f194badf72b9db370a29c 100644 (file)
@@ -31,13 +31,18 @@ library
                    Java.ClassPath
                    Java.ClassPath.Types
                    Java.ClassPath.Common
                    Java.ClassPath
                    Java.ClassPath.Types
                    Java.ClassPath.Common
+                   Java.JAR
                    Java.JAR.Archive
                    Java.JAR.Archive
+                   Java.META
+                   Java.META.Types
+                   Java.META.Spec
+                   Java.META.Parser
 
   Build-Depends:  base >= 3 && <= 5, containers, binary,
                   mtl, directory, filepath, utf8-string, array,
                   bytestring, data-binary-ieee754, binary-state,
                   control-monad-exception, data-default, MissingH,
 
   Build-Depends:  base >= 3 && <= 5, containers, binary,
                   mtl, directory, filepath, utf8-string, array,
                   bytestring, data-binary-ieee754, binary-state,
                   control-monad-exception, data-default, MissingH,
-                  LibZip, Glob
+                  LibZip, Glob, parsec >= 3 && <4
 
   ghc-options: -fwarn-unused-imports
 
 
   ghc-options: -fwarn-unused-imports