module Database.Ferry.Algebra.Render.XMLUtils where
import Text.XML.HaXml.Types
import Database.Ferry.Algebra.Data.Algebra
import qualified Data.Map as M
import Control.Monad.State
import Control.Monad.Writer
import Control.Monad.Reader
type ColName = String
type Graph = (AlgNode, [(Algebra, AlgNode)])
type GraphNode = Int
type XMLNode = Int
type Dictionary = M.Map GraphNode XMLNode
type XML = WriterT [Element ()] (ReaderT (M.Map AlgNode Algebra, M.Map AlgNode [String], Bool) (State (Int, Dictionary)))
getTags :: GraphNode -> XML (Maybe [String])
getTags i = do
(_, ts, _) <- ask
return $ M.lookup i ts
debugEnabled :: XML Bool
debugEnabled = do
(_,_,d) <- ask
return d
isDefined :: GraphNode -> XML (Maybe XMLNode)
isDefined g = do
(_, d) <- get
return $ M.lookup g d
freshId :: XML Int
freshId = do
(n, d) <- get
put (n + 1, d)
return n
addNodeTrans :: GraphNode -> XMLNode -> XML ()
addNodeTrans gId xId = do
(n, d) <- get
put (n, M.insert gId xId d)
getNode :: Int -> XML Algebra
getNode i = do
(nodes, _, _) <- ask
return $ nodes M.! i
runXML :: Bool -> M.Map AlgNode Algebra -> M.Map AlgNode [String] -> XML a -> [Element ()]
runXML debug m t = snd . fst . flip runState (0, M.empty) . flip runReaderT (m, t, debug) . runWriterT
infixr 0 `childsOf`
infixr 0 `dataChildOf`
infixr 0 `attrsOf`
childsOf :: [Element ()] -> Element () -> Element ()
childsOf cs (Elem n attrs cs') = Elem n attrs $ cs' ++ [CElem c () | c <- cs]
dataChildOf :: Show a => a -> Element () -> Element ()
dataChildOf v (Elem n attrs cs) = Elem n attrs $ (CString False (show v) ()) : cs
stringChildOf :: String -> Element () -> Element ()
stringChildOf v (Elem n attrs cs) = Elem n attrs $ (CString False v ()) : cs
column :: String -> Bool -> Element ()
column n v = let new = case v of
True -> "true"
False -> "false"
in [attr "name" n, attr "new" new] `attrsOf` xmlElem "column"
typeN :: ATy -> Element ()
typeN t = [attr "name" $ show t] `attrsOf` xmlElem "type"
xmlElem :: String -> Element ()
xmlElem n = Elem (N n) [] []
node :: XMLNode -> String -> Element ()
node xId t = [attr "id" $ show xId, attr "kind" t] `attrsOf` xmlElem "node"
contentNode :: Element ()
contentNode = xmlElem "content"
attr :: String -> String -> Attribute
attr n v = (N n, AttValue [Left v])
attrsOf :: [Attribute] -> Element () -> Element ()
attrsOf at (Elem n attrs cs) = Elem n (at ++ attrs) cs