1 {-# LANGUAGE GeneralizedNewtypeDeriving #-}
2 {-# LANGUAGE OverloadedStrings #-}
3 {-# OPTIONS_GHC -fno-warn-orphans #-}
4 module Language.XML where
6 import Control.Applicative (Applicative(..))
8 import Data.Default.Class (Default(..))
9 import Data.Eq (Eq(..))
10 import Data.Function (($), (.))
12 import Data.Map.Strict (Map)
13 import Data.Ord (Ord(..))
14 import Data.Semigroup (Semigroup(..))
15 import Data.Sequence (Seq)
16 import Data.String (IsString(..))
17 import Data.Text (Text)
18 import Data.TreeSeq.Strict (Tree)
19 import Prelude (error)
20 import Text.Show (Show(..), showsPrec, showChar, showString)
21 import qualified Data.List as List
22 import qualified Data.Text as Text
24 import Language.TCT.Cell
27 type XML = Tree (Cell XmlName) (Cell XmlLeaf)
33 { xmlNamePrefix :: Text
34 , xmlNameSpace :: Text
35 , xmlNameLocal :: Text
37 instance Show XmlName where
38 showsPrec _p XmlName{xmlNameSpace="", ..} =
39 showString (Text.unpack xmlNameLocal)
40 showsPrec _p XmlName{..} =
41 if Text.null xmlNameSpace
42 then showString (Text.unpack xmlNameLocal)
45 showString (Text.unpack xmlNameSpace) .
47 showString (Text.unpack xmlNameLocal)
48 instance Eq XmlName where
49 XmlName _ sx lx == XmlName _ sy ly = sx == sy && lx == ly
50 instance Ord XmlName where
51 XmlName _ sx lx `compare` XmlName _ sy ly = compare sx sy <> compare lx ly
52 instance IsString XmlName where
53 fromString "" = XmlName "" "" ""
54 fromString full@('{':rest) =
55 case List.break (== '}') rest of
56 (_, "") -> error ("Invalid Clark notation: " <> show full)
57 (ns, local) -> XmlName "" (Text.pack ns) (Text.pack $ List.drop 1 local)
58 fromString local = XmlName "" "" (Text.pack local)
60 xmlLocalName :: Text -> XmlName
61 xmlLocalName = XmlName "" ""
65 = XmlAttr XmlName Text
68 deriving (Eq,Ord,Show)
73 { xmlPosAncestors :: [(XmlName,Count)]
74 , xmlPosPrecedingsSiblings :: Map XmlName Count
80 deriving (Eq, Ord, Show)
83 newtype Nat1 = Nat1 Int
84 deriving (Eq, Ord, Show)
87 newtype Ident = Ident { unIdent :: Text }
88 deriving (Eq,Show,Default,IsString)
89 instance Default Text where
93 newtype URL = URL Text
94 deriving (Eq,Show,Default)
97 newtype Path = Path Text
98 deriving (Eq,Show,Default)
102 = MayText { unMayText :: Text }
103 deriving (Eq,Show,Default)
104 instance Semigroup MayText where
109 whenMayText :: Applicative m => MayText -> (MayText -> m ()) -> m ()
110 whenMayText (MayText "") _f = pure ()
111 whenMayText t f = f t