module Main(main) where import XPath import Text.ParserCombinators.Parsec ( parse ) import System import XmlTree hiding (mkNode) import HdomParser import EditFilters ( canonicalizeForXPath ) import Data.Maybe ( fromJust, fromMaybe ) -- ----------------------------------------------------------------------------- -- test data file :: String -- file = "hxpath/example1.xml" file = "example1.xml" --file = "example2.xslt" -- file = "namespace0.xml" testExpr :: String --testExpr = "xsl:*/xsl:template" --testExpr = "xsl:stylesheet/xsl:template[1]/*" --testExpr = "descendant-or-self::xsl:apply-templates" testExpr = "rezept/*[self::zutat and position()=3]" --testExpr = "count(rezept/zutat/*)" --testExpr = "rezept/anleitung/zutat[name()]" --testExpr = "rezept/zutat[1] | rezept/anleitung" --testExpr = "(rezept/zutat/attribute::*)" --testExpr = "rezept/comment()" --testExpr = "string(rezept/anleitung)" --testExpr = "-5 +4 ---3 +-2" --testExpr = "23 + 1.3423424 - 342342. * .2131 - 0.3123213" --testExpr = "true() and false()" --testExpr = "true() or false()" --testExpr = "true() or false() + 6" -- ----------------------------------------------------------------------------- -- take the first cmdline argument as the xpath expression -- and the second argument as source name main :: IO () main = do argv <- getArgs case length argv of 0 -> run' $ runXPathEval (newRoot (xattr "source" file)) testExpr 1 -> run' $ runXPathEval (newRoot (xattr "source" file)) (head argv) _ -> run' $ runXPathEval (newRoot (xattr "source" (head $ drop 1 argv))) (head argv) runXPathEval :: XmlTree -> String -> XState state () runXPathEval fileName exprStr = do doc <- (getWellformedDoc ) fileName -- parse the document case (parse parseXPath "" exprStr) of Left parseError -> io $ do putStrLn exprStr putStr "parse error at " print parseError Right expr -> io $ do putStrLn (exprStr ++ "\n") let exprNS = propagatExpr expr -- the parsed expression as string representation putStrLn (formatNTree id (expr2XPathTree exprNS) ++ "\n") -- extract all attributes from the dtd, they are stored in the variable environment -- let idAttr = (("", "idAttr"), getIdAttr (head doc)) let idAttr = (("", "idAttr"), idAttributesToXPathValue (getIdAttributes (head doc))) -- canonicalize the tree for XPath and convert the XmlTree in a naviagable tree -- let navTreeDoc = ntree $ head $ canonicalizeForXPath $$ doc let navTreeDoc = ntree $ head $ propagateNamespaces $ head $ canonicalizeForXPath $$ doc -- evaluate the expression with the variable environment, the context, the expression and the tree let result = evalExpr (idAttr:(fst varEnv),[]) (1, 1, navTreeDoc) exprNS (XPVNode [navTreeDoc]) putStrLn "result:" -- print result putStrLn (xPValue2String result) -- let namesp = propagateNamespaces $ head $ canonicalizeForXPath $$ doc -- putStrLn (xshow namesp) -- putStrLn (show (head $ canonicalizeForXPath $$ doc)) -- putStrLn ("") -- putStrLn (show namesp) -- | -- Convert list of ID attributes from DTD into a space separated 'XPVString' -- idAttributesToXPathValue :: XmlTrees -> XPathValue idAttributesToXPathValue ts = XPVString (foldr (\ n -> ( (valueOfDTD "value" n ++ " ") ++)) [] ts) -- | -- Extracts all ID-attributes from the document type definition (DTD). -- getIdAttributes :: XmlFilter getIdAttributes = getChildren .> isXDTD .> deep (isIdAttrType) -- neu nsList :: NamespaceTable nsList = [("xsl","http://www.w3.org/1999/XSL/Transform"), ("q","http:://q"),("w","http:://w")] propagatExpr :: Expr -> Expr propagatExpr (GenExpr op ex) = GenExpr op (map propagatExpr ex) propagatExpr (FctExpr n arg) = FctExpr n (map propagatExpr arg) propagatExpr (FilterExpr (primary:predicate)) = FilterExpr ((propagatExpr primary) : (map propagatExpr predicate)) propagatExpr (PathExpr Nothing lp@(Just _)) = PathExpr Nothing (propagatLocpath lp) propagatExpr (PathExpr (Just fe) lp@(Just _)) = PathExpr (Just (propagatExpr fe)) (propagatLocpath lp) propagatExpr q = q propagatLocpath :: Maybe LocationPath -> Maybe LocationPath propagatLocpath (Just (LocPath rel steps)) = Just (LocPath rel (map propagatStep steps)) propagatLocpath a = a propagatStep :: XStep -> XStep propagatStep (Step axis nt []) = Step axis (propagatNt nt) [] propagatStep (Step axis nt expr) = Step axis (propagatNt nt) (map propagatExpr expr) propagatNt :: NodeTest -> NodeTest propagatNt (NameTest s) = NameTest (setNamespace nsList s) propagatNt a = a