X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=Java%2FJAR%2FArchive.hs;h=5425ab5aa7b7d72e1a298c111240fe566cda6794;hb=480abc5f790688ae7572beadbf4fdf8dbc7135f7;hp=0963a156eb61250a0cdfdd19c91fb7f45ec7bcc3;hpb=368ce628bdf8a7fa772a6860aed12f00baea3906;p=hs-java.git diff --git a/Java/JAR/Archive.hs b/Java/JAR/Archive.hs index 0963a15..5425ab5 100644 --- a/Java/JAR/Archive.hs +++ b/Java/JAR/Archive.hs @@ -1,9 +1,12 @@ -- | This module defines functions to read Java JAR files. module Java.JAR.Archive where +import Control.Monad.Trans import qualified Codec.Archive.LibZip as Zip import Data.Binary +import Data.List import qualified Data.ByteString.Lazy as B +import System.FilePath import Java.ClassPath.Types import Java.ClassPath.Common @@ -18,8 +21,10 @@ readJAREntry jarfile path = do -- | Read all entires from JAR file 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 $ filter good files) + where + good file = ".class" `isSuffixOf` file -- | Read one class from JAR file readFromJAR :: FilePath -> FilePath -> IO (Class Direct) @@ -28,3 +33,23 @@ readFromJAR jarfile path = do let bstr = B.pack content return $ classFile2Direct (decode bstr) +checkClassTree :: [Tree CPEntry] -> IO [Tree (FilePath, Class Direct)] +checkClassTree forest = mapFMF check forest + where + check _ (NotLoaded path) = do + cls <- parseClassFile path + return (path, cls) + check a (Loaded path cls) = return (a path, cls) + check a (NotLoadedJAR jar path) = do + cls <- readFromJAR jar (a path) + return (a path, cls) + check a (LoadedJAR _ cls) = + return (a show (thisClass cls), cls) + +zipJAR :: [Tree (FilePath, Class Direct)] -> Zip.Archive () +zipJAR forest = do + mapFM go forest + return () + where + go (path, cls) = Zip.addFile path =<< Zip.sourceBuffer (B.unpack $ encodeClass cls) +