module Rest.Gen.Base.XML ( getXmlSchema , showSchema , showExample ) where import Data.List import Text.XML.HXT.Arrow.Pickle import Text.XML.HXT.Arrow.Pickle.Schema getXmlSchema :: XmlPickler a => proxy a -> Schema getXmlSchema = theSchema . getPU getPU :: XmlPickler a => proxy a -> PU a getPU _ = xpickle showSchema :: Schema -> String showSchema sch = case result of [] -> "" [x] -> x xs -> intercalate "\n" $ [""] ++ indent xs ++ [""] where result = showSchema' "" sch showSchema' :: String -> Schema -> [String] showSchema' ats Any = [""] showSchema' _ (Seq []) = [] showSchema' ats (Seq [x]) = showSchema' ats x showSchema' ats (Seq ss) = [""] ++ indent (concatMap (showSchema' "") ss) ++ [""] showSchema' _ (Alt []) = [] showSchema' ats (Alt [x]) = showSchema' ats x showSchema' ats (Alt ss) = [""] ++ indent (concatMap (showSchema' "") ss) ++ [""] showSchema' ats (Rep l u s) = showSchema' (ats ++ concatMap (' ':) (mn ++ mx)) s where mn = if l >= 0 then ["minOccurs=" ++ show l] else [] mx = if u >= 0 then ["maxOccurs=" ++ show u] else [] showSchema' ats (Element n (CharData dty)) = [""] showSchema' ats (Element n (Seq [])) = [""] showSchema' ats (Element n s) = [""] ++ indent ( [""] ++ indent (showSchema' "" s) ++ [""]) ++ [""] showSchema' ats (Attribute n (CharData dty)) = [""] showSchema' ats (ElemRef n) = [""] showSchema' _ _ = [] dataToString :: DataTypeDescr -> String dataToString (DTDescr _ n _) = "xs:" ++ n indent :: [String] -> [String] indent = map (" " ++) showExample :: Schema -> String showExample sch = intercalate "\n" $ showExample' sch where showExample' :: Schema -> [String] showExample' Any = [""] showExample' (Seq []) = [] showExample' (Seq [x]) = showExample' x showExample' (Seq ss) = concatMap showExample' ss showExample' (Alt []) = [] showExample' (Alt (x : _)) = showExample' x showExample' (Rep _ _ s) = showExample' s showExample' (Element n (CharData _)) = ["<" ++ n ++ ">string"] showExample' (Element n (Seq [])) = ["<" ++ n ++ "/>"] showExample' (Element n s) = ["<" ++ n ++ ">"] ++ indent (showExample' s) ++ [""] showExample' _ = []