1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
-- ------------------------------------------------------------
{- |
Module : HXPath
Copyright : Copyright (C) 2005 Torbel Kuseler, Uwe Schmidt
License : MIT
Maintainer : Uwe Schmidt
Maintainer : uwe@fh-wedel.de
Stability : experimental
Portability: portable
HXPath - XPath Evaluator of the Haskell XML Toolbox (Arrow version)
-}
-- ------------------------------------------------------------
module Main
where
import Text.XML.HXT.Arrow.XmlState.TypeDefs
import Text.XML.HXT.Core
import Text.XML.HXT.Curl
import Text.XML.HXT.XPath
import System.Console.GetOpt
import System.Environment
import System.Exit
import System.IO
-- ------------------------------------------------------------
-- |
-- the main program
main :: IO ()
main
= do
argv <- getArgs -- get the commandline arguments
(al, expr, src) <- cmdlineOpts argv -- and evaluate them, return a key-value list
[rc] <- runX (xpath al expr src) -- run the parser arrow
exitProg (rc >= c_err) -- set return code and terminate
-- ------------------------------------------------------------
exitProg :: Bool -> IO a
exitProg True = exitWith (ExitFailure 1)
exitProg False = exitWith ExitSuccess
-- ------------------------------------------------------------
-- |
-- the /real/ main program
--
-- runs in the trivial XmlState monad (with user state set to ())
-- so IO and access to global options is possible
xpath :: SysConfigList -> String -> String -> IOSArrow b Int
xpath cf expr src
= configSysVars cf -- set all global config options, the output file and the
>>> -- other user options are stored as key-value pairs in the stystem state
readDocument [withCurl []] src
>>>
evalXPathExpr
>>>
traceMsg 1 "evaluation finished"
>>>
traceSource
>>>
traceTree
>>>
( formatXPathResult $< getSysVar theIndent )
>>>
writeDocument [] "-"
>>>
getErrStatus
where
evalXPathExpr :: IOSArrow XmlTree XmlTree
evalXPathExpr
= traceMsg 1 ("evaluating XPath expression: " ++ expr)
>>>
replaceChildren ( getXPathTreesInDoc expr
>>>
filterErrorMsg
)
formatXPathResult :: Bool -> IOSArrow XmlTree XmlTree
formatXPathResult indent
= replaceChildren ( mkelem "xpath-result"
[ sattr "expr" expr, sattr "source" src ]
[ newline, getChildren >>> (this <+> newline) ]
)
where
newline
| indent = txt "\n"
| otherwise = none
-- ------------------------------------------------------------
--
-- the options definition part
-- see doc for System.Console.GetOpt
progName :: String
progName = "HXPath"
options :: [OptDescr SysConfig]
options
= generalOptions
++
inputOptions
++
outputOptions
usage :: [String] -> IO a
usage errl
| null errl
= do
hPutStrLn stdout use
exitProg False
| otherwise
= do
hPutStrLn stderr (concat errl ++ "\n" ++ use)
exitProg True
where
header = "HXPath - XPath Evaluator of the Haskell XML Toolbox (Arrow Version)\n" ++
"Usage: " ++ progName ++ " [OPTION...] <XPath expr> <URL or FILE>"
use = usageInfo header options
cmdlineOpts :: [String] -> IO ([SysConfig], String, String)
cmdlineOpts argv
= case (getOpt Permute options argv) of
(scfg,n,[] )
-> do
(ex, sa) <- src n
help (getConfigAttr a_help scfg)
return (scfg, ex, sa)
(_,_,errs)
-> usage errs
where
src [expr, url]
= return (expr, url)
src []
= usage ["XPath expression and input file/url missing"]
src [_]
= usage ["input file/url missing"]
src _
= usage ["too many arguments"]
help "1" = usage []
help _ = return ()
-- ------------------------------------------------------------
|