-------------------------------------------------------------------------------- -- $Id: RepCommands.hs,v 1.1 2004/03/26 12:18:05 graham Exp $ -- -- Copyright (c) 2003, G. KLYNE. All rights reserved. -- See end of this file for licence information. -------------------------------------------------------------------------------- -- | -- Module : RepCommands -- Copyright : (c) 2003, Graham Klyne -- License : GPL V2 -- -- Maintainer : Graham Klyne -- Stability : provisional -- Portability : H98 -- -- RepCommands: functions to deal with indivudual Swish command options. -- -------------------------------------------------------------------------------- module RepCommands ( repFormat , repInput , repOutput ) where import RepMonad ( RepStateIO, RepState(..) , setFormat, setGraph , setInfo, resetInfo, setError, resetError, setExitcode , emptyState , RepFormat(..) , repError , reportLines, reportLine ) import RepParser ( parseRepFromString ) import RDFGraph ( RDFGraph, merge ) import N3Formatter ( formatGraphAsShowS ) import GraphClass ( LDGraph(..) , Label(..) ) -- import MiscHelpers import ErrorM( ErrorM(..) ) import IO ( Handle(..), openFile, IOMode(..) , hPutStr, hPutStrLn, hClose, hGetContents , hIsOpen, hIsReadable, hIsWritable , stdin, stdout, stderr, try ) import Control.Monad.Trans( MonadTrans(..) ) import Control.Monad.State ( MonadState(..), modify, gets , StateT(..), execStateT ) import Maybe ( Maybe(..), isJust, fromJust ) import Monad ( when ) import System ( ExitCode(..) ) ------------------------------------------------------------ -- Set file format to supplied value ------------------------------------------------------------ repFormat :: RepFormat -> RepStateIO () repFormat fmt = modify $ setFormat fmt ------------------------------------------------------------ -- Read graph from named file ------------------------------------------------------------ repInput :: String -> RepStateIO () repInput fnam = do { maybegraph <- repReadReport fnam ; case maybegraph of Just g -> modify $ setGraph g otherwise -> return () } ------------------------------------------------------------ -- Output graph to named file ------------------------------------------------------------ repOutput :: String -> RepStateIO () repOutput fnam = do { maybehandleclose <- repWriteFile fnam ; case maybehandleclose of Just (h,c) -> do { repOutputGraph fnam h ; if c then lift $ hClose h else return () } otherwise -> return () } repOutputGraph :: String -> Handle -> RepStateIO () repOutputGraph fnam hnd = do { fmt <- gets $ format ; case fmt of N3 -> repFormatN3 fnam hnd otherwise -> repError ("Unsupported file format: "++(show fmt)) 4 } repFormatN3 :: String -> Handle -> RepStateIO () repFormatN3 fnam hnd = do { out <- gets $ formatGraphAsShowS . graph ; lift $ hPutStr hnd (out "") } ------------------------------------------------------------ -- Common input functions ------------------------------------------------------------ -- -- Keep the logic separate for reading file data and -- parsing it to an RDF graph value. repReadReport :: String -> RepStateIO (Maybe RDFGraph) repReadReport fnam = do { maybefile <- repOpenFile fnam ; case maybefile of Just (h,i) -> do { res <- repParse fnam i ; lift $ hClose h ; return res } otherwise -> return Nothing } -- Open and read file, returning its handle and content, or Nothing -- WARNING: the handle must not be closed until input is fully evaluated repOpenFile :: String -> RepStateIO (Maybe (Handle,String)) repOpenFile fnam = do { (hnd,hop) <- lift $ if null fnam then return (stdin,True) else do { o <- try (openFile fnam ReadMode) ; case o of Left e -> return (stdin,False) Right h -> return (h,True) } ; hrd <- lift $ hIsReadable hnd ; res <- if hop && hrd then do { ; fc <- lift $ hGetContents hnd ; return $ Just (hnd,fc) } else do { lift $ hClose hnd ; repError ("Cannot read file: "++fnam) 3 ; return Nothing } ; return res } repParse :: String -> String -> RepStateIO (Maybe RDFGraph) repParse fnam inp = do { fmt <- gets $ format ; case fmt of _ -> repParseRep fnam inp otherwise -> do { repError ("Unsupported file format: "++(show fmt)) 4 ; return Nothing } } repParseRep :: String -> String -> RepStateIO (Maybe RDFGraph) repParseRep fnam inp = do { let pres = parseRepFromString (Just fnam) inp ; case pres of Error err -> do { repError ("Report description syntax error in file " ++fnam++": "++err) 2 ; return Nothing } Result gr -> return $ Just gr } -- Open file for writing, returning its handle, or Nothing -- Also returned is a flag indicating whether or not the -- handled should be closed when writing is done (if writing -- to standard output, the handle should not be closed as the -- run-time system should deal with that). repWriteFile :: String -> RepStateIO (Maybe (Handle,Bool)) repWriteFile fnam = do { (hnd,hop,cls) <- lift $ if null fnam then return (stdout,True,False) else do { o <- try (openFile fnam WriteMode) ; case o of Left e -> return (stderr,False,False) Right h -> return (h,True,True) } ; hwt <- lift $ hIsWritable hnd ; if hop && hwt then return $ Just (hnd,cls) else do { if cls then lift $ hClose hnd else return () ; repError ("Cannot write file: "++fnam) 3 ; return Nothing } } -------------------------------------------------------------------------------- -- -- Copyright (c) 2004, G. KLYNE. All rights reserved. -- -- This is free software; you can redistribute it and/or modify -- it under the terms of the GNU General Public License as published by -- the Free Software Foundation; either version 2 of the License, or -- (at your option) any later version. -- -- This software is distributed in the hope that it will be useful, -- but WITHOUT ANY WARRANTY; without even the implied warranty of -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -- GNU General Public License for more details. -- -- You should have received a copy of the GNU General Public License -- along with RepToRDF; if not, write to: -- The Free Software Foundation, Inc., -- 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -- or view the web page at: -- http://www.gnu.org/copyleft/gpl.html -- -------------------------------------------------------------------------------- -- $Source: /file/cvsdev/CompileRDF/RepCommands.hs,v $ -- $Author: graham $ -- $Revision: 1.1 $ -- $Log: RepCommands.hs,v $ -- Revision 1.1 2004/03/26 12:18:05 graham -- Created report description compiler --