From 359cf0fa124cd3bc0ffedf8ecee77022970ba562 Mon Sep 17 00:00:00 2001 From: "Ilya V. Portnov" Date: Tue, 4 Oct 2011 14:26:05 +0600 Subject: [PATCH] Cleanup, minor updates. --- JVM/Common.hs | 6 ----- JVM/Converter.hs | 1 + Java/JAR.hs | 58 +++++++++++++++++++++++++++++++++++++++++++++ Java/JAR/Archive.hs | 19 ++++++--------- Java/JAR/Tree.hs | 3 --- Java/META.hs | 7 +++++- Java/META/Parser.hs | 7 +++++- Java/META/Spec.hs | 4 ---- Java/META/Types.hs | 2 -- hs-java.cabal | 7 +++++- 10 files changed, 84 insertions(+), 30 deletions(-) create mode 100644 Java/JAR.hs delete mode 100644 Java/JAR/Tree.hs diff --git a/JVM/Common.hs b/JVM/Common.hs index 65a6c6f..1b0e5eb 100644 --- a/JVM/Common.hs +++ b/JVM/Common.hs @@ -8,12 +8,9 @@ module JVM.Common 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.Char -import Data.String 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 IsString B.ByteString where - fromString s = B.pack $ map (fromIntegral . ord) $ encodeString s - toCharList :: B.ByteString -> [Int] toCharList bstr = map fromIntegral $ B.unpack bstr diff --git a/JVM/Converter.hs b/JVM/Converter.hs index 52b3483..0c26dd1 100644 --- a/JVM/Converter.hs +++ b/JVM/Converter.hs @@ -18,6 +18,7 @@ import Data.Bits 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 diff --git a/Java/JAR.hs b/Java/JAR.hs new file mode 100644 index 0000000..1c92f50 --- /dev/null +++ b/Java/JAR.hs @@ -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' + diff --git a/Java/JAR/Archive.hs b/Java/JAR/Archive.hs index d84be49..0963a15 100644 --- a/Java/JAR/Archive.hs +++ b/Java/JAR/Archive.hs @@ -1,8 +1,6 @@ -- | 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 @@ -12,9 +10,14 @@ import Java.ClassPath.Common 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 -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) @@ -25,11 +28,3 @@ readFromJAR jarfile path = do 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 index 7057aa2..0000000 --- a/Java/JAR/Tree.hs +++ /dev/null @@ -1,3 +0,0 @@ - -module JAR.Tree where - diff --git a/Java/META.hs b/Java/META.hs index b04b30a..b945a31 100644 --- a/Java/META.hs +++ b/Java/META.hs @@ -3,9 +3,14 @@ module Java.META (module Java.META.Types, module Java.META.Parser, - module Java.META.Spec) + module Java.META.Spec, + Manifest (..), + ManifestEntry (..)) where +import qualified Data.Map as M +import Data.Map ((!)) + import Java.META.Types import Java.META.Parser import Java.META.Spec diff --git a/Java/META/Parser.hs b/Java/META/Parser.hs index 14c1407..f92f49b 100644 --- a/Java/META/Parser.hs +++ b/Java/META/Parser.hs @@ -1,6 +1,7 @@ module Java.META.Parser - (parseMetaFile) where + (parseMeta, + parseMetaFile) where 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") + +parseMeta :: String -> Either ParseError META +parseMeta str = parse pMETA "" (str ++ "\n\n") + diff --git a/Java/META/Spec.hs b/Java/META/Spec.hs index 1e296fb..afda143 100644 --- a/Java/META/Spec.hs +++ b/Java/META/Spec.hs @@ -1,9 +1,5 @@ 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 diff --git a/Java/META/Types.hs b/Java/META/Types.hs index f058f5a..c211291 100644 --- a/Java/META/Types.hs +++ b/Java/META/Types.hs @@ -2,8 +2,6 @@ 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] diff --git a/hs-java.cabal b/hs-java.cabal index 50b5f57..a3b9182 100644 --- a/hs-java.cabal +++ b/hs-java.cabal @@ -31,13 +31,18 @@ library Java.ClassPath Java.ClassPath.Types Java.ClassPath.Common + Java.JAR 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, - LibZip, Glob + LibZip, Glob, parsec >= 3 && <4 ghc-options: -fwarn-unused-imports -- 2.25.1