module XML.HXQ.XTree where
import System.IO
import XMLParse(XMLEvent(..))
import HXML(AttList)
import XML.HXQ.Parser(Ast(..))
import Database.HDBC(Statement)
instance Eq Statement where x == y = False
type Tag = String
data XTree = XElem !Tag !AttList !Int XTree [XTree]
| XText !String
| XInt !Int
| XFloat !Float
| XBool !Bool
| XPI Tag String
| XGERef Tag
| XComment String
| XError String
| XStmt Statement
| XNoPad
deriving Eq
type XSeq = [XTree]
showAL :: AttList -> String
showAL = foldr (\(a,v) r -> " "++a++"=\""++v++"\""++r) []
showXT :: XTree -> Bool -> String
showXT e pad
= case e of
XElem tag al _ _ [] -> "<"++tag++showAL al++"/>"
XElem tag al _ _ xs -> "<"++tag++showAL al++">"++showXS xs++"</"++tag++">"
XText text -> p++text
XInt n -> p++show n
XFloat n -> p++show n
XBool v -> p++if v then "true" else "false"
XComment s -> "<!--"++s++"-->"
XPI n s -> "<?"++n++" "++s++">"
XError s -> error s
_ -> ""
where p = if pad then " " else ""
showXS :: XSeq -> String
showXS [] = ""
showXS (x:xs) = showXT x False ++ sXS xs
where sXS (XNoPad:x:xs) = (showXT x False) ++ sXS xs
sXS (x:xs) = (showXT x True) ++ sXS xs
sXS _ = ""
instance Show XTree where
show t = showXT t False
putXSeq :: XSeq -> IO ()
putXSeq xs = hSetBuffering stdout NoBuffering >> putStrLn (showXS xs)
type Stream = [XMLEvent]
noParentError = error "parent references are not supported yet"
materializeWithoutParent :: Stream -> XTree
materializeWithoutParent stream
= XElem "document" [] 1 noParentError
[head (filter (\x -> case x of XElem _ _ _ _ _ -> True; _ -> False)
((\(x,_,_)->x) (ml stream 2)))]
where m ((TextEvent t):xs) i = (XText t,xs,i)
m ((EmptyEvent n atts):xs) i = (XElem n atts i noParentError [],xs,i+1)
m ((StartEvent n atts):xs) i
= let (el,xs',i') = ml xs (i+1)
in (XElem n atts i noParentError el,xs',i')
m ((PIEvent n s):xs) i = (XPI n s,xs,i)
m ((CommentEvent s):xs) i = (XComment s,xs,i)
m ((GERefEvent n):xs) i = (XGERef n,xs,i)
m ((ErrorEvent s):xs) i = (XError s,xs,i)
m (_:xs) i = (XError "unrecognized XML event",xs,i)
m [] i = (XError "unbalanced tags",[],i)
ml [] i = ([],[],i)
ml ((EndEvent n):xs) i = ([],xs,i)
ml xs i = let (e,xs',i') = m xs i
(el,xs'',i'') = ml xs' i'
in (e:el,xs'',i'')
materializeWithParent :: Stream -> XTree
materializeWithParent stream = root
where root = XElem "document" [] 1 (error "Trying to access the root parent")
[head (filter (\x -> case x of XElem _ _ _ _ _ -> True; _ -> False)
((\(x,_,_)->x) (ml stream 2 root)))]
m ((TextEvent t):xs) i _ = (XText t,xs,i)
m ((EmptyEvent n atts):xs) i p = (XElem n atts i p [],xs,i+1)
m ((StartEvent n atts):xs) i p
= let (el,xs',i') = ml xs (i+1) node
node = XElem n atts i p el
in (node,xs',i')
m ((PIEvent n s):xs) i _ = (XPI n s,xs,i)
m ((CommentEvent s):xs) i _ = (XComment s,xs,i)
m ((GERefEvent n):xs) i _ = (XGERef n,xs,i)
m ((ErrorEvent s):xs) i _ = (XError s,xs,i)
m (_:xs) i _ = (XError "unrecognized XML event",xs,i)
m [] i _ = (XError "unbalanced tags",[],i)
ml [] i _ = ([],[],i)
ml ((EndEvent n):xs) i _ = ([],xs,i)
ml xs i p = let (e,xs',i') = m xs i p
(el,xs'',i'') = ml xs' i' p
in (e:el,xs'',i'')
materialize :: Stream -> XTree
materialize = materializeWithoutParent