module Main where
+import Language.Haskell.Pretty
+import Language.Haskell.Syntax
+import Text.XML.HXT.Compilation
+import Text.XML.HXT.Arrow
+import qualified System.IO.UTF8 as Uio
+
+
main :: IO ()
-main = return ()
+main = do let fpath = "examples/tiny.xml"
+
+ [hsMod] <- runX ( setErrorMsgHandler False fail
+ >>>
+ constA fpath
+ >>>
+ readFromDocument [ (a_validate , v_0)
+ , (a_check_namespaces, v_1)
+ ]
+ >>>
+ ( constA (Module "Foo")
+ &&&
+ constA (HsIdent "foo")
+ &&&
+ compileDocument
+ )
+ >>>
+ wrapIntoModule
+ )
+ Uio.putStrLn (prettyPrint hsMod)
module Text.XML.HXT.Compilation
- (
+ ( compileDocument
+ , wrapIntoModule
)
where
+
+import Language.Haskell.Syntax
+import Text.XML.HXT.Arrow
+import Text.XML.HXT.Compilation.XmlTree
+
+
+compileDocument :: ArrowXml a => a XmlTree (HsQualType, HsExp)
+compileDocument = uniqueNamespacesFromDeclAndQNames
+ >>>
+ ( constA (HsQualType [] (HsTyCon (UnQual (HsIdent "XmlTree"))))
+ &&&
+ arr compileXmlTree
+ )
+
+
+wrapIntoModule :: Arrow a => a (Module, (HsName, (HsQualType, HsExp))) HsModule
+wrapIntoModule = proc (modName, (symName, (symType, symExp))) ->
+ do let hsModule = HsModule undefined modName (Just exports) imports decls
+ exports = [HsEVar (UnQual symName)]
+ imports = [ HsImportDecl undefined (Module "Text.XML.HXT.Arrow") False Nothing Nothing
+ , HsImportDecl undefined (Module "Text.XML.HXT.DOM.XmlNode") True (Just (Module "XN")) Nothing
+ ]
+ decls = [ HsTypeSig undefined [symName] symType
+ , HsFunBind [HsMatch undefined symName [] (HsUnGuardedRhs symExp) []]
+ ]
+ returnA -< hsModule
module Text.XML.HXT.Compilation.XmlTree
( compileXmlTree
+ , compileXmlTrees
+ , compileXNode
+ , compileDTDElem
+ , compileAttributes
+ , compileQName
)
where
import Text.XML.HXT.DOM.TypeDefs
-compileXmlTrees :: XmlTrees -> HsExp
-compileXmlTrees = HsList . map compileXmlTree
-
-
compileXmlTree :: XmlTree -> HsExp
compileXmlTree t
= let node = compileXNode (getNode t)
children = HsList (map compileXmlTree (getChildren t))
in
- xn "mkTree" $: node $: children
+ xn "mkTree" $: HsParen node $: children
+
+
+compileXmlTrees :: XmlTrees -> HsExp
+compileXmlTrees = HsList . map compileXmlTree
compileXNode :: XNode -> HsExp
compileXNode (XEntityRef s) = xn "mkEntityRef" $: litStr s
compileXNode (XCmt s) = xn "mkCmt" $: litStr s
compileXNode (XCdata s) = xn "mkCdata" $: litStr s
-compileXNode (XPi qn ts) = xn "mkPi" $: compileQName qn $: compileXmlTrees ts
-compileXNode (XTag qn ts) = xn "mkElementNode" $: compileQName qn $: compileXmlTrees ts
-compileXNode (XDTD de as) = xn "mkDTDNode" $: compileDTDElem de $: compileAttributes as
-compileXNode (XAttr qn) = xn "mkAttrNode" $: compileQName qn
-compileXNode (XError n s) = xn "mkError" $: litInt n $: litStr s
+compileXNode (XPi qn ts) = xn "mkPi" $: HsParen (compileQName qn) $: compileXmlTrees ts
+compileXNode (XTag qn ts) = xn "mkElementNode" $: HsParen (compileQName qn) $: compileXmlTrees ts
+compileXNode (XDTD de as) = xn "mkDTDNode" $: HsParen (compileDTDElem de) $: HsParen (compileAttributes as)
+compileXNode (XAttr qn) = xn "mkAttrNode" $: HsParen (compileQName qn)
+compileXNode (XError n s) = xn "mkError" $: litInt n $: litStr s
compileDTDElem :: DTDElem -> HsExp
xn :: String -> HsExp
xn = HsVar . Qual (Module "XN") . HsIdent
+unq :: String -> HsExp
+unq = HsVar . UnQual . HsIdent
+
litStr :: String -> HsExp
litStr = HsLit . HsString
litInt = HsLit . HsInt . toInteger
-infixl $:
+infixr 5 $:
($:) :: HsExp -> HsExp -> HsExp
-($:) = HsApp
+a $: b = HsApp a b
compileQName :: QName -> HsExp
compileQName (QN prefix local ns)
- = xn "mkQName" $: litStr prefix $: litStr local $: litStr ns
+ = unq "mkQName" $: litStr prefix $: litStr local $: litStr ns