From 480abc5f790688ae7572beadbf4fdf8dbc7135f7 Mon Sep 17 00:00:00 2001 From: "Ilya V. Portnov" Date: Wed, 18 Jan 2012 16:15:43 +0600 Subject: [PATCH] Implement creating ZIP (JAR) files from tree of classes. refs #15 --- Java/ClassPath/Common.hs | 10 ++++++++++ Java/JAR/Archive.hs | 22 ++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/Java/ClassPath/Common.hs b/Java/ClassPath/Common.hs index e20286f..e50b1e8 100644 --- a/Java/ClassPath/Common.hs +++ b/Java/ClassPath/Common.hs @@ -3,6 +3,7 @@ module Java.ClassPath.Common where import Data.List import Data.String.Utils (split) +import System.FilePath import Java.ClassPath.Types @@ -19,6 +20,15 @@ mapTM :: (Monad m, Functor m) => (t -> m a) -> Tree t -> m (Tree a) mapTM fn (Directory dir forest) = Directory dir `fmap` mapFM fn forest mapTM fn (File a) = File `fmap` fn a +mapFMF :: (Monad m, Functor m) => (FilePath -> t -> m a) -> [Tree t] -> m [Tree a] +mapFMF fn forest = mapM (mapTMF fn) forest + +mapTMF :: (Monad m, Functor m) => (FilePath -> t -> m a) -> Tree t -> m (Tree a) +mapTMF fn t = go "" t + where + go path (Directory dir forest) = Directory dir `fmap` mapM (go $ path dir) forest + go path (File a) = File `fmap` fn path a + -- | map on tree mapT :: (t -> a) -> Tree t -> Tree a mapT fn (Directory dir forest) = Directory dir (mapF fn forest) diff --git a/Java/JAR/Archive.hs b/Java/JAR/Archive.hs index a4dd026..5425ab5 100644 --- a/Java/JAR/Archive.hs +++ b/Java/JAR/Archive.hs @@ -1,10 +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 @@ -31,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) + -- 2.25.1