module Exercise04 where import Data.Char (isSpace) {-WETT-} -- Extracts all tags from the XML string getTags :: String -> [String] getTags xs = helper xs "" False where {- Input: String: XML input String: Accumulated tag Bool: Are we in a tag? Output: [String]: Parsed tags -} helper :: String -> String -> Bool -> [String] helper "" _ b = [" " | b] helper (x:ys) tag b | x == '<' && not b = helper ys "" True | (x == '<' && b) || (x == '>' && not b) = [" "] | x == '>' && b = reverse (dropWhile isSpace tag) : getTags ys | otherwise = helper ys ([x | b] ++ tag) b -- Checks whether syntax and semantics are valid. check :: [String] -> Bool check xs = all (notElem ' ') xs && helper xs [] where helper :: [String] -> [String] -> Bool helper [] st = null st -- Falls es keine Tokens mehr zu bearbeiten gibt helper (('/':_):_) [] = False -- Schließender Tag obwohl es keinen offenen Tag gab helper (('/':t):ys) (s:st) = s == t && helper ys st -- Schließender Tag helper (t:ys) st = helper ys (t:st) -- Offener Tag xmlLight :: String -> Bool xmlLight = check . getTags {-TTEW-} -- Zum Testen von XML Dateien ohne neuzukompilieren main :: IO () main = do xml <- readFile "invalid.xml" print $ xmlLight xml